Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 216b8c9

Browse files
Merge pull request #93 from KirovAir/Read_request_from_response
Only check for locked request if not read already.
2 parents afe22a4 + 1073c97 commit 216b8c9

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ internal SessionEventArgs(int bufferSize, Func<SessionEventArgs, Task> httpRespo
6262
ProxyClient = new ProxyClient();
6363
WebSession = new HttpWebClient();
6464
}
65-
66-
65+
6766
/// <summary>
6867
/// Read request body content as bytes[] for current session
6968
/// </summary>
@@ -98,7 +97,7 @@ private async Task ReadRequestBody()
9897
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream, WebSession.Request.ContentLength);
9998

10099
}
101-
else if(WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0)
100+
else if (WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0)
102101
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, requestBodyStream, long.MaxValue);
103102
}
104103
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray());
@@ -108,7 +107,7 @@ private async Task ReadRequestBody()
108107
//So that next time we can deliver body from cache
109108
WebSession.Request.RequestBodyRead = true;
110109
}
111-
110+
112111
}
113112

114113
/// <summary>
@@ -134,7 +133,7 @@ private async Task ReadResponseBody()
134133
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, WebSession.Response.ContentLength);
135134

136135
}
137-
else if(WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0)
136+
else if (WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0)
138137
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, long.MaxValue);
139138
}
140139

@@ -152,10 +151,13 @@ private async Task ReadResponseBody()
152151
/// <returns></returns>
153152
public async Task<byte[]> GetRequestBody()
154153
{
155-
if (WebSession.Request.RequestLocked)
156-
throw new Exception("You cannot call this function after request is made to server.");
154+
if (!WebSession.Request.RequestBodyRead)
155+
{
156+
if (WebSession.Request.RequestLocked)
157+
throw new Exception("You cannot call this function after request is made to server.");
157158

158-
await ReadRequestBody();
159+
await ReadRequestBody();
160+
}
159161
return WebSession.Request.RequestBody;
160162
}
161163
/// <summary>
@@ -164,12 +166,13 @@ public async Task<byte[]> GetRequestBody()
164166
/// <returns></returns>
165167
public async Task<string> GetRequestBodyAsString()
166168
{
167-
if (WebSession.Request.RequestLocked)
168-
throw new Exception("You cannot call this function after request is made to server.");
169-
170-
171-
await ReadRequestBody();
169+
if (!WebSession.Request.RequestBodyRead)
170+
{
171+
if (WebSession.Request.RequestLocked)
172+
throw new Exception("You cannot call this function after request is made to server.");
172173

174+
await ReadRequestBody();
175+
}
173176
//Use the encoding specified in request to decode the byte[] data to string
174177
return WebSession.Request.RequestBodyString ?? (WebSession.Request.RequestBodyString = WebSession.Request.Encoding.GetString(WebSession.Request.RequestBody));
175178
}
@@ -285,7 +288,7 @@ public async Task SetResponseBodyString(string body)
285288
var bodyBytes = WebSession.Response.Encoding.GetBytes(body);
286289

287290
await SetResponseBody(bodyBytes);
288-
}
291+
}
289292

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

346349
WebSession.Request.CancelRequest = true;
347350
}
348-
351+
349352
/// a generic responder method
350353
public async Task Respond(Response response)
351354
{

0 commit comments

Comments
 (0)