Commit 216b8c90 authored by justcoding121's avatar justcoding121 Committed by GitHub

Merge pull request #93 from KirovAir/Read_request_from_response

Only check for locked request if not read already.
parents afe22a41 1073c975
...@@ -62,8 +62,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -62,8 +62,7 @@ namespace Titanium.Web.Proxy.EventArguments
ProxyClient = new ProxyClient(); ProxyClient = new ProxyClient();
WebSession = new HttpWebClient(); WebSession = new HttpWebClient();
} }
/// <summary> /// <summary>
/// Read request body content as bytes[] for current session /// Read request body content as bytes[] for current session
/// </summary> /// </summary>
...@@ -98,7 +97,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -98,7 +97,7 @@ namespace Titanium.Web.Proxy.EventArguments
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream, WebSession.Request.ContentLength); await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream, WebSession.Request.ContentLength);
} }
else if(WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0) else if (WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0)
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, requestBodyStream, long.MaxValue); await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, requestBodyStream, long.MaxValue);
} }
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray()); WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray());
...@@ -108,7 +107,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -108,7 +107,7 @@ namespace Titanium.Web.Proxy.EventArguments
//So that next time we can deliver body from cache //So that next time we can deliver body from cache
WebSession.Request.RequestBodyRead = true; WebSession.Request.RequestBodyRead = true;
} }
} }
/// <summary> /// <summary>
...@@ -134,7 +133,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -134,7 +133,7 @@ namespace Titanium.Web.Proxy.EventArguments
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, WebSession.Response.ContentLength); await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, WebSession.Response.ContentLength);
} }
else if(WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0) else if (WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0)
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, long.MaxValue); await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, long.MaxValue);
} }
...@@ -152,10 +151,13 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -152,10 +151,13 @@ namespace Titanium.Web.Proxy.EventArguments
/// <returns></returns> /// <returns></returns>
public async Task<byte[]> GetRequestBody() public async Task<byte[]> GetRequestBody()
{ {
if (WebSession.Request.RequestLocked) if (!WebSession.Request.RequestBodyRead)
throw new Exception("You cannot call this function after request is made to server."); {
if (WebSession.Request.RequestLocked)
throw new Exception("You cannot call this function after request is made to server.");
await ReadRequestBody(); await ReadRequestBody();
}
return WebSession.Request.RequestBody; return WebSession.Request.RequestBody;
} }
/// <summary> /// <summary>
...@@ -164,12 +166,13 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -164,12 +166,13 @@ namespace Titanium.Web.Proxy.EventArguments
/// <returns></returns> /// <returns></returns>
public async Task<string> GetRequestBodyAsString() public async Task<string> GetRequestBodyAsString()
{ {
if (WebSession.Request.RequestLocked) if (!WebSession.Request.RequestBodyRead)
throw new Exception("You cannot call this function after request is made to server."); {
if (WebSession.Request.RequestLocked)
throw new Exception("You cannot call this function after request is made to server.");
await ReadRequestBody();
await ReadRequestBody();
}
//Use the encoding specified in request to decode the byte[] data to string //Use the encoding specified in request to decode the byte[] data to string
return WebSession.Request.RequestBodyString ?? (WebSession.Request.RequestBodyString = WebSession.Request.Encoding.GetString(WebSession.Request.RequestBody)); return WebSession.Request.RequestBodyString ?? (WebSession.Request.RequestBodyString = WebSession.Request.Encoding.GetString(WebSession.Request.RequestBody));
} }
...@@ -285,7 +288,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -285,7 +288,7 @@ namespace Titanium.Web.Proxy.EventArguments
var bodyBytes = WebSession.Response.Encoding.GetBytes(body); var bodyBytes = WebSession.Response.Encoding.GetBytes(body);
await SetResponseBody(bodyBytes); await SetResponseBody(bodyBytes);
} }
private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream) private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream)
{ {
...@@ -345,7 +348,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -345,7 +348,7 @@ namespace Titanium.Web.Proxy.EventArguments
WebSession.Request.CancelRequest = true; WebSession.Request.CancelRequest = true;
} }
/// a generic responder method /// a generic responder method
public async Task Respond(Response response) public async Task Respond(Response response)
{ {
......
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