public void ComprehendRequestHeaders()
        {
            string etag = _etag;

            string[] ifMatch = _request.GetHeaderUnmodified("If-Match");
            if (ifMatch != null)
            {
                _ifMatchState = PreconditionState.PreconditionFailed;
                foreach (var segment in new HeaderSegmentCollection(ifMatch))
                {
                    if (segment.Data.Equals(etag, StringComparison.Ordinal))
                    {
                        _ifMatchState = PreconditionState.ShouldProcess;
                        break;
                    }
                }
            }

            string[] ifNoneMatch = _request.GetHeaderUnmodified("If-None-Match");
            if (ifNoneMatch != null)
            {
                _ifNoneMatchState = PreconditionState.ShouldProcess;
                foreach (var segment in new HeaderSegmentCollection(ifNoneMatch))
                {
                    if (segment.Data.Equals(etag, StringComparison.Ordinal))
                    {
                        _ifNoneMatchState = PreconditionState.NotModified;
                        break;
                    }
                }
            }

            string ifModifiedSince = _request.GetHeader("If-Modified-Since");

            if (ifModifiedSince != null)
            {
                bool matches = string.Equals(ifModifiedSince, _lastModifiedString, StringComparison.Ordinal);
                _ifModifiedSinceState = matches ? PreconditionState.NotModified : PreconditionState.ShouldProcess;
            }

            string ifUnmodifiedSince = _request.GetHeader("If-Unmodified-Since");

            if (ifUnmodifiedSince != null)
            {
                bool matches = string.Equals(ifModifiedSince, _lastModifiedString, StringComparison.Ordinal);
                _ifUnmodifiedSinceState = matches ? PreconditionState.ShouldProcess : PreconditionState.PreconditionFailed;
            }
        }
Esempio n. 2
0
        public static bool TryParseAuthorizationHeader(OwinRequest request,
                                                       out AuthenticationHeaderValue authorization, out string error)
        {
            string[] unparsedHeaders = request.GetHeaderUnmodified("Authorization");

            string unparsedHeader;

            if (unparsedHeaders != null)
            {
                if (unparsedHeaders.Length == 1)
                {
                    unparsedHeader = unparsedHeaders[0];
                }
                else
                {
                    authorization = null;
                    // TODO: Get error messages from resources.
                    error = "Only one Authorization header may be present.";
                    return(false);
                }
            }
            else
            {
                unparsedHeader = null;
            }

            AuthenticationHeaderValue parsedHeader;

            if (unparsedHeader != null)
            {
                if (!AuthenticationHeaderValue.TryParse(unparsedHeader, out parsedHeader))
                {
                    authorization = null;
                    // TODO: Get error messages from resources.
                    error = "The Authorization header is malformed.";
                    return(false);
                }
            }
            else
            {
                parsedHeader = null;
            }

            authorization = parsedHeader;
            error         = null;
            return(true);
        }
        private IEncoding SelectCompression(IDictionary <string, object> environment)
        {
            var request = new OwinRequest(environment);

            var bestAccept = new Accept {
                Encoding = "identity", Quality = 0
            };
            IEncoding bestEncoding = null;

            string[] acceptEncoding = request.GetHeaderUnmodified("accept-encoding");
            if (acceptEncoding != null)
            {
                foreach (var segment in new HeaderSegmentCollection(acceptEncoding))
                {
                    if (!segment.Data.HasValue)
                    {
                        continue;
                    }
                    Accept accept = Parse(segment.Data.Value);
                    if (accept.Quality == 0 || accept.Quality < bestAccept.Quality)
                    {
                        continue;
                    }
                    IEncoding compression = _options.EncodingProvider.GetCompression(accept.Encoding);
                    if (compression == null)
                    {
                        continue;
                    }
                    bestAccept   = accept;
                    bestEncoding = compression;
                    if (accept.Quality == 1000)
                    {
                        break;
                    }
                }
            }
            return(bestEncoding);
        }