Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit bb2d9b0

Browse files
committed
request body should be compressed earlier for expect 100-continue (to update the content length in header)
1 parent dff59f0 commit bb2d9b0

File tree

3 files changed

+61
-71
lines changed

3 files changed

+61
-71
lines changed

Titanium.Web.Proxy/Helpers/HttpWriter.cs

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,9 @@ public Task WriteLineAsync(string value)
9494
/// <returns></returns>
9595
public async Task WriteHeadersAsync(HeaderCollection headers, bool flush = true)
9696
{
97-
if (headers != null)
97+
foreach (var header in headers)
9898
{
99-
foreach (var header in headers)
100-
{
101-
await header.WriteToStreamAsync(this);
102-
}
99+
await header.WriteToStreamAsync(this);
103100
}
104101

105102
await WriteLineAsync();
@@ -274,52 +271,12 @@ private async Task CopyBytesFromStream(CustomBinaryReader reader, long count, Ac
274271
/// <returns></returns>
275272
protected async Task WriteAsync(RequestResponseBase requestResponse, bool flush = true)
276273
{
277-
if (requestResponse.HasBody)
278-
{
279-
bool isChunked = requestResponse.IsChunked;
280-
string contentEncoding = requestResponse.ContentEncoding;
281-
282-
var body = requestResponse.Body;
283-
if (contentEncoding != null && body != null)
284-
{
285-
body = GetCompressedBody(contentEncoding, body);
286-
287-
if (isChunked == false)
288-
{
289-
requestResponse.ContentLength = body.Length;
290-
}
291-
else
292-
{
293-
requestResponse.ContentLength = -1;
294-
}
295-
}
296-
297-
await WriteHeadersAsync(requestResponse.Headers, flush);
298-
await WriteBodyAsync(body, isChunked);
299-
}
300-
else
301-
{
302-
await WriteHeadersAsync(requestResponse.Headers, flush);
303-
}
304-
}
274+
var body = requestResponse.CompressBodyAndUpdateContentLength();
275+
await WriteHeadersAsync(requestResponse.Headers, flush);
305276

306-
/// <summary>
307-
/// get the compressed body from given bytes
308-
/// </summary>
309-
/// <param name="encodingType"></param>
310-
/// <param name="body"></param>
311-
/// <returns></returns>
312-
internal byte[] GetCompressedBody(string encodingType, byte[] body)
313-
{
314-
var compressor = CompressionFactory.GetCompression(encodingType);
315-
using (var ms = new MemoryStream())
277+
if (body != null)
316278
{
317-
using (var zip = compressor.GetStream(ms))
318-
{
319-
zip.Write(body, 0, body.Length);
320-
}
321-
322-
return ms.ToArray();
279+
await WriteBodyAsync(body, requestResponse.IsChunked);
323280
}
324281
}
325282
}

Titanium.Web.Proxy/Http/RequestResponseBase.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.IO;
45
using System.Linq;
56
using System.Text;
6-
using System.Threading.Tasks;
7+
using Titanium.Web.Proxy.Compression;
78
using Titanium.Web.Proxy.Extensions;
89
using Titanium.Web.Proxy.Helpers;
910
using Titanium.Web.Proxy.Models;
@@ -150,6 +151,55 @@ internal set
150151

151152
internal abstract void EnsureBodyAvailable(bool throwWhenNotReadYet = true);
152153

154+
/// <summary>
155+
/// get the compressed body from given bytes
156+
/// </summary>
157+
/// <param name="encodingType"></param>
158+
/// <param name="body"></param>
159+
/// <returns></returns>
160+
internal byte[] GetCompressedBody(string encodingType, byte[] body)
161+
{
162+
var compressor = CompressionFactory.GetCompression(encodingType);
163+
using (var ms = new MemoryStream())
164+
{
165+
using (var zip = compressor.GetStream(ms))
166+
{
167+
zip.Write(body, 0, body.Length);
168+
}
169+
170+
return ms.ToArray();
171+
}
172+
}
173+
174+
internal byte[] CompressBodyAndUpdateContentLength()
175+
{
176+
bool isChunked = IsChunked;
177+
string contentEncoding = ContentEncoding;
178+
179+
if (HasBody)
180+
{
181+
var body = Body;
182+
if (contentEncoding != null && body != null)
183+
{
184+
body = GetCompressedBody(contentEncoding, body);
185+
186+
if (isChunked == false)
187+
{
188+
ContentLength = body.Length;
189+
}
190+
else
191+
{
192+
ContentLength = -1;
193+
}
194+
}
195+
196+
return body;
197+
}
198+
199+
ContentLength = 0;
200+
return null;
201+
}
202+
153203
/// <summary>
154204
/// Body as string
155205
/// Use the encoding specified to decode the byte[] data to string

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,8 @@ private async Task HandleHttpSessionRequest(TcpClient client, CustomBufferedStre
422422
if (request.UpgradeToWebSocket)
423423
{
424424
//prepare the prefix content
425-
var requestHeaders = request.Headers;
426425
await connection.StreamWriter.WriteLineAsync(httpCmd);
427-
await connection.StreamWriter.WriteHeadersAsync(requestHeaders);
426+
await connection.StreamWriter.WriteHeadersAsync(request.Headers);
428427
string httpStatus = await connection.StreamReader.ReadLineAsync();
429428

430429
Response.ParseResponseLine(httpStatus, out var responseVersion, out int responseStatusCode,
@@ -499,6 +498,8 @@ private async Task HandleHttpSessionRequestInternal(TcpConnection connection, Se
499498
var request = args.WebSession.Request;
500499
request.Locked = true;
501500

501+
var body = request.CompressBodyAndUpdateContentLength();
502+
502503
//if expect continue is enabled then send the headers first
503504
//and see if server would return 100 conitinue
504505
if (request.ExpectContinue)
@@ -538,25 +539,7 @@ private async Task HandleHttpSessionRequestInternal(TcpConnection connection, Se
538539
if (request.IsBodyRead)
539540
{
540541
var writer = args.WebSession.ServerConnection.StreamWriter;
541-
bool isChunked = request.IsChunked;
542-
string contentEncoding = request.ContentEncoding;
543-
544-
var body = request.Body;
545-
if (contentEncoding != null && body != null)
546-
{
547-
body = writer.GetCompressedBody(contentEncoding, body);
548-
549-
if (isChunked == false)
550-
{
551-
request.ContentLength = body.Length;
552-
}
553-
else
554-
{
555-
request.ContentLength = -1;
556-
}
557-
}
558-
559-
await writer.WriteBodyAsync(body, isChunked);
542+
await writer.WriteBodyAsync(body, request.IsChunked);
560543
}
561544
else
562545
{

0 commit comments

Comments
 (0)