1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Collections . Specialized ;
4
+ using System . Diagnostics ;
4
5
using System . IO ;
5
6
using System . Linq ;
7
+ using System . Text ;
6
8
using System . Web ;
7
9
using Exceptionless . Dependency ;
8
10
using Exceptionless . Extensions ;
12
14
namespace Exceptionless . ExtendedData {
13
15
internal static class RequestInfoCollector {
14
16
private const int MAX_DATA_ITEM_LENGTH = 1000 ;
17
+ private const int MAX_BODY_SIZE = 50 * 1024 ;
15
18
16
19
public static RequestInfo Collect ( HttpContextBase context , ExceptionlessConfiguration config ) {
17
20
if ( context == null )
@@ -52,28 +55,7 @@ public static RequestInfo Collect(HttpContextBase context, ExceptionlessConfigur
52
55
info . Cookies = context . Request . Cookies . ToDictionary ( exclusionList ) ;
53
56
54
57
if ( config . IncludePostData ) {
55
- if ( context . Request . Form . Count > 0 ) {
56
- info . PostData = context . Request . Form . ToDictionary ( exclusionList ) ;
57
- } else if ( context . Request . ContentLength > 0 ) {
58
- if ( context . Request . ContentLength < 1024 * 50 ) {
59
- try {
60
- if ( context . Request . InputStream . CanSeek && context . Request . InputStream . Position > 0 )
61
- context . Request . InputStream . Position = 0 ;
62
-
63
- if ( context . Request . InputStream . Position == 0 ) {
64
- using ( var inputStream = new StreamReader ( context . Request . InputStream ) )
65
- info . PostData = inputStream . ReadToEnd ( ) ;
66
- } else {
67
- info . PostData = "Unable to get POST data: The stream could not be reset." ;
68
- }
69
- } catch ( Exception ex ) {
70
- info . PostData = "Error retrieving POST data: " + ex . Message ;
71
- }
72
- } else {
73
- string value = Math . Round ( context . Request . ContentLength / 1024m , 0 ) . ToString ( "N0" ) ;
74
- info . PostData = String . Format ( "Data is too large ({0}kb) to be included." , value ) ;
75
- }
76
- }
58
+ info . PostData = GetPostData ( context , config , exclusionList ) ;
77
59
}
78
60
79
61
if ( config . IncludeQueryString ) {
@@ -87,6 +69,77 @@ public static RequestInfo Collect(HttpContextBase context, ExceptionlessConfigur
87
69
return info ;
88
70
}
89
71
72
+ private static object GetPostData ( HttpContextBase context , ExceptionlessConfiguration config , string [ ] exclusionList ) {
73
+ var log = config . Resolver . GetLog ( ) ;
74
+
75
+ if ( context . Request . Form . Count > 0 ) {
76
+ log . Debug ( "Reading POST data from Request.Form" ) ;
77
+
78
+ return context . Request . Form . ToDictionary ( exclusionList ) ;
79
+ }
80
+
81
+ var contentLength = context . Request . ContentLength ;
82
+ if ( contentLength == 0 ) {
83
+ string message = "Content-length was zero, empty post." ;
84
+ log . Debug ( message ) ;
85
+ return message ;
86
+ }
87
+
88
+ if ( contentLength > MAX_BODY_SIZE ) {
89
+ string value = Math . Round ( contentLength / 1024m , 0 ) . ToString ( "N0" ) ;
90
+ string message = String . Format ( "Data is too large ({0}kb) to be included." , value ) ;
91
+ log . Debug ( message ) ;
92
+ return message ;
93
+ }
94
+
95
+ try {
96
+ if ( ! context . Request . InputStream . CanSeek ) {
97
+ string message = "Unable to get POST data: The stream could not be reset." ;
98
+ log . Debug ( message ) ;
99
+ return message ;
100
+ }
101
+
102
+ long originalPosition = context . Request . InputStream . Position ;
103
+ if ( context . Request . InputStream . Position > 0 ) {
104
+ context . Request . InputStream . Position = 0 ;
105
+ }
106
+
107
+ log . FormattedDebug ( "Reading POST, original position: {0}" , originalPosition ) ;
108
+
109
+ if ( context . Request . InputStream . Position != 0 ) {
110
+ string message = "Unable to get POST data: The stream position was not at 0." ;
111
+ log . Debug ( message ) ;
112
+ return message ;
113
+ }
114
+
115
+ var maxDataToRead = contentLength == 0 ? MAX_BODY_SIZE : contentLength ;
116
+
117
+ // pass default values, except for leaveOpen: true. This prevents us from disposing the underlying stream
118
+ using ( var inputStream = new StreamReader ( context . Request . InputStream , Encoding . UTF8 , true , 1024 , true ) ) {
119
+ var sb = new StringBuilder ( ) ;
120
+ int numRead ;
121
+
122
+ int bufferSize = Math . Min ( 1024 , maxDataToRead ) ;
123
+
124
+ char [ ] buffer = new char [ bufferSize ] ;
125
+ while ( ( numRead = inputStream . ReadBlock ( buffer , 0 , bufferSize ) ) > 0 && ( sb . Length + numRead ) < maxDataToRead ) {
126
+ sb . Append ( buffer , 0 , numRead ) ;
127
+ }
128
+ string postData = sb . ToString ( ) ;
129
+
130
+ context . Request . InputStream . Position = originalPosition ;
131
+
132
+ log . FormattedDebug ( "Reading POST, set back to position: {0}" , originalPosition ) ;
133
+ return postData ;
134
+ }
135
+ }
136
+ catch ( Exception ex ) {
137
+ string message = $ "Error retrieving POST data: { ex . Message } ";
138
+ log . Error ( message ) ;
139
+ return message ;
140
+ }
141
+ }
142
+
90
143
private static readonly List < string > _ignoredFormFields = new List < string > {
91
144
"__*"
92
145
} ;
0 commit comments