1 #define EMBEDDED_IN_1_0
4 // System.Net.EndPointListener
7 // Gonzalo Paniagua Javier (gonzalo@novell.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.
34 using System.Net.Sockets;
35 using System.Collections;
36 using System.Security.Cryptography;
37 using System.Security.Cryptography.X509Certificates;
38 using Mono.Security.Authenticode;
40 using System; using System.Net; namespace MonoHttp {
42 sealed class EndPointListener : IHttpListenerContextBinder
46 Hashtable prefixes; // Dictionary <ListenerPrefix, HttpListener>
47 ArrayList unhandled; // List<ListenerPrefix> unhandled; host = '*'
48 ArrayList all; // List<ListenerPrefix> all; host = '+'
50 AsymmetricAlgorithm key;
53 public EndPointListener (IPAddress addr, int port, bool secure)
58 LoadCertificateAndKey (addr, port);
62 endpoint = new IPEndPoint (addr, port);
63 sock = new Socket (addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
66 sock.BeginAccept (OnAccept, this);
67 prefixes = new Hashtable ();
70 void LoadCertificateAndKey (IPAddress addr, int port)
72 // Actually load the certificate
74 string dirname = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
75 string path = Path.Combine (dirname, ".mono");
76 path = Path.Combine (path, "httplistener");
77 string cert_file = Path.Combine (path, String.Format ("{0}.cer", port));
78 string pvk_file = Path.Combine (path, String.Format ("{0}.pvk", port));
79 cert = new X509Certificate2 (cert_file);
80 key = PrivateKey.CreateFromFile (pvk_file).RSA;
86 static void OnAccept (IAsyncResult ares)
88 EndPointListener epl = (EndPointListener) ares.AsyncState;
89 Socket accepted = null;
91 accepted = epl.sock.EndAccept (ares);
93 // Anything to do here?
96 epl.sock.BeginAccept (OnAccept, epl);
98 if (accepted != null) {
107 if (accepted == null)
110 if (epl.secure && (epl.key == null)) {
114 HttpConnection conn = new HttpConnection (accepted, epl);
115 conn.BeginReadRequest ();
118 public bool BindContext (HttpListenerContext context)
120 HttpListenerRequest req = context.Request;
121 ListenerPrefix prefix;
122 HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
123 if (listener == null)
126 context.Listener = listener;
127 context.Connection.Prefix = prefix;
128 listener.RegisterContext (context);
132 public void UnbindContext (HttpListenerContext context)
134 if (context == null || context.Request == null)
137 HttpListenerRequest req = context.Request;
138 ListenerPrefix prefix;
139 HttpListener listener = SearchListener (req.UserHostName, req.RawUrl, out prefix);
140 if (listener != null)
141 listener.UnregisterContext (context);
144 HttpListener SearchListener (string host, string raw_url, out ListenerPrefix prefix)
150 //TODO: We should use a ReaderWriterLock between this and the add/remove operations.
152 int colon = host.IndexOf (':');
154 host = host.Substring (0, colon);
160 if (MonoHttp.Utility.MaybeUri (raw_url) && Uri.TryCreate (raw_url, UriKind.Absolute, out raw_uri))
162 try { raw_uri = new Uri (raw_url); } catch { raw_uri = null; } if (raw_uri != null)
165 path = raw_uri.PathAndQuery;
167 path = HttpUtility.UrlDecode (raw_url);
169 string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
171 HttpListener best_match = null;
172 int best_length = -1;
175 if (host != null && host != "") {
176 foreach (ListenerPrefix p in prefixes.Keys) {
177 string ppath = p.Path;
178 if (ppath.Length < best_length)
181 if (p.Host == host && (path.StartsWith (ppath) || path_slash.StartsWith (ppath))) {
182 best_length = ppath.Length;
183 best_match = (HttpListener) prefixes [p];
187 if (best_length != -1)
191 best_match = MatchFromList (host, path, unhandled, out prefix);
192 if (best_match != null)
195 best_match = MatchFromList (host, path, all, out prefix);
196 if (best_match != null)
202 HttpListener MatchFromList (string host, string path, ArrayList list, out ListenerPrefix prefix)
208 HttpListener best_match = null;
209 int best_length = -1;
211 foreach (ListenerPrefix p in list) {
212 string ppath = p.Path;
213 if (ppath.Length < best_length)
216 if (path.StartsWith (ppath)) {
217 best_length = ppath.Length;
218 best_match = p.Listener;
226 void AddSpecial (ArrayList coll, ListenerPrefix prefix)
231 foreach (ListenerPrefix p in coll) {
232 if (p.Path == prefix.Path) //TODO: code
233 throw new HttpListenerException (400, "Prefix already in use.");
239 void RemoveSpecial (ArrayList coll, ListenerPrefix prefix)
245 for (int i = 0; i < c; i++) {
246 ListenerPrefix p = (ListenerPrefix) coll [i];
247 if (p.Path == prefix.Path) {
255 void CheckIfRemove ()
257 if (prefixes.Count > 0)
260 if (unhandled != null && unhandled.Count > 0)
263 if (all != null && all.Count > 0)
266 EndPointManager.RemoveEndPoint (this, endpoint);
274 public void AddPrefix (ListenerPrefix prefix, HttpListener listener)
277 if (prefix.Host == "*") {
278 if (unhandled == null)
279 unhandled = new ArrayList ();
281 prefix.Listener = listener;
282 AddSpecial (unhandled, prefix);
286 if (prefix.Host == "+") {
288 all = new ArrayList ();
289 prefix.Listener = listener;
290 AddSpecial (all, prefix);
294 if (prefixes.ContainsKey (prefix)) {
295 HttpListener other = (HttpListener) prefixes [prefix];
296 if (other != listener) // TODO: code.
297 throw new HttpListenerException (400, "There's another listener for " + prefix);
301 prefixes [prefix] = listener;
305 public void RemovePrefix (ListenerPrefix prefix, HttpListener listener)
308 if (prefix.Host == "*") {
309 RemoveSpecial (unhandled, prefix);
313 if (prefix.Host == "+") {
314 RemoveSpecial (all, prefix);
318 if (prefixes.ContainsKey (prefix)) {
319 prefixes.Remove (prefix);