From 1073c975703f998f66b89e1764732f15cded8629 Mon Sep 17 00:00:00 2001 From: "j.sander" Date: Wed, 6 Jul 2016 09:14:26 +0200 Subject: [PATCH] Only check for locked request if not read already. --- .../EventArguments/SessionEventArgs.cs | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs b/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs index 977404367..f54881369 100644 --- a/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs +++ b/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs @@ -62,8 +62,7 @@ internal SessionEventArgs(int bufferSize, Func httpRespo ProxyClient = new ProxyClient(); WebSession = new HttpWebClient(); } - - + /// /// Read request body content as bytes[] for current session /// @@ -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()); @@ -108,7 +107,7 @@ private async Task ReadRequestBody() //So that next time we can deliver body from cache WebSession.Request.RequestBodyRead = true; } - + } /// @@ -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); } @@ -152,10 +151,13 @@ private async Task ReadResponseBody() /// public async Task 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; } /// @@ -164,12 +166,13 @@ public async Task GetRequestBody() /// public async Task 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)); } @@ -285,7 +288,7 @@ public async Task SetResponseBodyString(string body) var bodyBytes = WebSession.Response.Encoding.GetBytes(body); await SetResponseBody(bodyBytes); - } + } private async Task GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream) { @@ -345,7 +348,7 @@ public async Task Redirect(string url) WebSession.Request.CancelRequest = true; } - + /// a generic responder method public async Task Respond(Response response) {