示例#1
0
        /// <summary>
        /// Invoke.
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task Invoke(IDictionary <string, object> environment)
        {
            await _next(environment);

            var    response = new Microsoft.Owin.OwinResponse(environment);
            string encoding = response.Headers.Get("Content-Encoding");

            if (string.Equals(encoding, "gzip", StringComparison.OrdinalIgnoreCase))
            {
                response.Body = new System.IO.Compression.GZipStream(response.Body, System.IO.Compression.CompressionMode.Decompress);
            }
        }
示例#2
0
        private static Microsoft.Owin.OwinRequest CreateRequest(string url, string method)
        {
            var uri     = new Uri(url);
            var request = CreateOwinRequest();

            request.Protocol = "HTTP/1.1";
            request.Method   = method;
            var response = new Microsoft.Owin.OwinResponse(request.Environment);

            response.Body = new System.IO.MemoryStream();
            BuildRequestFromUri(request, uri);
            return(request);
        }
示例#3
0
        /// <summary>
        /// Invoke.
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task Invoke(IDictionary <string, object> environment)
        {
            await _next(environment);

            var request  = new Microsoft.Owin.OwinRequest(environment);
            var response = new Microsoft.Owin.OwinResponse(environment);

            if ((response.StatusCode >= 100 && response.StatusCode <= 199) || response.StatusCode == 204 || response.StatusCode == 304 || request.Method.Equals("HEAD", StringComparison.OrdinalIgnoreCase))
            {
                response.Body.Close();
                response.Body = System.IO.Stream.Null;
            }
        }
示例#4
0
        /// <summary>
        /// Constrói a url para redirecionamento.
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private string BuildRedirectUrl(Microsoft.Owin.OwinResponse response)
        {
            string location = response.Headers.Get("Location");
            Uri    uri;

            if (Uri.TryCreate(location, UriKind.Relative, out uri))
            {
                var previousRequest = new Microsoft.Owin.OwinRequest(response.Environment);
                var uriBuilder      = new UriBuilder(previousRequest.Uri);
                uriBuilder.Path = location;
                return(uriBuilder.ToString());
            }
            return(location);
        }
示例#5
0
        /// <summary>
        /// Invoke.
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task Invoke(IDictionary <string, object> environment)
        {
            await _next(environment);

            var response = new Microsoft.Owin.OwinResponse(environment);

            if (response.Body == System.IO.Stream.Null)
            {
                return;
            }
            string contentLengthRaw = response.Headers.Get("Content-Length");
            long   contentLength;

            if (contentLengthRaw != null && Int64.TryParse(contentLengthRaw, out contentLength))
            {
                response.Body = new Http.ContentLengthStream(response.Body, contentLength);
            }
        }
示例#6
0
        /// <summary>
        /// Invoke.
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task Invoke(IDictionary <string, object> environment)
        {
            await _next(environment);

            var response = new Microsoft.Owin.OwinResponse(environment);

            if (response.Body == System.IO.Stream.Null)
            {
                return;
            }
            string connection = response.Headers.Get("Connection");

            if (response.Protocol != null && response.Protocol.Equals("HTTP/1.1", StringComparison.OrdinalIgnoreCase) && String.Equals(connection, "Close", StringComparison.OrdinalIgnoreCase))
            {
                var ms = new System.IO.MemoryStream();
                using (response.Body)
                    await response.Body.CopyToAsync(ms).ConfigureAwait(continueOnCapturedContext: false);
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                response.Body = ms;
            }
        }
示例#7
0
        /// <summary>
        /// Invoke.
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task Invoke(IDictionary <string, object> environment)
        {
            int maxRedirects = _maxRedirects;

            while (maxRedirects >= 0)
            {
                await _next(environment);

                var response = new Microsoft.Owin.OwinResponse(environment);
                if (response.StatusCode == 302 || response.StatusCode == 301)
                {
                    string url = BuildRedirectUrl(response);
                    environment.Clear();
                    RequestBuilder.BuildGet(environment, url);
                }
                else
                {
                    break;
                }
                maxRedirects--;
            }
        }