-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpHeaders.cs
154 lines (137 loc) · 6.36 KB
/
HttpHeaders.cs
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
149
150
151
152
153
154
using System;
using System.Collections;
using System.Collections.Generic;
namespace Dell.Premier.Web.Common.HttpClient
{
/// <summary><see cref="HttpHeaders" /> represents a collection of HTTP header values.</summary>
public class HttpHeaders : IEnumerable<FieldValuePair>
{
private readonly Dictionary<string, List<FieldValuePair>> _headers = new Dictionary<string, List<FieldValuePair>>();
/// <summary>
/// Indexer to <see cref="Get" /> or <see cref="Set" /> values within the header collection. If no value is
/// set for the <paramref name="field" /> the indexer returns an empty string; this indexer never returns
/// null. If the <paramref name="value" /> is set to null the header is removed from the collection. All
/// <see cref="Get" /> and <see cref="Set" /> operations on this indexer are case-insensitive.
/// </summary>
/// <param name="field">The field name (case-insensitive).</param>
/// <returns>The value.</returns>
public string this[string field]
{
get { return Get(field); }
set { Set(field, value); }
}
/// <summary>Gets the enumerator.</summary>
/// <returns>The enumerator.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>Returns an enumerator that iterates through the header collection.</summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the
/// header collection.
/// </returns>
public IEnumerator<FieldValuePair> GetEnumerator()
{
foreach (var fieldValuePairs in _headers.Values)
{
foreach (var fieldValuePair in fieldValuePairs)
{
yield return fieldValuePair;
}
}
}
/// <summary>
/// Add adds a header entry for the <paramref name="field" /> and <paramref name="value" />. If the
/// <paramref name="field" /> already exists a new entry is appended to the header collection. If
/// <paramref name="value" /> is null nothing is added.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="field" /> is null.</exception>
/// <param name="field">The field name.</param>
/// <param name="value">The value.</param>
public void Add(string field, string value)
{
if (field == null)
throw new ArgumentNullException(nameof(field));
if (value == null)
{
return;
}
var lowerField = field.ToLowerInvariant();
List<FieldValuePair> values;
if (!_headers.TryGetValue(lowerField, out values))
{
values = new List<FieldValuePair>();
_headers[lowerField] = values;
}
values.Add(new FieldValuePair(field, value));
}
/// <summary>
/// Delete removes the given <paramref name="field" /> (case-insensitive) from the header collection. If the
/// <paramref name="field" /> doesn't exist the method silently returns.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="field" /> is null.</exception>
/// <param name="field">The field name (case-insensitive).</param>
public void Delete(string field)
{
if (field == null)
throw new ArgumentNullException(nameof(field));
var lowerField = field.ToLowerInvariant();
_headers.Remove(lowerField);
}
/// <summary>
/// <para>
/// Get returns the value of the specified <paramref name="field" /> (case-insensitive). If multiple
/// values are set for the header the first one is returned. If no value is set for the
/// <paramref name="field" /> the method returns an empty string. This method never returns null.
/// </para>
/// <para>
/// To retrieve all header values iterate over the <see cref="HttpHeaders" /> value.
/// </para>
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="field" /> is null.</exception>
/// <param name="field">The field name (case-insensitive).</param>
/// <returns>The value.</returns>
public string Get(string field)
{
if (field == null)
throw new ArgumentNullException(nameof(field));
var lowerField = field.ToLowerInvariant();
List<FieldValuePair> values;
if (!_headers.TryGetValue(lowerField, out values))
{
return string.Empty;
}
return values[0].Value;
}
/// <summary>
/// <para>
/// Set sets the specified <paramref name="field" /> (case-insensitive) to the
/// <paramref name="value" />. If <paramref name="value" /> is null the entry for
/// <paramref name="field" /> is deleted.
/// </para>
/// <para>
/// HTTP allows providing the same header multiple times. For that behavior, see <see cref="Add" />.
/// </para>
/// <para>
/// Calling <see cref="Set" /> sets the header value to the single <paramref name="value" />
/// specified, clearing previous entries for the same <paramref name="field" />, if any.
/// </para>
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="field" /> is null.</exception>
/// <param name="field">The field name (case-insensitive).</param>
/// <param name="value">The value.</param>
public void Set(string field, string value)
{
if (field == null)
throw new ArgumentNullException(nameof(field));
if (value == null)
{
Delete(field);
return;
}
var lowerField = field.ToLowerInvariant();
_headers[lowerField] = new List<FieldValuePair> { new FieldValuePair(field, value) };
}
}
}