public ChunkedInputStream(HttpListenerContext context, Stream stream, byte[] buffer, int offset, int length) : base(stream, buffer, offset, length) { this.context = context; WebHeaderCollection coll = (WebHeaderCollection)context.Request.Headers; decoder = new ChunkStream(coll); }
void Cleanup(bool close_existing) { lock (registry) { if (close_existing) { // Need to copy this since closing will call UnregisterContext ICollection keys = registry.Keys; var all = new HttpListenerContext[keys.Count]; keys.CopyTo(all, 0); registry.Clear(); for (int i = all.Length - 1; i >= 0; i--) { all[i].Connection.Close(true); } } lock (connections.SyncRoot) { ICollection keys = connections.Keys; var conns = new HttpConnection[keys.Count]; keys.CopyTo(conns, 0); connections.Clear(); for (int i = conns.Length - 1; i >= 0; i--) { conns[i].Close(true); } } lock (ctx_queue) { var ctxs = (HttpListenerContext[])ctx_queue.ToArray(typeof(HttpListenerContext)); ctx_queue.Clear(); for (int i = ctxs.Length - 1; i >= 0; i--) { ctxs[i].Connection.Close(true); } } lock (wait_queue) { Exception exc = new ObjectDisposedException("listener"); foreach (ListenerAsyncResult ares in wait_queue) { ares.Complete(exc); } wait_queue.Clear(); } } }
internal void UnregisterContext(HttpListenerContext context) { lock (registry) { registry.Remove(context); } lock (ctx_queue) { int idx = ctx_queue.IndexOf(context); if (idx >= 0) { ctx_queue.RemoveAt(idx); } } }
internal AuthenticationSchemes SelectAuthenticationScheme(HttpListenerContext context) { if (AuthenticationSchemeSelectorDelegate != null) { return AuthenticationSchemeSelectorDelegate(context.Request); } else { return auth_schemes; } }
internal void RegisterContext(HttpListenerContext context) { lock (registry) { registry[context] = context; } ListenerAsyncResult ares = null; lock (wait_queue) { if (wait_queue.Count == 0) { lock (ctx_queue) { ctx_queue.Add(context); } } else { ares = (ListenerAsyncResult)wait_queue[0]; wait_queue.RemoveAt(0); } } if (ares != null) { ares.Complete(context); } }
internal void Complete(HttpListenerContext context, bool synch) { if (forward != null) { forward.Complete(context, synch); return; } this.synch = synch; this.context = context; lock (locker) { AuthenticationSchemes schemes = context.Listener.SelectAuthenticationScheme(context); if ((schemes == AuthenticationSchemes.Basic || context.Listener.AuthenticationSchemes == AuthenticationSchemes.Negotiate) && context.Request.Headers["Authorization"] == null) { context.Response.StatusCode = 401; context.Response.Headers["WWW-Authenticate"] = schemes + " realm=\"" + context.Listener.Realm + "\""; context.Response.OutputStream.Close(); IAsyncResult ares = context.Listener.BeginGetContext(cb, state); this.forward = (ListenerAsyncResult)ares; lock (forward.locker) { if (handle != null) { forward.handle = handle; } } ListenerAsyncResult next = forward; for (int i = 0; next.forward != null; i++) { if (i > 20) { Complete(new HttpListenerException(400, "Too many authentication errors")); } next = next.forward; } } else { completed = true; this.synch = false; if (handle != null) { handle.Set(); } if (cb != null) { ThreadPool.UnsafeQueueUserWorkItem(InvokeCB, this); } } } }
internal void Complete(HttpListenerContext context) { Complete(context, false); }
internal HttpListenerResponse(HttpListenerContext context) { this.context = context; }