Exemple #1
0
 public HttpContext(RawHttpRequest request, RawHttpResponse response, ClientSession clientSession, HttpSessionHandler sessionHandler)
 {
     ClientSession  = clientSession ?? throw new ArgumentNullException(nameof(clientSession));
     SessionHandler = sessionHandler ?? throw new ArgumentNullException(nameof(sessionHandler));
     Request        = request ?? throw new ArgumentNullException(nameof(request));
     Response       = response ?? throw new ArgumentNullException(nameof(response));
 }
Exemple #2
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();
            }
        }
Exemple #3
0
        public async Task ProcessRequestAsync(HttpContextPipelineHandlerContext context)
        {
            var bodyLength = 0;

            if (context.HttpContext.Request.Headers.TryGetValue(HttpHeader.ContentLength, out var v))
            {
                bodyLength = int.Parse(v);
            }

            if (bodyLength == 0)
            {
                context.HttpContext.Request.Body = new MemoryStream(0);
                return;
            }

            if (context.HttpContext.Request.Headers.ValueEquals(HttpHeader.Expect, "100-Continue"))
            {
                var response = new RawHttpResponse
                {
                    Version    = context.HttpContext.Request.Version,
                    StatusCode = (int)HttpStatusCode.Continue
                };

                await context.HttpContext.SessionHandler.ResponseWriter.WriteAsync(response, context.HttpContext.ClientSession.CancellationToken);
            }

            while (context.HttpContext.SessionHandler.RequestReader.BufferLength < bodyLength)
            {
                await context.HttpContext.SessionHandler.RequestReader.FetchChunk(context.HttpContext.ClientSession.CancellationToken);
            }

            context.HttpContext.Request.Body = new MemoryStream(bodyLength);
            for (var i = 0; i < bodyLength; i++)
            {
                context.HttpContext.Request.Body.WriteByte(context.HttpContext.SessionHandler.RequestReader.DequeueFromBuffer());
            }

            context.HttpContext.Request.Body.Position = 0;
        }
        public void Http_SerializeHttpRequest()
        {
            var response = new RawHttpResponse
            {
                StatusCode = (int)HttpStatusCode.BadRequest,
                Body       = new MemoryStream(Encoding.UTF8.GetBytes("{\"text\":1234}")),
                Headers    =
                {
                    ["A"] = 1.ToString(),
                    ["B"] = "x"
                }
            };

            var memoryStream = new MemoryStream();
            var serializer   = new RawHttpResponseWriter(memoryStream, HttpServerOptions.Default);

            serializer.WriteAsync(response, CancellationToken.None).Wait();

            var requiredBuffer = Convert.FromBase64String("SFRUUC8xLjEgNDAwIEJhZFJlcXVlc3QNCkE6MQ0KQjp4DQpDb250ZW50LVR5cGU6dGV4dC9wbGFpbjsgY2hhcnNldD11dGYtOA0KQ29udGVudC1MZW5ndGg6MTMNCg0KeyJ0ZXh0IjoxMjM0fQ==");

            Assert.IsTrue(memoryStream.ToArray().SequenceEqual(requiredBuffer));
        }