void ListnerLoop() { try { using (TcpClient tcpClient = _tcpListener.AcceptTcpClient()) { HttpResponse response = new HttpResponse(tcpClient); try { _log.Debug("Client conected: {0}", tcpClient.Client.RemoteEndPoint); if (!tcpClient.Client.Connected) { return; } string requestSb = GetRequestString(tcpClient); if (string.IsNullOrEmpty(requestSb)) { return; } SimpleWebRequest request = new SimpleWebRequest(requestSb); _log.Debug("Request: {0}", requestSb); ProcessRequest(request, response); } catch (HttpException he) { _log.Error("Parcing HTTP request: {0}", he); response.SendPage(HttpStatus.BadRequest, he.Message); } } } catch (SocketException se) { _log.Error("Error accepting client: {0}", se); } }
public bool ProcessQuery(SimpleWebRequest request, HttpResponse response) { if (request.Path.Contains("..")) { return(false); } if (!request.Path.StartsWith(_virtPath, StringComparison.InvariantCultureIgnoreCase)) { return(false); } string path = request.Path.Substring(_virtPath.Length).Replace('/', '\\'); string fullPath = Path.Combine(_baseDir, path.Trim('\\')); if (File.Exists(fullPath)) { using (var stream = File.OpenRead(fullPath)) { string mimeType = MimeTypeMap.GetMimeMapping(fullPath); response.SendFile(HttpStatus.OK, stream, mimeType); return(true); } } else { if (Directory.Exists(fullPath)) { ListDirectory(request.Path, fullPath, request, response); return(true); } } return(false); }
private void ProcessRequest(SimpleWebRequest request, HttpResponse response) { bool handled = false; foreach (var dispatcher in Dispatchers) { handled = dispatcher.ProcessQuery(request, response); if (handled) { break; } } if (!handled) { response.SendPage(HttpStatus.NotFound, "Requested page is not found"); } }
private string GetErrorPage(SimpleWebRequest request, HttpResponse response, HttpStatus httpStatus, string message) { string statusStrng = response.GetHttpStatusString(httpStatus); IPEndPoint ep = response.LocalEndpoint; StringBuilder errorPage = new StringBuilder(); errorPage.Append("<html>\n<head>\n"); errorPage.AppendFormat("<title>{0}</title>\n", statusStrng); errorPage.Append("</head>\n<body>\n"); errorPage.AppendFormat("<h1>{0}</h1>\n", statusStrng); errorPage.AppendFormat("<p>{0}</p>\n", message); errorPage.Append("<hr>\n"); errorPage.AppendFormat("</address>\"{0}\" Server at {1} Port {2} </address>\n", "Web server CE", ep.Address, ep.Port); errorPage.Append("</body>\n</html>\n"); return(errorPage.ToString()); }
public bool ProcessQuery(SimpleWebRequest request, HttpResponse response) { if (!(string.IsNullOrEmpty(request.Path) || request.Path == "/")) { return(false); } string path = "index.html"; string fullPath = Path.Combine(_baseDir, path); if (File.Exists(fullPath)) { using (var stream = File.OpenRead(fullPath)) { string mimeType = MimeTypeMap.GetMimeMapping(fullPath); response.SendFile(HttpStatus.OK, stream, mimeType); return(true); } } return(false); }
private void ListDirectory(string path, string fullPath, SimpleWebRequest request, HttpResponse response) { string host; request.Options.TryGetValue("Host", out host); StringBuilder page = new StringBuilder(); page.Append("<html>\n<head>\n"); page.AppendFormat("<title>Content of: {0}</title>\n", path); page.Append("</head>\n<body>\n"); page.AppendFormat("<h1>Content of: {0}</h1>\n", path); page.Append("<div>\n"); foreach (string entry in Directory.GetFileSystemEntries(fullPath)) { string ename = entry.Substring(fullPath.Length).Trim('\\', '/'); string eUrl = string.Format("http://{0}{1}/{2}", host, path == "/" ? "" : Uri.EscapeUriString(path), Uri.EscapeUriString(ename)); page.AppendFormat("<p><a href=\"{0}\">{1}</a></p>\n", eUrl, ename); } page.Append("</div></body>\n</html>\n"); response.SendPage(HttpStatus.OK, page.ToString()); }
public bool ProcessQuery(SimpleWebRequest request, HttpResponse response) { response.SendPage(HttpStatus.NotFound, GetErrorPage(request, response, HttpStatus.NotFound, "Requested page is not found")); return(true); }