Commit d1db0843 authored by Honfika's avatar Honfika

support chunked request

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