10
10
using Titanium . Web . Proxy . Http . Responses ;
11
11
using Titanium . Web . Proxy . Models ;
12
12
using Titanium . Web . Proxy . Network ;
13
+ using Titanium . Web . Proxy . Network . Tcp ;
13
14
using Titanium . Web . Proxy . StreamExtended . Network ;
14
15
15
16
namespace Titanium . Web . Proxy . EventArguments
@@ -22,8 +23,6 @@ namespace Titanium.Web.Proxy.EventArguments
22
23
/// </summary>
23
24
public class SessionEventArgs : SessionEventArgsBase
24
25
{
25
- private static readonly byte [ ] emptyData = new byte [ 0 ] ;
26
-
27
26
/// <summary>
28
27
/// Backing field for corresponding public property
29
28
/// </summary>
@@ -37,8 +36,8 @@ public class SessionEventArgs : SessionEventArgsBase
37
36
/// <summary>
38
37
/// Constructor to initialize the proxy
39
38
/// </summary>
40
- internal SessionEventArgs ( ProxyServer server , ProxyEndPoint endPoint , ProxyClient proxyClient , ConnectRequest ? connectRequest , CancellationTokenSource cancellationTokenSource )
41
- : base ( server , endPoint , proxyClient , connectRequest , new Request ( ) , cancellationTokenSource )
39
+ internal SessionEventArgs ( ProxyServer server , ProxyEndPoint endPoint , TcpClientConnection clientConnection , HttpClientStream clientStream , ConnectRequest ? connectRequest , CancellationTokenSource cancellationTokenSource )
40
+ : base ( server , endPoint , clientConnection , clientStream , connectRequest , new Request ( ) , cancellationTokenSource )
42
41
{
43
42
}
44
43
@@ -66,14 +65,9 @@ public bool ReRequest
66
65
/// </summary>
67
66
public event EventHandler < MultipartRequestPartSentEventArgs > ? MultipartRequestPartSent ;
68
67
69
- private CustomBufferedStream getStreamReader ( bool isRequest )
70
- {
71
- return isRequest ? ProxyClient . ClientStream : HttpClient . Connection . Stream ;
72
- }
73
-
74
- private HttpWriter getStreamWriter ( bool isRequest )
68
+ private HttpStream getStream ( bool isRequest )
75
69
{
76
- return isRequest ? ( HttpWriter ) ProxyClient . ClientStreamWriter : HttpClient . Connection . StreamWriter ;
70
+ return isRequest ? ( HttpStream ) ClientStream : HttpClient . Connection . Stream ;
77
71
}
78
72
79
73
/// <summary>
@@ -110,7 +104,10 @@ private async Task readRequestBodyAsync(CancellationToken cancellationToken)
110
104
else
111
105
{
112
106
var body = await readBodyAsync ( true , cancellationToken ) ;
113
- request . Body = body ;
107
+ if ( ! request . BodyAvailable )
108
+ {
109
+ request . Body = body ;
110
+ }
114
111
115
112
// Now set the flag to true
116
113
// So that next time we can deliver body from cache
@@ -182,7 +179,10 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
182
179
else
183
180
{
184
181
var body = await readBodyAsync ( false , cancellationToken ) ;
185
- response . Body = body ;
182
+ if ( ! response . BodyAvailable )
183
+ {
184
+ response . Body = body ;
185
+ }
186
186
187
187
// Now set the flag to true
188
188
// So that next time we can deliver body from cache
@@ -193,21 +193,19 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
193
193
194
194
private async Task < byte [ ] > readBodyAsync ( bool isRequest , CancellationToken cancellationToken )
195
195
{
196
- using ( var bodyStream = new MemoryStream ( ) )
197
- {
198
- var writer = new HttpWriter ( bodyStream , BufferPool ) ;
199
-
200
- if ( isRequest )
201
- {
202
- await CopyRequestBodyAsync ( writer , TransformationMode . Uncompress , cancellationToken ) ;
203
- }
204
- else
205
- {
206
- await CopyResponseBodyAsync ( writer , TransformationMode . Uncompress , cancellationToken ) ;
207
- }
196
+ using var bodyStream = new MemoryStream ( ) ;
197
+ using var http = new HttpStream ( bodyStream , BufferPool ) ;
208
198
209
- return bodyStream . ToArray ( ) ;
199
+ if ( isRequest )
200
+ {
201
+ await CopyRequestBodyAsync ( http , TransformationMode . Uncompress , cancellationToken ) ;
202
+ }
203
+ else
204
+ {
205
+ await CopyResponseBodyAsync ( http , TransformationMode . Uncompress , cancellationToken ) ;
210
206
}
207
+
208
+ return bodyStream . ToArray ( ) ;
211
209
}
212
210
213
211
/// <summary>
@@ -225,18 +223,16 @@ internal async Task SyphonOutBodyAsync(bool isRequest, CancellationToken cancell
225
223
return ;
226
224
}
227
225
228
- using ( var bodyStream = new MemoryStream ( ) )
229
- {
230
- var writer = new HttpWriter ( bodyStream , BufferPool ) ;
231
- await copyBodyAsync ( isRequest , true , writer , TransformationMode . None , null , cancellationToken ) ;
232
- }
226
+ using var bodyStream = new MemoryStream ( ) ;
227
+ using var http = new HttpStream ( bodyStream , BufferPool ) ;
228
+ await copyBodyAsync ( isRequest , true , http , TransformationMode . None , null , cancellationToken ) ;
233
229
}
234
230
235
231
/// <summary>
236
232
/// This is called when the request is PUT/POST/PATCH to read the body
237
233
/// </summary>
238
234
/// <returns></returns>
239
- internal async Task CopyRequestBodyAsync ( HttpWriter writer , TransformationMode transformation , CancellationToken cancellationToken )
235
+ internal async Task CopyRequestBodyAsync ( IHttpStreamWriter writer , TransformationMode transformation , CancellationToken cancellationToken )
240
236
{
241
237
var request = HttpClient . Request ;
242
238
@@ -245,7 +241,7 @@ internal async Task CopyRequestBodyAsync(HttpWriter writer, TransformationMode t
245
241
// send the request body bytes to server
246
242
if ( contentLength > 0 && hasMulipartEventSubscribers && request . IsMultipartFormData )
247
243
{
248
- var reader = getStreamReader ( true ) ;
244
+ var reader = getStream ( true ) ;
249
245
var boundary = HttpHelper . GetBoundaryFromContentType ( request . ContentType ) ;
250
246
251
247
using ( var copyStream = new CopyStream ( reader , writer , BufferPool ) )
@@ -275,14 +271,14 @@ internal async Task CopyRequestBodyAsync(HttpWriter writer, TransformationMode t
275
271
}
276
272
}
277
273
278
- internal async Task CopyResponseBodyAsync ( HttpWriter writer , TransformationMode transformation , CancellationToken cancellationToken )
274
+ internal async Task CopyResponseBodyAsync ( IHttpStreamWriter writer , TransformationMode transformation , CancellationToken cancellationToken )
279
275
{
280
276
await copyBodyAsync ( false , false , writer , transformation , OnDataReceived , cancellationToken ) ;
281
277
}
282
278
283
- private async Task copyBodyAsync ( bool isRequest , bool useOriginalHeaderValues , HttpWriter writer , TransformationMode transformation , Action < byte [ ] , int , int > ? onCopy , CancellationToken cancellationToken )
279
+ private async Task copyBodyAsync ( bool isRequest , bool useOriginalHeaderValues , IHttpStreamWriter writer , TransformationMode transformation , Action < byte [ ] , int , int > ? onCopy , CancellationToken cancellationToken )
284
280
{
285
- var stream = getStreamReader ( isRequest ) ;
281
+ var stream = getStream ( isRequest ) ;
286
282
287
283
var requestResponse = isRequest ? ( RequestResponseBase ) HttpClient . Request : HttpClient . Response ;
288
284
@@ -309,10 +305,8 @@ private async Task copyBodyAsync(bool isRequest, bool useOriginalHeaderValues, H
309
305
310
306
try
311
307
{
312
- using ( var bufStream = new CustomBufferedStream ( s , BufferPool , true ) )
313
- {
314
- await writer . CopyBodyAsync ( bufStream , false , - 1 , onCopy , cancellationToken ) ;
315
- }
308
+ var http = new HttpStream ( s , BufferPool , true ) ;
309
+ await writer . CopyBodyAsync ( http , false , - 1 , onCopy , cancellationToken ) ;
316
310
}
317
311
finally
318
312
{
@@ -595,7 +589,7 @@ public void Redirect(string url, bool closeServerConnection = false)
595
589
var response = new RedirectResponse ( ) ;
596
590
response . HttpVersion = HttpClient . Request . HttpVersion ;
597
591
response . Headers . AddHeader ( KnownHeaders . Location , url ) ;
598
- response . Body = emptyData ;
592
+ response . Body = Array . Empty < byte > ( ) ;
599
593
600
594
Respond ( response , closeServerConnection ) ;
601
595
}
0 commit comments