forked from bonesoul/uhttpsharp
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathHttpHeadersExtensions.cs
More file actions
59 lines (49 loc) · 1.65 KB
/
Copy pathHttpHeadersExtensions.cs
File metadata and controls
59 lines (49 loc) · 1.65 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
using System;
using System.Text;
namespace uhttpsharp.Headers
{
public static class HttpHeadersExtensions
{
public static bool KeepAliveConnection(this IHttpHeaders headers)
{
string value;
return headers.TryGetByName("connection", out value)
&& value.Equals("Keep-Alive", StringComparison.InvariantCultureIgnoreCase);
}
public static bool TryGetByName<T>(this IHttpHeaders headers, string name, out T value)
{
string stringValue;
if (headers.TryGetByName(name, out stringValue))
{
value = (T) Convert.ChangeType(stringValue, typeof(T));
return true;
}
value = default(T);
return false;
}
public static T GetByName<T>(this IHttpHeaders headers, string name)
{
T value;
headers.TryGetByName(name, out value);
return value;
}
public static T GetByNameOrDefault<T>(this IHttpHeaders headers, string name, T defaultValue)
{
T value;
if (headers.TryGetByName(name, out value))
{
return value;
}
return defaultValue;
}
public static string ToUriData(this IHttpHeaders headers)
{
var builder = new StringBuilder();
foreach (var header in headers)
{
builder.AppendFormat("{0}={1}&", Uri.EscapeDataString(header.Key), Uri.EscapeDataString(header.Value));
}
return builder.ToString(0, builder.Length - 1);
}
}
}