예제 #1
0
        private static MediaTypeFormatterMatch CreateMatch(double?quality, MediaTypeFormatterMatchRanking ranking)
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            MediaTypeHeaderValue   mediaType = new MediaTypeHeaderValue("text/test");

            return(new MediaTypeFormatterMatch(formatter, mediaType, quality, ranking));
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaTypeFormatterMatch"/> class.
 /// </summary>
 /// <param name="formatter">The matching formatter.</param>
 /// <param name="mediaType">The media type. Can be <c>null</c> in which case the media type <c>application/octet-stream</c> is used.</param>
 /// <param name="quality">The quality of the match. Can be <c>null</c> in which case it is considered a full match with a value of 1.0</param>
 /// <param name="ranking">The kind of match.</param>
 public MediaTypeFormatterMatch(MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, double?quality, MediaTypeFormatterMatchRanking ranking)
 {
     Formatter = formatter;
     MediaType = mediaType != null ? mediaType : MediaTypeConstants.ApplicationOctetStreamMediaType;
     Quality   = quality ?? FormattingUtilities.Match;
     Ranking   = ranking;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaTypeFormatterMatch"/> class.
 /// </summary>
 /// <param name="formatter">The matching formatter.</param>
 /// <param name="mediaType">
 /// The media type. Can be <c>null</c> in which case the media type <c>application/octet-stream</c> is used.
 /// </param>
 /// <param name="quality">
 /// The quality of the match. Can be <c>null</c> in which case it is considered a full match with a value of
 /// 1.0.
 /// </param>
 /// <param name="ranking">The kind of match.</param>
 public MediaTypeFormatterMatch(
     MediaTypeFormatter formatter,
     MediaTypeHeaderValue mediaType,
     double? quality,
     MediaTypeFormatterMatchRanking ranking)
 {
     Formatter = formatter;
     MediaType = mediaType != null ? mediaType : MediaTypeConstants.ApplicationOctetStreamMediaType;
     Quality = quality ?? FormattingUtilities.Match;
     Ranking = ranking;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaTypeFormatterMatch"/> class.
        /// </summary>
        /// <param name="formatter">The matching formatter.</param>
        /// <param name="mediaType">The media type. Can be <c>null</c> in which case the media type <c>application/octet-stream</c> is used.</param>
        /// <param name="quality">The quality of the match. Can be <c>null</c> in which case it is considered a full match with a value of 1.0</param>
        /// <param name="ranking">The kind of match.</param>
        public MediaTypeFormatterMatch(MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, double? quality, MediaTypeFormatterMatchRanking ranking)
        {
            if (formatter == null)
            {
                throw Error.ArgumentNull("formatter");
            }

            this.Formatter = formatter;
            this.MediaType = mediaType != null ? mediaType.Clone() : MediaTypeConstants.ApplicationOctetStreamMediaType;
            this.Quality = quality ?? FormattingUtilities.Match;
            this.Ranking = ranking;
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaTypeFormatterMatch"/> class.
        /// </summary>
        /// <param name="formatter">The matching formatter.</param>
        /// <param name="mediaType">The media type. Can be <c>null</c> in which case the media type <c>application/octet-stream</c> is used.</param>
        /// <param name="quality">The quality of the match. Can be <c>null</c> in which case it is considered a full match with a value of 1.0</param>
        /// <param name="ranking">The kind of match.</param>
        public MediaTypeFormatterMatch(MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, double?quality, MediaTypeFormatterMatchRanking ranking)
        {
            if (formatter == null)
            {
                throw Error.ArgumentNull("formatter");
            }

            Formatter = formatter;
            MediaType = mediaType != null?mediaType.Clone() : MediaTypeConstants.ApplicationOctetStreamMediaType;

            Quality = quality ?? FormattingUtilities.Match;
            Ranking = ranking;
        }
        /// <summary>
        /// Determines whether two <see cref="MediaTypeHeaderValue"/> instances match. The instance
        /// <paramref name="mediaType1"/> is said to match <paramref name="mediaType2"/> if and only if
        /// <paramref name="mediaType1"/> is a strict subset of the values and parameters of
        /// <paramref name="mediaType2"/>.
        /// That is, if the media type and media type parameters of <paramref name="mediaType1"/> are all present
        /// and match those of <paramref name="mediaType2"/> then it is a match even though
        /// <paramref name="mediaType2"/> may have additional parameters.
        /// </summary>
        /// <param name="mediaType1">The first media type.</param>
        /// <param name="mediaType2">The second media type.</param>
        /// <param name="mediaType2Range">
        /// Indicates whether <paramref name="mediaType2"/> is a regular media type, a subtype media range, or a full
        /// media range.
        /// </param>
        /// <returns><c>true</c> if this is a subset of <paramref name="mediaType2"/>; false otherwise.</returns>
        public static bool IsSubsetOf(
            this MediaTypeHeaderValue mediaType1,
            MediaTypeHeaderValue mediaType2,
            out MediaTypeFormatterMatchRanking mediaType2Range)
        {
            // Performance-sensitive
            Debug.Assert(mediaType1 != null);

            if (mediaType2 == null)
            {
                mediaType2Range = MediaTypeFormatterMatchRanking.None;
                return false;
            }

            var parsedMediaType1 = new ParsedMediaTypeHeaderValue(mediaType1);
            var parsedMediaType2 = new ParsedMediaTypeHeaderValue(mediaType2);
            mediaType2Range = parsedMediaType2.IsAllMediaRange ? MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderAllMediaRange :
                parsedMediaType2.IsSubtypeMediaRange ? MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderSubtypeMediaRange :
                MediaTypeFormatterMatchRanking.None;

            if (!parsedMediaType1.TypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderAllMediaRange)
                {
                    return false;
                }
            }
            else if (!parsedMediaType1.SubTypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderSubtypeMediaRange)
                {
                    return false;
                }
            }
            else
            {
                mediaType2Range = MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderLiteral;
            }

            // So far we either have a full match or a subset match. Now check that all of
            // mediaType1's parameters are present and equal in mediatype2
            // Optimize for the common case where the parameters inherit from Collection<T> and cache the count which
            // is faster for Collection<T>.
            var parameters1 = mediaType1.Parameters.AsCollection();
            var parameterCount1 = parameters1.Count;
            var parameters2 = mediaType2.Parameters.AsCollection();
            var parameterCount2 = parameters2.Count;
            for (var i = 0; i < parameterCount1; i++)
            {
                var parameter1 = parameters1[i];
                var found = false;
                for (var j = 0; j < parameterCount2; j++)
                {
                    var parameter2 = parameters2[j];
                    if (parameter1.Equals(parameter2))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// Determines whether two <see cref="MediaTypeHeaderValue"/> instances match. The instance
        /// <paramref name="mediaType1"/> is said to match <paramref name="mediaType2"/> if and only if
        /// <paramref name="mediaType1"/> is a strict subset of the values and parameters of
        /// <paramref name="mediaType2"/>.
        /// That is, if the media type and media type parameters of <paramref name="mediaType1"/> are all present
        /// and match those of <paramref name="mediaType2"/> then it is a match even though
        /// <paramref name="mediaType2"/> may have additional parameters.
        /// </summary>
        /// <param name="mediaType1">The first media type.</param>
        /// <param name="mediaType2">The second media type.</param>
        /// <param name="mediaType2Range">
        /// Indicates whether <paramref name="mediaType2"/> is a regular media type, a subtype media range, or a full
        /// media range.
        /// </param>
        /// <returns><c>true</c> if this is a subset of <paramref name="mediaType2"/>; false otherwise.</returns>
        public static bool IsSubsetOf(
            this MediaTypeHeaderValue mediaType1,
            MediaTypeHeaderValue mediaType2,
            out MediaTypeFormatterMatchRanking mediaType2Range)
        {
            // Performance-sensitive
            Debug.Assert(mediaType1 != null);

            if (mediaType2 == null)
            {
                mediaType2Range = MediaTypeFormatterMatchRanking.None;
                return(false);
            }

            var parsedMediaType1 = new ParsedMediaTypeHeaderValue(mediaType1);
            var parsedMediaType2 = new ParsedMediaTypeHeaderValue(mediaType2);

            mediaType2Range = parsedMediaType2.IsAllMediaRange ? MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderAllMediaRange :
                              parsedMediaType2.IsSubtypeMediaRange ? MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderSubtypeMediaRange :
                              MediaTypeFormatterMatchRanking.None;

            if (!parsedMediaType1.TypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderAllMediaRange)
                {
                    return(false);
                }
            }
            else if (!parsedMediaType1.SubTypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderSubtypeMediaRange)
                {
                    return(false);
                }
            }
            else
            {
                mediaType2Range = MediaTypeFormatterMatchRanking.MatchOnRequestAcceptHeaderLiteral;
            }

            // So far we either have a full match or a subset match. Now check that all of
            // mediaType1's parameters are present and equal in mediatype2
            // Optimize for the common case where the parameters inherit from Collection<T> and cache the count which
            // is faster for Collection<T>.
            var parameters1     = mediaType1.Parameters.AsCollection();
            var parameterCount1 = parameters1.Count;
            var parameters2     = mediaType2.Parameters.AsCollection();
            var parameterCount2 = parameters2.Count;

            for (var i = 0; i < parameterCount1; i++)
            {
                var parameter1 = parameters1[i];
                var found      = false;
                for (var j = 0; j < parameterCount2; j++)
                {
                    var parameter2 = parameters2[j];
                    if (parameter1.Equals(parameter2))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    return(false);
                }
            }
            return(true);
        }
 private static MediaTypeFormatterMatch CreateMatch(double? quality, MediaTypeFormatterMatchRanking ranking)
 {
     MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
     MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("text/test");
     return new MediaTypeFormatterMatch(formatter, mediaType, quality, ranking);
 }