-
Notifications
You must be signed in to change notification settings - Fork 44
/
HttpRequestPackageDecorator.cs
268 lines (242 loc) · 10.4 KB
/
HttpRequestPackageDecorator.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
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
namespace Rollbar.Net.AspNet
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using Rollbar.Common;
using Rollbar.Diagnostics;
using Rollbar.DTOs;
/// <summary>
/// Class HttpRequestPackageDecorator.
/// Implements the <see cref="Rollbar.RollbarPackageDecoratorBase" /></summary>
/// <seealso cref="Rollbar.RollbarPackageDecoratorBase" />
public class HttpRequestPackageDecorator
: RollbarPackageDecoratorBase
{
/// <summary>
/// The HTTP request
/// </summary>
private readonly HttpRequestBase _httpRequest;
/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestPackageDecorator"/> class.
/// </summary>
/// <param name="packageToDecorate">The package to decorate.</param>
/// <param name="httpRequest">The HTTP request.</param>
public HttpRequestPackageDecorator(IRollbarPackage packageToDecorate, HttpRequest httpRequest)
: this(packageToDecorate, new HttpRequestWrapper(httpRequest), false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestPackageDecorator"/> class.
/// </summary>
/// <param name="packageToDecorate">The package to decorate.</param>
/// <param name="httpRequest">The HTTP request.</param>
/// <param name="mustApplySynchronously">if set to <c>true</c> [must apply synchronously].</param>
public HttpRequestPackageDecorator(IRollbarPackage packageToDecorate, HttpRequest httpRequest, bool mustApplySynchronously)
: this(packageToDecorate, new HttpRequestWrapper(httpRequest), mustApplySynchronously)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestPackageDecorator"/> class.
/// </summary>
/// <param name="packageToDecorate">The package to decorate.</param>
/// <param name="httpRequest">The HTTP request.</param>
public HttpRequestPackageDecorator(IRollbarPackage packageToDecorate, HttpRequestBase httpRequest)
: this(packageToDecorate, httpRequest, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestPackageDecorator"/> class.
/// </summary>
/// <param name="packageToDecorate">The package to decorate.</param>
/// <param name="httpRequest">The HTTP request.</param>
/// <param name="mustApplySynchronously">if set to <c>true</c> [must apply synchronously].</param>
public HttpRequestPackageDecorator(IRollbarPackage packageToDecorate, HttpRequestBase httpRequest, bool mustApplySynchronously)
: base(packageToDecorate, mustApplySynchronously)
{
Assumption.AssertNotNull(httpRequest, nameof(httpRequest));
this._httpRequest = httpRequest;
}
/// <summary>
/// Decorates the specified rollbar data.
/// </summary>
/// <param name="rollbarData">The rollbar data.</param>
protected override void Decorate(Data? rollbarData)
{
if(rollbarData == null)
{
return;
}
if(this._httpRequest == null || rollbarData == null)
{
return; //nothing to do...
}
// try harvesting Request DTO info:
///////////////////////////////////
Request request = new();
request.Url = this._httpRequest.Url?.ToString();
request.Method = this._httpRequest.HttpMethod;
request.Headers = this._httpRequest.Headers?.ToCompactStringDictionary();
try
{
request.GetParams = this._httpRequest.QueryString?.ToCompactObjectDictionary();
}
catch
{
// calls in try-block may throw an exception. we are just trying our best...
}
try
{
request.PostParams = this._httpRequest.Unvalidated?.Form?.ToCompactObjectDictionary();
}
catch
{
// calls in try-block may throw an exception. we are just trying our best...
}
// add posted files to the post collection
try
{
if (this._httpRequest.Files?.Count > 0)
{
var files =
this._httpRequest.Files.AllKeys.ToDictionary(k => k, k => this._httpRequest.Files[k].RenderAsString());
if (request.PostParams == null)
{
request.PostParams = new Dictionary<string, object>(files.Count);
}
foreach (var file in files)
{
request.PostParams.Add(file.Key, "FILE: " + file.Value);
}
}
}
catch (HttpException)
{
// Files from request could not be read here because they are streamed
// and have been read earlier by e.g. WCF Rest Service or Open RIA Services
}
// if the X-Forwarded-For header exists, use that as the user's IP.
// that will be the true remote IP of a user behind a proxy server or load balancer
var forwardedFor = this._httpRequest.Headers?["X-Forwarded-For"];
if (!string.IsNullOrEmpty(forwardedFor) && forwardedFor!.Contains(","))
{
forwardedFor = forwardedFor.Split(',').Last().Trim();
}
try
{
request.UserIp =
forwardedFor ?? this._httpRequest.UserHostAddress;
}
catch
{
// calls in try-block may throw an exception. we are just trying our best...
}
try
{
request.Params =
this._httpRequest.RequestContext.RouteData.Values
.ToDictionary(v => v.Key, v => v.Value.ToString()).ToObjectDictionary();
}
catch
{
// calls in try-block may throw an exception. we are just trying our best...
}
// try harvesting custom HTTP session info:
try
{
var httpSession = this._httpRequest.RequestContext?.HttpContext?.Session;
if (httpSession != null)
{
request["session"] = httpSession.Keys
.Cast<string>()
.Where(key => key != null)
.ToDictionary(key => key, key => httpSession[key].RenderAsString());
}
}
catch
{
// Session state may not be configured.
}
rollbarData.Request = request;
// let's try to get any useful data from the HTTP request's server variables:
/////////////////////////////////////////////////////////////////////////////
try
{
var serverVariables = this._httpRequest?.ServerVariables;
if (serverVariables != null)
{
// try harvesting Person DTO info:
//////////////////////////////////
string[] serverVarsOfInterest = new[]
{
"AUTH_USER",
"LOGON_USER",
"REMOTE_USER",
};
string? username = null;
foreach (var serverVar in serverVarsOfInterest)
{
try
{
username = serverVariables[serverVar];
}
catch
{
// calls in try-block may throw an exception. we are just trying our best...
}
if (!string.IsNullOrWhiteSpace(username))
{
break;
}
}
if (!string.IsNullOrWhiteSpace(username))
{
rollbarData.Person = new Person(username!)
{
UserName = username!,
};
}
// try harvesting Server DTO info:
//////////////////////////////////
var host = serverVariables.Get("HTTP_HOST");
if (string.IsNullOrEmpty(host))
{
host = serverVariables.Get("SERVER_NAME");
}
var root = serverVariables.Get("APPL_PHYSICAL_PATH");
if (string.IsNullOrEmpty(root))
{
root = HttpRuntime.AppDomainAppPath ?? Environment.CurrentDirectory;
}
var machine = Environment.MachineName;
var webServer = serverVariables["SERVER_SOFTWARE"];
if (!string.IsNullOrWhiteSpace(host)
|| !string.IsNullOrWhiteSpace(root)
|| !string.IsNullOrWhiteSpace(machine)
|| !string.IsNullOrWhiteSpace(webServer)
)
{
rollbarData.Server = new Server { Host = host, Root = root, };
rollbarData.Server["host_machine"] = machine;
rollbarData.Server["host_webserver"] = webServer;
}
}
else if (!HostingEnvironment.IsHosted)
{
// try harvesting Person DTO info (from Environment):
/////////////////////////////////////////////////////
rollbarData.Person = new Person($"{Environment.MachineName}\\{Environment.UserName}")
{
UserName = Environment.UserName,
};
}
}
catch
{
// calls in try-block may throw an exception. we are just trying our best...
}
}
}
}