public static Dictionary <string, MultiPartEntry> Parse(Request request) { var mps = new Dictionary <string, MultiPartEntry> (); var contentType = request.headers.Get("Content-Type"); if (contentType.Contains("multipart/form-data")) { var boundary = request.body.Substring(0, request.body.IndexOf("\r\n")) + "\r\n"; var parts = request.body.Split(new string[] { boundary }, System.StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { var sep = part.IndexOf("\r\n\r\n"); if (sep == -1) { continue; } var headerText = part.Substring(0, sep); var mp = new MultiPartEntry(); mp.headers.Read(headerText); mp.Value = part.Substring(sep); if (mp.headers.Contains("Content-Disposition")) { var s = mp.headers.Get("Content-Disposition"); var nm = new Regex(@"(?<=name\=\"")(.*?)(?=\"")").Match(s); if (nm.Success) { mp.Name = nm.Value.Trim(); } var fm = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")").Match(s); if (fm.Success) { mp.Filename = fm.Value.Trim(); } } if (mp.Name != null) { mps.Add(mp.Name, mp); } } } return(mps); }
void ServeHTTP(TcpClient tc) { var stream = tc.GetStream(); var line = ReadLine(stream); if (line == null) { return; } var top = line.Trim().Split(' '); if (top.Length != 3) { return; } var req = new HttpRequest() { HttpMethod = top [0], RawUrl = top [1], protocol = top [2] }; if (req.RawUrl.StartsWith("http://")) { req.Url = new Uri(req.RawUrl); } else { req.Url = new Uri("http://" + System.Net.IPAddress.Any + ":" + port + req.RawUrl); } while (true) { var headerline = ReadLine(stream); if (headerline.Length == 0) { break; } req.Headers.AddHeaderLine(headerline); } req.InputStream = stream; string contentLength = req.Headers.Get("Content-Length"); if (contentLength != null) { var count = int.Parse(contentLength); var bytes = new byte[count]; var offset = 0; while (count > 0) { offset = stream.Read(bytes, offset, count); count -= offset; } req.body = System.Text.Encoding.UTF8.GetString(bytes); } string[] contentTypes = req.Headers.GetValues("Content-Type"); if (contentTypes != null && Array.IndexOf(contentTypes, "multipart/form-data") >= 0) { req.formData = MultiPartEntry.Parse(req); } if (processRequestsInMainThread) { lock (mainThreadRequests) { mainThreadRequests.Enqueue(req); } } else { var response = new HttpResponse(); ProcessRequest(req, response); } }
void ServeHTTP(Socket sock) { var stream = new HttpStream(sock); var line = ReadLine(stream); if (line == null) { return; } var top = line.Trim().Split(' '); if (top.Length != 3) { return; } var req = new HttpRequest(stream) { HttpMethod = top [0], RawUrl = top [1], protocol = top [2] }; // XXX: Should use a regex to check for the scheme, could also be e.g. https:// if (req.RawUrl.StartsWith("http://")) { // .NET API specifies that the RawURL starts with the path req.RawUrl = req.RawUrl.Remove(0, "http://".Length); req.Url = new Uri(req.RawUrl); } else { req.Url = new Uri("http://" + System.Net.IPAddress.Any + ":" + port + req.RawUrl); } while (true) { var headerline = ReadLine(stream); if (headerline.Length == 0) { break; } req.Headers.AddHeaderLine(headerline); } string contentLength = req.Headers.Get("Content-Length"); if (contentLength != null) { var count = int.Parse(contentLength); var bytes = new byte[count]; var offset = 0; int len = -1; do { len = stream.Read(bytes, offset, count); offset += len; count -= len; } while (len > 0 && count > 0); req.body = System.Text.Encoding.UTF8.GetString(bytes); } string[] contentTypes = req.Headers.GetValues("Content-Type"); if (contentTypes != null && Array.IndexOf(contentTypes, "multipart/form-data") >= 0) { req.formData = MultiPartEntry.Parse(req); } if (processRequestsInMainThread) { lock (mainThreadRequests) { mainThreadRequests.Enqueue(req); } } else { ProcessRequest(req); } }