private async void Process() { try { while (_client.Connected) { // TODO : Configuration. var limitedStream = new NotFlushingStream(new LimitedStream(_stream)); var request = await _requestProvider.Provide(new MyStreamReader(limitedStream)).ConfigureAwait(false); if (request != null) { UpdateLastOperationTime(); var context = new HttpContext(request, _client.RemoteEndPoint); Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.RemoteEndPoint); await _requestHandler(context).ConfigureAwait(false); if (context.Response != null) { var streamWriter = new StreamWriter(limitedStream) { AutoFlush = false }; await WriteResponse(context, streamWriter).ConfigureAwait(false); await limitedStream.ExplicitFlushAsync().ConfigureAwait(false); if (!request.Headers.KeepAliveConnection() || context.Response.CloseConnection) { _client.Close(); } } UpdateLastOperationTime(); } else { _client.Close(); } } } catch (Exception e) { // Hate people who make bad calls. Logger.WarnException(string.Format("Error while serving : {0}", _remoteEndPoint), e); _client.Close(); } Logger.InfoFormat("Lost Client {0}", _remoteEndPoint); }
private async void Process() { try { while (_client.Connected) { var request = await _requestProvider.Provide(_inputStream).ConfigureAwait(false); if (request != null) { var context = new HttpContext(request); Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.RemoteEndPoint); var getResponse = BuildHandlers(context)(); await getResponse.ConfigureAwait(false); var response = context.Response; if (response != null) { await response.WriteResponse(_outputStream).ConfigureAwait(false); if (!request.Headers.KeepAliveConnection() || response.CloseConnection) { _client.Close(); } } } else { _client.Close(); } } } catch (SocketException) { } catch (IOException) { // Socket exceptions on read will be re-thrown as IOException by BufferedStream } Logger.InfoFormat("Lost Client {0}", _remoteEndPoint); }
private async void Process() { try { while (_client.Connected) { // TODO : Configuration. var limitedStream = new LimitedStream(_stream, readLimit: 1024*1024, writeLimit: 1024*1024); var streamReader = new StreamReader(limitedStream); var request = await _requestProvider.Provide(streamReader).ConfigureAwait(false); if (request != null) { UpdateLastOperationTime(); var context = new HttpContext(request, _client.RemoteEndPoint); Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.RemoteEndPoint); await _requestHandler(context).ConfigureAwait(false); if (context.Response != null) { var streamWriter = new StreamWriter(limitedStream); await WriteResponse(context, streamWriter); } UpdateLastOperationTime(); } else { _client.Close(); } } } catch (Exception e) { // Hate people who make bad calls. Logger.Warn(string.Format("Error while serving : {0}", _remoteEndPoint), e); _client.Close(); } Logger.InfoFormat("Lost Client {0}", _remoteEndPoint); }
private async void Process() { try { while (_client.Connected) { // TODO : Extract read and write limit to configuration. var requestStream = new LimitedStream(_stream, readLimit: 1024*1024, writeLimit: 1024*1024); var request = await _requestProvider.Provide(new HttpStreamReader(requestStream, Encoding.UTF8)).ConfigureAwait(false); if (request != null) { var context = new HttpContext(request); Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.RemoteEndPoint); var getResponse = BuildHandlers(context)(); await getResponse.ConfigureAwait(false); var response = context.Response; if (response != null) { var streamWriter = new StreamWriter(requestStream); // Headers await response.WriteHeaders(streamWriter).ConfigureAwait(false); // Cookies if (context.Cookies.Touched) { await streamWriter.WriteAsync(context.Cookies.ToCookieData()) .ConfigureAwait(false); await streamWriter.FlushAsync().ConfigureAwait(false); } // Empty Line await requestStream.WriteAsync(CrLfBuffer, 0, CrLfBuffer.Length).ConfigureAwait(false); // Body await response.WriteResponse(streamWriter).ConfigureAwait(false); if (!request.Headers.KeepAliveConnection() || response.CloseConnection) { _client.Close(); } } } else { _client.Close(); } } } catch (SocketException) { } catch (IOException) { // Socket exceptions on read will be re-thrown as IOException by BufferedStream // ALSO : LimitedStream throws IOException on limit exceed. } catch (Exception e) { // Hate people who make bad calls. Logger.Warn(string.Format("Error while serving : {0} : {1}{2}", _remoteEndPoint, Environment.NewLine, e)); _client.Close(); } Logger.InfoFormat("Lost Client {0}", _remoteEndPoint); }
private async Task WriteResponse(HttpContext context, StreamWriter writer) { IHttpResponse response = context.Response; IHttpRequest request = context.Request; // Headers await writer.WriteLineAsync(string.Format("HTTP/1.1 {0} {1}", (int)response.ResponseCode, response.ResponseCode)) .ConfigureAwait(false); foreach (var header in response.Headers) { await writer.WriteLineAsync(string.Format("{0}: {1}", header.Key, header.Value)).ConfigureAwait(false); } // Cookies if (context.Cookies.Touched) { await writer.WriteAsync(context.Cookies.ToCookieData()) .ConfigureAwait(false); } // Empty Line await writer.WriteLineAsync().ConfigureAwait(false); // Body await response.WriteBody(writer).ConfigureAwait(false); await writer.FlushAsync().ConfigureAwait(false); }
private async Task WriteResponse(HttpContext context, StreamWriter writer) { IHttpResponse response = context.Response; IHttpRequest request = context.Request; // Headers await writer.WriteLineAsync(string.Format("HTTP/1.1 {0} {1}", (int)response.ResponseCode, response.ResponseCode)); foreach (var header in response.Headers) { await writer.WriteLineAsync(string.Format("{0}: {1}", header.Key, header.Value)); } // Cookies if (context.Cookies.Touched) { await writer.WriteAsync(context.Cookies.ToCookieData()) .ConfigureAwait(false); } await writer.FlushAsync().ConfigureAwait(false); // Empty Line await writer.BaseStream.WriteAsync(CrLfBuffer, 0, CrLfBuffer.Length).ConfigureAwait(false); // Body await response.WriteBody(writer).ConfigureAwait(false); if (!request.Headers.KeepAliveConnection() || response.CloseConnection) { _client.Close(); } else { await writer.FlushAsync().ConfigureAwait(false); await writer.BaseStream.FlushAsync().ConfigureAwait(false); } }