示例#1
0
        public Func <Stream, Stream> GetEncoder(HttpHeaderValueCollection <StringWithQualityHeaderValue> list)
        {
            if (list != null && list.Count() > 0)
            {
                var headerValue = list.OrderByDescending(e => e.Quality ?? 1.0D)
                                  .Where(e => !e.Quality.HasValue ||
                                         e.Quality.Value > 0.0D)
                                  .FirstOrDefault(e => supported.Keys
                                                  .Contains(e.Value, StringComparer.OrdinalIgnoreCase));

                if (headerValue != null)
                {
                    return(GetStreamForSchema(headerValue.Value));
                }

                if (list.Any(e => e.Value == "*" &&
                             (!e.Quality.HasValue || e.Quality.Value > 0.0D)))
                {
                    var encoding = supported.Keys.Where(se =>
                                                        !list.Any(e =>
                                                                  e.Value.Equals(se, StringComparison.OrdinalIgnoreCase) &&
                                                                  e.Quality.HasValue &&
                                                                  e.Quality.Value == 0.0D))
                                   .FirstOrDefault();
                    if (encoding != null)
                    {
                        return(GetStreamForSchema(encoding));
                    }
                }

                if (list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase) &&
                             e.Quality.HasValue && e.Quality.Value == 0.0D))
                {
                    throw new NegotiationFailedException();
                }

                // Case 4: Client is not willing to accept one of the encodings
                // we support and is not willing to accept identity
                if (list.Any(e => e.Value == "*" &&
                             (e.Quality.HasValue || e.Quality.Value == 0.0D)))
                {
                    if (!list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new NegotiationFailedException();
                    }
                }
            }
            return(null);
        }