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

Commit a745dd3

Browse files
committed
#823 prep work with TODO comments
1 parent 285e023 commit a745dd3

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/Titanium.Web.Proxy/EventArguments/BeforeBodySendEventArgs.cs src/Titanium.Web.Proxy/EventArguments/BeforeBodyWriteEventArgs.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace Titanium.Web.Proxy.EventArguments
1010

1111
public class BeforeBodyWriteEventArgs : ProxyEventArgsBase
1212
{
13-
internal BeforeBodyWriteEventArgs(SessionEventArgs session, byte[] bodyBytes, bool isChunked, bool isFinalChunk) : base(session.Server, session.ClientConnection)
13+
internal BeforeBodyWriteEventArgs(SessionEventArgs session, byte[] bodyBytes, bool isChunked, bool isLastChunk) : base(session.Server, session.ClientConnection)
1414
{
1515
Session = session;
1616
BodyBytes = bodyBytes;
1717
IsChunked = isChunked;
18-
IsFinalChunk = isFinalChunk;
18+
IsLastChunk = isLastChunk;
1919
}
2020

2121

@@ -34,7 +34,7 @@ internal BeforeBodyWriteEventArgs(SessionEventArgs session, byte[] bodyBytes, bo
3434
/// Indicates if this is the last chunk from client or server stream, when request is chunked.
3535
/// Override this property to true if there are more bytes to write.
3636
/// </summary>
37-
public bool IsFinalChunk { get; set; }
37+
public bool IsLastChunk { get; set; }
3838

3939
/// <summary>
4040
/// The bytes about to be written. If IsChunked is true, this will be a chunk of the bytes to be written.

src/Titanium.Web.Proxy/Helpers/HttpStream.cs

+22
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,14 @@ public Task CopyBodyAsync(IHttpStreamWriter writer, bool isChunked, long content
10541054
bool isRequest,
10551055
SessionEventArgs args, CancellationToken cancellationToken)
10561056
{
1057+
1058+
#if DEBUG
1059+
if ((isRequest && args.HttpClient.Request.OriginalHasBody && !args.HttpClient.Request.IsBodyRead && server.ShouldCallBeforeRequestBodyWrite())
1060+
|| (!isRequest && args.HttpClient.Response.OriginalHasBody && !args.HttpClient.Response.IsBodyRead && server.ShouldCallBeforeResponseBodyWrite()))
1061+
{
1062+
return handleBodyWrite(writer, isChunked, contentLength, isRequest, args, cancellationToken);
1063+
}
1064+
#endif
10571065
// For chunked request we need to read data as they arrive, until we reach a chunk end symbol
10581066
if (isChunked)
10591067
{
@@ -1070,6 +1078,20 @@ public Task CopyBodyAsync(IHttpStreamWriter writer, bool isChunked, long content
10701078
return copyBytesToStream(writer, contentLength, isRequest, args, cancellationToken);
10711079
}
10721080

1081+
private Task handleBodyWrite(IHttpStreamWriter writer, bool isChunked, long contentLength,
1082+
bool isRequest, SessionEventArgs args, CancellationToken cancellationToken)
1083+
{
1084+
var originalContentLength = isRequest ? args.HttpClient.Request.OriginalContentLength : args.HttpClient.Response.OriginalContentLength;
1085+
var originalIsChunked = isRequest ? args.HttpClient.Request.OriginalIsChunked : args.HttpClient.Response.OriginalIsChunked;
1086+
1087+
//1. Read bytes from original stream (max length of bytes will be equal to bufferPool.BufferSize).
1088+
//2. Call BeforeBodyWrite event handler with BeforeBodyWriteEventArgs.BodyBytes set to the bytes read from original stream.
1089+
//3. Write BeforeBodyWriteEventArgs.BodyBytes to the target stream when BeforeBodyWriteEventArgs.BodyBytes is not null or empty.
1090+
//4. Exit when 'long contentLength' parameter number of bytes are written to target stream (when not chunked) or
1091+
//when BeforeBodyWriteEventArgs.IsLastChunk is true after callback (when chunked).
1092+
throw new NotImplementedException();
1093+
}
1094+
10731095
/// <summary>
10741096
/// Copies the given input bytes to output stream chunked
10751097
/// </summary>

src/Titanium.Web.Proxy/RequestHandler.cs

+18
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,23 @@ private async Task onBeforeRequest(SessionEventArgs args)
400400
await BeforeRequest.InvokeAsync(this, args, ExceptionFunc);
401401
}
402402
}
403+
404+
internal bool ShouldCallBeforeRequestBodyWrite()
405+
{
406+
if (OnRequestBodyWrite != null)
407+
{
408+
return true;
409+
}
410+
411+
return false;
412+
}
413+
414+
internal async Task OnBeforeRequestBodyWrite(BeforeBodyWriteEventArgs args)
415+
{
416+
if (OnRequestBodyWrite != null)
417+
{
418+
await OnRequestBodyWrite.InvokeAsync(this, args, ExceptionFunc);
419+
}
420+
}
403421
}
404422
}

src/Titanium.Web.Proxy/ResponseHandler.cs

+18
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,23 @@ private async Task onAfterResponse(SessionEventArgs args)
150150
await AfterResponse.InvokeAsync(this, args, ExceptionFunc);
151151
}
152152
}
153+
154+
internal bool ShouldCallBeforeResponseBodyWrite()
155+
{
156+
if (OnResponseBodyWrite != null)
157+
{
158+
return true;
159+
}
160+
161+
return false;
162+
}
163+
164+
internal async Task OnBeforeResponseBodyWrite(BeforeBodyWriteEventArgs args)
165+
{
166+
if (OnResponseBodyWrite != null)
167+
{
168+
await OnResponseBodyWrite.InvokeAsync(this, args, ExceptionFunc);
169+
}
170+
}
153171
}
154172
}

0 commit comments

Comments
 (0)