Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ internal SessionEventArgs(int bufferSize, Func<SessionEventArgs, Task> httpRespo
ProxyClient = new ProxyClient();
WebSession = new HttpWebClient();
}



/// <summary>
/// Read request body content as bytes[] for current session
/// </summary>
Expand Down Expand Up @@ -98,7 +97,7 @@ private async Task ReadRequestBody()
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);
}
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray());
Expand All @@ -108,7 +107,7 @@ private async Task ReadRequestBody()
//So that next time we can deliver body from cache
WebSession.Request.RequestBodyRead = true;
}

}

/// <summary>
Expand All @@ -134,7 +133,7 @@ private async Task ReadResponseBody()
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);
}

Expand All @@ -152,10 +151,13 @@ private async Task ReadResponseBody()
/// <returns></returns>
public async Task<byte[]> GetRequestBody()
{
if (WebSession.Request.RequestLocked)
throw new Exception("You cannot call this function after request is made to server.");
if (!WebSession.Request.RequestBodyRead)
{
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;
}
/// <summary>
Expand All @@ -164,12 +166,13 @@ public async Task<byte[]> GetRequestBody()
/// <returns></returns>
public async Task<string> GetRequestBodyAsString()
{
if (WebSession.Request.RequestLocked)
throw new Exception("You cannot call this function after request is made to server.");


await ReadRequestBody();
if (!WebSession.Request.RequestBodyRead)
{
if (WebSession.Request.RequestLocked)
throw new Exception("You cannot call this function after request is made to server.");

await ReadRequestBody();
}
//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));
}
Expand Down Expand Up @@ -285,7 +288,7 @@ public async Task SetResponseBodyString(string body)
var bodyBytes = WebSession.Response.Encoding.GetBytes(body);

await SetResponseBody(bodyBytes);
}
}

private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream)
{
Expand Down Expand Up @@ -345,7 +348,7 @@ public async Task Redirect(string url)

WebSession.Request.CancelRequest = true;
}

/// a generic responder method
public async Task Respond(Response response)
{
Expand Down