static EndPointListener GetEPListener(IPAddress addr, int port, HttpListener listener, bool secure) { Hashtable p = null; // Dictionary<int, EndPointListener> if (ip_to_endpoints.ContainsKey(addr)) { p = (Hashtable)ip_to_endpoints [addr]; } else { p = new Hashtable(); ip_to_endpoints [addr] = p; } EndPointListener epl = null; if (p.ContainsKey(port)) { epl = (EndPointListener)p [port]; } else { epl = new EndPointListener(addr, port, secure); p [port] = epl; } return(epl); }
public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep) { lock (ip_to_endpoints) { // Dictionary<int, EndPointListener> p Hashtable p = null; p = (Hashtable)ip_to_endpoints [ep.Address]; p.Remove(ep.Port); epl.Close(); } }
static void RemovePrefixInternal(string prefix, HttpListener listener) { ListenerPrefix lp = new ListenerPrefix(prefix); if (lp.Path.IndexOf('%') != -1) { return; } if (lp.Path.IndexOf("//") != -1) { return; } EndPointListener epl = GetEPListener(IPAddress.Any, lp.Port, listener, lp.Secure); epl.RemovePrefix(lp, listener); }
static void AddPrefixInternal(string p, HttpListener listener) { ListenerPrefix lp = new ListenerPrefix(p); if (lp.Path.IndexOf('%') != -1) { throw new HttpListenerException(400, "Invalid path."); } if (lp.Path.IndexOf("//") != -1) // TODO: Code? { throw new HttpListenerException(400, "Invalid path."); } // Always listens on all the interfaces, no matter the host name/ip used. EndPointListener epl = GetEPListener(IPAddress.Any, lp.Port, listener, lp.Secure); epl.AddPrefix(lp, listener); }
static void OnAccept(IAsyncResult ares) { EndPointListener epl = (EndPointListener)ares.AsyncState; Socket accepted = null; try { accepted = epl.sock.EndAccept(ares); } catch { // Anything to do here? } finally { try { epl.sock.BeginAccept(OnAccept, epl); } catch { if (accepted != null) { try { accepted.Close(); } catch {} accepted = null; } } } if (accepted == null) { return; } if (epl.secure && (epl.key == null)) { accepted.Close(); return; } HttpConnection conn = new HttpConnection(accepted, epl); conn.BeginReadRequest(); }