private static EndPointListener GetEpListener(ILogger logger, string host, int port, HttpListener listener, bool secure) { IPAddress addr; if (host == "*" || host == "+") { addr = GetIpAnyAddress(); } else if (IPAddress.TryParse(host, out addr) == false) { try { IPHostEntry iphost = Dns.GetHostEntry(host); if (iphost != null) { addr = iphost.AddressList[0]; } else { addr = GetIpAnyAddress(); } } catch { addr = GetIpAnyAddress(); } } Dictionary <int, EndPointListener> p = null; // Dictionary<int, EndPointListener> if (!IpToEndpoints.TryGetValue(addr, out p)) { p = new Dictionary <int, EndPointListener>(); IpToEndpoints[addr] = p; } EndPointListener epl = null; if (p.ContainsKey(port)) { epl = (EndPointListener)p[port]; } else { epl = new EndPointListener(listener, addr, port, secure, listener.Certificate, logger); p[port] = epl; } return(epl); }
public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep) { lock (IpToEndpoints) { // Dictionary<int, EndPointListener> p Dictionary <int, EndPointListener> p; if (IpToEndpoints.TryGetValue(ep.Address, out p)) { p.Remove(ep.Port); if (p.Count == 0) { IpToEndpoints.Remove(ep.Address); } } epl.Close(); } }
private static void RemovePrefixInternal(ILogger logger, string prefix, HttpListener listener) { ListenerPrefix lp = new ListenerPrefix(prefix); if (lp.Path.IndexOf('%') != -1) { return; } if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1) { return; } EndPointListener epl = GetEpListener(logger, lp.Host, lp.Port, listener, lp.Secure); epl.RemovePrefix(lp, listener); }
private static void AddPrefixInternal(ILogger logger, string p, HttpListener listener) { ListenerPrefix lp = new ListenerPrefix(p); if (lp.Path.IndexOf('%') != -1) { throw new HttpListenerException(400, "Invalid path."); } if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1) // TODO: Code? { throw new HttpListenerException(400, "Invalid path."); } // listens on all the interfaces if host name cannot be parsed by IPAddress. EndPointListener epl = GetEpListener(logger, lp.Host, lp.Port, listener, lp.Secure); epl.AddPrefix(lp, listener); }