public void Start() { try { _socket = CreateSocketBindAndListen(AddressFamily.InterNetwork, _listenAddress, _port); } catch { // Any Address is valid for IPv6 and IPv4 as well. // So this exception will only occur in pure IPv6 environments with // _listenAddress set to IPv4 Loopback _socket = CreateSocketBindAndListen(AddressFamily.InterNetworkV6, IPAddress.IPv6Loopback, _port); } ThreadPool.QueueUserWorkItem(delegate { while (!_shutdownInProgress) { try { Socket acceptedSocket = _socket.Accept(); ThreadPool.QueueUserWorkItem(delegate { if (!_shutdownInProgress) { var conn = new Connection(this, acceptedSocket); // wait for at least some input if (conn.WaitForRequestBytes() == 0) { conn.WriteErrorAndClose(400); return; } // find or create host Host host = GetHost(); if (host == null) { conn.WriteErrorAndClose(500); return; } // process request in worker app domain host.ProcessRequest(conn); } }); } catch { Thread.Sleep(100); } } }); if (Started != null) { Started(this, new EventArgs()); } }
private bool TryParseRequest() { Reset(); ReadAllHeaders(); if (_headerBytes == null || _endHeadersOffset < 0 || _headerByteStrings == null || _headerByteStrings.Count == 0) { Connection.WriteErrorAndClose(400); return(false); } ParseRequestLine(); // Check for bad path if (IsBadPath()) { Connection.WriteErrorAndClose(400); return(false); } bool clientScriptPath; // Check if the path is not well formed or is not for the current app if (!Host.IsVirtualPathInApp(Path, out clientScriptPath)) { // HACK: Avoid out parameter!!! IsClientScriptPath = clientScriptPath; Connection.WriteErrorAndClose(404); return(false); } // HACK: Avoid out parameter!!! IsClientScriptPath = clientScriptPath; ParseHeaders(); ParsePostedContent(); return(true); }
private void ParseRequestLine() { var requestLine = _headerByteStrings[0]; var elems = requestLine.Split(' '); if (elems == null || elems.Length < 2 || elems.Length > 3) { Connection.WriteErrorAndClose(400); return; } Verb = elems[0].GetString(); var urlBytes = elems[1]; Url = urlBytes.GetString(); if (elems.Length == 3) { Prot = elems[2].GetString(); } else { Prot = "HTTP/1.0"; } // query string int iqs = urlBytes.IndexOf('?'); if (iqs > 0) { _queryStringBytes = urlBytes.Substring(iqs + 1).GetBytes(); } else { _queryStringBytes = new byte[0]; } iqs = Url.IndexOf('?'); if (iqs > 0) { Path = Url.Substring(0, iqs); QueryString = Url.Substring(iqs + 1); } else { Path = Url; _queryStringBytes = new byte[0]; } // url-decode path if (Path.IndexOf('%') >= 0) { Path = HttpUtility.UrlDecode(Path, Encoding.UTF8); iqs = Url.IndexOf('?'); if (iqs >= 0) { Url = Path + Url.Substring(iqs); } else { Url = Path; } } // path info var lastDot = Path.LastIndexOf('.'); var lastSlh = Path.LastIndexOf('/'); if (lastDot >= 0 && lastSlh >= 0 && lastDot < lastSlh) { var ipi = Path.IndexOf('/', lastDot); FilePath = Path.Substring(0, ipi); PathInfo = Path.Substring(ipi); } else { FilePath = Path; PathInfo = String.Empty; } PathTranslated = MapPath(FilePath); }
public void Start() { try { _socket = CreateSocketBindAndListen(AddressFamily.InterNetwork, _listenAddress, _port); } catch { // Any Address is valid for IPv6 and IPv4 as well. // So this exception will only occur in pure IPv6 environments with // _listenAddress set to IPv4 Loopback _socket = CreateSocketBindAndListen(AddressFamily.InterNetworkV6, IPAddress.IPv6Loopback, _port); } ThreadPool.QueueUserWorkItem(delegate { while (!_shutdownInProgress) { try { Socket acceptedSocket = _socket.Accept(); ThreadPool.QueueUserWorkItem(delegate { if (!_shutdownInProgress) { var conn = new Connection(this, acceptedSocket); // wait for at least some input if (conn.WaitForRequestBytes() == 0) { conn.WriteErrorAndClose(400); return; } // find or create host Host host = GetHost(); if (host == null) { conn.WriteErrorAndClose(500); return; } // process request in worker app domain host.ProcessRequest(conn); } }); } catch { Thread.Sleep(100); } } }); if (Started != null) { Started(this, new EventArgs()); } }