1 #define EMBEDDED_IN_1_0
4 // System.Net.EndPointManager
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System; using System.Net; namespace MonoHttp {
36 sealed class EndPointManager
38 // Dictionary<IPAddress, Dictionary<int, EndPointListener>>
39 static Hashtable ip_to_endpoints = new Hashtable ();
41 private EndPointManager ()
45 public static void AddListener (HttpListener listener)
47 ArrayList added = new ArrayList ();
49 lock (ip_to_endpoints) {
50 foreach (string prefix in listener.Prefixes) {
51 AddPrefixInternal (prefix, listener);
56 foreach (string prefix in added) {
57 RemovePrefix (prefix, listener);
63 public static void AddPrefix (string prefix, HttpListener listener)
65 lock (ip_to_endpoints) {
66 AddPrefixInternal (prefix, listener);
70 static void AddPrefixInternal (string p, HttpListener listener)
72 ListenerPrefix lp = new ListenerPrefix (p);
73 if (lp.Path.IndexOf ('%') != -1)
74 throw new HttpListenerException (400, "Invalid path.");
76 if (lp.Path.IndexOf ("//") != -1) // TODO: Code?
77 throw new HttpListenerException (400, "Invalid path.");
79 // Always listens on all the interfaces, no matter the host name/ip used.
80 EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
81 epl.AddPrefix (lp, listener);
84 static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
86 Hashtable p = null; // Dictionary<int, EndPointListener>
87 if (ip_to_endpoints.ContainsKey (addr)) {
88 p = (Hashtable) ip_to_endpoints [addr];
91 ip_to_endpoints [addr] = p;
94 EndPointListener epl = null;
95 if (p.ContainsKey (port)) {
96 epl = (EndPointListener) p [port];
98 epl = new EndPointListener (addr, port, secure);
105 public static void RemoveEndPoint (EndPointListener epl, IPEndPoint ep)
107 lock (ip_to_endpoints) {
108 // Dictionary<int, EndPointListener> p
110 p = (Hashtable) ip_to_endpoints [ep.Address];
113 ip_to_endpoints.Remove (ep.Address);
119 public static void RemoveListener (HttpListener listener)
121 lock (ip_to_endpoints) {
122 foreach (string prefix in listener.Prefixes) {
123 RemovePrefixInternal (prefix, listener);
128 public static void RemovePrefix (string prefix, HttpListener listener)
130 lock (ip_to_endpoints) {
131 RemovePrefixInternal (prefix, listener);
135 static void RemovePrefixInternal (string prefix, HttpListener listener)
137 ListenerPrefix lp = new ListenerPrefix (prefix);
138 if (lp.Path.IndexOf ('%') != -1)
141 if (lp.Path.IndexOf ("//") != -1)
144 EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
145 epl.RemovePrefix (lp, listener);