-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHttpHeadersProcessor.cs
More file actions
144 lines (114 loc) · 4.47 KB
/
HttpHeadersProcessor.cs
File metadata and controls
144 lines (114 loc) · 4.47 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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
namespace Vano.Tools.Azure
{
public class HttpHeadersProcessor
{
private List<Tuple<string, string>> _requestHeaders;
private List<Tuple<string, string>> _responseHeaders;
private string _statusCode;
private string _host;
private readonly HashSet<string> _sensitiveHeaders = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "Authorization" };
private IList<Tuple<string, string>> RequestHeaders
{
get
{
if (_requestHeaders == null)
{
_requestHeaders = new List<Tuple<string, string>>();
}
return _requestHeaders;
}
}
public IList<Tuple<string, string>> ResponseHeaders
{
get
{
if (_responseHeaders == null)
{
_responseHeaders = new List<Tuple<string, string>>();
}
return _responseHeaders;
}
}
#region WebHeaders
public void CaptureWebHeadersFromRequest(string host, WebHeaderCollection requestHeaders)
{
_host = host;
CaptureWebHeaders(requestHeaders, RequestHeaders);
}
public void CaptureWebHeadersFromResponse(HttpStatusCode statusCode, WebHeaderCollection responseHeaders)
{
_statusCode = string.Format("{0} ({1})", ((int)statusCode).ToString(), statusCode.ToString());
CaptureWebHeaders(responseHeaders, ResponseHeaders);
}
private IList<Tuple<string, string>> CaptureWebHeaders(WebHeaderCollection headers, IList<Tuple<string, string>> target)
{
target.Clear();
for (int i = 0; i < headers.Count; ++i)
{
string header = headers.GetKey(i);
if (_sensitiveHeaders.Contains(header))
{
continue;
}
foreach (string value in headers.GetValues(i))
{
target.Add(new Tuple<string, string>(header, value));
}
}
return target;
}
#endregion
#region HttpHeaders
public void CaptureHttpHeadersFromRequest(string host, HttpHeaders requestHeaders, bool displaySecrets)
{
_host = host;
CaptureHttpHeaders(requestHeaders, RequestHeaders, displaySecrets);
}
public void CaptureHttpHeadersFromResponse(HttpStatusCode statusCode, HttpHeaders responseHeaders, bool displaySecrets)
{
_statusCode = string.Format("{0} ({1})", ((int)statusCode).ToString(), statusCode.ToString());
CaptureHttpHeaders(responseHeaders, ResponseHeaders, displaySecrets);
}
private IList<Tuple<string, string>> CaptureHttpHeaders(HttpHeaders headers, IList<Tuple<string, string>> target, bool displaySecrets)
{
target.Clear();
foreach(KeyValuePair<string, IEnumerable<string>> header in headers)
{
foreach (string value in header.Value)
{
string postProcessedValue = _sensitiveHeaders.Contains(header.Key) && displaySecrets == false ?
"●●●●●●●●" :
value;
target.Add(new Tuple<string, string>(header.Key, postProcessedValue));
}
}
return target;
}
#endregion
public string GetFormattedRequestHeaders()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("Host: {0}", _host ?? string.Empty));
return GetFormattedHeaders(sb, RequestHeaders);
}
public string GetFormattedResponseHeaders()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("Status: {0}", _statusCode ?? "0"));
return GetFormattedHeaders(sb, ResponseHeaders);
}
private string GetFormattedHeaders(StringBuilder sb, IList<Tuple<string, string>> headersDictionary)
{
foreach (var kvp in headersDictionary)
{
sb.AppendLine(string.Format("{0}: {1}", kvp.Item1, kvp.Item2));
}
return sb.ToString();
}
}
}