public override void Close()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) + "::Close()");
            }
#endif
            if (_closeCalled)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) +
                                              "::Close() stream already closed returning");
                }
#endif
                return;
            }

            _closeCalled = true;
            if (DataAvailable)
            {
                //
                // drain stream data
                //
                for (;;)
                {
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) +
                                                  "::Close() draining LeftToRead:" + _leftToRead);
                    }
#endif
                    int read;
                    try
                    {
                        read = Read(_drainBuffer, 0, _drainBuffer.Length);
                    }
                    catch (Exception exception)
                    {
                        read = -1;
#if DEBUG
                        if (HttpTraceHelper.ExceptionCaught.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) +
                                                      "::Close() caught exception in Stream.Read(): " + exception);
                        }
#endif
                    }
                    if (read <= 0)
                    {
                        break;
                    }
                }
            }

            _connectionState.Request.Close();
        }
        public void HandleConnection()
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                          "::HandleConnection() _keepAlive:" + KeepAlive);
            }
#endif
            if (!_handleConnectionCalled)
            {
                _handleConnectionCalled = true;
                if (KeepAlive)
                {
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                                  "::HandleConnection() KeepAlive calling StartReceiving()");
                    }
#endif
                    //
                    // fix connection parsing state and buffer offsets
                    //
                    _connectionState.ParserState = ParseState.None;
                    _connectionState.StartReceiving();
                }
            }
        }
Пример #3
0
        public HttpListenerWebRequest GetRequest()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) + "::GetRequest()");
            }
#endif
            var asyncResult = BeginGetRequest(null, null) as HttpListenerAsyncResult;
            if (_timeout != System.Threading.Timeout.Infinite && !asyncResult.IsCompleted)
            {
                asyncResult.AsyncWaitHandle.WaitOne(_timeout, false);
                if (!asyncResult.IsCompleted)
                {
                    var exception = new Exception("Timeout");
#if DEBUG
                    if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                                  "::GetRequest() throwing: " + exception);
                    }
#endif
                    throw exception;
                }
            }
            return(asyncResult.Request);
        }
        public void Complete(bool completedSynchronously)
        {
            CompletedSynchronously = completedSynchronously;
            IsCompleted            = true;

            if (_asyncWaitHandle != null)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("ListenerAsyncResult#" + HttpTraceHelper.HashString(this) +
                                              "::GetRequestCallback() Signaling event");
                }
#endif

                _asyncWaitHandle.Set();
            }

            if (_asyncCallback != null)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("ListenerAsyncResult#" + HttpTraceHelper.HashString(this) +
                                              "::GetRequestCallback() Calling callback");
                }
#endif
                _asyncCallback(this);
            }
        }
        public void HandleConnection()
        {
            //
            // cleans up request side and decides on wether to close the connection or not.
            //
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                          "::HandleConnection() calling HttpListenerWebRequest#" +
                                          HttpTraceHelper.HashString(this) + "::HandleConnection()");
            }
#endif

            Request.HandleConnection();

            if (!KeepAlive)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                              "::HandleConnection() !KeepAlive closing the Connection");
                }
#endif
                Request.ConnectionState.Close();
            }
        }
Пример #6
0
        public void Start(IPEndPoint ipEndPoint, bool prefixFiltering)
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                          "::Start() ipEndPoint:" + ipEndPoint + " prefixFiltering:" + prefixFiltering);
            }
#endif
            if (_prefixFiltering)
            {
                Exception exception = new NotSupportedException();
#if DEBUG
                if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                              "::Start() throwing: " + exception);
                }
#endif
                throw exception;
            }
            _IPEndPoint = ipEndPoint;
            _timeout    = System.Threading.Timeout.Infinite;
            _requestReady.Reset();
            _prefixFiltering = prefixFiltering;

            _accepterSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // this might throw if the address is already in use
            _accepterSocket.Bind(_IPEndPoint);
            _accepterSocket.Listen((int)SocketOptionName.MaxConnections);
            _accepterSocket.BeginAccept(_staticAcceptCallback, this);
        }
        public Stream GetRequestStream()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                          "::GetRequestStream()");
            }
#endif
            if (_stream == null)
            {
                if (!_connectionState.Listener.Auto100Continue)
                {
                    //
                    // if the client shut off this feature and hasn't sent
                    // the 100 Continue then we will send it here.
                    // (if Delay100Continue is -1 we still won't send it at all)
                    //
                    Send100Continue();
                }
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                              "::GetRequestStream() creating ListenerRequestStream()");
                }
#endif
                _stream = new HttpListenerRequestStream(_connectionState, ContentLength, Chunked);
            }
            return(_stream);
        }
        public override void Close()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) + "::Close()");
            }
