forked from jvano/VisualARM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHeadersProcessor.cs
More file actions
105 lines (85 loc) · 3.06 KB
/
HttpHeadersProcessor.cs
File metadata and controls
105 lines (85 loc) · 3.06 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
using FastColoredTextBoxNS;
using System;
using System.Collections.Generic;
using System.Net;
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> _headersToExclude = 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;
}
}
public void CaptureRequest(string host, WebHeaderCollection requestHeaders)
{
_host = host;
CaptureHeaders(requestHeaders, RequestHeaders);
}
public void CaptureResponse(HttpStatusCode statusCode, WebHeaderCollection responseHeaders)
{
_statusCode = string.Format("{0} ({1})", ((int)statusCode).ToString(), statusCode.ToString());
CaptureHeaders(responseHeaders, ResponseHeaders);
}
private IList<Tuple<string, string>> CaptureHeaders(WebHeaderCollection headers, IList<Tuple<string, string>> target)
{
target.Clear();
for (int i = 0; i < headers.Count; ++i)
{
string header = headers.GetKey(i);
if (_headersToExclude.Contains(header))
{
continue;
}
foreach (string value in headers.GetValues(i))
{
target.Add(new Tuple<string, string>(header, value));
}
}
return target;
}
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();
}
}
}