14
14
namespace Exceptionless . AspNetCore {
15
15
public static class RequestInfoCollector {
16
16
private const int MAX_BODY_SIZE = 50 * 1024 ;
17
+ private const int MAX_DATA_ITEM_LENGTH = 1000 ;
18
+
17
19
public static RequestInfo Collect ( HttpContext context , ExceptionlessConfiguration config ) {
18
20
if ( context == null )
19
21
return null ;
20
22
21
23
var info = new RequestInfo {
22
24
HttpMethod = context . Request . Method ,
23
25
IsSecure = context . Request . IsHttps ,
24
- Path = context . Request . Path . HasValue ? context . Request . Path . Value : "/" ,
26
+ Path = context . Request . Path . HasValue ? context . Request . Path . Value : "/"
25
27
} ;
26
28
27
29
if ( config . IncludeIpAddress )
@@ -32,13 +34,16 @@ public static RequestInfo Collect(HttpContext context, ExceptionlessConfiguratio
32
34
33
35
info . Port = context . Request . Host . Port . GetValueOrDefault ( info . IsSecure ? 443 : 80 ) ;
34
36
35
- if ( context . Request . Headers . ContainsKey ( HeaderNames . UserAgent ) )
36
- info . UserAgent = context . Request . Headers [ HeaderNames . UserAgent ] . ToString ( ) ;
37
+ if ( context . Request . Headers . TryGetValue ( HeaderNames . UserAgent , out var userAgentHeader ) )
38
+ info . UserAgent = userAgentHeader . ToString ( ) ;
37
39
38
- if ( context . Request . Headers . ContainsKey ( HeaderNames . Referer ) )
39
- info . Referrer = context . Request . Headers [ HeaderNames . Referer ] . ToString ( ) ;
40
+ if ( context . Request . Headers . TryGetValue ( HeaderNames . Referer , out var refererHeader ) )
41
+ info . Referrer = refererHeader . ToString ( ) ;
40
42
41
43
var exclusionList = config . DataExclusions as string [ ] ?? config . DataExclusions . ToArray ( ) ;
44
+ if ( config . IncludeHeaders )
45
+ info . Headers = context . Request . Headers . ToHeaderDictionary ( exclusionList ) ;
46
+
42
47
if ( config . IncludeCookies )
43
48
info . Cookies = context . Request . Cookies . ToDictionary ( exclusionList ) ;
44
49
@@ -93,17 +98,15 @@ private static object GetPostData(HttpContext context, ExceptionlessConfiguratio
93
98
return message ;
94
99
}
95
100
96
- var maxDataToRead = contentLength == 0 ? MAX_BODY_SIZE : contentLength ;
97
-
98
101
// pass default values, except for leaveOpen: true. This prevents us from disposing the underlying stream
99
102
using ( var inputStream = new StreamReader ( context . Request . Body , Encoding . UTF8 , true , 1024 , true ) ) {
100
103
var sb = new StringBuilder ( ) ;
101
104
int numRead ;
102
105
103
- int bufferSize = ( int ) Math . Min ( 1024 , maxDataToRead ) ;
106
+ int bufferSize = ( int ) Math . Min ( 1024 , contentLength ) ;
104
107
105
108
char [ ] buffer = new char [ bufferSize ] ;
106
- while ( ( numRead = inputStream . ReadBlock ( buffer , 0 , bufferSize ) ) > 0 && ( sb . Length + numRead ) <= maxDataToRead ) {
109
+ while ( ( numRead = inputStream . ReadBlock ( buffer , 0 , bufferSize ) ) > 0 && ( sb . Length + numRead ) <= contentLength ) {
107
110
sb . Append ( buffer , 0 , numRead ) ;
108
111
}
109
112
string postData = sb . ToString ( ) ;
@@ -121,8 +124,15 @@ private static object GetPostData(HttpContext context, ExceptionlessConfiguratio
121
124
}
122
125
}
123
126
124
- private static readonly List < string > _ignoredFormFields = new List < string > {
125
- "__*"
127
+ private static readonly List < string > _ignoredHeaders = new List < string > {
128
+ HeaderNames . Authorization ,
129
+ HeaderNames . Cookie ,
130
+ HeaderNames . Host ,
131
+ HeaderNames . Method ,
132
+ HeaderNames . Path ,
133
+ HeaderNames . ProxyAuthorization ,
134
+ HeaderNames . Referer ,
135
+ HeaderNames . UserAgent
126
136
} ;
127
137
128
138
private static readonly List < string > _ignoredCookies = new List < string > {
@@ -131,20 +141,44 @@ private static object GetPostData(HttpContext context, ExceptionlessConfiguratio
131
141
"*SessionId*"
132
142
} ;
133
143
134
- private static Dictionary < string , string > ToDictionary ( this IRequestCookieCollection cookies , IList < string > exclusions ) {
144
+ private static readonly List < string > _ignoredFormFields = new List < string > {
145
+ "__*"
146
+ } ;
147
+
148
+ private static Dictionary < string , string [ ] > ToHeaderDictionary ( this IEnumerable < KeyValuePair < string , StringValues > > headers , string [ ] exclusions ) {
149
+ var d = new Dictionary < string , string [ ] > ( ) ;
150
+
151
+ foreach ( var header in headers ) {
152
+ if ( String . IsNullOrEmpty ( header . Key ) || _ignoredHeaders . Contains ( header . Key ) || header . Key . AnyWildcardMatches ( exclusions ) )
153
+ continue ;
154
+
155
+ string [ ] values = header . Value . Where ( hv => hv != null && hv . Length < MAX_DATA_ITEM_LENGTH ) . ToArray ( ) ;
156
+ if ( values . Length == 0 )
157
+ continue ;
158
+
159
+ d [ header . Key ] = values ;
160
+ }
161
+
162
+ return d ;
163
+ }
164
+
165
+ private static Dictionary < string , string > ToDictionary ( this IRequestCookieCollection cookies , string [ ] exclusions ) {
135
166
var d = new Dictionary < string , string > ( ) ;
136
167
137
168
foreach ( var kvp in cookies ) {
138
169
if ( String . IsNullOrEmpty ( kvp . Key ) || kvp . Key . AnyWildcardMatches ( _ignoredCookies ) || kvp . Key . AnyWildcardMatches ( exclusions ) )
139
170
continue ;
140
171
141
- d . Add ( kvp . Key , kvp . Value ) ;
172
+ if ( kvp . Value == null || kvp . Value . Length >= MAX_DATA_ITEM_LENGTH )
173
+ continue ;
174
+
175
+ d [ kvp . Key ] = kvp . Value ;
142
176
}
143
177
144
178
return d ;
145
179
}
146
180
147
- private static Dictionary < string , string > ToDictionary ( this IEnumerable < KeyValuePair < string , StringValues > > values , IEnumerable < string > exclusions ) {
181
+ private static Dictionary < string , string > ToDictionary ( this IEnumerable < KeyValuePair < string , StringValues > > values , string [ ] exclusions ) {
148
182
var d = new Dictionary < string , string > ( ) ;
149
183
150
184
foreach ( var kvp in values ) {
@@ -153,10 +187,12 @@ private static Dictionary<string, string> ToDictionary(this IEnumerable<KeyValue
153
187
154
188
try {
155
189
string value = kvp . Value . ToString ( ) ;
156
- d . Add ( kvp . Key , value ) ;
190
+ if ( value . Length >= MAX_DATA_ITEM_LENGTH )
191
+ continue ;
192
+
193
+ d [ kvp . Key ] = value ;
157
194
} catch ( Exception ex ) {
158
- if ( ! d . ContainsKey ( kvp . Key ) )
159
- d . Add ( kvp . Key , ex . Message ) ;
195
+ d [ kvp . Key ] = $ "EXCEPTION: { ex . Message } ";
160
196
}
161
197
}
162
198
0 commit comments