/// <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 MediaTypeHeaderValueRange mediaType2Range)
        {
            // Performance-sensitive
            Contract.Assert(mediaType1 != null);

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

            ParsedMediaTypeHeaderValue parsedMediaType1 = new ParsedMediaTypeHeaderValue(mediaType1);
            ParsedMediaTypeHeaderValue parsedMediaType2 = new ParsedMediaTypeHeaderValue(mediaType2);
            mediaType2Range = parsedMediaType2.IsAllMediaRange ? MediaTypeHeaderValueRange.AllMediaRange :
                parsedMediaType2.IsSubtypeMediaRange ? MediaTypeHeaderValueRange.SubtypeMediaRange :
                MediaTypeHeaderValueRange.None;

            if (!parsedMediaType1.TypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeHeaderValueRange.AllMediaRange)
                {
                    return false;
                }
            }
            else if (!parsedMediaType1.SubTypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeHeaderValueRange.SubtypeMediaRange)
                {
                    return false;
                }
            }

            // 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>.
            Collection<NameValueHeaderValue> parameters1 = mediaType1.Parameters.AsCollection();
            int parameterCount1 = parameters1.Count;
            Collection<NameValueHeaderValue> parameters2 = mediaType2.Parameters.AsCollection(); 
            int parameterCount2 = parameters2.Count;
            for (int i = 0; i < parameterCount1; i++)
            {
                NameValueHeaderValue parameter1 = parameters1[i];
                bool found = false;
                for (int j = 0; j < parameterCount2; j++)
                {
                    NameValueHeaderValue parameter2 = parameters2[j];
                    if (parameter1.Equals(parameter2))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    return false;
                }
            }
            return true;
        }
示例#2
0
        /// <summary>
        /// Compares two <see cref="MediaTypeWithQualityHeaderValue"/> based on their quality value (a.k.a their "q-value").
        /// Values with identical q-values are considered equal (i.e the result is 0) with the exception that sub-type wild-cards are
        /// considered less than specific media types and full wild-cards are considered less than sub-type wild-cards. This allows to
        /// sort a sequence of <see cref="StringWithQualityHeaderValue"/> following their q-values in the order of specific media types,
        /// sub-type wildcards, and last any full wild-cards.
        /// </summary>
        /// <param name="mediaType1">The first <see cref="MediaTypeWithQualityHeaderValue"/> to compare.</param>
        /// <param name="mediaType2">The second <see cref="MediaTypeWithQualityHeaderValue"/> to compare.</param>
        /// <returns></returns>
        public int Compare(MediaTypeWithQualityHeaderValue mediaType1, MediaTypeWithQualityHeaderValue mediaType2)
        {
            Contract.Assert(mediaType1 != null, "The 'mediaType1' parameter should not be null.");
            Contract.Assert(mediaType2 != null, "The 'mediaType2' parameter should not be null.");

            if (Object.ReferenceEquals(mediaType1, mediaType2))
            {
                return(0);
            }

            int returnValue = CompareBasedOnQualityFactor(mediaType1, mediaType2);

            if (returnValue == 0)
            {
                ParsedMediaTypeHeaderValue parsedMediaType1 = new ParsedMediaTypeHeaderValue(mediaType1);
                ParsedMediaTypeHeaderValue parsedMediaType2 = new ParsedMediaTypeHeaderValue(mediaType2);

                if (!parsedMediaType1.TypesEqual(ref parsedMediaType2))
                {
                    if (parsedMediaType1.IsAllMediaRange)
                    {
                        return(-1);
                    }
                    else if (parsedMediaType2.IsAllMediaRange)
                    {
                        return(1);
                    }
                    else if (parsedMediaType1.IsSubtypeMediaRange && !parsedMediaType2.IsSubtypeMediaRange)
                    {
                        return(-1);
                    }
                    else if (!parsedMediaType1.IsSubtypeMediaRange && parsedMediaType2.IsSubtypeMediaRange)
                    {
                        return(1);
                    }
                }
                else if (!parsedMediaType1.SubTypesEqual(ref parsedMediaType2))
                {
                    if (parsedMediaType1.IsSubtypeMediaRange)
                    {
                        return(-1);
                    }
                    else if (parsedMediaType2.IsSubtypeMediaRange)
                    {
                        return(1);
                    }
                }
            }

            return(returnValue);
        }
示例#3
0
        /// <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 MediaTypeHeaderValueRange mediaType2Range)
        {
            // Performance-sensitive
            Contract.Assert(mediaType1 != null);

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

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

            mediaType2Range = parsedMediaType2.IsAllMediaRange ? MediaTypeHeaderValueRange.AllMediaRange :
                              parsedMediaType2.IsSubtypeMediaRange ? MediaTypeHeaderValueRange.SubtypeMediaRange :
                              MediaTypeHeaderValueRange.None;

            if (!parsedMediaType1.TypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeHeaderValueRange.AllMediaRange)
                {
                    return(false);
                }
            }
            else if (!parsedMediaType1.SubTypesEqual(ref parsedMediaType2))
            {
                if (mediaType2Range != MediaTypeHeaderValueRange.SubtypeMediaRange)
                {
                    return(false);
                }
            }

            // 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>.
            Collection <NameValueHeaderValue> parameters1 = mediaType1.Parameters.AsCollection();
            int parameterCount1 = parameters1.Count;
            Collection <NameValueHeaderValue> parameters2 = mediaType2.Parameters.AsCollection();
            int parameterCount2 = parameters2.Count;

            for (int i = 0; i < parameterCount1; i++)
            {
                NameValueHeaderValue parameter1 = parameters1[i];
                bool found = false;
                for (int j = 0; j < parameterCount2; j++)
                {
                    NameValueHeaderValue parameter2 = parameters2[j];
                    if (parameter1.Equals(parameter2))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    return(false);
                }
            }
            return(true);
        }