public HttpHeaders(SocketReader socketReader) { int lineNum = 0; string line; string lastField = null; while ("\r\n" != (line = socketReader.NextLine())) { ++lineNum; Match match = headerFieldRegex.Match(line); if (match.Success) { string name = match.Groups[1].Value.Trim().ToLower(); string value = match.Groups[2].Value; if (!fields.Contains(name)) { fields[name] = value; } else { fields[name] = (string)fields[name] + "," + value; } // in case continuation lines follow lastField = name; } else { Match match2 = continuationRegex.Match(line); if (match2.Success) { string value = match2.Groups[1].Value; if (lastField != null) { fields[lastField] = (string)fields[lastField] + value; } else { Trace.Fail("Illegal HTTP header: folding whitespace detected at illegal location (line " + lineNum + ")"); } } else { Trace.Fail("Illegal HTTP header: could not parse line " + lineNum); } } } }
public HttpHeaders(SocketReader socketReader) { int lineNum = 0; string line; string lastField = null; while ("\r\n" != (line = socketReader.NextLine())) { ++lineNum; Match match = headerFieldRegex.Match(line); if (match.Success) { string name = match.Groups[1].Value.Trim().ToLower(); string value = match.Groups[2].Value; if (!fields.Contains(name)) fields[name] = value; else fields[name] = (string)fields[name] + "," + value; // in case continuation lines follow lastField = name; } else { Match match2 = continuationRegex.Match(line); if (match2.Success) { string value = match2.Groups[1].Value; if (lastField != null) { fields[lastField] = (string)fields[lastField] + value; } else { Trace.Fail("Illegal HTTP header: folding whitespace detected at illegal location (line " + lineNum + ")"); } } else { Trace.Fail("Illegal HTTP header: could not parse line " + lineNum); } } } }
private void HandleConnection(Socket remote) { try { if (localOnly && !((IPEndPoint)remote.RemoteEndPoint).Address.Equals(IPAddress.Loopback)) { Trace.WriteLine("Rejecting non-loopback connection from remote host"); return; } else { SocketReader reader = new SocketReader(remote, 2048); string val = reader.NextLine(); Match match = reqLine.Match(val); if (!match.Success) { Trace.WriteLine("Rejecting malformed HTTP query: " + val); return; } else { HttpHeaders headers = new HttpHeaders(reader); string method = match.Groups[1].Value; string uri = match.Groups[2].Value; string version = match.Groups[3].Value; if (headers.ContentLength >= 0) { reader.SetBytesRemaining(headers.ContentLength); } else if (string.Compare(method, "GET", true, CultureInfo.InvariantCulture) == 0) { reader.SetBytesRemaining(0); } // Console.WriteLine("URI: " + uri); OnHandleConnectionBegin(); bool success = false; for (int i = 0; i < handlers.Count; i++) { HandlerRegistration reg = (HandlerRegistration)handlers[i]; if (reg.Pattern.IsMatch(uri)) { if (reg.Handler(method.ToUpper(CultureInfo.InvariantCulture), uri, headers, reader, remote)) { success = true; break; } } } if (!success) { HttpHelper.SendErrorCode(remote, 404, "Not found"); } return; } } } finally { remote.Shutdown(SocketShutdown.Both); remote.Close(); } }
private void HandleConnection(Socket remote) { try { if (localOnly && !((IPEndPoint)remote.RemoteEndPoint).Address.Equals(IPAddress.Loopback)) { Trace.WriteLine("Rejecting non-loopback connection from remote host"); return; } else { SocketReader reader = new SocketReader(remote, 2048); string val = reader.NextLine(); Match match = reqLine.Match(val); if (!match.Success) { Trace.WriteLine("Rejecting malformed HTTP query: " + val); return; } else { HttpHeaders headers = new HttpHeaders(reader); string method = match.Groups[1].Value; string uri = match.Groups[2].Value; string version = match.Groups[3].Value; if (headers.ContentLength >= 0) reader.SetBytesRemaining(headers.ContentLength); else if (string.Compare(method, "GET", true, CultureInfo.InvariantCulture) == 0) reader.SetBytesRemaining(0); // Console.WriteLine("URI: " + uri); OnHandleConnectionBegin(); bool success = false; for (int i = 0; i < handlers.Count; i++) { HandlerRegistration reg = (HandlerRegistration) handlers[i]; if (reg.Pattern.IsMatch(uri)) { if (reg.Handler(method.ToUpper(CultureInfo.InvariantCulture), uri, headers, reader, remote)) { success = true; break; } } } if (!success) HttpHelper.SendErrorCode(remote, 404, "Not found"); return; } } } finally { remote.Shutdown(SocketShutdown.Both); remote.Close(); } }