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.Generic;
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 Dictionary<HttpListenerContext,HttpListenerContext> registry;
46 List<HttpListenerContext> ctx_queue;
47 List<ListenerAsyncResult> wait_queue;
49 public HttpListener ()
51 prefixes = new HttpListenerPrefixCollection (this);
52 registry = new Dictionary<HttpListenerContext,HttpListenerContext> ();
53 ctx_queue = new List<HttpListenerContext> ();
54 wait_queue = new List<ListenerAsyncResult> ();
55 auth_schemes = AuthenticationSchemes.Anonymous;
58 // TODO: Digest, NTLM and Negotiate require ControlPrincipal
59 public AuthenticationSchemes AuthenticationSchemes {
60 get { return auth_schemes; }
67 //TODO: when is this called?
68 public AuthenticationSchemeSelector AuthenticationSchemeSelectorDelegate {
69 get { return auth_selector; }
72 auth_selector = value;
76 public bool IgnoreWriteExceptions {
77 get { return ignore_write_exceptions; }
80 ignore_write_exceptions = value;
84 public bool IsListening {
85 get { return listening; }
88 public static bool IsSupported {
92 public HttpListenerPrefixCollection Prefixes {
100 public string Realm {
101 get { return realm; }
108 [MonoTODO ("Support for NTLM needs some loving.")]
109 public bool UnsafeConnectionNtlmAuthentication {
110 get { return unsafe_ntlm_auth; }
113 unsafe_ntlm_auth = value;
141 void Close (bool force)
144 EndPointManager.RemoveListener (this);
148 void Cleanup (bool close_existing)
151 if (close_existing) {
152 foreach (HttpListenerContext context in registry.Keys) {
153 context.Connection.Close ();
155 registry.Clear (); // Just in case.
159 foreach (HttpListenerContext context in ctx_queue)
160 context.Connection.Close ();
166 foreach (ListenerAsyncResult ares in wait_queue) {
167 ares.Complete ("Listener was closed.");
174 public IAsyncResult BeginGetContext (AsyncCallback callback, Object state)
178 throw new InvalidOperationException ("Please, call Start before using this method.");
180 ListenerAsyncResult ares = new ListenerAsyncResult (callback, state);
182 // lock wait_queue early to avoid race conditions
185 HttpListenerContext ctx = GetContextFromQueue ();
187 ares.Complete (ctx, true);
192 wait_queue.Add (ares);
198 public HttpListenerContext EndGetContext (IAsyncResult asyncResult)
201 if (asyncResult == null)
202 throw new ArgumentNullException ("asyncResult");
204 ListenerAsyncResult ares = asyncResult as ListenerAsyncResult;
206 throw new ArgumentException ("Wrong IAsyncResult.", "asyncResult");
208 if (!ares.IsCompleted)
209 ares.AsyncWaitHandle.WaitOne ();
212 int idx = wait_queue.IndexOf (ares);
214 wait_queue.RemoveAt (idx);
217 HttpListenerContext context = ares.GetContext ();
218 if (auth_schemes != AuthenticationSchemes.Anonymous) {
219 context.ParseAuthentication ();
221 return context; // This will throw on error.
224 public HttpListenerContext GetContext ()
226 // The prefixes are not checked when using the async interface!?
227 if (prefixes.Count == 0)
228 throw new InvalidOperationException ("Please, call AddPrefix before using this method.");
230 IAsyncResult ares = BeginGetContext (null, null);
231 return EndGetContext (ares);
240 EndPointManager.AddListener (this);
251 void IDisposable.Dispose ()
256 Close (true); //TODO: Should we force here or not?
260 internal void CheckDisposed ()
263 throw new ObjectDisposedException (GetType ().ToString ());
266 // Must be called with a lock on ctx_queue
267 HttpListenerContext GetContextFromQueue ()
269 if (ctx_queue.Count == 0)
272 HttpListenerContext context = ctx_queue [0];
273 ctx_queue.RemoveAt (0);
277 internal void RegisterContext (HttpListenerContext context)
280 Monitor.Enter (registry);
281 registry [context] = context;
282 Monitor.Enter (wait_queue);
283 Monitor.Enter (ctx_queue);
284 if (wait_queue.Count == 0) {
285 ctx_queue.Add (context);
287 ListenerAsyncResult ares = wait_queue [0];
288 wait_queue.RemoveAt (0);
289 ares.Complete (context);
292 Monitor.Exit (ctx_queue);
293 Monitor.Exit (wait_queue);
294 Monitor.Exit (registry);
298 internal void UnregisterContext (HttpListenerContext context)
301 Monitor.Enter (registry);
302 Monitor.Enter (ctx_queue);
303 int idx = ctx_queue.IndexOf (context);
305 ctx_queue.RemoveAt (idx);
306 registry.Remove (context);
308 Monitor.Exit (ctx_queue);
309 Monitor.Exit (registry);