public HttpConnection(Socket sock, HttpEndPointListener epl, bool secure, X509Certificate cert) { _socket = sock; _epl = epl; _secure = secure; _cert = cert; if (secure == false) { _stream = new NetworkStream(sock, false); } else { #pragma warning disable CA5359 _sslStream = HttpListener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) => { if (c == null) { return(true); } _clientCert = c as X509Certificate2 ?? new X509Certificate2(c.GetRawCertData()); _clientCertErrors = new int[] { (int)e }; return(true); }); #pragma warning restore CA5359 _stream = _sslStream; } _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite); _sslStream?.AuthenticateAsServer(_cert, true, (SslProtocols)ServicePointManager.SecurityProtocol, false); Init(); }
private static void ProcessAccept(SocketAsyncEventArgs args) { Socket accepted = null; if (args.SocketError == SocketError.Success) { accepted = args.AcceptSocket; } HttpEndPointListener epl = (HttpEndPointListener)args.UserToken; Accept(epl._socket, args, ref accepted); if (accepted == null) { return; } if (epl._secure && epl._cert == null) { accepted.Close(); return; } HttpConnection conn = new HttpConnection(accepted, epl, epl._secure, epl._cert); lock (epl._unregisteredConnections) { epl._unregisteredConnections[conn] = conn; } conn.BeginReadRequest(); }
private static void AddPrefixInternal(string p, HttpListener listener) { int start = p.IndexOf(':') + 3; int colon = p.IndexOf(':', start); if (colon != -1) { // root can't be -1 here, since we've already checked for ending '/' in ListenerPrefix. int root = p.IndexOf('/', colon, p.Length - colon); string portString = p.Substring(colon + 1, root - colon - 1); int port; if (!int.TryParse(portString, out port) || port <= 0 || port >= 65536) { throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_port); } } ListenerPrefix lp = new ListenerPrefix(p); if (lp.Path.IndexOf('%') != -1) { throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path); } if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1) { throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path); } // listens on all the interfaces if host name cannot be parsed by IPAddress. HttpEndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure); epl.AddPrefix(lp, listener); }
private static HttpEndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure) { IPAddress addr; if (host == "*") { addr = IPAddress.Any; } else { try { addr = Dns.GetHostAddresses(host)[0]; if (IPAddress.Any.Equals(addr)) { // Don't support listening to 0.0.0.0, match windows behavior. throw new HttpListenerException(50, SR.net_listener_not_supported); } } catch { // Throw same error code as windows, request is not supported. throw new HttpListenerException(50, SR.net_listener_not_supported); } } Dictionary <int, HttpEndPointListener> p = null; if (s_ipEndPoints.ContainsKey(addr)) { p = s_ipEndPoints[addr]; } else { p = new Dictionary <int, HttpEndPointListener>(); s_ipEndPoints[addr] = p; } HttpEndPointListener epl = null; if (p.ContainsKey(port)) { epl = p[port]; } else { try { epl = new HttpEndPointListener(listener, addr, port, secure); } catch (SocketException ex) { throw new HttpListenerException(ex.ErrorCode, ex.Message); } p[port] = epl; } return(epl); }
private void Unbind() { if (_contextBound) { HttpEndPointListener.UnbindContext(_context); _contextBound = false; } }
private static HttpEndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure) { IPAddress addr; if (host == "*") { addr = IPAddress.Any; } else if (IPAddress.TryParse(host, out addr) == false) { try { IPHostEntry iphost = Dns.GetHostEntry(host); if (iphost != null) { addr = iphost.AddressList[0]; } else { addr = IPAddress.Any; } } catch { addr = IPAddress.Any; } } Dictionary <int, HttpEndPointListener> p = null; if (s_ipEndPoints.ContainsKey(addr)) { p = s_ipEndPoints[addr]; } else { p = new Dictionary <int, HttpEndPointListener>(); s_ipEndPoints[addr] = p; } HttpEndPointListener epl = null; if (p.ContainsKey(port)) { epl = p[port]; } else { epl = new HttpEndPointListener(listener, addr, port, secure); p[port] = epl; } return(epl); }
public static void RemoveEndPoint(HttpEndPointListener epl, IPEndPoint ep) { lock ((s_ipEndPoints as ICollection).SyncRoot) { Dictionary <int, HttpEndPointListener> p = null; p = s_ipEndPoints[ep.Address]; p.Remove(ep.Port); if (p.Count == 0) { s_ipEndPoints.Remove(ep.Address); } epl.Close(); } }
private static void RemovePrefixInternal(string prefix, HttpListener listener) { ListenerPrefix lp = new ListenerPrefix(prefix); if (lp.Path.IndexOf('%') != -1) { return; } if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1) { return; } HttpEndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure); epl.RemovePrefix(lp, listener); }
private static void AddPrefixInternal(string p, HttpListener listener) { ListenerPrefix lp = new ListenerPrefix(p); if (lp.Path.IndexOf('%') != -1) { throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path); } if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1) { throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path); } // listens on all the interfaces if host name cannot be parsed by IPAddress. HttpEndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure); epl.AddPrefix(lp, listener); }
private static void ProcessAccept(SocketAsyncEventArgs args) { HttpEndPointListener epl = (HttpEndPointListener)args.UserToken !; Socket?accepted = args.SocketError == SocketError.Success ? args.AcceptSocket : null; epl.Accept(args); if (accepted == null) { return; } if (epl._secure && epl._cert == null) { accepted.Close(); return; } HttpConnection conn; try { conn = new HttpConnection(accepted, epl, epl._secure, epl._cert !); } catch { accepted.Close(); return; } lock (epl._unregisteredConnections) { epl._unregisteredConnections[conn] = conn; } conn.BeginReadRequest(); }
public HttpConnection(Socket sock, HttpEndPointListener epl, bool secure, X509Certificate cert) { _socket = sock; _epl = epl; _secure = secure; _cert = cert; if (secure == false) { _stream = new NetworkStream(sock, false); } else { _sslStream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) => { if (c == null) { return(true); } var c2 = c as X509Certificate2; if (c2 == null) { c2 = new X509Certificate2(c.GetRawCertData()); } _clientCert = c2; _clientCertErrors = new int[] { (int)e }; return(true); }); _stream = _sslStream; } _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite); Init(); }
private static HttpEndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure, out bool alreadyExists) { alreadyExists = false; IPAddress addr; if (host == "*") { addr = IPAddress.Any; } else if (IPAddress.TryParse(host, out addr) == false) { try { IPHostEntry iphost = Dns.GetHostEntry(host); if (iphost != null) { addr = iphost.AddressList[0]; } else { addr = IPAddress.Any; } } catch { addr = IPAddress.Any; } } Dictionary <int, HttpEndPointListener> p = null; if (s_ipEndPoints.ContainsKey(addr)) { p = s_ipEndPoints[addr]; } else { p = new Dictionary <int, HttpEndPointListener>(); s_ipEndPoints[addr] = p; } HttpEndPointListener epl = null; if (p.ContainsKey(port)) { alreadyExists = true; epl = p[port]; } else { try { epl = new HttpEndPointListener(listener, addr, port, secure); } catch (SocketException ex) { throw new HttpListenerException(ex.ErrorCode, ex.Message); } p[port] = epl; } return(epl); }