2 // System.Net.HttpListener
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
31 using System.Collections;
32 using System.Threading;
34 namespace System.Net {
35 public sealed class HttpListener : IDisposable {
36 AuthenticationSchemes auth_schemes;
37 HttpListenerPrefixCollection prefixes;
38 AuthenticationSchemeSelector auth_selector;
40 bool ignore_write_exceptions;
41 bool unsafe_ntlm_auth;
45 Hashtable registry; // Dictionary<HttpListenerContext,HttpListenerContext>
46 ArrayList ctx_queue; // List<HttpListenerContext> ctx_queue;
47 ArrayList wait_queue; // List<ListenerAsyncResult> wait_queue;
49 public HttpListener ()
51 prefixes = new HttpListenerPrefixCollection (this);
52 registry = new Hashtable ();
53 ctx_queue = new ArrayList ();
54 wait_queue = new ArrayList ();
55 auth_schemes = AuthenticationSchemes.Anonymous;
58 // TODO: Digest, NTLM and Negotiate require ControlPrincipal
59 public AuthenticationSchemes AuthenticationSchemes {
60 get { return auth_schemes; }
67 public AuthenticationSchemeSelector AuthenticationSchemeSelectorDelegate {
68 get { return auth_selector; }
71 auth_selector = value;
75 public bool IgnoreWriteExceptions {
76 get { return ignore_write_exceptions; }
79 ignore_write_exceptions = value;
83 public bool IsListening {
84 get { return listening; }
87 public static bool IsSupported {
91 public HttpListenerPrefixCollection Prefixes {
100 get { return realm; }
107 [MonoTODO ("Support for NTLM needs some loving.")]
108 public bool UnsafeConnectionNtlmAuthentication {
109 get { return unsafe_ntlm_auth; }
112 unsafe_ntlm_auth = value;
142 void Close (bool force)
145 EndPointManager.RemoveListener (this);
149 void Cleanup (bool close_existing)
152 if (close_existing) {
153 foreach (HttpListenerContext context in registry.Keys) {
154 context.Connection.Close ();
156 registry.Clear (); // Just in case.
160 foreach (HttpListenerContext context in ctx_queue)
161 context.Connection.Close ();
167 foreach (ListenerAsyncResult ares in wait_queue) {
168 ares.Complete ("Listener was closed.");
175 public IAsyncResult BeginGetContext (AsyncCallback callback, Object state)
179 throw new InvalidOperationException ("Please, call Start before using this method.");
181 ListenerAsyncResult ares = new ListenerAsyncResult (callback, state);
183 // lock wait_queue early to avoid race conditions
186 HttpListenerContext ctx = GetContextFromQueue ();
188 ares.Complete (ctx, true);
193 wait_queue.Add (ares);
199 public HttpListenerContext EndGetContext (IAsyncResult asyncResult)
202 if (asyncResult == null)
203 throw new ArgumentNullException ("asyncResult");
205 ListenerAsyncResult ares = asyncResult as ListenerAsyncResult;
207 throw new ArgumentException ("Wrong IAsyncResult.", "asyncResult");
209 if (!ares.IsCompleted)
210 ares.AsyncWaitHandle.WaitOne ();
213 int idx = wait_queue.IndexOf (ares);
215 wait_queue.RemoveAt (idx);
218 HttpListenerContext context = ares.GetContext ();
219 context.ParseAuthentication (SelectAuthenticationScheme (context));
220 return context; // This will throw on error.
223 internal AuthenticationSchemes SelectAuthenticationScheme (HttpListenerContext context)
225 if (AuthenticationSchemeSelectorDelegate != null)
226 return AuthenticationSchemeSelectorDelegate (context.Request);
231 public HttpListenerContext GetContext ()
233 // The prefixes are not checked when using the async interface!?
234 if (prefixes.Count == 0)
235 throw new InvalidOperationException ("Please, call AddPrefix before using this method.");
237 IAsyncResult ares = BeginGetContext (null, null);
238 return EndGetContext (ares);
247 EndPointManager.AddListener (this);
258 void IDisposable.Dispose ()
263 Close (true); //TODO: Should we force here or not?
267 internal void CheckDisposed ()
270 throw new ObjectDisposedException (GetType ().ToString ());
273 // Must be called with a lock on ctx_queue
274 HttpListenerContext GetContextFromQueue ()
276 if (ctx_queue.Count == 0)
279 HttpListenerContext context = (HttpListenerContext) ctx_queue [0];
280 ctx_queue.RemoveAt (0);
284 internal void RegisterContext (HttpListenerContext context)
287 Monitor.Enter (registry);
288 registry [context] = context;
289 Monitor.Enter (wait_queue);
290 Monitor.Enter (ctx_queue);
291 if (wait_queue.Count == 0) {
292 ctx_queue.Add (context);
294 ListenerAsyncResult ares = (ListenerAsyncResult) wait_queue [0];
295 wait_queue.RemoveAt (0);
296 ares.Complete (context);
299 Monitor.Exit (ctx_queue);
300 Monitor.Exit (wait_queue);
301 Monitor.Exit (registry);
305 internal void UnregisterContext (HttpListenerContext context)
308 Monitor.Enter (registry);
309 Monitor.Enter (ctx_queue);
310 int idx = ctx_queue.IndexOf (context);
312 ctx_queue.RemoveAt (idx);
313 registry.Remove (context);
315 Monitor.Exit (ctx_queue);
316 Monitor.Exit (registry);