-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathHttpMessageLogFormatter.cs
218 lines (192 loc) · 9.58 KB
/
HttpMessageLogFormatter.cs
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
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
namespace NamespacePrefixPlaceholder.PowerShell
{
using NamespacePrefixPlaceholder.PowerShell.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
public static class HttpMessageLogFormatter
{
internal static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessage originalRequest)
{
var newRequest = new HttpRequestMessage(originalRequest.Method, originalRequest.RequestUri);
// Copy requestClone headers.
foreach (var header in originalRequest.Headers)
newRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
// Copy requestClone properties.
foreach (var property in originalRequest.Properties)
newRequest.Properties.Add(property);
// Set Content if previous requestClone had one.
if (originalRequest.Content != null)
{
// Try cloning request content; otherwise, skip due to https://github.com./dotnet/corefx/pull/19082 in .NET 4.x.
try
{
// HttpClient doesn't rewind streams and we have to explicitly do so.
var ms = new MemoryStream();
await originalRequest.Content.CopyToAsync(ms).ConfigureAwait(false);
ms.Position = 0;
newRequest.Content = new StreamContent(ms);
// Attempt to copy request content headers with a single retry.
// HttpHeaders dictionary is not thread-safe when targeting anything below .NET 7. For more information, see https://github.com./dotnet/runtime/issues/61798.
int retryCount = 0;
int maxRetryCount = 2;
while (retryCount < maxRetryCount)
{
try
{
originalRequest.Content?.Headers?.ToList().ForEach(header => newRequest.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value));
retryCount = maxRetryCount;
}
catch (InvalidOperationException)
{
retryCount++;
}
}
}
catch
{
}
}
return newRequest;
}
public static async Task<string> GetHttpRequestLogAsync(HttpRequestMessage request)
{
if (request == null) return string.Empty;
var requestClone = await request.CloneAsync().ConfigureAwait(false);
string body = string.Empty;
try
{
if (requestClone.Content != null)
{
body = FormatString(await requestClone.Content.ReadAsStringAsync());
}
else if (requestClone.Content == null && request.Content != null)
{
body = "Skipped: Content body was disposed before the logger could access it.";
}
}
catch { }
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"============================ HTTP REQUEST ============================{Environment.NewLine}");
stringBuilder.AppendLine($"HTTP Method:{Environment.NewLine}{requestClone.Method}{Environment.NewLine}");
stringBuilder.AppendLine($"Absolute Uri:{Environment.NewLine}{requestClone.RequestUri}{Environment.NewLine}");
stringBuilder.AppendLine($"Headers:{Environment.NewLine}{HeadersToString(requestClone.Headers)}{Environment.NewLine}");
stringBuilder.AppendLine($"Body:{Environment.NewLine}{SanitizeBody(body)}{Environment.NewLine}");
return stringBuilder.ToString();
}
public static async Task<string> GetHttpResponseLogAsync(HttpResponseMessage response)
{
if (response == null) return string.Empty;
string body = string.Empty;
try
{
body = (response.Content == null) ? string.Empty : FormatString(await response.Content.ReadAsStringAsync());
}
catch { }
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"============================ HTTP RESPONSE ============================{Environment.NewLine}");
stringBuilder.AppendLine($"Status Code:{Environment.NewLine}{response.StatusCode}{Environment.NewLine}");
stringBuilder.AppendLine($"Headers:{Environment.NewLine}{HeadersToString(response.Headers)}{Environment.NewLine}");
stringBuilder.AppendLine($"Body:{Environment.NewLine}{SanitizeBody(body)}{Environment.NewLine}");
return stringBuilder.ToString();
}
public static async Task<string> GetErrorLogAsync(HttpResponseMessage response, IMicrosoftGraphODataErrorsMainError odataError)
{
if (response == null) return string.Empty;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"{odataError?.Message}{Environment.NewLine}");
stringBuilder.AppendLine($"Status: {((int)response.StatusCode)} ({response.StatusCode})");
stringBuilder.AppendLine($"ErrorCode: {odataError?.Code}");
stringBuilder.AppendLine($"Date: {odataError?.InnerError?.Date}{Environment.NewLine}");
stringBuilder.AppendLine($"Headers:{Environment.NewLine}{HeadersToString(response.Headers)}{Environment.NewLine}");
return stringBuilder.ToString();
}
internal static string HeadersToString(HttpHeaders headers)
{
return HeadersToString(ConvertHttpHeadersToCollection(headers));
}
private static readonly Regex regexPattern = new Regex("(\\s*\"access_token\"\\s*:\\s*)\"[^\"]+\"", RegexOptions.Compiled);
private static object SanitizeBody(string body)
{
IList<Regex> regexList = new List<Regex>();
// Remove access_token:* instances in body.
regexList.Add(regexPattern);
foreach (Regex matcher in regexList)
{
body = matcher.Replace(body, "$1\"<redacted>\"");
}
// Remove password:* instances in body.
body = AbstractPasswords(body);
return body;
}
private static IDictionary<string, IEnumerable<string>> ConvertHttpHeadersToCollection(HttpHeaders headers)
{
headers.Remove("Authorization");
return headers.ToDictionary(a => a.Key, a => a.Value);
}
private static string HeadersToString(IDictionary<string, IEnumerable<string>> headers)
{
StringBuilder stringBuilder = headers.Aggregate(new StringBuilder(),
(sb, kvp) => sb.AppendLine(string.Format("{0,-30}: {1}", kvp.Key, String.Join(",", kvp.Value.ToArray()))));
if (stringBuilder.Length > 0)
stringBuilder.Remove(stringBuilder.Length - 2, 2);
return stringBuilder.ToString();
}
private static string FormatString(string content)
{
try
{
content = content.Trim();
if ((content.StartsWith("{") && content.EndsWith("}")) || // object
(content.StartsWith("[") && content.EndsWith("]"))) // array
{
return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(content), Formatting.Indented);
}
if (content.StartsWith("<"))
{
return XDocument.Parse(content).ToString();
}
}
catch
{
return content;
}
if (content.Length > Microsoft.Graph.PowerShell.Authentication.Constants.MaxContentLength)
{
return content.Substring(0, Microsoft.Graph.PowerShell.Authentication.Constants.MaxContentLength) + "\r\nDATA TRUNCATED DUE TO SIZE\r\n";
}
return content;
}
private static string AbstractPasswords(string content)
{
// Check if the JSON string contains "password" (case-insensitive)
if (content.IndexOf("password", StringComparison.OrdinalIgnoreCase) >= 0)
{
// Deserialize JSON into Dictionary
var obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
// Replace values for properties containing "password" (case-insensitive)
foreach (var key in obj.Keys)
{
if (key.IndexOf("password", StringComparison.OrdinalIgnoreCase) >= 0)
{
obj[key] = "***";
}
}
// Serialize the updated dictionary back to JSON
content = JsonConvert.SerializeObject(obj, Formatting.Indented);
}
return content;
}
}
}