2 // System.Net.HttpListener
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 // Copyright 2011 Xamarin Inc.
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.
33 using System.Collections;
34 using System.Threading;
36 using System.Threading.Tasks;
40 namespace System.Net {
41 public sealed class HttpListener : IDisposable {
42 AuthenticationSchemes auth_schemes;
43 HttpListenerPrefixCollection prefixes;
44 AuthenticationSchemeSelector auth_selector;
46 bool ignore_write_exceptions;
47 bool unsafe_ntlm_auth;
51 Hashtable registry; // Dictionary<HttpListenerContext,HttpListenerContext>
52 ArrayList ctx_queue; // List<HttpListenerContext> ctx_queue;
53 ArrayList wait_queue; // List<ListenerAsyncResult> wait_queue;
54 Hashtable connections;
56 public HttpListener ()
58 prefixes = new HttpListenerPrefixCollection (this);
59 registry = new Hashtable ();
60 connections = Hashtable.Synchronized (new Hashtable ());
61 ctx_queue = new ArrayList ();
62 wait_queue = new ArrayList ();
63 auth_schemes = AuthenticationSchemes.Anonymous;
66 // TODO: Digest, NTLM and Negotiate require ControlPrincipal
67 public AuthenticationSchemes AuthenticationSchemes {
68 get { return auth_schemes; }
75 public AuthenticationSchemeSelector AuthenticationSchemeSelectorDelegate {
76 get { return auth_selector; }
79 auth_selector = value;
83 public bool IgnoreWriteExceptions {
84 get { return ignore_write_exceptions; }
87 ignore_write_exceptions = value;
91 public bool IsListening {
92 get { return listening; }
95 public static bool IsSupported {
99 public HttpListenerPrefixCollection Prefixes {
107 public string Realm {
108 get { return realm; }
115 [MonoTODO ("Support for NTLM needs some loving.")]
116 public bool UnsafeConnectionNtlmAuthentication {
117 get { return unsafe_ntlm_auth; }
120 unsafe_ntlm_auth = value;
150 void Close (bool force)
153 EndPointManager.RemoveListener (this);
157 void Cleanup (bool close_existing)
160 if (close_existing) {
161 // Need to copy this since closing will call UnregisterContext
162 ICollection keys = registry.Keys;
163 var all = new HttpListenerContext [keys.Count];
164 keys.CopyTo (all, 0);
166 for (int i = all.Length - 1; i >= 0; i--)
167 all [i].Connection.Close (true);
170 lock (connections.SyncRoot) {
171 ICollection keys = connections.Keys;
172 var conns = new HttpConnection [keys.Count];
173 keys.CopyTo (conns, 0);
174 connections.Clear ();
175 for (int i = conns.Length - 1; i >= 0; i--)
176 conns [i].Close (true);
179 var ctxs = (HttpListenerContext []) ctx_queue.ToArray (typeof (HttpListenerContext));
181 for (int i = ctxs.Length - 1; i >= 0; i--)
182 ctxs [i].Connection.Close (true);
186 Exception exc = new ObjectDisposedException ("listener");
187 foreach (ListenerAsyncResult ares in wait_queue) {
195 public IAsyncResult BeginGetContext (AsyncCallback callback, Object state)
199 throw new InvalidOperationException ("Please, call Start before using this method.");
201 ListenerAsyncResult ares = new ListenerAsyncResult (callback, state);
203 // lock wait_queue early to avoid race conditions
206 HttpListenerContext ctx = GetContextFromQueue ();
208 ares.Complete (ctx, true);
213 wait_queue.Add (ares);
219 public HttpListenerContext EndGetContext (IAsyncResult asyncResult)
222 if (asyncResult == null)
223 throw new ArgumentNullException ("asyncResult");
225 ListenerAsyncResult ares = asyncResult as ListenerAsyncResult;
227 throw new ArgumentException ("Wrong IAsyncResult.", "asyncResult");
229 throw new ArgumentException ("Cannot reuse this IAsyncResult");
230 ares.EndCalled = true;
232 if (!ares.IsCompleted)
233 ares.AsyncWaitHandle.WaitOne ();
236 int idx = wait_queue.IndexOf (ares);
238 wait_queue.RemoveAt (idx);
241 HttpListenerContext context = ares.GetContext ();
242 context.ParseAuthentication (SelectAuthenticationScheme (context));
243 return context; // This will throw on error.
246 internal AuthenticationSchemes SelectAuthenticationScheme (HttpListenerContext context)
248 if (AuthenticationSchemeSelectorDelegate != null)
249 return AuthenticationSchemeSelectorDelegate (context.Request);
254 public HttpListenerContext GetContext ()
256 // The prefixes are not checked when using the async interface!?
257 if (prefixes.Count == 0)
258 throw new InvalidOperationException ("Please, call AddPrefix before using this method.");
260 ListenerAsyncResult ares = (ListenerAsyncResult) BeginGetContext (null, null);
262 return EndGetContext (ares);
271 EndPointManager.AddListener (this);
282 void IDisposable.Dispose ()
287 Close (true); //TODO: Should we force here or not?
292 public Task<HttpListenerContext> GetContextAsync ()
294 return Task<HttpListenerContext>.Factory.FromAsync (BeginGetContext, EndGetContext, null);
298 internal void CheckDisposed ()
301 throw new ObjectDisposedException (GetType ().ToString ());
304 // Must be called with a lock on ctx_queue
305 HttpListenerContext GetContextFromQueue ()
307 if (ctx_queue.Count == 0)
310 HttpListenerContext context = (HttpListenerContext) ctx_queue [0];
311 ctx_queue.RemoveAt (0);
315 internal void RegisterContext (HttpListenerContext context)
318 registry [context] = context;
320 ListenerAsyncResult ares = null;
322 if (wait_queue.Count == 0) {
324 ctx_queue.Add (context);
326 ares = (ListenerAsyncResult) wait_queue [0];
327 wait_queue.RemoveAt (0);
331 ares.Complete (context);
334 internal void UnregisterContext (HttpListenerContext context)
337 registry.Remove (context);
339 int idx = ctx_queue.IndexOf (context);
341 ctx_queue.RemoveAt (idx);
345 internal void AddConnection (HttpConnection cnc)
347 connections [cnc] = cnc;
350 internal void RemoveConnection (HttpConnection cnc)
352 connections.Remove (cnc);