@@ -72,15 +72,15 @@ private async Task ReadRequestBody()
72
72
if ( ( WebSession . Request . Method . ToUpper ( ) != "POST" && WebSession . Request . Method . ToUpper ( ) != "PUT" ) )
73
73
{
74
74
throw new BodyNotFoundException ( "Request don't have a body." +
75
- "Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body." ) ;
75
+ "Please verify that this request is a Http POST/PUT and request " +
76
+ "content length is greater than zero before accessing the body." ) ;
76
77
}
77
78
78
79
//Caching check
79
80
if ( WebSession . Request . RequestBody == null )
80
81
{
81
82
82
83
//If chunked then its easy just read the whole body with the content length mentioned in the request header
83
-
84
84
using ( var requestBodyStream = new MemoryStream ( ) )
85
85
{
86
86
//For chunked request we need to read data as they arrive, until we reach a chunk end symbol
@@ -94,13 +94,17 @@ private async Task ReadRequestBody()
94
94
if ( WebSession . Request . ContentLength > 0 )
95
95
{
96
96
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
97
- await this . ProxyClient . ClientStreamReader . CopyBytesToStream ( bufferSize , requestBodyStream , WebSession . Request . ContentLength ) ;
97
+ await this . ProxyClient . ClientStreamReader . CopyBytesToStream ( bufferSize , requestBodyStream ,
98
+ WebSession . Request . ContentLength ) ;
98
99
99
100
}
100
101
else if ( WebSession . Request . HttpVersion . Major == 1 && WebSession . Request . HttpVersion . Minor == 0 )
102
+ {
101
103
await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , requestBodyStream , long . MaxValue ) ;
104
+ }
102
105
}
103
- WebSession . Request . RequestBody = await GetDecompressedResponseBody ( WebSession . Request . ContentEncoding , requestBodyStream . ToArray ( ) ) ;
106
+ WebSession . Request . RequestBody = await GetDecompressedResponseBody ( WebSession . Request . ContentEncoding ,
107
+ requestBodyStream . ToArray ( ) ) ;
104
108
}
105
109
106
110
//Now set the flag to true
@@ -130,14 +134,18 @@ private async Task ReadResponseBody()
130
134
if ( WebSession . Response . ContentLength > 0 )
131
135
{
132
136
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
133
- await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , responseBodyStream , WebSession . Response . ContentLength ) ;
137
+ await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , responseBodyStream ,
138
+ WebSession . Response . ContentLength ) ;
134
139
135
140
}
136
141
else if ( WebSession . Response . HttpVersion . Major == 1 && WebSession . Response . HttpVersion . Minor == 0 )
142
+ {
137
143
await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , responseBodyStream , long . MaxValue ) ;
144
+ }
138
145
}
139
146
140
- WebSession . Response . ResponseBody = await GetDecompressedResponseBody ( WebSession . Response . ContentEncoding , responseBodyStream . ToArray ( ) ) ;
147
+ WebSession . Response . ResponseBody = await GetDecompressedResponseBody ( WebSession . Response . ContentEncoding ,
148
+ responseBodyStream . ToArray ( ) ) ;
141
149
142
150
}
143
151
//set this to true for caching
@@ -154,7 +162,9 @@ public async Task<byte[]> GetRequestBody()
154
162
if ( ! WebSession . Request . RequestBodyRead )
155
163
{
156
164
if ( WebSession . Request . RequestLocked )
165
+ {
157
166
throw new Exception ( "You cannot call this function after request is made to server." ) ;
167
+ }
158
168
159
169
await ReadRequestBody ( ) ;
160
170
}
@@ -169,7 +179,9 @@ public async Task<string> GetRequestBodyAsString()
169
179
if ( ! WebSession . Request . RequestBodyRead )
170
180
{
171
181
if ( WebSession . Request . RequestLocked )
182
+ {
172
183
throw new Exception ( "You cannot call this function after request is made to server." ) ;
184
+ }
173
185
174
186
await ReadRequestBody ( ) ;
175
187
}
@@ -184,7 +196,9 @@ public async Task<string> GetRequestBodyAsString()
184
196
public async Task SetRequestBody ( byte [ ] body )
185
197
{
186
198
if ( WebSession . Request . RequestLocked )
199
+ {
187
200
throw new Exception ( "You cannot call this function after request is made to server." ) ;
201
+ }
188
202
189
203
//syphon out the request body from client before setting the new body
190
204
if ( ! WebSession . Request . RequestBodyRead )
@@ -195,9 +209,13 @@ public async Task SetRequestBody(byte[] body)
195
209
WebSession . Request . RequestBody = body ;
196
210
197
211
if ( WebSession . Request . IsChunked == false )
212
+ {
198
213
WebSession . Request . ContentLength = body . Length ;
214
+ }
199
215
else
216
+ {
200
217
WebSession . Request . ContentLength = - 1 ;
218
+ }
201
219
}
202
220
203
221
/// <summary>
@@ -207,7 +225,9 @@ public async Task SetRequestBody(byte[] body)
207
225
public async Task SetRequestBodyString ( string body )
208
226
{
209
227
if ( WebSession . Request . RequestLocked )
228
+ {
210
229
throw new Exception ( "You cannot call this function after request is made to server." ) ;
230
+ }
211
231
212
232
//syphon out the request body from client before setting the new body
213
233
if ( ! WebSession . Request . RequestBodyRead )
@@ -226,7 +246,9 @@ public async Task SetRequestBodyString(string body)
226
246
public async Task < byte [ ] > GetResponseBody ( )
227
247
{
228
248
if ( ! WebSession . Request . RequestLocked )
249
+ {
229
250
throw new Exception ( "You cannot call this function before request is made to server." ) ;
251
+ }
230
252
231
253
await ReadResponseBody ( ) ;
232
254
return WebSession . Response . ResponseBody ;
@@ -239,11 +261,14 @@ public async Task<byte[]> GetResponseBody()
239
261
public async Task < string > GetResponseBodyAsString ( )
240
262
{
241
263
if ( ! WebSession . Request . RequestLocked )
264
+ {
242
265
throw new Exception ( "You cannot call this function before request is made to server." ) ;
266
+ }
243
267
244
268
await GetResponseBody ( ) ;
245
269
246
- return WebSession . Response . ResponseBodyString ?? ( WebSession . Response . ResponseBodyString = WebSession . Response . Encoding . GetString ( WebSession . Response . ResponseBody ) ) ;
270
+ return WebSession . Response . ResponseBodyString ??
271
+ ( WebSession . Response . ResponseBodyString = WebSession . Response . Encoding . GetString ( WebSession . Response . ResponseBody ) ) ;
247
272
}
248
273
249
274
/// <summary>
@@ -253,7 +278,9 @@ public async Task<string> GetResponseBodyAsString()
253
278
public async Task SetResponseBody ( byte [ ] body )
254
279
{
255
280
if ( ! WebSession . Request . RequestLocked )
281
+ {
256
282
throw new Exception ( "You cannot call this function before request is made to server." ) ;
283
+ }
257
284
258
285
//syphon out the response body from server before setting the new body
259
286
if ( WebSession . Response . ResponseBody == null )
@@ -265,9 +292,13 @@ public async Task SetResponseBody(byte[] body)
265
292
266
293
//If there is a content length header update it
267
294
if ( WebSession . Response . IsChunked == false )
295
+ {
268
296
WebSession . Response . ContentLength = body . Length ;
297
+ }
269
298
else
299
+ {
270
300
WebSession . Response . ContentLength = - 1 ;
301
+ }
271
302
}
272
303
273
304
/// <summary>
@@ -277,7 +308,9 @@ public async Task SetResponseBody(byte[] body)
277
308
public async Task SetResponseBodyString ( string body )
278
309
{
279
310
if ( ! WebSession . Request . RequestLocked )
311
+ {
280
312
throw new Exception ( "You cannot call this function before request is made to server." ) ;
313
+ }
281
314
282
315
//syphon out the response body from server before setting the new body
283
316
if ( WebSession . Response . ResponseBody == null )
@@ -308,10 +341,14 @@ private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte
308
341
public async Task Ok ( string html )
309
342
{
310
343
if ( WebSession . Request . RequestLocked )
344
+ {
311
345
throw new Exception ( "You cannot call this function after request is made to server." ) ;
346
+ }
312
347
313
348
if ( html == null )
349
+ {
314
350
html = string . Empty ;
351
+ }
315
352
316
353
var result = Encoding . Default . GetBytes ( html ) ;
317
354
@@ -341,7 +378,7 @@ public async Task Redirect(string url)
341
378
var response = new RedirectResponse ( ) ;
342
379
343
380
response . HttpVersion = WebSession . Request . HttpVersion ;
344
- response . ResponseHeaders . Add ( new Models . HttpHeader ( "Location" , url ) ) ;
381
+ response . ResponseHeaders . Add ( "Location" , new Models . HttpHeader ( "Location" , url ) ) ;
345
382
response . ResponseBody = Encoding . ASCII . GetBytes ( string . Empty ) ;
346
383
347
384
await Respond ( response ) ;
0 commit comments