Example #1
0
        public HttpListenerContext EndGetContext(IAsyncResult asyncResult)
        {
            this.CheckDisposed();
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            ListenerAsyncResult listenerAsyncResult = asyncResult as ListenerAsyncResult;

            if (listenerAsyncResult == null)
            {
                throw new ArgumentException("Wrong IAsyncResult.", "asyncResult");
            }
            if (listenerAsyncResult.EndCalled)
            {
                throw new InvalidOperationException("Cannot reuse this IAsyncResult.");
            }
            listenerAsyncResult.EndCalled = true;
            if (!listenerAsyncResult.IsCompleted)
            {
                listenerAsyncResult.AsyncWaitHandle.WaitOne();
            }
            object syncRoot = ((ICollection)this._waitQueue).SyncRoot;

            lock (syncRoot)
            {
                int num = this._waitQueue.IndexOf(listenerAsyncResult);
                if (num >= 0)
                {
                    this._waitQueue.RemoveAt(num);
                }
            }
            HttpListenerContext   context = listenerAsyncResult.GetContext();
            AuthenticationSchemes authenticationSchemes = this.SelectAuthenticationScheme(context);

            if (authenticationSchemes != AuthenticationSchemes.Anonymous)
            {
                context.SetUser(authenticationSchemes, this.Realm, this.UserCredentialsFinder);
            }
            return(context);
        }
        /// <summary>
        /// Ends an asynchronous operation to get an incoming request information.
        /// </summary>
        /// <remarks>
        /// This method completes an asynchronous operation started by calling the <see cref="BeginGetContext"/> method.
        /// </remarks>
        /// <returns>
        /// A <see cref="HttpListenerContext"/> that contains a client's request information.
        /// </returns>
        /// <param name="asyncResult">
        /// An <see cref="IAsyncResult"/> obtained by calling the <see cref="BeginGetContext"/> method.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="asyncResult"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="asyncResult"/> was not obtained by calling the <see cref="BeginGetContext"/> method.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// The EndGetContext method was already called for the specified <paramref name="asyncResult"/>.
        /// </exception>
        public HttpListenerContext EndGetContext(IAsyncResult asyncResult)
        {
            CheckDisposed();
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            ListenerAsyncResult ares = asyncResult as ListenerAsyncResult;

            if (ares == null)
            {
                throw new ArgumentException("Wrong IAsyncResult.", "asyncResult");
            }

            if (ares.EndCalled)
            {
                throw new InvalidOperationException("Cannot reuse this IAsyncResult.");
            }
            ares.EndCalled = true;

            if (!ares.IsCompleted)
            {
                ares.AsyncWaitHandle.WaitOne();
            }

            lock (((ICollection)wait_queue).SyncRoot) {
                int idx = wait_queue.IndexOf(ares);
                if (idx >= 0)
                {
                    wait_queue.RemoveAt(idx);
                }
            }

            HttpListenerContext context = ares.GetContext();

            context.ParseAuthentication(SelectAuthenticationScheme(context));
            return(context);            // This will throw on error.
        }
Example #3
0
        internal void RegisterContext(HttpListenerContext context)
        {
            lock (((ICollection)registry).SyncRoot)
                registry [context] = context;

            ListenerAsyncResult ares = null;

            lock (((ICollection)wait_queue).SyncRoot) {
                if (wait_queue.Count == 0)
                {
                    lock (((ICollection)ctx_queue).SyncRoot)
                        ctx_queue.Add(context);
                }
                else
                {
                    ares = wait_queue [0];
                    wait_queue.RemoveAt(0);
                }
            }
            if (ares != null)
            {
                ares.Complete(context);
            }
        }
Example #4
0
        /// <summary>
        /// Begins getting an incoming request information asynchronously.
        /// </summary>
        /// <remarks>
        /// This asynchronous operation must be completed by calling the <see cref="EndGetContext"/> method.
        /// Typically, the method is invoked by the <paramref name="callback"/> delegate.
        /// </remarks>
        /// <returns>
        /// An <see cref="IAsyncResult"/> that contains the status of the asynchronous operation.
        /// </returns>
        /// <param name="callback">
        /// An <see cref="AsyncCallback"/> delegate that references the method(s)
        /// called when the asynchronous operation completes.
        /// </param>
        /// <param name="state">
        /// An <see cref="object"/> that contains a user defined object to pass to the <paramref name="callback"/> delegate.
        /// </param>
        /// <exception cref="ObjectDisposedException">
        /// This object has been closed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// The <see cref="HttpListener"/> has not been started or is stopped currently.
        /// </exception>
        public IAsyncResult BeginGetContext(AsyncCallback callback, Object state)
        {
            CheckDisposed ();
            if (!listening)
                throw new InvalidOperationException ("Please, call Start before using this method.");

            ListenerAsyncResult ares = new ListenerAsyncResult (callback, state);

            // lock wait_queue early to avoid race conditions
            lock (((ICollection)wait_queue).SyncRoot) {
                lock (((ICollection)ctx_queue).SyncRoot) {
                    HttpListenerContext ctx = GetContextFromQueue ();
                    if (ctx != null) {
                        ares.Complete (ctx, true);
                        return ares;
                    }
                }

                wait_queue.Add (ares);
            }

            return ares;
        }
Example #5
0
        internal ListenerAsyncResult BeginGetContext(ListenerAsyncResult asyncResult)
        {
            CheckDisposed ();
              if (_prefixes.Count == 0)
            throw new InvalidOperationException ("The listener has no URI prefix on which listens.");

              if (!_listening)
            throw new InvalidOperationException ("The listener hasn't been started.");

              // Lock _waitQueue early to avoid race conditions.
              lock (_waitQueueSync) {
            lock (_ctxQueueSync) {
              var ctx = getContextFromQueue ();
              if (ctx != null) {
            asyncResult.Complete (ctx, true);
            return asyncResult;
              }
            }

            _waitQueue.Add (asyncResult);
              }

              return asyncResult;
        }
Example #6
0
    internal ListenerAsyncResult BeginGetContext (ListenerAsyncResult asyncResult)
    {
      CheckDisposed ();
      if (_prefixes.Count == 0)
        throw new InvalidOperationException (
          "Please, call AddPrefix before using this method.");

      if (!_listening)
        throw new InvalidOperationException (
          "Please, call Start before using this method.");

      // Lock _waitQueue early to avoid race conditions
      lock (((ICollection) _waitQueue).SyncRoot) {
        lock (((ICollection) _contextQueue).SyncRoot) {
          var context = getContextFromQueue ();
          if (context != null) {
            asyncResult.Complete (context, true);
            return asyncResult;
          }
        }

        _waitQueue.Add (asyncResult);
      }

      return asyncResult;
    }