@@ -62,8 +62,7 @@ internal SessionEventArgs(int bufferSize, Func<SessionEventArgs, Task> httpRespo
62
62
ProxyClient = new ProxyClient ( ) ;
63
63
WebSession = new HttpWebClient ( ) ;
64
64
}
65
-
66
-
65
+
67
66
/// <summary>
68
67
/// Read request body content as bytes[] for current session
69
68
/// </summary>
@@ -98,7 +97,7 @@ private async Task ReadRequestBody()
98
97
await this . ProxyClient . ClientStreamReader . CopyBytesToStream ( bufferSize , requestBodyStream , WebSession . Request . ContentLength ) ;
99
98
100
99
}
101
- else if ( WebSession . Request . HttpVersion . Major == 1 && WebSession . Request . HttpVersion . Minor == 0 )
100
+ else if ( WebSession . Request . HttpVersion . Major == 1 && WebSession . Request . HttpVersion . Minor == 0 )
102
101
await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , requestBodyStream , long . MaxValue ) ;
103
102
}
104
103
WebSession . Request . RequestBody = await GetDecompressedResponseBody ( WebSession . Request . ContentEncoding , requestBodyStream . ToArray ( ) ) ;
@@ -108,7 +107,7 @@ private async Task ReadRequestBody()
108
107
//So that next time we can deliver body from cache
109
108
WebSession . Request . RequestBodyRead = true ;
110
109
}
111
-
110
+
112
111
}
113
112
114
113
/// <summary>
@@ -134,7 +133,7 @@ private async Task ReadResponseBody()
134
133
await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , responseBodyStream , WebSession . Response . ContentLength ) ;
135
134
136
135
}
137
- else if ( WebSession . Response . HttpVersion . Major == 1 && WebSession . Response . HttpVersion . Minor == 0 )
136
+ else if ( WebSession . Response . HttpVersion . Major == 1 && WebSession . Response . HttpVersion . Minor == 0 )
138
137
await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , responseBodyStream , long . MaxValue ) ;
139
138
}
140
139
@@ -152,10 +151,13 @@ private async Task ReadResponseBody()
152
151
/// <returns></returns>
153
152
public async Task < byte [ ] > GetRequestBody ( )
154
153
{
155
- if ( WebSession . Request . RequestLocked )
156
- throw new Exception ( "You cannot call this function after request is made to server." ) ;
154
+ if ( ! WebSession . Request . RequestBodyRead )
155
+ {
156
+ if ( WebSession . Request . RequestLocked )
157
+ throw new Exception ( "You cannot call this function after request is made to server." ) ;
157
158
158
- await ReadRequestBody ( ) ;
159
+ await ReadRequestBody ( ) ;
160
+ }
159
161
return WebSession . Request . RequestBody ;
160
162
}
161
163
/// <summary>
@@ -164,12 +166,13 @@ public async Task<byte[]> GetRequestBody()
164
166
/// <returns></returns>
165
167
public async Task < string > GetRequestBodyAsString ( )
166
168
{
167
- if ( WebSession . Request . RequestLocked )
168
- throw new Exception ( "You cannot call this function after request is made to server." ) ;
169
-
170
-
171
- await ReadRequestBody ( ) ;
169
+ if ( ! WebSession . Request . RequestBodyRead )
170
+ {
171
+ if ( WebSession . Request . RequestLocked )
172
+ throw new Exception ( "You cannot call this function after request is made to server." ) ;
172
173
174
+ await ReadRequestBody ( ) ;
175
+ }
173
176
//Use the encoding specified in request to decode the byte[] data to string
174
177
return WebSession . Request . RequestBodyString ?? ( WebSession . Request . RequestBodyString = WebSession . Request . Encoding . GetString ( WebSession . Request . RequestBody ) ) ;
175
178
}
@@ -285,7 +288,7 @@ public async Task SetResponseBodyString(string body)
285
288
var bodyBytes = WebSession . Response . Encoding . GetBytes ( body ) ;
286
289
287
290
await SetResponseBody ( bodyBytes ) ;
288
- }
291
+ }
289
292
290
293
private async Task < byte [ ] > GetDecompressedResponseBody ( string encodingType , byte [ ] responseBodyStream )
291
294
{
@@ -345,7 +348,7 @@ public async Task Redirect(string url)
345
348
346
349
WebSession . Request . CancelRequest = true ;
347
350
}
348
-
351
+
349
352
/// a generic responder method
350
353
public async Task Respond ( Response response )
351
354
{
0 commit comments