Skip to content

IHttpRequestBodyDetectionFeature for HTTP/3 GET requests #62275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public interface IHttpRequestBodyDetectionFeature
/// <item><description>
/// It's an HTTP/2 request that did not set the END_STREAM flag on the initial headers frame.
/// </description></item>
/// <item><description>
/// It's an HTTP/3 request that did not set the END_STREAM flag on the initial headers frame.
/// </description></item>
/// </list>
/// The final request body length may still be zero for the chunked or HTTP/2 scenarios.
/// <para>
Expand All @@ -35,6 +38,9 @@ public interface IHttpRequestBodyDetectionFeature
/// <item><description>
/// It's an HTTP/2 request that set END_STREAM on the initial headers frame.
/// </description></item>
/// <item><description>
/// It's an HTTP/3 request that set END_STREAM on the initial headers frame.
/// </description></item>
/// </list>
/// </para>
/// When false, the request body should never return data.
Expand Down
21 changes: 19 additions & 2 deletions src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internal abstract partial class Http3Stream : HttpProtocol, IHttp3Stream, IHttpS
private bool _isMethodConnect;
private bool _isWebTransportSessionAccepted;
private Http3MessageBody? _messageBody;
private bool _requestBodyStarted;

private readonly ManualResetValueTaskSource<object?> _appCompletedTaskSource = new();
private readonly Lock _completionLock = new();
Expand All @@ -65,6 +66,17 @@ internal abstract partial class Http3Stream : HttpProtocol, IHttp3Stream, IHttpS
private bool IsAbortedRead => (_completionState & StreamCompletionFlags.AbortedRead) == StreamCompletionFlags.AbortedRead;
public bool IsCompleted => (_completionState & StreamCompletionFlags.Completed) == StreamCompletionFlags.Completed;

public bool ReceivedEmptyRequestBody
{
get
{
lock (_completionLock)
{
return EndStreamReceived && !_requestBodyStarted;
}
}
}

public Pipe RequestBodyPipe { get; private set; } = default!;
public long? InputRemaining { get; internal set; }
public QPackDecoder QPackDecoder { get; private set; } = default!;
Expand Down Expand Up @@ -559,7 +571,7 @@ private void CompleteStream(bool errored)

TryClose();
}

RequestBodyPipe.Reader.Complete();
_http3Output.Complete();

// Stream will be pooled after app completed.
Expand Down Expand Up @@ -928,7 +940,7 @@ private Task ProcessDataFrameAsync(in ReadOnlySequence<byte> payload)
{
return Task.CompletedTask;
}

_requestBodyStarted = true;
foreach (var segment in payload)
{
RequestBodyPipe.Writer.Write(segment.Span);
Expand Down Expand Up @@ -966,6 +978,11 @@ protected override string CreateRequestId()

protected override MessageBody CreateMessageBody()
{
if (ReceivedEmptyRequestBody)
{
return MessageBody.ZeroContentLengthClose;
}

if (_messageBody != null)
{
_messageBody.Reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3291,6 +3291,32 @@ public async Task Control_FrameParsingSingleByteAtATimeWorks()
await outboundcontrolStream.ReceiveEndAsync();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task CanHaveBody_ReturnsFalseWithoutRequestBody(bool endstream)
{
var headers = new[]
{
new KeyValuePair<string, string>(InternalHeaderNames.Method, "GET"),
new KeyValuePair<string, string>(InternalHeaderNames.Path, "/"),
new KeyValuePair<string, string>(InternalHeaderNames.Scheme, "http"),
new KeyValuePair<string, string>(InternalHeaderNames.Authority, "localhost:80"),
};

var requestStream = await Http3Api.InitializeConnectionAndStreamsAsync(context =>
{
Assert.Equal(!endstream, context.Features.Get<IHttpRequestBodyDetectionFeature>().CanHaveBody);
context.Response.StatusCode = 200;
return Task.CompletedTask;
}, headers, endStream: endstream);

var responseHeaders = await requestStream.ExpectHeadersAsync();
Assert.Equal("200", responseHeaders[InternalHeaderNames.Status]);

await requestStream.ExpectReceiveEndOfStream();
}

private async Task WriteOneByteAtATime(PipeReader reader, PipeWriter writer)
{
var res = await reader.ReadAsync();
Expand Down
Loading