/// <summary> /// Handles http requests and sends a response /// </summary> /// <param name="client">Tcp client</param> /// <param name="info">Request info</param> private async void HandleRequest(SimpleTcpClient client, RequestInfo info) { if (info.Methode == MethodeType.GET) { string fullPath = rootFullPath + info.Content; if (File.Exists(fullPath)) { string type = Mapper.GetContentType(Path.GetExtension(fullPath)); DateTime modifiedTime = File.GetLastWriteTime(fullPath); string encoding = null; string charset = null; byte[] content = null; if (type.StartsWith("text")) //text encoding { string data; if (Path.GetExtension(fullPath) == ".cs") { data = ExecuteScript(fullPath); } else { data = File.ReadAllText(fullPath); } content = Encoding.GetEncoding(config.DefaultCharset).GetBytes(data); encoding = null; charset = config.DefaultCharset; } else //byte encoding { if (info.Encoding.HasFlag(EncodingType.GZIP)) { content = Compressions.GZip(File.ReadAllBytes(fullPath)); encoding = "gzip"; } else if (info.Encoding.HasFlag(EncodingType.DEFLATE)) { content = Compressions.Deflate(File.ReadAllBytes(fullPath)); encoding = "deflate"; } else { content = File.ReadAllBytes(fullPath); } } int fullLength = content.Length; content = DoRangeManipulation(content, info.StartRange, info.EndRange); ResponseInfo response = new ResponseInfo("1.1", "200", "OK", modifiedTime.ToString(), null, type, content, info.KeepAlive, charset, encoding); await client.SendAsync(response.CreateResponse(lf, fullLength, info.StartRange, info.EndRange)); } else { await client.SendAsync(GetErrorResponse(404).CreateResponse(lf)); //file not found (404) } } else { await client.SendAsync(GetErrorResponse(405).CreateResponse(lf)); //Unsupported methode } if (!info.KeepAlive) { client.Stop(); } }