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

Commit 5734f5f

Browse files
committed
do not allow to read body twice
1 parent cf98fdf commit 5734f5f

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ private async Task readRequestBodyAsync(CancellationToken cancellationToken)
8585
// If not already read (not cached yet)
8686
if (!request.IsBodyRead)
8787
{
88+
if (request.IsBodyReceived)
89+
{
90+
throw new Exception("Request body was already received.");
91+
}
92+
8893
if (request.HttpVersion == HttpHeader.Version20)
8994
{
9095
// do not send to the remote endpoint
@@ -103,6 +108,7 @@ private async Task readRequestBodyAsync(CancellationToken cancellationToken)
103108
// Now set the flag to true
104109
// So that next time we can deliver body from cache
105110
request.IsBodyRead = true;
111+
request.IsBodyReceived = true;
106112
}
107113
else
108114
{
@@ -115,6 +121,7 @@ private async Task readRequestBodyAsync(CancellationToken cancellationToken)
115121
// Now set the flag to true
116122
// So that next time we can deliver body from cache
117123
request.IsBodyRead = true;
124+
request.IsBodyReceived = true;
118125
}
119126
}
120127
}
@@ -160,6 +167,11 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
160167
// If not already read (not cached yet)
161168
if (!response.IsBodyRead)
162169
{
170+
if (response.IsBodyReceived)
171+
{
172+
throw new Exception("Response body was already received.");
173+
}
174+
163175
if (response.HttpVersion == HttpHeader.Version20)
164176
{
165177
// do not send to the remote endpoint
@@ -178,6 +190,7 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
178190
// Now set the flag to true
179191
// So that next time we can deliver body from cache
180192
response.IsBodyRead = true;
193+
response.IsBodyReceived = true;
181194
}
182195
else
183196
{
@@ -190,6 +203,7 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
190203
// Now set the flag to true
191204
// So that next time we can deliver body from cache
192205
response.IsBodyRead = true;
206+
response.IsBodyReceived = true;
193207
}
194208
}
195209
}
@@ -221,14 +235,15 @@ private async Task<byte[]> readBodyAsync(bool isRequest, CancellationToken cance
221235
internal async Task SyphonOutBodyAsync(bool isRequest, CancellationToken cancellationToken)
222236
{
223237
var requestResponse = isRequest ? (RequestResponseBase)HttpClient.Request : HttpClient.Response;
224-
if (requestResponse.IsBodyRead || !requestResponse.OriginalHasBody)
238+
if (requestResponse.IsBodyReceived || !requestResponse.OriginalHasBody)
225239
{
226240
return;
227241
}
228242

229243
var reader = isRequest ? (HttpStream)ClientStream : HttpClient.Connection.Stream;
230244

231245
await reader.CopyBodyAsync(requestResponse, true, NullWriter.Instance, TransformationMode.None, isRequest, this, cancellationToken);
246+
requestResponse.IsBodyReceived = true;
232247
}
233248

234249
/// <summary>
@@ -272,11 +287,15 @@ internal async Task CopyRequestBodyAsync(IHttpStreamWriter writer, Transformatio
272287
{
273288
await reader.CopyBodyAsync(request, false, writer, transformation, true, this, cancellationToken);
274289
}
290+
291+
request.IsBodyReceived = true;
275292
}
276293

277294
private async Task copyResponseBodyAsync(IHttpStreamWriter writer, TransformationMode transformation, CancellationToken cancellationToken)
278295
{
279-
await HttpClient.Connection.Stream.CopyBodyAsync(HttpClient.Response, false, writer, transformation, false, this, cancellationToken);
296+
var response = HttpClient.Response;
297+
await HttpClient.Connection.Stream.CopyBodyAsync(response, false, writer, transformation, false, this, cancellationToken);
298+
response.IsBodyReceived = true;
280299
}
281300

282301
/// <summary>

src/Titanium.Web.Proxy/Http/RequestResponseBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ internal set
211211

212212
internal bool BodyAvailable => BodyInternal != null;
213213

214+
private bool isBodyReceived;
215+
216+
internal bool IsBodyReceived
217+
{
218+
get => isBodyReceived;
219+
set
220+
{
221+
if (isBodyReceived)
222+
{
223+
;
224+
}
225+
isBodyReceived = value;
226+
}
227+
}
228+
214229
internal bool IsBodySent { get; set; }
215230

216231
internal abstract void EnsureBodyAvailable(bool throwWhenNotReadYet = true);

src/Titanium.Web.Proxy/Http2/Http2Helper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
394394
}
395395

396396
rr.IsBodyRead = true;
397+
rr.IsBodyReceived = true;
397398

398399
var tcs = rr.ReadHttp2BodyTaskCompletionSource;
399400
rr.ReadHttp2BodyTaskCompletionSource = null;

src/Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ await handleHttpSessionRequest(args, null, args.ClientConnection.NegotiatedAppli
120120
await serverStream.CopyBodyAsync(response, false, clientStream, TransformationMode.None,
121121
false, args, cancellationToken);
122122
}
123+
124+
response.IsBodyReceived = true;
123125
}
124126

125127
args.TimeLine["Response Sent"] = DateTime.UtcNow;

0 commit comments

Comments
 (0)