forked from erdomke/HttpClientPolyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpMethod.cs
More file actions
158 lines (130 loc) · 3.85 KB
/
HttpMethod.cs
File metadata and controls
158 lines (130 loc) · 3.85 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
149
150
151
152
153
154
155
156
157
158
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
namespace System.Net.Http
{
public class HttpMethod : IEquatable<HttpMethod>
{
private readonly string _method;
private int _hashcode;
private static readonly HttpMethod s_getMethod = new HttpMethod("GET");
private static readonly HttpMethod s_putMethod = new HttpMethod("PUT");
private static readonly HttpMethod s_postMethod = new HttpMethod("POST");
private static readonly HttpMethod s_deleteMethod = new HttpMethod("DELETE");
private static readonly HttpMethod s_headMethod = new HttpMethod("HEAD");
private static readonly HttpMethod s_optionsMethod = new HttpMethod("OPTIONS");
private static readonly HttpMethod s_traceMethod = new HttpMethod("TRACE");
// Don't expose CONNECT as static property, since it's used by the transport to connect to a proxy.
// CONNECT is not used by users directly.
public static HttpMethod Get
{
get { return s_getMethod; }
}
public static HttpMethod Put
{
get { return s_putMethod; }
}
public static HttpMethod Post
{
get { return s_postMethod; }
}
public static HttpMethod Delete
{
get { return s_deleteMethod; }
}
public static HttpMethod Head
{
get { return s_headMethod; }
}
public static HttpMethod Options
{
get { return s_optionsMethod; }
}
public static HttpMethod Trace
{
get { return s_traceMethod; }
}
public string Method
{
get { return _method; }
}
public HttpMethod(string method)
{
if (string.IsNullOrEmpty(method))
{
throw new ArgumentException("The value cannot be null or empty.", nameof(method));
}
if (HttpRuleParser.GetTokenLength(method, 0) != method.Length)
{
throw new FormatException("The format of the HTTP method is invalid.");
}
_method = method;
}
#region IEquatable<HttpMethod> Members
public bool Equals(HttpMethod other)
{
if ((object)other == null)
{
return false;
}
if (object.ReferenceEquals(_method, other._method))
{
// Strings are static, so there is a good chance that two equal methods use the same reference
// (unless they differ in case).
return true;
}
return string.Equals(_method, other._method, StringComparison.OrdinalIgnoreCase);
}
#endregion
public override bool Equals(object obj)
{
return Equals(obj as HttpMethod);
}
public override int GetHashCode()
{
if (_hashcode == 0)
{
// If _method is already uppercase, _method.GetHashCode() can be
// used instead of _method.ToUpperInvariant().GetHashCode(),
// avoiding the unnecessary extra string allocation.
_hashcode = IsUpperAscii(_method) ?
_method.GetHashCode() :
_method.ToUpperInvariant().GetHashCode();
}
return _hashcode;
}
public override string ToString()
{
return _method.ToString();
}
public static bool operator ==(HttpMethod left, HttpMethod right)
{
if ((object)left == null)
{
return ((object)right == null);
}
else if ((object)right == null)
{
return ((object)left == null);
}
return left.Equals(right);
}
public static bool operator !=(HttpMethod left, HttpMethod right)
{
return !(left == right);
}
private static bool IsUpperAscii(string value)
{
for (int i = 0; i < value.Length; i++)
{
char c = value[i];
if (!(c >= 'A' && c <= 'Z'))
{
return false;
}
}
return true;
}
}
}