Commit 3d805670 authored by Honfika's avatar Honfika

support chunked request

parent 19af6cdb
......@@ -452,19 +452,20 @@ namespace Titanium.Web.Proxy.EventArguments
/// <param name="body"></param>
public async Task SetRequestBody(byte[] body)
{
if (WebSession.Request.RequestLocked)
var request = WebSession.Request;
if (request.RequestLocked)
{
throw new Exception("You cannot call this function after request is made to server.");
}
//syphon out the request body from client before setting the new body
if (!WebSession.Request.IsBodyRead)
if (!request.IsBodyRead)
{
await ReadRequestBodyAsync();
}
WebSession.Request.Body = body;
WebSession.Request.ContentLength = WebSession.Request.IsChunked ? -1 : body.Length;
request.Body = body;
request.UpdateContentLength();
}
/// <summary>
......@@ -526,23 +527,18 @@ namespace Titanium.Web.Proxy.EventArguments
throw new Exception("You cannot call this function before request is made to server.");
}
var response = WebSession.Response;
//syphon out the response body from server before setting the new body
if (WebSession.Response.Body == null)
if (response.Body == null)
{
await GetResponseBody();
}
WebSession.Response.Body = body;
response.Body = body;
//If there is a content length header update it
if (WebSession.Response.IsChunked == false)
{
WebSession.Response.ContentLength = body.Length;
}
else
{
WebSession.Response.ContentLength = -1;
}
response.UpdateContentLength();
}
/// <summary>
......
......@@ -368,6 +368,11 @@ namespace Titanium.Web.Proxy.Http
return true;
}
internal void UpdateContentLength()
{
ContentLength = IsChunked ? -1 : body.Length;
}
/// <summary>
/// Finish the session
/// </summary>
......
......@@ -281,6 +281,11 @@ namespace Titanium.Web.Proxy.Http
statusDescription = httpResult[2];
}
internal void UpdateContentLength()
{
ContentLength = IsChunked ? -1 : body.Length;
}
/// <summary>
/// Finish the session
/// </summary>
......
......@@ -530,17 +530,25 @@ namespace Titanium.Web.Proxy
//If request was modified by user
if (request.IsBodyRead)
{
var body = request.Body;
bool isChunked = request.IsChunked;
string contentEncoding = request.ContentEncoding;
if (request.ContentEncoding != null)
var body = request.Body;
if (contentEncoding != null && body != null)
{
body = GetCompressedResponseBody(request.ContentEncoding, body);
}
body = GetCompressedBody(contentEncoding, body);
//chunked send is not supported as of now
request.ContentLength = body.Length;
if (isChunked == false)
{
request.ContentLength = body.Length;
}
else
{
request.ContentLength = -1;
}
}
await args.WebSession.ServerConnection.StreamWriter.WriteAsync(body);
await args.WebSession.ServerConnection.StreamWriter.WriteBodyAsync(body, isChunked);
}
else
{
......
......@@ -83,9 +83,9 @@ namespace Titanium.Web.Proxy
string contentEncoding = response.ContentEncoding;
var body = response.Body;
if (contentEncoding != null)
if (contentEncoding != null && body != null)
{
body = GetCompressedResponseBody(contentEncoding, body);
body = GetCompressedBody(contentEncoding, body);
if (isChunked == false)
{
......@@ -118,19 +118,19 @@ namespace Titanium.Web.Proxy
}
/// <summary>
/// get the compressed response body from give response bytes
/// get the compressed body from given bytes
/// </summary>
/// <param name="encodingType"></param>
/// <param name="responseBody"></param>
/// <param name="body"></param>
/// <returns></returns>
private byte[] GetCompressedResponseBody(string encodingType, byte[] responseBody)
private byte[] GetCompressedBody(string encodingType, byte[] body)
{
var compressor = CompressionFactory.GetCompression(encodingType);
using (var ms = new MemoryStream())
{
using (var zip = compressor.GetStream(ms))
{
zip.Write(responseBody, 0, responseBody.Length);
zip.Write(body, 0, body.Length);
}
return ms.ToArray();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment