-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Added support for binding the raw request body #39388
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
daf6a86
Added support for binding the raw request body
davidfowl 154abd1
Fix some issues add more features
davidfowl d08f132
Random clean up
davidfowl 3e2fff8
Remove support for byte[] and ReadOnlyMemory<byte>
davidfowl 547ce9b
Fix method names and remove extra fluff
davidfowl c7d1168
Removed extra space
davidfowl 8a17a3b
Removed JSON from the error message
davidfowl 34244b1
Formatting
davidfowl 7c9b45e
Remove the boxing for raw body binding
davidfowl d2085fd
Added more tests
davidfowl f45db2b
Small nit
davidfowl a0711dc
Added more tests
davidfowl caf7d66
Remove tuple and use ValueTask instead of Task
davidfowl df767a7
Set the body to a null stream after consuming it
davidfowl 191a9cf
PR feedback
davidfowl a6637e0
Make sure we can read from the original pipereader when we unwind fro…
davidfowl 39152b3
Support scenarios where nulling out the stream doesn't change the pip…
davidfowl 0210b28
Remove support for the raw body as a ROS<byte>
davidfowl f5c305a
Small reverts
davidfowl 5b03d54
Fixed more things
davidfowl c1f4063
Moar
davidfowl 1cd9735
Reverted more stuff
davidfowl 27c3f48
One more I hope
davidfowl 9a7f593
Fixed the test
davidfowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
#nullable enable | ||
|
||
using System.Buffers; | ||
using System.Globalization; | ||
using System.IO.Pipelines; | ||
using System.Linq.Expressions; | ||
|
@@ -1376,6 +1377,74 @@ public async Task RequestDelegatePopulatesFromBodyParameter(Delegate action) | |
Assert.Equal(originalTodo.Name, ((ITodo)deserializedRequestBody!).Name); | ||
} | ||
|
||
public static object[][] RawFromBodyActions | ||
{ | ||
get | ||
{ | ||
void TestByteArray(HttpContext httpContext, byte[] body) | ||
{ | ||
httpContext.Items.Add("body", body); | ||
} | ||
|
||
void TestReadOnlyMemory(HttpContext httpContext, ReadOnlyMemory<byte> body) | ||
{ | ||
httpContext.Items.Add("body", body.ToArray()); | ||
} | ||
|
||
void TestReadOnlySequence(HttpContext httpContext, ReadOnlySequence<byte> body) | ||
{ | ||
httpContext.Items.Add("body", body.ToArray()); | ||
} | ||
|
||
void TestStream(HttpContext httpContext, Stream stream) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat. |
||
{ | ||
var ms = new MemoryStream(); | ||
stream.CopyTo(ms); | ||
httpContext.Items.Add("body", ms.ToArray()); | ||
} | ||
|
||
return new[] | ||
{ | ||
new[] { (Action<HttpContext, byte[]>)TestByteArray }, | ||
new[] { (Action<HttpContext, ReadOnlyMemory<byte>>)TestReadOnlyMemory }, | ||
new object[] { (Action<HttpContext, ReadOnlySequence<byte>>)TestReadOnlySequence }, | ||
new object[] { (Action<HttpContext, Stream>)TestStream }, | ||
}; | ||
} | ||
} | ||
|
||
|
||
[Theory] | ||
[MemberData(nameof(RawFromBodyActions))] | ||
public async Task RequestDelegatePopulatesFromRawBodyParameter(Delegate action) | ||
{ | ||
var httpContext = CreateHttpContext(); | ||
|
||
var requestBodyBytes = JsonSerializer.SerializeToUtf8Bytes(new | ||
{ | ||
Name = "Write more tests!" | ||
}); | ||
|
||
var stream = new MemoryStream(requestBodyBytes); | ||
httpContext.Request.Body = stream; | ||
|
||
httpContext.Request.Headers["Content-Length"] = stream.Length.ToString(CultureInfo.InvariantCulture); | ||
httpContext.Features.Set<IHttpRequestBodyDetectionFeature>(new RequestBodyDetectionFeature(true)); | ||
|
||
var mock = new Mock<IServiceProvider>(); | ||
httpContext.RequestServices = mock.Object; | ||
|
||
var factoryResult = RequestDelegateFactory.Create(action); | ||
|
||
var requestDelegate = factoryResult.RequestDelegate; | ||
|
||
await requestDelegate(httpContext); | ||
|
||
var rawRequestBody = httpContext.Items["body"]; | ||
Assert.NotNull(rawRequestBody); | ||
Assert.Equal(requestBodyBytes, (byte[])rawRequestBody!); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ExplicitFromBodyActions))] | ||
public async Task RequestDelegateRejectsEmptyBodyGivenExplicitFromBodyParameter(Delegate action) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is
PipeReader.AdvanceTo
called to say the body is consumed? Before or after the request delegate is executed?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about this, it's actually broken for non Kestrel servers since nobody completes that PipeReader currently. I'll need to add more logic there. Good catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm actually no, it seems like we complete the pipe reader in both cases
aspnetcore/src/Http/Http/src/Features/RequestBodyPipeFeature.cs
Lines 35 to 48 in 694507a