Пример #1
0
        public void DisableBuffering_IfEnvironmentIsNull_DoesNotThrow()
        {
            // Arrange
            IDictionary <string, object> environment = null;
            IOwinRequest request = CreateStubRequest(environment);

            // Act & Assert
            Assert.DoesNotThrow(() => request.DisableBuffering());
        }
Пример #2
0
        public void DisableBuffering_IfServerDisableResponseBufferingIsNotAction_DoesNotThrow()
        {
            // Arrange
            object nonAction = new object();
            IDictionary <string, object> environment = CreateStubEnvironment(nonAction);
            IOwinRequest request = CreateStubRequest(environment);

            // Act & Assert
            Assert.DoesNotThrow(() => request.DisableBuffering());
        }
Пример #3
0
        public void DisableBuffering_IfServerDisableResponseBufferingIsAbsent_DoesNotThrow()
        {
            // Arrange
            Mock <IDictionary <string, object> > environmentMock = new Mock <IDictionary <string, object> >(MockBehavior.Strict);
            IDictionary <string, object>         environment     = CreateStubEnvironment(null, hasDisableBufferingAction: false);
            IOwinRequest request = CreateStubRequest(environment);

            // Act & Assert
            Assert.DoesNotThrow(() => request.DisableBuffering());
        }
        private async Task InvokeCore(
            IOwinContext context,
            IOwinRequest owinRequest,
            IOwinResponse owinResponse
            )
        {
            CancellationToken cancellationToken = owinRequest.CallCancelled;
            HttpContent       requestContent;

            bool bufferInput = _bufferPolicySelector.UseBufferedInputStream(hostContext: context);

            if (!bufferInput)
            {
                owinRequest.DisableBuffering();
            }

            if (!owinRequest.Body.CanSeek && bufferInput)
            {
                requestContent = await CreateBufferedRequestContentAsync(
                    owinRequest,
                    cancellationToken
                    );
            }
            else
            {
                requestContent = CreateStreamedRequestContent(owinRequest);
            }

            HttpRequestMessage request = CreateRequestMessage(owinRequest, requestContent);

            MapRequestProperties(request, context);

            SetPrincipal(owinRequest.User);

            HttpResponseMessage response = null;
            bool callNext;

            try
            {
                response = await _messageInvoker.SendAsync(request, cancellationToken);

                // Handle null responses
                if (response == null)
                {
                    throw Error.InvalidOperation(OwinResources.SendAsync_ReturnedNull);
                }

                // Handle soft 404s where no route matched - call the next component
                if (IsSoftNotFound(request, response))
                {
                    callNext = true;
                }
                else
                {
                    callNext = false;

                    // Compute Content-Length before calling UseBufferedOutputStream because the default implementation
                    // accesses that header and we want to catch any exceptions calling TryComputeLength here.

                    if (
                        response.Content == null ||
                        await ComputeContentLengthAsync(
                            request,
                            response,
                            owinResponse,
                            cancellationToken
                            )
                        )
                    {
                        bool bufferOutput = _bufferPolicySelector.UseBufferedOutputStream(response);

                        if (!bufferOutput)
                        {
                            owinResponse.DisableBuffering();
                        }
                        else if (response.Content != null)
                        {
                            response = await BufferResponseContentAsync(
                                request,
                                response,
                                cancellationToken
                                );
                        }

                        if (
                            await PrepareHeadersAsync(
                                request,
                                response,
                                owinResponse,
                                cancellationToken
                                )
                            )
                        {
                            await SendResponseMessageAsync(
                                request,
                                response,
                                owinResponse,
                                cancellationToken
                                );
                        }
                    }
                }
            }
            finally
            {
                request.DisposeRequestResources();
                request.Dispose();
                if (response != null)
                {
                    response.Dispose();
                }
            }

            // Call the next component if no route matched
            if (callNext && Next != null)
            {
                await Next.Invoke(context);
            }
        }
        private async Task InvokeCore(IOwinContext context, IOwinRequest owinRequest, IOwinResponse owinResponse)
        {
            CancellationToken cancellationToken = owinRequest.CallCancelled;
            HttpContent requestContent;

            bool bufferInput = _bufferPolicySelector.UseBufferedInputStream(hostContext: context);

            if (!bufferInput)
            {
                owinRequest.DisableBuffering();
            }

            if (!owinRequest.Body.CanSeek && bufferInput)
            {
                requestContent = await CreateBufferedRequestContentAsync(owinRequest, cancellationToken);
            }
            else
            {
                requestContent = CreateStreamedRequestContent(owinRequest);
            }

            HttpRequestMessage request = CreateRequestMessage(owinRequest, requestContent);
            MapRequestProperties(request, context);

            SetPrincipal(owinRequest.User);

            HttpResponseMessage response = null;
            bool callNext;

            try
            {
                response = await _messageInvoker.SendAsync(request, cancellationToken);

                // Handle null responses
                if (response == null)
                {
                    throw Error.InvalidOperation(OwinResources.SendAsync_ReturnedNull);
                }

                // Handle soft 404s where no route matched - call the next component
                if (IsSoftNotFound(request, response))
                {
                    callNext = true;
                }
                else
                {
                    callNext = false;

                    // Compute Content-Length before calling UseBufferedOutputStream because the default implementation
                    // accesses that header and we want to catch any exceptions calling TryComputeLength here.

                    if (response.Content == null
                        || await ComputeContentLengthAsync(request, response, owinResponse, cancellationToken))
                    {
                        bool bufferOutput = _bufferPolicySelector.UseBufferedOutputStream(response);

                        if (!bufferOutput)
                        {
                            owinResponse.DisableBuffering();
                        }
                        else if (response.Content != null)
                        {
                            response = await BufferResponseContentAsync(request, response, cancellationToken);
                        }

                        if (await PrepareHeadersAsync(request, response, owinResponse, cancellationToken))
                        {
                            await SendResponseMessageAsync(request, response, owinResponse, cancellationToken);
                        }
                    }
                }
            }
            finally
            {
                request.DisposeRequestResources();
                request.Dispose();
                if (response != null)
                {
                    response.Dispose();
                }
            }

            // Call the next component if no route matched
            if (callNext && Next != null)
            {
                await Next.Invoke(context);
            }
        }