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 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(); } }
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 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 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 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 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 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(); } } }
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; }
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); }
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; }
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 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 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 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; }
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 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 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; }
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(); } }
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(); }
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); }
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); }
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); } }
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; }
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; } }
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); }
public void Send100Continue() { #if DEBUG if (HttpTraceHelper.Api.TraceVerbose) { HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) + "::Send100Continue()"); } #endif // // don't send if we don't need to // if (Delay100Continue < 0 || _sent100Continue) { return; } // // make sure we've interpreted headers // InterpretHeaders(); var expect = Headers["Expect"]; if (expect != null && expect.ToLower(CultureInfo.InvariantCulture).IndexOf("100-continue") >= 0 && Uploading) { // // send 100-continue here // _sent100Continue = true; if (Delay100Continue > 0) { // // this feature is useful for testing // #if DEBUG if (HttpTraceHelper.InternalLog.TraceVerbose) { HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) + "::Send100Continue() delaying Send100Continue() for:" + Delay100Continue); } #endif Thread.Sleep(Delay100Continue); } // we null out the Socket when we cleanup // make a local copy to avoid null reference exceptions var checkSocket = _connectionState.ConnectionSocket; if (checkSocket != null) { #if DEBUG if (HttpTraceHelper.InternalLog.TraceVerbose) { HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) + "::Send100Continue() Send100Continue() sending 100 Continue on the socket"); } if (HttpTraceHelper.Socket.TraceVerbose) { HttpTraceHelper.WriteLine("HttpListenerWebRequest#" + HttpTraceHelper.HashString(this) + "::Send100Continue() calling Socket.Send()"); } #endif checkSocket.Send(HttpWebListener.ResponseTo100ContinueBytes); } } }