/// <summary> /// Matches the supported media types against the list of media types specified in the Accept header or ContentType header of the message. Matching follows the /// rules for media type matching as described in RFC 2616. /// </summary> /// <param name="sourceTypes">The set of media types to be matched against the <paramref name="targetTypes"/>.</param> /// <param name="targetTypes">The set of media types the <paramref name="sourceTypes"/> will be matched against.</param> /// <returns>The best <see cref="MediaTypeMatchInfo"/> found during the matching process or null if no match was found.</returns> private static MediaTypeMatchInfo MatchMediaTypes(IEnumerable <MediaType> sourceTypes, MediaType[] targetTypes) { Debug.Assert(sourceTypes != null, "sourceTypes != null"); Debug.Assert(targetTypes != null, "targetTypes != null"); MediaTypeMatchInfo selectedMatchInfo = null; int sourceIndex = 0; if (sourceTypes != null) { foreach (MediaType sourceType in sourceTypes) { int targetIndex = 0; foreach (MediaType targetType in targetTypes) { // match the type name parts and parameters of the media type MediaTypeMatchInfo currentMatchInfo = new MediaTypeMatchInfo(sourceType, targetType, sourceIndex, targetIndex); if (!currentMatchInfo.IsMatch) { targetIndex++; continue; } if (selectedMatchInfo == null) { selectedMatchInfo = currentMatchInfo; } else { int comparisonResult = selectedMatchInfo.CompareTo(currentMatchInfo); if (comparisonResult < 0) { // If the selected match is less specific than the current match, use the current match. selectedMatchInfo = currentMatchInfo; } } targetIndex++; } sourceIndex++; } } if (selectedMatchInfo == null) { return(null); } return(selectedMatchInfo); }
private static MediaTypeMatchInfo MatchMediaTypes(IEnumerable <MediaType> sourceTypes, MediaType[] targetTypes) { MediaTypeMatchInfo info = null; int sourceIndex = 0; if (sourceTypes != null) { foreach (MediaType type in sourceTypes) { int targetIndex = 0; foreach (MediaType type2 in targetTypes) { MediaTypeMatchInfo other = new MediaTypeMatchInfo(type, type2, sourceIndex, targetIndex); if (!other.IsMatch) { targetIndex++; } else { if (info == null) { info = other; } else if (info.CompareTo(other) < 0) { info = other; } targetIndex++; } } sourceIndex++; } } if (info == null) { return(null); } return(info); }