Skip to content

Commit 6738f46

Browse files
author
ladeak
committed
CanHaveRequestBody feature for post request with json content.
1 parent d609fee commit 6738f46

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

src/CHttpServer/CHttpServer/Http3/Http3Stream.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public Http3Stream(FeatureCollection features)
6060
(typeof(IHttpResponseFeature), this),
6161
(typeof(IHttpResponseBodyFeature), this),
6262
(typeof(IHttpResponseTrailersFeature), this),
63-
(typeof(IRequestBodyPipeFeature), this));
63+
(typeof(IRequestBodyPipeFeature), this),
64+
(typeof(IHttpRequestBodyDetectionFeature), this));
6465

6566
//_features.Add<IHttpRequestBodyDetectionFeature>(this);
6667
//_features.Add<IHttpRequestLifetimeFeature>(this);
@@ -137,6 +138,7 @@ public async Task ProcessStreamAsync<TContext>(IHttpApplication<TContext> applic
137138
}
138139
_dataReader.AdvanceTo(readResult.Buffer.GetPosition(totalBufferConsumed), readResult.Buffer.End);
139140
}
141+
await _requestDataToAppPipe.Writer.CompleteAsync();
140142
if (applicationProcessing != null)
141143
await applicationProcessing;
142144
}
@@ -238,6 +240,7 @@ private StreamReadingStatus NextRequestReadingState(Task? applicationProcessing,
238240
case 0x0: // DATA
239241
if (applicationProcessing == null)
240242
throw new Http3ConnectionException(ErrorCodes.H3FrameUnexpected);
243+
CanHaveBody = true;
241244
streamReadingState = StreamReadingStatus.ReadingPayloadData;
242245
break;
243246
case 0x1: // HEADERS
@@ -257,7 +260,6 @@ private StreamReadingStatus NextRequestReadingState(Task? applicationProcessing,
257260

258261
private async Task CloseStreamAsync()
259262
{
260-
await _requestDataToAppPipe.Writer.CompleteAsync();
261263
await _requestDataToAppPipe.Reader.CompleteAsync();
262264
await _dataReader.CompleteAsync();
263265
await _responseHeaderWriter.CompleteAsync();
@@ -380,3 +382,8 @@ internal partial class Http3Stream : IHttpResponseTrailersFeature
380382
private readonly Http3ResponseHeaderCollection _responseTrailers;
381383
public IHeaderDictionary Trailers { get => _responseTrailers; set => throw new PlatformNotSupportedException(); }
382384
}
385+
386+
internal partial class Http3Stream : IHttpRequestBodyDetectionFeature
387+
{
388+
public bool CanHaveBody { get; set; }
389+
}

tests/CHttpServer.Tests/Http3/Http3FramingStreamWriterTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Buffers;
2-
using System.Formats.Asn1;
32
using System.Net.Quic;
43
using System.Runtime.InteropServices;
54
using CHttpServer.Http3;

tests/CHttpServer.Tests/Http3/Http3IntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public async Task HttpContext_WritesResponse()
135135
Assert.Equal("some content", content);
136136
}
137137

138-
[Fact(Skip = "WIP")]
138+
[Fact]
139139
public async Task TestPost()
140140
{
141141
var client = CreateClient();

tests/CHttpServer.Tests/Http3/Http3StreamTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,28 @@ public async Task SingleWrite_HeaderFrame_ReservedFrame_DataFrame()
154154
var sut = new Http3Stream([]);
155155

156156
var clientStream = await quicConnection.ClientConnection.OpenOutboundStreamAsync(QuicStreamType.Bidirectional, TestContext.Current.CancellationToken);
157-
byte[] data = [.. await GetHeadersFrame(), .. GetData(30), .. GetReservedFrame(750), .. GetData(30)];
157+
byte[] data = [.. await GetHeadersFrame(), .. GetData(30), .. GetReservedFrame(750), ..GetData(30)];
158158
await clientStream.WriteAsync(data, TestContext.Current.CancellationToken);
159159
await clientStream.FlushAsync(TestContext.Current.CancellationToken);
160160

161161
TaskCompletionSource tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
162162
sut.Initialize(null, await serverStreamTask);
163-
var testApp = new TestBase.TestApplication(ctx =>
163+
var testApp = new TestBase.TestApplication(async ctx =>
164164
{
165165
Assert.Equal("/", ctx.Request.Path);
166166
Assert.Equal("https", ctx.Request.Scheme);
167167
Assert.Equal("localhost", ctx.Request.Host.ToString());
168168
Assert.Equal(HttpMethod.Get.ToString(), ctx.Request.Method);
169-
// TODO: assert data
169+
var content = new MemoryStream();
170+
await ctx.Request.BodyReader.CopyToAsync(content);
171+
Assert.Equal(60, content.Length);
170172
tcs.SetResult();
171-
return Task.CompletedTask;
172173
});
173174
var processing = sut.ProcessStreamAsync(testApp, TestContext.Current.CancellationToken);
174175

175-
await tcs.Task.WaitAsync(DefaultTimeout, TestContext.Current.CancellationToken);
176176
clientStream.Close();
177+
await tcs.Task.WaitAsync(DefaultTimeout, TestContext.Current.CancellationToken);
178+
177179
await processing;
178180
await quicConnection.DisposeAsync();
179181
}
@@ -186,7 +188,7 @@ public async Task SingleWrite_InOrderFrames_Throw()
186188
var sut = new Http3Stream([]);
187189

188190
var clientStream = await quicConnection.ClientConnection.OpenOutboundStreamAsync(QuicStreamType.Bidirectional, TestContext.Current.CancellationToken);
189-
byte[] data = [.. GetData(30), .. await GetHeadersFrame()]; //DATA before HEADERS is in-order
191+
byte[] data = [.. GetData(30), .. await GetHeadersFrame()]; // DATA before HEADERS is in-order
190192
await clientStream.WriteAsync(data, TestContext.Current.CancellationToken);
191193
await clientStream.FlushAsync(TestContext.Current.CancellationToken);
192194

0 commit comments

Comments
 (0)