1
- using StackifyLib . Models ;
2
- using System ;
1
+ using System ;
3
2
using System . Collections . Generic ;
4
3
using System . Linq ;
5
- using System . Threading . Tasks ;
6
4
using Microsoft . AspNetCore . Http ;
7
5
using Microsoft . AspNetCore . Routing ;
8
- using Microsoft . Extensions . Primitives ;
9
6
using Microsoft . Extensions . DependencyInjection ;
7
+ using Microsoft . Extensions . Primitives ;
8
+ using StackifyLib . Models ;
10
9
11
10
namespace StackifyLib . AspNetCore
12
11
{
@@ -22,67 +21,43 @@ internal static void WebRequestDetail_SetWebRequestDetail(WebRequestDetail detai
22
21
var context = Configure . ServiceProvider ? . GetService < IHttpContextAccessor > ( ) ? . HttpContext ;
23
22
24
23
if ( context == null )
24
+ {
25
25
return ;
26
+ }
26
27
27
28
Load ( context , detail ) ;
28
29
}
29
30
30
31
private static void Load ( HttpContext context , WebRequestDetail detail )
31
32
{
32
33
if ( context == null || context . Request == null )
34
+ {
33
35
return ;
36
+ }
34
37
35
- HttpRequest request = context . Request ;
38
+ var request = context . Request ;
36
39
37
40
try
38
41
{
39
42
detail . HttpMethod = request . Method ;
40
-
41
- detail . UserIPAddress = context ? . Connection ? . RemoteIpAddress ? . ToString ( ) ;
42
-
43
-
44
- //if (context.Items != null && context.Items.Contains("Stackify.ReportingUrl"))
45
- //{
46
- // ReportingUrl = context.Items["Stackify.ReportingUrl"].ToString();
47
- //}
48
-
49
-
50
- if ( request . IsHttps )
51
- {
52
- detail . RequestProtocol = "https" ;
53
- }
54
- else
55
- {
56
- detail . RequestProtocol = "http" ;
57
- }
58
- detail . RequestUrl = detail . RequestProtocol + "//" + request . Host + request . Path ;
59
-
60
-
43
+ detail . UserIPAddress = context . Connection ? . RemoteIpAddress ? . ToString ( ) ;
44
+ detail . RequestProtocol = request . IsHttps ? "https" : "http" ;
45
+ detail . RequestUrl = $ "{ detail . RequestProtocol } //{ request . Host } { request . Path } ";
61
46
detail . MVCAction = context . GetRouteValue ( "action" ) ? . ToString ( ) ;
62
47
detail . MVCController = context . GetRouteValue ( "controller" ) ? . ToString ( ) ;
63
48
64
- if ( ! string . IsNullOrEmpty ( detail . MVCAction ) && ! string . IsNullOrEmpty ( detail . MVCController ) )
49
+ if ( string . IsNullOrEmpty ( detail . MVCAction ) == false && string . IsNullOrEmpty ( detail . MVCController ) == false )
65
50
{
66
- detail . ReportingUrl = detail . MVCController + "." + detail . MVCAction ;
51
+ detail . ReportingUrl = $ " { detail . MVCController } . { detail . MVCAction } " ;
67
52
}
68
-
69
-
70
- //if (request.AppRelativeCurrentExecutionFilePath != null)
71
- //{
72
- // RequestUrlRoot = request.AppRelativeCurrentExecutionFilePath.TrimStart('~');
73
- //}
74
-
75
53
}
76
54
catch ( Exception )
77
55
{
78
-
56
+ // ignored
79
57
}
80
58
81
-
82
-
83
59
try
84
60
{
85
-
86
61
if ( request . QueryString != null )
87
62
{
88
63
detail . QueryString = ToKeyValues ( request . Query , null , null ) ;
@@ -95,12 +70,12 @@ private static void Load(HttpContext context, WebRequestDetail detail)
95
70
Config . ErrorHeaderBadKeys = new List < string > ( ) ;
96
71
}
97
72
98
- if ( ! Config . ErrorHeaderBadKeys . Contains ( "cookie" ) )
73
+ if ( Config . ErrorHeaderBadKeys . Contains ( "cookie" ) == false )
99
74
{
100
75
Config . ErrorHeaderBadKeys . Add ( "cookie" ) ;
101
76
}
102
77
103
- if ( ! Config . ErrorHeaderBadKeys . Contains ( "authorization" ) )
78
+ if ( Config . ErrorHeaderBadKeys . Contains ( "authorization" ) == false )
104
79
{
105
80
Config . ErrorHeaderBadKeys . Add ( "authorization" ) ;
106
81
}
@@ -117,66 +92,33 @@ private static void Load(HttpContext context, WebRequestDetail detail)
117
92
{
118
93
detail . PostData = ToKeyValues ( request . Form , null , null ) ;
119
94
}
120
-
121
- //sessions return a byte array...
122
- //if (context.Session != null && Config.CaptureSessionVariables && Config.ErrorSessionGoodKeys.Any())
123
- //{
124
- // SessionData = new Dictionary<string, string>();
125
-
126
- // foreach (var key in Config.ErrorSessionGoodKeys)
127
- // {
128
- // SessionData[key] = context.Session
129
- // }
130
-
131
-
132
- //}
133
-
134
- //if (Config.CaptureErrorPostdata)
135
- //{
136
- // var contentType = context.Request.Headers["Content-Type"];
137
-
138
- // if (contentType != "text/html" && contentType != "application/x-www-form-urlencoded" &&
139
- // context.Request.RequestType != "GET")
140
- // {
141
- // int length = 4096;
142
- // string postBody = new StreamReader(context.Request.InputStream).ReadToEnd();
143
- // if (postBody.Length < length)
144
- // {
145
- // length = postBody.Length;
146
- // }
147
-
148
- // PostDataRaw = postBody.Substring(0, length);
149
- // }
150
- //}
151
95
}
152
96
catch ( Exception )
153
97
{
98
+ // ignored
154
99
}
155
100
}
156
101
157
- //IEnumerable<KeyValuePair<string, StringValues>>
158
- //IEnumerable<KeyValuePair<string, string>>
159
102
internal static Dictionary < string , string > ToKeyValues ( IEnumerable < KeyValuePair < string , StringValues > > collection , List < string > goodKeys , List < string > badKeys )
160
103
{
161
- //var keys = collection.Keys;
162
104
var items = new Dictionary < string , string > ( ) ;
163
105
164
- foreach ( var item in collection )
106
+ foreach ( KeyValuePair < string , StringValues > item in collection )
165
107
{
166
- string key = item . Key ;
108
+ var key = item . Key ;
109
+
167
110
try
168
111
{
169
112
object val = item . Value . ToString ( ) ;
170
113
171
- if ( val != null && ! string . IsNullOrWhiteSpace ( val . ToString ( ) ) && items . ContainsKey ( key ) )
114
+ if ( val != null && string . IsNullOrWhiteSpace ( val . ToString ( ) ) == false && items . ContainsKey ( key ) )
172
115
{
173
116
AddKey ( key , val . ToString ( ) , items , goodKeys , badKeys ) ;
174
117
}
175
-
176
118
}
177
119
catch ( Exception )
178
120
{
179
-
121
+ // ignored
180
122
}
181
123
}
182
124
@@ -185,25 +127,24 @@ internal static Dictionary<string, string> ToKeyValues(IEnumerable<KeyValuePair<
185
127
186
128
internal static Dictionary < string , string > ToKeyValues ( IEnumerable < KeyValuePair < string , string > > collection , List < string > goodKeys , List < string > badKeys )
187
129
{
188
- //var keys = collection.Keys;
189
130
var items = new Dictionary < string , string > ( ) ;
190
131
191
- foreach ( var item in collection )
132
+ foreach ( KeyValuePair < string , string > item in collection )
192
133
{
193
- string key = item . Key ;
134
+ var key = item . Key ;
135
+
194
136
try
195
137
{
196
138
object val = item . Value ;
197
139
198
- if ( val != null && ! string . IsNullOrWhiteSpace ( val . ToString ( ) ) && items . ContainsKey ( key ) )
140
+ if ( val != null && string . IsNullOrWhiteSpace ( val . ToString ( ) ) == false && items . ContainsKey ( key ) )
199
141
{
200
142
AddKey ( key , val . ToString ( ) , items , goodKeys , badKeys ) ;
201
143
}
202
-
203
144
}
204
145
catch ( Exception )
205
146
{
206
-
147
+ // ignored
207
148
}
208
149
}
209
150
@@ -218,14 +159,15 @@ internal static void AddKey(string key, string value, Dictionary<string, string>
218
159
dictionary [ key ] = "X-MASKED-X" ;
219
160
return ;
220
161
}
162
+
221
163
//if not in the good key list, return
222
164
//if good key list is empty, we let it take it
223
- else if ( goodKeys != null && goodKeys . Any ( ) && ! goodKeys . Any ( x => x . Equals ( key , StringComparison . CurrentCultureIgnoreCase ) ) )
165
+ if ( goodKeys != null && goodKeys . Any ( ) && goodKeys . Any ( x => x . Equals ( key , StringComparison . CurrentCultureIgnoreCase ) ) == false )
224
166
{
225
167
return ;
226
168
}
227
169
228
170
dictionary [ key ] = value ;
229
171
}
230
172
}
231
- }
173
+ }
0 commit comments