2 // System.Net.EndPointListener
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #if NET_2_0 && SECURITY_DEP
32 using System.Net.Sockets;
33 using System.Collections;
34 using System.Security.Cryptography;
35 using System.Security.Cryptography.X509Certificates;
36 using Mono.Security.Authenticode;
38 namespace System.Net {
39 sealed class EndPointListener
43 Hashtable prefixes; // Dictionary <ListenerPrefix, HttpListener>
44 ArrayList unhandled; // List<ListenerPrefix> unhandled; host = '*'
45 ArrayList all; // List<ListenerPrefix> all; host = '+'
46 X509Certificate2 cert;
47 AsymmetricAlgorithm key;
50 public EndPointListener (IPAddress addr, int port, bool secure)
54 LoadCertificateAndKey (addr, port);
57 endpoint = new IPEndPoint (addr, port);
58 sock = new Socket (addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
61 sock.BeginAccept (OnAccept, this);
62 prefixes = new Hashtable ();
65 void LoadCertificateAndKey (IPAddress addr, int port)
67 // Actually load the certificate
69 string dirname = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
70 string path = Path.Combine (dirname, ".mono");
71 path = Path.Combine (path, "httplistener");
72 string cert_file = Path.Combine (path, String.Format ("{0}.cer", port));
73 string pvk_file = Path.Combine (path, String.Format ("{0}.pvk", port));
74 cert = new X509Certificate2 (cert_file);
75 key = PrivateKey.CreateFromFile (pvk_file).RSA;
81 static void OnAccept (IAsyncResult ares)
83 EndPointListener epl = (EndPointListener) ares.AsyncState;
84 Socket accepted = null;
86 accepted = epl.sock.EndAccept (ares);
88 // Anything to do here?
91 epl.sock.BeginAccept (OnAccept, epl);
93 if (accepted != null) {
102 if (accepted == null)
105 if (epl.secure && (epl.cert == null || epl.key == null)) {
109 HttpConnection conn = new HttpConnection (accepted, epl, epl.secure, epl.cert, epl.key);
110 conn.BeginReadRequest ();
113 public bool BindContext (HttpListenerContext context)
115 HttpListenerRequest req = context.Request;
116 ListenerPrefix prefix;
117 HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
118 if (listener == null)
121 context.Listener = listener;
122 context.Connection.Prefix = prefix;
123 listener.RegisterContext (context);
127 public void UnbindContext (HttpListenerContext context)
129 if (context == null || context.Request == null)
132 HttpListenerRequest req = context.Request;
133 ListenerPrefix prefix;
134 HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
135 if (listener != null)
136 listener.UnregisterContext (context);
139 HttpListener SearchListener (string host, string raw_url, out ListenerPrefix prefix)
145 //TODO: We should use a ReaderWriterLock between this and the add/remove operations.
147 int colon = host.IndexOf (':');
149 host = host.Substring (0, colon);
154 if (Uri.MaybeUri (raw_url) && Uri.TryCreate (raw_url, UriKind.Absolute, out raw_uri))
155 path = raw_uri.PathAndQuery;
157 path = HttpUtility.UrlDecode (raw_url);
159 string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
161 HttpListener best_match = null;
162 int best_length = -1;
165 if (host != null && host != "") {
166 foreach (ListenerPrefix p in prefixes.Keys) {
167 string ppath = p.Path;
168 if (ppath.Length < best_length)
171 if (p.Host == host && (path.StartsWith (ppath) || path_slash.StartsWith (ppath))) {
172 best_length = ppath.Length;
173 best_match = (HttpListener) prefixes [p];
177 if (best_length != -1)
181 best_match = MatchFromList (host, path, unhandled, out prefix);
182 if (best_match != null)
185 best_match = MatchFromList (host, path, all, out prefix);
186 if (best_match != null)
192 HttpListener MatchFromList (string host, string path, ArrayList list, out ListenerPrefix prefix)
198 HttpListener best_match = null;
199 int best_length = -1;
201 foreach (ListenerPrefix p in list) {
202 string ppath = p.Path;
203 if (ppath.Length < best_length)
206 if (path.StartsWith (ppath)) {
207 best_length = ppath.Length;
208 best_match = p.Listener;
216 void AddSpecial (ArrayList coll, ListenerPrefix prefix)
221 foreach (ListenerPrefix p in coll) {
222 if (p.Path == prefix.Path) //TODO: code
223 throw new HttpListenerException (400, "Prefix already in use.");
229 void RemoveSpecial (ArrayList coll, ListenerPrefix prefix)
235 for (int i = 0; i < c; i++) {
236 ListenerPrefix p = (ListenerPrefix) coll [i];
237 if (p.Path == prefix.Path) {
245 void CheckIfRemove ()
247 if (prefixes.Count > 0)
250 if (unhandled != null && unhandled.Count > 0)
253 if (all != null && all.Count > 0)
256 EndPointManager.RemoveEndPoint (this, endpoint);
264 public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
267 if (prefix.Host == "*") {
268 if (unhandled == null)
269 unhandled = new ArrayList ();
271 prefix.Listener = listener;
272 AddSpecial (unhandled, prefix);
276 if (prefix.Host == "+") {
278 all = new ArrayList ();
279 prefix.Listener = listener;
280 AddSpecial (all, prefix);
284 if (prefixes.ContainsKey (prefix)) {
285 HttpListener other = (HttpListener) prefixes [prefix];
286 if (other != listener) // TODO: code.
287 throw new HttpListenerException (400, "There's another listener for " + prefix);
291 prefixes [prefix] = listener;
295 public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
298 if (prefix.Host == "*") {
299 RemoveSpecial (unhandled, prefix);
303 if (prefix.Host == "+") {
304 RemoveSpecial (all, prefix);
308 if (prefixes.ContainsKey (prefix)) {
309 prefixes.Remove (prefix);