Example #1
0
        internal HttpContextBuilder(IHttpApplication <Context> application, bool allowSynchronousIO)
        {
            _application       = application ?? throw new ArgumentNullException(nameof(application));
            AllowSynchronousIO = allowSynchronousIO;
            _httpContext       = new DefaultHttpContext();

            var request = _httpContext.Request;

            request.Protocol = "HTTP/1.1";
            request.Method   = HttpMethods.Get;

            _httpContext.Features.Set <IHttpBodyControlFeature>(this);
            _httpContext.Features.Set <IHttpResponseFeature>(_responseFeature);
            _httpContext.Features.Set <IHttpRequestLifetimeFeature>(_requestLifetimeFeature);

            _responseStream       = new ResponseStream(ReturnResponseMessageAsync, AbortRequest, () => AllowSynchronousIO);
            _responseFeature.Body = _responseStream;
        }
Example #2
0
        internal HttpContextBuilder(IHttpApplication <Context> application)
        {
            _application = application ?? throw new ArgumentNullException(nameof(application));
            _httpContext = new DefaultHttpContext();

            var request = _httpContext.Request;

            request.Protocol = "HTTP/1.1";
            request.Method   = HttpMethods.Get;

            _httpContext.Features.Set <IHttpResponseFeature>(_responseFeature);
            var requestLifetimeFeature = new HttpRequestLifetimeFeature();

            requestLifetimeFeature.RequestAborted = _requestAbortedSource.Token;
            _httpContext.Features.Set <IHttpRequestLifetimeFeature>(requestLifetimeFeature);

            _responseStream       = new ResponseStream(ReturnResponseMessageAsync, AbortRequest);
            _responseFeature.Body = _responseStream;
        }
Example #3
0
        internal HttpContextBuilder(ApplicationWrapper application, bool allowSynchronousIO, bool preserveExecutionContext)
        {
            _application              = application ?? throw new ArgumentNullException(nameof(application));
            AllowSynchronousIO        = allowSynchronousIO;
            _preserveExecutionContext = preserveExecutionContext;
            _httpContext              = new DefaultHttpContext();
            _responseFeature          = new ResponseFeature(Abort);

            var request = _httpContext.Request;

            request.Protocol = "HTTP/1.1";
            request.Method   = HttpMethods.Get;

            _httpContext.Features.Set <IHttpBodyControlFeature>(this);
            _httpContext.Features.Set <IHttpResponseFeature>(_responseFeature);
            _httpContext.Features.Set <IHttpResponseStartFeature>(_responseFeature);
            _httpContext.Features.Set <IHttpRequestLifetimeFeature>(_requestLifetimeFeature);
            _httpContext.Features.Set <IHttpResponseTrailersFeature>(_responseTrailersFeature);

            _responseStream       = new ResponseStream(ReturnResponseMessageAsync, AbortRequest, () => AllowSynchronousIO, () => _responseReadCompleteCallback?.Invoke(_httpContext));
            _responseFeature.Body = _responseStream;
        }
Example #4
0
            internal RequestState(HttpRequestMessage request, PathString pathBase, IHttpApplication <Context> application)
            {
                _request              = request;
                _application          = application;
                _responseTcs          = new TaskCompletionSource <HttpResponseMessage>();
                _requestAbortedSource = new CancellationTokenSource();
                _pipelineFinished     = false;

                if (request.RequestUri.IsDefaultPort)
                {
                    request.Headers.Host = request.RequestUri.Host;
                }
                else
                {
                    request.Headers.Host = request.RequestUri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped);
                }

                var contextFeatures = new FeatureCollection();

                contextFeatures.Set <IHttpRequestFeature>(new RequestFeature());
                _responseFeature = new ResponseFeature();
                contextFeatures.Set <IHttpResponseFeature>(_responseFeature);
                Context = application.CreateContext(contextFeatures);
                var httpContext = Context.HttpContext;

                var serverRequest = httpContext.Request;

                serverRequest.Protocol = "HTTP/" + request.Version.ToString(2);
                serverRequest.Scheme   = request.RequestUri.Scheme;
                serverRequest.Method   = request.Method.ToString();

                var        fullPath = PathString.FromUriComponent(request.RequestUri);
                PathString remainder;

                if (fullPath.StartsWithSegments(pathBase, out remainder))
                {
                    serverRequest.PathBase = pathBase;
                    serverRequest.Path     = remainder;
                }
                else
                {
                    serverRequest.PathBase = PathString.Empty;
                    serverRequest.Path     = fullPath;
                }

                serverRequest.QueryString = QueryString.FromUriComponent(request.RequestUri);

                foreach (var header in request.Headers)
                {
                    serverRequest.Headers.Append(header.Key, header.Value.ToArray());
                }
                var requestContent = request.Content;

                if (requestContent != null)
                {
                    foreach (var header in request.Content.Headers)
                    {
                        serverRequest.Headers.Append(header.Key, header.Value.ToArray());
                    }
                }

                _responseStream                 = new ResponseStream(ReturnResponseMessage, AbortRequest);
                httpContext.Response.Body       = _responseStream;
                httpContext.Response.StatusCode = 200;
                httpContext.RequestAborted      = _requestAbortedSource.Token;
            }