#endif
            if (_writeChunked)
            {
                //
                // send the trailer
                //
                var buffer = new byte[3] {
                    (byte)'0', 0x0D, 0x0A
                };
#if DEBUG
                if (HttpTraceHelper.Socket.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) +
                                              "::Close() calling Socket.Send() Length:3");
                }
#endif
                // we null out the Socket when we cleanup
                // make a local copy to avoid null reference exceptions
                var checkSocket = _httpListenerWebResponse.Request.ConnectionState.ConnectionSocket;
                if (checkSocket != null)
                {
                    checkSocket.Send(buffer, 0, 3, SocketFlags.None);
                }
            }

            _httpListenerWebResponse.HandleConnection();
        }
        public HttpListenerAsyncResult(AsyncCallback callback, object userState, HttpWebListener httpWebListener)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerAsyncResult#" + HttpTraceHelper.HashString(this) + "::.ctor()");
            }
#endif
            AsyncState     = userState;
            _asyncCallback = callback;
            Listener       = httpWebListener;
        }
        public override void SetLength(long value)
        {
            Exception exception = new NotSupportedException();

#if DEBUG
            if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) +
                                          "::SetLength() throwing: " + exception);
            }
#endif
            throw exception;
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            Exception exception = new NotSupportedException();

#if DEBUG
            if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) +
                                          "::Seek() throwing: " + exception);
            }
#endif
            throw exception;
        }
        public HttpListenerResponseStream(HttpListenerWebResponse httpListenerWebResponse, long contentLength,
                                          bool writeChunked)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) +
                                          "::.ctor() contentLength:" + contentLength + " writeChunked:" + writeChunked);
            }
#endif
            _httpListenerWebResponse = httpListenerWebResponse;
            _contentLength           = contentLength;
            _writeChunked            = writeChunked;
        }
Пример #13
0
        public void Close()
        {
            // we null out the Socket when we cleanup
            // make a local copy to avoid null reference exceptions
            var checkSocket = ConnectionSocket;

            // null out the Socket as soon as possible, it's still possible
            // that two or more callers will execute the method below on the same
            // instance. for now that's ok.
            ConnectionSocket = null;
            if (checkSocket != null)
            {
                // gracefully close (allow user to read the data)
                // ignore failures on the socket
                try
                {
#if DEBUG
                    if (HttpTraceHelper.Socket.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine(string.Format(
                                                      "ConnectionState#{0}::ctor() calling Socket.Shutdown()",
                                                      HttpTraceHelper.HashString(this)));
                    }
#endif
                    checkSocket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception exception)
                {
#if DEBUG
                    if (HttpTraceHelper.ExceptionCaught.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine(
                            string.Format("ConnectionState#{0}::Close() caught exception in Socket.Shutdown(): {1}",
                                          HttpTraceHelper.HashString(this), exception));
                    }
#endif
                }
#if DEBUG
                if (HttpTraceHelper.Socket.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::Close() calling Socket.Close()",
                                                            HttpTraceHelper.HashString(this)));
                }
#endif
                checkSocket.Close();
            }

            Interlocked.Decrement(ref _activeConnections);
        }
Пример #14
0
        /// <summary>
        ///  Callback used for asynchronously reading a request stream.
        ///  Used in order to process transfers will still accepting
        ///  new connections.
        /// </summary>
        /// <param name="asyncResult">The Async object representing the results of the read.</param>
        public void ReadCallback(IAsyncResult asyncResult)
        {
            try
            {
                var stateObject = asyncResult.AsyncState as HttpApplication;
                var readBytes   = stateObject.RequestStream.EndRead(asyncResult);

                if (readBytes > 0)
                {
                    stateObject.ReadBytes += readBytes;

                    if (stateObject.ReadBytes > 262144 /* 256k */)
                    {
                        throw new Exception("Request exceeded limit");
                    }

                    // Write data received to the memory stream buffer
                    stateObject.Buffer.Write(stateObject.ReadBuffer, 0, readBytes);
                    stateObject.RequestStream.BeginRead(stateObject.ReadBuffer, 0, stateObject.ReadBuffer.Length,
                                                        _readCB,
                                                        stateObject);
                }
                else
                {
                    // Look to see which handler should be called
                    // Rewind the stream
                    stateObject.Buffer.Position = 0;
                    var ns = stateObject.HttpRequest.RequestUri.Segments[1];
                    if (_nsModules.Contains(ns))
                    {
                        var nsHandler = (IHttpNamespaceHandler)_nsModules[ns];
                        nsHandler.ProcessRequest(stateObject);
                    }
                    else
                    {
                        HandleUnsupportedNamespace(stateObject);
                    }
                }
            }
            catch (Exception exception)
            {
#if DEBUG
                if (HttpTraceHelper.ExceptionCaught.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("Caught exception:" + exception);
                }
#endif
            }
        }
        public HttpListenerRequestStream(HttpConnectionState connectionState, long contentLength, bool readChunked)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) +
                                          "::.ctor() contentLength:" + contentLength + " readChunked:" + readChunked);
            }
