Esempio n. 1
0
        private async Task AcceptConnectionsAsync(CancellationToken cancellationToken)
        {
            try
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    var client = await _socketWrapper.AcceptAsync().ConfigureAwait(false);

                    HttpNetTrace.Information(nameof(HttpServer), "Client '{0}' connected.", client.Identifier);

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    Task.Run(async() => await HandleClientConnectionAsync(client).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception exception)
            {
                HttpNetTrace.Error(nameof(HttpServer), exception, "Unhandled exception while accepting clients.");
            }
            finally
            {
                _cancellationTokenSource?.Cancel(false);
            }
        }
Esempio n. 2
0
        internal async Task HandleHttpRequestAsync(HttpContext httpContext)
        {
            try
            {
                var handler = RequestHandler;
                if (handler == null)
                {
                    return;
                }

                await RequestHandler.HandleHttpRequestAsync(httpContext);
            }
            catch (Exception exception)
            {
                HttpNetTrace.Error(nameof(HttpServer), exception, "Unhandled exception while handling received HTTP request.");
            }
        }
Esempio n. 3
0
        public Task ProcessRequestAsync(HttpContextPipelineHandlerContext context)
        {
            var body = "<no body>";

            if (context.HttpContext.Request.Body != null)
            {
                using (var streamReader = new StreamReader(context.HttpContext.Request.Body, Encoding.UTF8, false, 1024, true))
                {
                    body = streamReader.ReadToEnd();
                }

                context.HttpContext.Request.Body.Position = 0;
            }

            HttpNetTrace.Verbose(nameof(TraceHandler), context.HttpContext.Request.Method + " " + context.HttpContext.Request.Uri + " " + body);
            return(Task.FromResult(0));
        }
Esempio n. 4
0
        public async Task ProcessAsync()
        {
            RawHttpRequest httpRequest;

            try
            {
                httpRequest = await RequestReader.ReadAsync(_clientSession.CancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                _clientSession.Close();
                return;
            }
            catch (Exception exception)
            {
                HttpNetTrace.Error(nameof(HttpSessionHandler), exception, "Unhandled exceptio while processing HTTP request.");
                _clientSession.Close();
                return;
            }

            var httpResponse = new RawHttpResponse
            {
                Version    = httpRequest.Version,
                Headers    = new Dictionary <string, string>(),
                StatusCode = (int)HttpStatusCode.OK
            };

            var httpContext = new HttpContext(httpRequest, httpResponse, _clientSession, this);
            await _clientSession.HandleHttpRequestAsync(httpContext);

            if (httpContext.Response != null)
            {
                await ResponseWriter.WriteAsync(httpContext.Response, _clientSession.CancellationToken);

                HttpNetTrace.Verbose(nameof(HttpSessionHandler), "Response '{0}' sent to '{1}'.", httpContext.Response.StatusCode, _clientSession.Client.Identifier);
            }

            if (httpContext.CloseConnection)
            {
                _clientSession.Close();
            }
        }
Esempio n. 5
0
 private async Task HandleClientConnectionAsync(IClientSocketWrapper client)
 {
     using (var clientSession = new ClientSession(client, this, _options))
     {
         try
         {
             await clientSession.RunAsync().ConfigureAwait(false);
         }
         catch (OperationCanceledException)
         {
         }
         catch (Exception exception)
         {
             HttpNetTrace.Error(nameof(HttpServer), exception, "Unhandled exception while handling cient connection.");
         }
         finally
         {
             HttpNetTrace.Information(nameof(HttpServer), "Client '{0}' disconnected.", client.Identifier);
             await client.DisconnectAsync();
         }
     }
 }
Esempio n. 6
0
 public Task ProcessResponseAsync(HttpContextPipelineHandlerContext context)
 {
     HttpNetTrace.Verbose(nameof(TraceHandler), context.HttpContext.Response.StatusCode + " " + context.HttpContext.Response.ReasonPhrase);
     return(Task.FromResult(0));
 }