@@ -20,14 +20,16 @@ namespace Titanium.Web.Proxy.EventArguments
2020 /// </summary>
2121 public class SessionEventArgs : EventArgs , IDisposable
2222 {
23+
2324 /// <summary>
24- /// Constructor to initialize the proxy
25+ /// Size of Buffers used by this object
2526 /// </summary>
26- internal SessionEventArgs ( )
27- {
28- ProxyClient = new ProxyClient ( ) ;
29- WebSession = new HttpWebClient ( ) ;
30- }
27+ private readonly int bufferSize ;
28+
29+ /// <summary>
30+ /// Holds a reference to proxy response handler method
31+ /// </summary>
32+ private readonly Func < SessionEventArgs , Task > httpResponseHandler ;
3133
3234 /// <summary>
3335 /// Holds a reference to client
@@ -50,13 +52,18 @@ internal SessionEventArgs()
5052
5153
5254 /// <summary>
53- /// implement any cleanup here
55+ /// Constructor to initialize the proxy
5456 /// </summary>
55- public void Dispose ( )
57+ internal SessionEventArgs ( int bufferSize , Func < SessionEventArgs , Task > httpResponseHandler )
5658 {
59+ this . bufferSize = bufferSize ;
60+ this . httpResponseHandler = httpResponseHandler ;
5761
62+ ProxyClient = new ProxyClient ( ) ;
63+ WebSession = new HttpWebClient ( ) ;
5864 }
5965
66+
6067 /// <summary>
6168 /// Read request body content as bytes[] for current session
6269 /// </summary>
@@ -80,19 +87,19 @@ private async Task ReadRequestBody()
8087 //For chunked request we need to read data as they arrive, until we reach a chunk end symbol
8188 if ( WebSession . Request . IsChunked )
8289 {
83- await this . ProxyClient . ClientStreamReader . CopyBytesToStreamChunked ( requestBodyStream ) ;
90+ await this . ProxyClient . ClientStreamReader . CopyBytesToStreamChunked ( bufferSize , requestBodyStream ) ;
8491 }
8592 else
8693 {
8794 //If not chunked then its easy just read the whole body with the content length mentioned in the request header
8895 if ( WebSession . Request . ContentLength > 0 )
8996 {
9097 //If not chunked then its easy just read the amount of bytes mentioned in content length header of response
91- await this . ProxyClient . ClientStreamReader . CopyBytesToStream ( requestBodyStream , WebSession . Request . ContentLength ) ;
98+ await this . ProxyClient . ClientStreamReader . CopyBytesToStream ( bufferSize , requestBodyStream , WebSession . Request . ContentLength ) ;
9299
93100 }
94101 else if ( WebSession . Request . HttpVersion . Major == 1 && WebSession . Request . HttpVersion . Minor == 0 )
95- await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( requestBodyStream , long . MaxValue ) ;
102+ await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , requestBodyStream , long . MaxValue ) ;
96103 }
97104 WebSession . Request . RequestBody = await GetDecompressedResponseBody ( WebSession . Request . ContentEncoding , requestBodyStream . ToArray ( ) ) ;
98105 }
@@ -117,18 +124,18 @@ private async Task ReadResponseBody()
117124 //If chuncked the read chunk by chunk until we hit chunk end symbol
118125 if ( WebSession . Response . IsChunked )
119126 {
120- await WebSession . ServerConnection . StreamReader . CopyBytesToStreamChunked ( responseBodyStream ) ;
127+ await WebSession . ServerConnection . StreamReader . CopyBytesToStreamChunked ( bufferSize , responseBodyStream ) ;
121128 }
122129 else
123130 {
124131 if ( WebSession . Response . ContentLength > 0 )
125132 {
126133 //If not chunked then its easy just read the amount of bytes mentioned in content length header of response
127- await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( responseBodyStream , WebSession . Response . ContentLength ) ;
134+ await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , responseBodyStream , WebSession . Response . ContentLength ) ;
128135
129136 }
130137 else if ( WebSession . Response . HttpVersion . Major == 1 && WebSession . Response . HttpVersion . Minor == 0 )
131- await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( responseBodyStream , long . MaxValue ) ;
138+ await WebSession . ServerConnection . StreamReader . CopyBytesToStream ( bufferSize , responseBodyStream , long . MaxValue ) ;
132139 }
133140
134141 WebSession . Response . ResponseBody = await GetDecompressedResponseBody ( WebSession . Response . ContentEncoding , responseBodyStream . ToArray ( ) ) ;
@@ -285,7 +292,7 @@ private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte
285292 var decompressionFactory = new DecompressionFactory ( ) ;
286293 var decompressor = decompressionFactory . Create ( encodingType ) ;
287294
288- return await decompressor . Decompress ( responseBodyStream ) ;
295+ return await decompressor . Decompress ( responseBodyStream , bufferSize ) ;
289296 }
290297
291298
@@ -338,7 +345,7 @@ public async Task Redirect(string url)
338345
339346 WebSession . Request . CancelRequest = true ;
340347 }
341-
348+
342349 /// a generic responder method
343350 public async Task Respond ( Response response )
344351 {
@@ -349,8 +356,15 @@ public async Task Respond(Response response)
349356
350357 WebSession . Response = response ;
351358
352- await ProxyServer . HandleHttpSessionResponse ( this ) ;
359+ await httpResponseHandler ( this ) ;
353360 }
354361
362+ /// <summary>
363+ /// implement any cleanup here
364+ /// </summary>
365+ public void Dispose ( )
366+ {
367+
368+ }
355369 }
356370}
0 commit comments