#endif
            _connectionState  = connectionState;
            _contentLength    = contentLength;
            _leftToRead       = contentLength;
            _chunkParserState = ParseState.ChunkSize;
            _readChunked      = readChunked;
            DataAvailable     = _connectionState.Request.Uploading;
        }
        public HttpListenerWebResponse(HttpListenerWebRequest httpListenerRequest)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                          "::.ctor() keepAlive:" + httpListenerRequest.KeepAlive);
            }
#endif
            Request         = httpListenerRequest;
            Date            = DateTime.MinValue;
            Headers         = new WebHeaderCollection();
            ProtocolVersion = new Version(1, 1);
            SentHeaders     = false;
            KeepAlive       = httpListenerRequest.KeepAlive;
        }
Пример #17
0
        public HttpListenerWebRequest EndGetRequest(IAsyncResult asyncResult)
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                          "::EndGetRequest() asyncResult#" + HttpTraceHelper.HashString(asyncResult));
            }
#endif
            var castedAsyncResult = asyncResult as HttpListenerAsyncResult;
            if (!castedAsyncResult.IsCompleted)
            {
                castedAsyncResult.AsyncWaitHandle.WaitOne(System.Threading.Timeout.Infinite, false);
            }
            return(castedAsyncResult.Request);
        }
        public Stream GetResponseStream()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                          "::GetResponseStream()");
            }
#endif
            if (_stream == null)
            {
                //
                // if _bufferHeaders is true we won't send the headers here, but we'll
                // do it later. at the latest when the user closes the response stream
                //
                if (!BufferHeaders)
                {
                    //
                    // this call can throw, the exception will be presented to the user
                    // calling GetResponseStream()
                    //
                    SerializeHeaders();

                    // we null out the Socket when we cleanup
                    // make a local copy to avoid null reference exceptions
                    var checkSocket = Request.ConnectionState.ConnectionSocket;
                    if (checkSocket != null)
                    {
#if DEBUG
                        if (HttpTraceHelper.Socket.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                                      "::GetResponseStream() calling Socket.Send() Length:" +
                                                      HeadersBuffer.Length);
                        }
#endif
                        checkSocket.Send(HeadersBuffer);
                        SentHeaders = true;
                    }
                }

                _stream = new HttpListenerResponseStream(this, ContentLength, Chunked);
            }
            return(_stream);
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) + "::Write()");
            }
#endif
            Exception exception = new NotSupportedException();
#if DEBUG
            if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) +
                                          "::Write() throwing: " + exception);
            }
#endif
            throw exception;
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) + "::Read()");
            }
#endif
            Exception exception = new NotSupportedException();
#if DEBUG
            if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerResponseStream#" + HttpTraceHelper.HashString(this) +
                                          "::Read() throwing: " + exception);
            }
#endif
            throw exception;
        }
Пример #21
0
        public HttpConnectionState(Socket socket, int bufferSize, HttpWebListener httpWebListener)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::ctor() New client connected from: {1}",
                                                        HttpTraceHelper.HashString(this), socket.RemoteEndPoint));
            }
#endif
            Interlocked.Increment(ref _activeConnections);
            Listener         = httpWebListener;
            ConnectionSocket = socket;
            ConnectionBuffer = new byte[bufferSize];

            ParserState  = ParseState.None;
            EndOfOffset  = 0;
            ParsedOffset = 0;
        }
Пример #22
0
        public HttpListenerWebRequest(HttpConnectionState connectionState)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) + "::.ctor()");
            }
#endif
            _connectionState = connectionState;
#if DEBUG
            if (HttpTraceHelper.Socket.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                          "::.ctor() calling Socket.get_RemoteEndPoint()");
            }
#endif
            RemoteEndPoint = (IPEndPoint)(connectionState.ConnectionSocket.RemoteEndPoint);
        }
        public void Close()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) + "::Close()");
            }
#endif
            if (_stream == null)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                              "::Close() _sentHeaders:" + SentHeaders);
                }
