* Makefile: Add a System.Core.dll reference; embed monodoc.xml as a
[mono.git] / mcs / class / System.Runtime.Remoting / MonoHttp / EndPointManager.cs
1 #define EMBEDDED_IN_1_0
2
3 //
4 // System.Net.EndPointManager
5 //
6 // Author:
7 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 //
9 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if !NET_2_0
32
33 using System.Collections;
34 using System.Collections.Generic;
35 using System; using System.Net; namespace MonoHttp {
36         sealed class EndPointManager
37         {
38                 // Dictionary<IPAddress, Dictionary<int, EndPointListener>>
39                 static Hashtable ip_to_endpoints = new Hashtable ();
40                 
41                 private EndPointManager ()
42                 {
43                 }
44
45                 public static void AddListener (HttpListener listener)
46                 {
47                         ArrayList added = new ArrayList ();
48                         try {
49                                 lock (ip_to_endpoints) {
50                                         foreach (string prefix in listener.Prefixes) {
51                                                 AddPrefixInternal (prefix, listener);
52                                                 added.Add (prefix);
53                                         }
54                                 }
55                         } catch {
56                                 foreach (string prefix in added) {
57                                         RemovePrefix (prefix, listener);
58                                 }
59                                 throw;
60                         }
61                 }
62
63                 public static void AddPrefix (string prefix, HttpListener listener)
64                 {
65                         lock (ip_to_endpoints) {
66                                 AddPrefixInternal (prefix, listener);
67                         }
68                 }
69
70                 static void AddPrefixInternal (string p, HttpListener listener)
71                 {
72                         ListenerPrefix lp = new ListenerPrefix (p);
73                         if (lp.Path.IndexOf ('%') != -1)
74                                 throw new HttpListenerException (400, "Invalid path.");
75
76                         if (lp.Path.IndexOf ("//") != -1) // TODO: Code?
77                                 throw new HttpListenerException (400, "Invalid path.");
78
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);
82                 }
83
84                 static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
85                 {
86                         Hashtable p = null;  // Dictionary<int, EndPointListener>
87                         if (ip_to_endpoints.ContainsKey (addr)) {
88                                 p = (Hashtable) ip_to_endpoints [addr];
89                         } else {
90                                 p = new Hashtable ();
91                                 ip_to_endpoints [addr] = p;
92                         }
93
94                         EndPointListener epl = null;
95                         if (p.ContainsKey (port)) {
96                                 epl = (EndPointListener) p [port];
97                         } else {
98                                 epl = new EndPointListener (addr, port, secure);
99                                 p [port] = epl;
100                         }
101
102                         return epl;
103                 }
104
105                 public static void RemoveEndPoint (EndPointListener epl, IPEndPoint ep)
106                 {
107                         lock (ip_to_endpoints) {
108                                 // Dictionary<int, EndPointListener> p
109                                 Hashtable p = null;
110                                 p = (Hashtable) ip_to_endpoints [ep.Address];
111                                 p.Remove (ep.Port);
112                                 if (p.Count == 0) {
113                                         ip_to_endpoints.Remove (ep.Address);
114                                 }
115                                 epl.Close ();
116                         }
117                 }
118
119                 public static void RemoveListener (HttpListener listener)
120                 {
121                         lock (ip_to_endpoints) {
122                                 foreach (string prefix in listener.Prefixes) {
123                                         RemovePrefixInternal (prefix, listener);
124                                 }
125                         }
126                 }
127
128                 public static void RemovePrefix (string prefix, HttpListener listener)
129                 {
130                         lock (ip_to_endpoints) {
131                                 RemovePrefixInternal (prefix, listener);
132                         }
133                 }
134
135                 static void RemovePrefixInternal (string prefix, HttpListener listener)
136                 {
137                         ListenerPrefix lp = new ListenerPrefix (prefix);
138                         if (lp.Path.IndexOf ('%') != -1)
139                                 return;
140
141                         if (lp.Path.IndexOf ("//") != -1)
142                                 return;
143
144                         EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
145                         epl.RemovePrefix (lp, listener);
146                 }
147         }
148 }
149 #endif
150