forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestHeaderMapping.cs
More file actions
148 lines (132 loc) · 6.82 KB
/
RequestHeaderMapping.cs
File metadata and controls
148 lines (132 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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
{
/// <summary>
/// This class provides a mapping from an arbitrary HTTP request header field to a <see cref="MediaTypeHeaderValue"/>
/// used to select <see cref="MediaTypeFormatter"/> instances for handling the entity body of an <see cref="HttpRequestMessage"/>
/// or <see cref="HttpResponseMessage"/>.
/// <remarks>This class only checks header fields associated with <see cref="HttpRequestMessage.Headers"/> for a match. It does
/// not check header fields associated with <see cref="HttpResponseMessage.Headers"/> or <see cref="HttpContent.Headers"/> instances.</remarks>
/// </summary>
public class RequestHeaderMapping : MediaTypeMapping
{
/// <summary>
/// Initializes a new instance of the <see cref="RequestHeaderMapping"/> class.
/// </summary>
/// <param name="headerName">Name of the header to match.</param>
/// <param name="headerValue">The header value to match.</param>
/// <param name="valueComparison">The value comparison to use when matching <paramref name="headerValue"/>.</param>
/// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is
/// considered a match if it matches a substring of the actual header value.</param>
/// <param name="mediaType">The media type to use if <paramref name="headerName"/> and <paramref name="headerValue"/>
/// is considered a match.</param>
public RequestHeaderMapping(string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring, string mediaType)
: base(mediaType)
{
Initialize(headerName, headerValue, valueComparison, isValueSubstring);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestHeaderMapping"/> class.
/// </summary>
/// <param name="headerName">Name of the header to match.</param>
/// <param name="headerValue">The header value to match.</param>
/// <param name="valueComparison">The <see cref="StringComparison"/> to use when matching <paramref name="headerValue"/>.</param>
/// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is
/// considered a match if it matches a substring of the actual header value.</param>
/// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to use if <paramref name="headerName"/> and <paramref name="headerValue"/>
/// is considered a match.</param>
public RequestHeaderMapping(string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring, MediaTypeHeaderValue mediaType)
: base(mediaType)
{
Initialize(headerName, headerValue, valueComparison, isValueSubstring);
}
/// <summary>
/// Gets the name of the header to match.
/// </summary>
public string HeaderName { get; private set; }
/// <summary>
/// Gets the header value to match.
/// </summary>
public string HeaderValue { get; private set; }
/// <summary>
/// Gets the <see cref="StringComparison"/> to use when matching <see cref="HeaderValue"/>.
/// </summary>
public StringComparison HeaderValueComparison { get; private set; }
/// <summary>
/// Gets a value indicating whether <see cref="HeaderValue"/> is
/// a matched as a substring of the actual header value.
/// this instance is value substring.
/// </summary>
/// <value>
/// <c>true</c> if <see cref="HeaderValue"/> is to be matched as a substring; otherwise <c>false</c>.
/// </value>
public bool IsValueSubstring { get; private set; }
/// <summary>
/// Returns a value indicating whether the current <see cref="RequestHeaderMapping"/>
/// instance can return a <see cref="MediaTypeHeaderValue"/> from <paramref name="request"/>.
/// </summary>
/// <param name="request">The <see cref="HttpRequestMessage"/> to check.</param>
/// <returns>
/// The quality of the match. It must be between <c>0.0</c> and <c>1.0</c>.
/// A value of <c>0.0</c> signifies no match.
/// A value of <c>1.0</c> signifies a complete match.
/// </returns>
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<string> 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;
}
}
}