public void ReturnResponse(NetworkStream stream, Response response) { byte[] _response = (response.Content != null) ? response.GetResponseHeaderAsByteArray().Concat(response.Content).ToArray() : response.GetResponseHeaderAsByteArray(); stream.Write(_response, 0, _response.Length); stream.Flush(); stream.Close(); }
public Response GetResponseForRequest(Request request, string root) { Response response = new Response(request); response.StatusCode = 200; response.StatusMessage = "OK"; bool dirBrowsing = false; if (!ParseValidUrl(request.Url)) { request.Url = FixUrl(request.Url); string defPage = GetDefaultPage(request, root); dirBrowsing = (ServerConfig.DirectoryBrowsing && defPage == null && request.Port == ServerConfig.WebPort); if (!dirBrowsing) request.Url = request.Url + defPage; Console.WriteLine("Dir browsing: " + dirBrowsing); } response.Path = root + request.Url; if (File.Exists(response.Path) || dirBrowsing) { if (!dirBrowsing) response.Content = File.ReadAllBytes(response.Path); else response.Content = LoadDirBrowsingPage(response.Path); } if (!response.SetDefaultHeaders()) response.Content = GetErrorPage(response.StatusCode); return response; }
public void HandleTimeout(TcpClient client) { Response response = new Response(); response.StatusCode = 408; response.StatusMessage = "Request Timeout"; response.AddHeader("Connection", "keep-alive"); response.AddHeader("Content-type", "text/html"); response.AddHeader("Cache-Control", "no-cache, must-revalidate"); response.AddHeader("Server", ServerConfig.Name); response.Content = GetErrorPage(response.StatusCode); ReturnResponse(client.GetStream(), response); }
public RouteHandler(Request request, Response response) { _request = request; _response = response; }