#endif

                if (!SentHeaders)
                {
                    SerializeHeaders();
                    // we null out the Socket when we cleanup
                    // make a local copy to avoid null reference exceptions
                    var checkSocket = Request.ConnectionState.ConnectionSocket;
                    if (checkSocket != null)
                    {
#if DEBUG
                        if (HttpTraceHelper.Socket.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                                      "::Close() calling Socket.Send() Length:" + HeadersBuffer.Length);
                        }
#endif
                        checkSocket.Send(HeadersBuffer);
                        SentHeaders = true;
                    }
                }
                HandleConnection();
            }
            else
            {
                _stream.Close();
            }
        }
Пример #24
0
        public HttpListenerWebRequest GetNextRequest()
        {
            HttpListenerWebRequest httpListenerRequest = null;

            lock (_pendingRequests)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                              "::GetNextRequest() _pendingRequests.Count: " + _pendingRequests.Count);
                }
#endif

                if (_pendingRequests.Count > 0)
                {
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                                  "::GetNextRequest() calling Dequeue()");
                    }
#endif

                    httpListenerRequest = (HttpListenerWebRequest)_pendingRequests.Dequeue();

#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                                  "::GetNextRequest() calling Reset()");
                    }
#endif

                    _requestReady.Reset();
                }
            }
            return(httpListenerRequest);
        }
Пример #25
0
        public void Close()
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                          "::Close() closing Stream");
            }
#endif
            if (Uploading)
            {
                GetRequestStream().Close();
            }
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                          "::Close() calling HandleConnection()");
            }
#endif
            HandleConnection();
        }
Пример #26
0
        public HttpListenerWebResponse GetResponse()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                          "::GetResponse()");
            }
#endif
            if (_response == null)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) +
                                              "::GetResponse() creating HttpListenerWebResponse()");
                }
#endif
                _response = new HttpListenerWebResponse(this);
            }
            return(_response);
        }
Пример #27
0
        public void RemoveUriPrefix(Uri uriPrefix)
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                          "::RemoveUriPrefix() uriPrefix:" + uriPrefix);
            }
#endif
            if (_prefixFiltering)
            {
                Exception exception = new NotSupportedException();
#if DEBUG
                if (HttpTraceHelper.ExceptionThrown.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                              "::RemoveUriPrefix() throwing: " + exception);
                }
#endif
                throw exception;
            }
        }
        private static void GetRequestCallback(object stateObject, bool signaled)
        {
            var asyncResult = stateObject as HttpListenerAsyncResult;

#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ListenerAsyncResult#" + HttpTraceHelper.HashString(asyncResult) +
                                          "::GetRequestCallback()");
            }
#endif

            asyncResult.Request = asyncResult.Listener.GetNextRequest();

            if (asyncResult.Request == null)
            {
                //
                // false alarm, request went to another thread, queue again.
                //
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("ListenerAsyncResult#" + HttpTraceHelper.HashString(asyncResult) +
                                              "::GetRequestCallback() calling RegisterWaitForSingleObject()");
                }
#endif

                ThreadPool.RegisterWaitForSingleObject(
                    asyncResult.Listener.RequestReady,
                    StaticCallback,
                    asyncResult,
                    -1,
                    true);
            }
            else
            {
                asyncResult.Complete(!signaled);
            }
        }
Пример #29
0
        public IAsyncResult BeginGetRequest(AsyncCallback callback, object stateObject)
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) + "::BeginGetRequest()");
            }
#endif
            var asyncResult = new HttpListenerAsyncResult(callback, stateObject, this)
            {
                Request = GetNextRequest()
            };

            //
            // check to see if there are requests in the queue
            //

            if (asyncResult.Request == null)
            {
                //
                // if not go async
                //
                ThreadPool.RegisterWaitForSingleObject(
                    _requestReady,
                    HttpListenerAsyncResult.StaticCallback,
                    asyncResult,
                    -1,
                    true);
            }
            else
            {
                //
                // otherwise complete sync
                //
                asyncResult.Complete(true);
            }

            return(asyncResult);
        }
Пример #30
0
        public void Stop()
        {
#if DEBUG
            if (HttpTraceHelper.Api.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) + "::Stop()");
            }
#endif
            //
            // this Close() will cause any pending Accept()
            // to complete and throw, so the thread will cleanup and exit.
            //
            if (_accepterSocket != null)
            {
                _accepterSocket.Close();
                _accepterSocket = null;
            }

            lock (_acceptedConnections)
            {
                foreach (HttpConnectionState connectionState in _acceptedConnections)
                {
                    connectionState.Close();
                }
                _acceptedConnections.Clear();
            }

            lock (_pendingRequests)
            {
                foreach (HttpListenerWebRequest httpListenerRequest in  _pendingRequests)
                {
                    httpListenerRequest.Close();
                }
                _pendingRequests.Clear();
            }

            _requestReady.Reset();
            _IPEndPoint = null;
        }