async Task ProcessClientAsync(TcpClient client, CancellationTokenSource cancelTokenSource) { var stream = (Stream)client.GetStream(); var sslStream = _serverCertificate != null ? new SslStream(stream, false) : null; try { if (sslStream != null) { sslStream.AuthenticateAsServer(_serverCertificate, false, SslProtocols.Tls12, true); //DisplaySslStream(sslStream); //sslStream.ReadTimeout = 5000; //sslStream.WriteTimeout = 5000; stream = sslStream; } var lines = await new LineParser().Parse(stream, cancelTokenSource.Token); var httpRequest = HttpRequest.Parse(lines); if (httpRequest == null) { return; } var responseChannel = new HttpResponseChannel(client, stream); var httpContext = new HttpContext(httpRequest, responseChannel, cancelTokenSource.Token); await _handler(httpContext); } catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); if (e.InnerException != null) { Console.WriteLine($"Inner exception: {e.InnerException.Message}"); } stream.Close(); client.Close(); Console.WriteLine("Connection closed."); } }
/// <summary> /// Initializes a new instance of the <see cref="HttpContext"/> class. /// </summary> /// <param name="httpRequest">The HTTP request.</param> /// <param name="responseChannel">The response channel.</param> /// <param name="token">The token.</param> public HttpContext(HttpRequest httpRequest, HttpResponseChannel responseChannel, CancellationToken token) { HttpRequest = httpRequest; ResponseChannel = responseChannel; Token = token; }