forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequestHeader.cs
More file actions
459 lines (400 loc) · 14.4 KB
/
HttpRequestHeader.cs
File metadata and controls
459 lines (400 loc) · 14.4 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System;
using System.Collections.Generic;
using System.Text;
using Waher.Content;
using Waher.Networking.HTTP.HeaderFields;
using Waher.Networking.HTTP.Vanity;
namespace Waher.Networking.HTTP
{
/// <summary>
/// Contains information about all fields in an HTTP request header.
/// </summary>
public class HttpRequestHeader : HttpHeader
{
private Dictionary<string, string> query = null;
private HttpFieldAccept accept = null;
private HttpFieldAcceptCharset acceptCharset = null;
private HttpFieldAcceptEncoding acceptEncoding = null;
private HttpFieldAcceptLanguage acceptLanguage = null;
private HttpFieldAuthorization authorization = null;
private HttpFieldCookie cookie = null;
private HttpFieldExpect expect = null;
private HttpFieldFrom from = null;
private HttpFieldHost host = null;
private HttpFieldIfMatch ifMatch = null;
private HttpFieldIfModifiedSince ifModifiedSince = null;
private HttpFieldIfNoneMatch ifNoneMatch = null;
private HttpFieldIfRange ifRange = null;
private HttpFieldIfUnmodifiedSince ifUnmodifiedSince = null;
private HttpFieldReferer referer = null;
private HttpFieldRange range = null;
private HttpFieldUserAgent userAgent = null;
private HttpFieldUpgradeInsecureRequests upgradeInsequreRequests = null;
private string method = string.Empty;
private string resource = string.Empty;
private string resourcePart = string.Empty;
private string queryString = string.Empty;
private string fragment = string.Empty;
private readonly string uriScheme = string.Empty;
private double httpVersion = -1;
/// <summary>
/// Contains information about all fields in an HTTP request header.
/// </summary>
/// <param name="Header">HTTP Header.</param>
/// <param name="VanityResources">Registered vanity resources.</param>
/// <param name="UriScheme">URI Scheme.</param>
public HttpRequestHeader(string Header, VanityResources VanityResources, string UriScheme)
: base(Header, VanityResources)
{
this.uriScheme = UriScheme;
}
/// <summary>
/// Contains information about all fields in an HTTP request header.
/// </summary>
/// <param name="Method">HTTP Method.</param>
/// <param name="Resource">Resource.</param>
/// <param name="Version">HTTP Version.</param>
/// <param name="UriScheme">URI scheme.</param>
/// <param name="VanityResources">Registered vanity resource.</param>
/// <param name="Headers">HTTP Header fields.</param>
public HttpRequestHeader(string Method, string Resource, string Version, string UriScheme, VanityResources VanityResources,
params KeyValuePair<string, string>[] Headers)
: base(Method + " " + Resource + " HTTP/" + Version, VanityResources, Headers)
{
this.uriScheme = UriScheme;
}
/// <summary>
/// Parses the first row of an HTTP header.
/// </summary>
/// <param name="Row">First row.</param>
/// <param name="VanityResources">Registered vanity resources.</param>
protected override void ParseFirstRow(string Row, VanityResources VanityResources)
{
int i = Row.IndexOf(' ');
if (i > 0)
{
this.method = Row.Substring(0, i).ToUpper();
int j = Row.LastIndexOf(' ');
if (j > 0 && j < Row.Length - 5 && Row.Substring(j + 1, 5) == "HTTP/")
{
this.resourcePart = Row.Substring(i + 1, j - i - 1).Trim();
VanityResources?.CheckVanityResource(ref this.resourcePart);
this.resource = this.resourcePart;
if (CommonTypes.TryParse(Row.Substring(j + 6), out this.httpVersion))
{
i = this.resource.IndexOf('?');
if (i >= 0)
{
this.queryString = this.resource.Substring(i + 1);
this.resource = this.resource.Substring(0, i);
i = this.queryString.IndexOf('#');
if (i >= 0)
{
this.fragment = this.queryString.Substring(i + 1);
this.queryString = this.queryString.Substring(0, i);
}
this.query = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
foreach (string Part in this.queryString.Split('&'))
{
i = Part.IndexOf('=');
if (i < 0)
this.query[Part] = string.Empty;
else
this.query[Part.Substring(0, i)] = Part.Substring(i + 1);
}
}
else
{
i = this.resource.IndexOf('#');
if (i >= 0)
{
this.fragment = this.resource.Substring(i + 1);
this.resource = this.resource.Substring(0, i);
}
}
}
else
this.httpVersion = -1;
}
}
}
/// <summary>
/// HTTP Method
/// </summary>
public string Method { get { return this.method; } }
/// <summary>
/// Resource
/// </summary>
public string Resource { get { return this.resource; } }
/// <summary>
/// Contains original resource part of request.
/// </summary>
public string ResourcePart { get { return this.resourcePart; } }
/// <summary>
/// HTTP Version.
/// </summary>
public double HttpVersion { get { return this.httpVersion; } }
/// <summary>
/// Query string. To get the values of individual query parameters, use the <see cref="TryGetQueryParameter"/> method.
/// </summary>
public string QueryString { get { return this.queryString; } }
/// <summary>
/// Fragment.
/// </summary>
public string Fragment { get { return this.fragment; } }
/// <summary>
/// URI scheme.
/// </summary>
public string UriScheme { get { return this.uriScheme; } }
/// <summary>
/// Tries to get the value of an individual query parameter, if available.
/// </summary>
/// <param name="QueryParameter">Query parameter name. This parameter is case insensitive.</param>
/// <param name="Value">Query parameter value, if found.</param>
/// <returns>If a query parameter with the given name was found.</returns>
public bool TryGetQueryParameter(string QueryParameter, out string Value)
{
if (!(this.query is null))
return this.query.TryGetValue(QueryParameter, out Value);
else
{
Value = string.Empty;
return false;
}
}
/// <summary>
/// All query parameters.
/// </summary>
public KeyValuePair<string, string>[] QueryParameters
{
get
{
if (this.queryParameters is null)
{
if (this.query is null)
this.queryParameters = new KeyValuePair<string, string>[0];
else
{
KeyValuePair<string, string>[] P = new KeyValuePair<string, string>[this.query.Count];
int i = 0;
foreach (KeyValuePair<string, string> P2 in this.query)
P[i++] = P2;
this.queryParameters = P;
}
}
return this.queryParameters;
}
}
private KeyValuePair<string, string>[] queryParameters = null;
/// <summary>
/// Parses a specific HTTP header field.
/// </summary>
/// <param name="KeyLower">Lower-case version of field name.</param>
/// <param name="Key">Field name, as it appears in the header.</param>
/// <param name="Value">Unparsed header field value</param>
/// <returns>HTTP header field object, corresponding to the particular field.</returns>
protected override HttpField ParseField(string KeyLower, string Key, string Value)
{
switch (KeyLower)
{
case "accept": return this.accept = new HttpFieldAccept(Key, Value);
case "accept-charset": return this.acceptCharset = new HttpFieldAcceptCharset(Key, Value);
case "accept-encoding": return this.acceptEncoding = new HttpFieldAcceptEncoding(Key, Value);
case "accept-language": return this.acceptLanguage = new HttpFieldAcceptLanguage(Key, Value);
case "authorization": return this.authorization = new HttpFieldAuthorization(Key, Value);
case "cookie": return this.cookie = new HttpFieldCookie(Key, Value);
case "expect": return this.expect = new HttpFieldExpect(Key, Value);
case "from": return this.from = new HttpFieldFrom(Key, Value);
case "host": return this.host = new HttpFieldHost(Key, Value);
case "if-match": return this.ifMatch = new HttpFieldIfMatch(Key, Value);
case "if-modified-since": return this.ifModifiedSince = new HttpFieldIfModifiedSince(Key, Value);
case "if-none-match": return this.ifNoneMatch = new HttpFieldIfNoneMatch(Key, Value);
case "if-range": return this.ifRange = new HttpFieldIfRange(Key, Value);
case "if-unmodified-since": return this.ifUnmodifiedSince = new HttpFieldIfUnmodifiedSince(Key, Value);
case "referer": return this.referer = new HttpFieldReferer(Key, Value);
case "range": return this.range = new HttpFieldRange(Key, Value);
case "user-agent": return this.userAgent = new HttpFieldUserAgent(Key, Value);
case "upgrade-insecure-requests": return this.upgradeInsequreRequests = new HttpFieldUpgradeInsecureRequests(Key, Value);
default: return base.ParseField(KeyLower, Key, Value);
}
}
/// <summary>
/// Accept HTTP Field header. (RFC 2616, §14.1)
/// </summary>
public HttpFieldAccept Accept { get { return this.accept; } }
/// <summary>
/// Accept-Charset HTTP Field header. (RFC 2616, §14.2)
/// </summary>
public HttpFieldAcceptCharset AcceptCharset { get { return this.acceptCharset; } }
/// <summary>
/// Accept-Encoding HTTP Field header. (RFC 2616, §14.3)
/// </summary>
public HttpFieldAcceptEncoding AcceptEncoding { get { return this.acceptEncoding; } }
/// <summary>
/// Accept-Language HTTP Field header. (RFC 2616, §14.4)
/// </summary>
public HttpFieldAcceptLanguage AcceptLanguage { get { return this.acceptLanguage; } }
/// <summary>
/// Authorization HTTP Field header. (RFC 2616, §14.8)
/// </summary>
public HttpFieldAuthorization Authorization { get { return this.authorization; } }
/// <summary>
/// Cookie HTTP Field header. (RFC 6265, §5.2)
/// </summary>
public HttpFieldCookie Cookie { get { return this.cookie; } }
/// <summary>
/// Expect HTTP Field header. (RFC 2616, §14.20)
/// </summary>
public HttpFieldExpect Expect { get { return this.expect; } }
/// <summary>
/// From HTTP Field header. (RFC 2616, §14.22)
/// </summary>
public HttpFieldFrom From { get { return this.from; } }
/// <summary>
/// Host HTTP Field header. (RFC 2616, §14.23)
/// </summary>
public HttpFieldHost Host { get { return this.host; } }
/// <summary>
/// If-Match HTTP Field header. (RFC 2616, §14.24)
/// </summary>
public HttpFieldIfMatch IfMatch { get { return this.ifMatch; } }
/// <summary>
/// If-Modified-Since HTTP Field header. (RFC 2616, §14.25)
/// </summary>
public HttpFieldIfModifiedSince IfModifiedSince { get { return this.ifModifiedSince; } }
/// <summary>
/// If-None-Match HTTP Field header. (RFC 2616, §14.26)
/// </summary>
public HttpFieldIfNoneMatch IfNoneMatch { get { return this.ifNoneMatch; } }
/// <summary>
/// If-Range HTTP Field header. (RFC 2616, §14.27)
/// </summary>
public HttpFieldIfRange IfRange { get { return this.ifRange; } }
/// <summary>
/// If-Unmodified-Since HTTP Field header. (RFC 2616, §14.28)
/// </summary>
public HttpFieldIfUnmodifiedSince IfUnmodifiedSince { get { return this.ifUnmodifiedSince; } }
/// <summary>
/// Referer HTTP Field header. (RFC 2616, §14.36)
/// </summary>
public HttpFieldReferer Referer { get { return this.referer; } }
/// <summary>
/// Range HTTP Field header. (RFC 2616, §14.35)
/// </summary>
public HttpFieldRange Range { get { return this.range; } }
/// <summary>
/// UserAgent HTTP Field header. (RFC 2616, §14.43)
/// </summary>
public HttpFieldUserAgent UserAgent { get { return this.userAgent; } }
/// <summary>
/// Upgrade-Insecure-Requests HTTP Field header.
/// https://www.w3.org/TR/upgrade-insecure-requests/
/// </summary>
public HttpFieldUpgradeInsecureRequests UpgradeInsecureRequests { get { return this.upgradeInsequreRequests; } }
/// <summary>
/// If the method is safe, according to §9.1.1, RFC 2616, and the HTTP Method registry at IANA:
/// http://www.iana.org/assignments/http-methods/http-methods.xhtml.
/// </summary>
public bool IsMethodSafe
{
get
{
switch (this.method)
{
case "GET":
case "HEAD":
case "OPTIONS":
case "PRI":
case "PROPFIND":
case "REPORT":
case "SEARCH":
case "TRACE":
return true;
default:
return false;
}
}
}
/// <summary>
/// If the method is idempotent, according to §9.1.2, RFC 2616, and the HTTP Method registry at IANA:
/// http://www.iana.org/assignments/http-methods/http-methods.xhtml.
/// </summary>
public bool IsMethodIdempotent
{
get
{
switch (this.method)
{
case "POST":
case "CONNECT":
case "LOCK":
case "PATCH":
return false;
default:
return true;
}
}
}
/// <summary>
/// If the message contains, apart from the header, a message body also.
/// </summary>
public override bool HasMessageBody
{
get
{
// Summary taken from http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
if (this.method == "TRACE")
return false;
if (this.expect != null && this.expect.Continue100)
return false;
if (this.ContentLength != null && this.ContentLength.ContentLength > 0)
return true;
if (!(this.TransferEncoding is null))
return true;
return false;
}
}
/// <summary>
/// Gets an absolute URL for the request.
/// </summary>
/// <returns>URL corresponding to request.</returns>
public string GetURL()
{
return this.GetURL(true, false);
}
/// <summary>
/// Gets an absolute URL for the request.
/// </summary>
/// <param name="IncludeQuery">If the query portion of the URL should be returned.</param>
/// <returns>URL corresponding to request.</returns>
public string GetURL(bool IncludeQuery)
{
return this.GetURL(IncludeQuery, false);
}
/// <summary>
/// Gets an absolute URL for the request.
/// </summary>
/// <param name="IncludeQuery">If the query portion of the URL should be returned.</param>
/// <param name="IncludeFragment">If the fragment portion of the URL should be returned.</param>
/// <returns>URL corresponding to request.</returns>
public string GetURL(bool IncludeQuery, bool IncludeFragment)
{
StringBuilder Result = new StringBuilder();
Result.Append(this.uriScheme);
Result.Append("://");
Result.Append(this.host?.Value ?? string.Empty);
Result.Append(this.resource);
if (IncludeQuery && !string.IsNullOrEmpty(this.queryString))
{
Result.Append('?');
Result.Append(this.queryString);
}
if (IncludeFragment && !string.IsNullOrEmpty(this.fragment))
{
Result.Append('#');
Result.Append(this.fragment);
}
return Result.ToString();
}
}
}