// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net.Http.Headers;
using System.Web.Http;
namespace System.Net.Http.Formatting
{
///
/// This class provides a mapping from an arbitrary HTTP request header field to a
/// used to select instances for handling the entity body of an
/// or .
/// This class only checks header fields associated with for a match. It does
/// not check header fields associated with or instances.
///
public class RequestHeaderMapping : MediaTypeMapping
{
///
/// Initializes a new instance of the class.
///
/// Name of the header to match.
/// The header value to match.
/// The value comparison to use when matching .
/// if set to true then is
/// considered a match if it matches a substring of the actual header value.
/// The media type to use if and
/// is considered a match.
public RequestHeaderMapping(string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring, string mediaType)
: base(mediaType)
{
Initialize(headerName, headerValue, valueComparison, isValueSubstring);
}
///
/// Initializes a new instance of the class.
///
/// Name of the header to match.
/// The header value to match.
/// The to use when matching .
/// if set to true then is
/// considered a match if it matches a substring of the actual header value.
/// The to use if and
/// is considered a match.
public RequestHeaderMapping(string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring, MediaTypeHeaderValue mediaType)
: base(mediaType)
{
Initialize(headerName, headerValue, valueComparison, isValueSubstring);
}
///
/// Gets the name of the header to match.
///
public string HeaderName { get; private set; }
///
/// Gets the header value to match.
///
public string HeaderValue { get; private set; }
///
/// Gets the to use when matching .
///
public StringComparison HeaderValueComparison { get; private set; }
///
/// Gets a value indicating whether is
/// a matched as a substring of the actual header value.
/// this instance is value substring.
///
///
/// true if is to be matched as a substring; otherwise false.
///
public bool IsValueSubstring { get; private set; }
///
/// Returns a value indicating whether the current
/// instance can return a from .
///
/// The to check.
///
/// The quality of the match. It must be between 0.0 and 1.0.
/// A value of 0.0 signifies no match.
/// A value of 1.0 signifies a complete match.
///
public override double TryMatchMediaType(HttpRequestMessage request)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
return MatchHeaderValue(request, HeaderName, HeaderValue, HeaderValueComparison, IsValueSubstring);
}
private static double MatchHeaderValue(HttpRequestMessage request, string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring)
{
Contract.Assert(request != null, "request should not be null");
Contract.Assert(headerName != null, "header name should not be null");
Contract.Assert(headerValue != null, "header value should not be null");
IEnumerable values;
if (request.Headers.TryGetValues(headerName, out values))
{
foreach (string value in values)
{
if (isValueSubstring)
{
if (value.IndexOf(headerValue, valueComparison) != -1)
{
return FormattingUtilities.Match;
}
}
else
{
if (value.Equals(headerValue, valueComparison))
{
return FormattingUtilities.Match;
}
}
}
}
return FormattingUtilities.NoMatch;
}
private void Initialize(string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring)
{
if (String.IsNullOrWhiteSpace(headerName))
{
throw Error.ArgumentNull("headerName");
}
if (String.IsNullOrWhiteSpace(headerValue))
{
throw Error.ArgumentNull("headerValue");
}
StringComparisonHelper.Validate(valueComparison, "valueComparison");
HeaderName = headerName;
HeaderValue = headerValue;
HeaderValueComparison = valueComparison;
IsValueSubstring = isValueSubstring;
}
}
}