Skip to content

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 24 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
61 changes: 52 additions & 9 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -61,6 +62,7 @@ public static partial class RequestDelegateFactory
private static readonly MemberExpression QueryExpr = Expression.Property(HttpRequestExpr, typeof(HttpRequest).GetProperty(nameof(HttpRequest.Query))!);
private static readonly MemberExpression HeadersExpr = Expression.Property(HttpRequestExpr, typeof(HttpRequest).GetProperty(nameof(HttpRequest.Headers))!);
private static readonly MemberExpression FormExpr = Expression.Property(HttpRequestExpr, typeof(HttpRequest).GetProperty(nameof(HttpRequest.Form))!);
private static readonly MemberExpression RequestStreamExpr = Expression.Property(HttpRequestExpr, typeof(HttpRequest).GetProperty(nameof(HttpRequest.Body))!);
private static readonly MemberExpression FormFilesExpr = Expression.Property(FormExpr, typeof(IFormCollection).GetProperty(nameof(IFormCollection.Files))!);
private static readonly MemberExpression StatusCodeExpr = Expression.Property(HttpResponseExpr, typeof(HttpResponse).GetProperty(nameof(HttpResponse.StatusCode))!);
private static readonly MemberExpression CompletedTaskExpr = Expression.Property(null, (PropertyInfo)GetMemberInfo<Func<Task>>(() => Task.CompletedTask));
Expand Down Expand Up @@ -199,7 +201,7 @@ private static Expression[] CreateArguments(ParameterInfo[]? parameters, Factory
var errorMessage = BuildErrorMessageForInferredBodyParameter(factoryContext);
throw new InvalidOperationException(errorMessage);
}
if (factoryContext.JsonRequestBodyParameter is not null &&
if (factoryContext.RequestBodyParameter is not null &&
factoryContext.FirstFormRequestBodyParameter is not null)
{
var errorMessage = BuildErrorMessageForFormAndJsonBodyParameters(factoryContext);
Expand Down Expand Up @@ -302,6 +304,10 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
{
return BindParameterFromFormFile(parameter, parameter.Name, factoryContext, RequestDelegateFactoryConstants.FormFileParameter);
}
else if (parameter.ParameterType == typeof(Stream))
{
return RequestStreamExpr;
}
else if (ParameterBindingMethodCache.HasBindAsyncMethod(parameter))
{
return BindParameterFromBindAsync(parameter, factoryContext);
Expand Down Expand Up @@ -549,7 +555,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,

private static Func<object?, HttpContext, Task> HandleRequestBodyAndCompileRequestDelegate(Expression responseWritingMethodCall, FactoryContext factoryContext)
{
if (factoryContext.JsonRequestBodyParameter is null && !factoryContext.ReadForm)
if (factoryContext.RequestBodyParameter is null && !factoryContext.ReadForm)
{
if (factoryContext.ParameterBinders.Count > 0)
{
Expand Down Expand Up @@ -590,11 +596,14 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,

private static Func<object?, HttpContext, Task> HandleRequestBodyAndCompileRequestDelegateForJson(Expression responseWritingMethodCall, FactoryContext factoryContext)
{
Debug.Assert(factoryContext.JsonRequestBodyParameter is not null, "factoryContext.JsonRequestBodyParameter is null for a JSON body.");
Debug.Assert(factoryContext.RequestBodyParameter is not null, "factoryContext.JsonRequestBodyParameter is null for a JSON body.");

var bodyType = factoryContext.JsonRequestBodyParameter.ParameterType;
var parameterTypeName = TypeNameHelper.GetTypeDisplayName(factoryContext.JsonRequestBodyParameter.ParameterType, fullName: false);
var parameterName = factoryContext.JsonRequestBodyParameter.Name;
var bodyType = factoryContext.RequestBodyParameter.ParameterType;
var isRawBodyType = bodyType == typeof(byte[]) ||
bodyType == typeof(ReadOnlyMemory<byte>) ||
bodyType == typeof(ReadOnlySequence<byte>);
var parameterTypeName = TypeNameHelper.GetTypeDisplayName(factoryContext.RequestBodyParameter.ParameterType, fullName: false);
var parameterName = factoryContext.RequestBodyParameter.Name;

Debug.Assert(parameterName is not null, "CreateArgument() should throw if parameter.Name is null.");

Expand All @@ -621,6 +630,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
var (bodyValue, successful) = await TryReadBodyAsync(
httpContext,
bodyType,
isRawBodyType,
parameterTypeName,
parameterName,
factoryContext.AllowEmptyRequestBody,
Expand All @@ -645,6 +655,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
var (bodyValue, successful) = await TryReadBodyAsync(
httpContext,
bodyType,
isRawBodyType,
parameterTypeName,
parameterName,
factoryContext.AllowEmptyRequestBody,
Expand All @@ -662,6 +673,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
static async Task<(object? FormValue, bool Successful)> TryReadBodyAsync(
HttpContext httpContext,
Type bodyType,
bool isRawBodyType,
string parameterTypeName,
string parameterName,
bool allowEmptyRequestBody,
Expand All @@ -679,6 +691,37 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,

if (feature?.CanHaveBody == true)
{
if (isRawBodyType)
{
var bodyReader = httpContext.Request.BodyReader;

while (true)
{
var result = await bodyReader.ReadAsync();
var buffer = result.Buffer;

if (result.IsCompleted)
{
if (bodyType == typeof(ReadOnlySequence<byte>))
{
// REVIEW: Does this need to be a copy? We can tell users to consume the buffer
// immediately in the action
return (buffer, true);
Copy link
Member

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?

Copy link
Member Author

@davidfowl davidfowl Jan 9, 2022

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.

Copy link
Member Author

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

if (_internalPipeReader == null ||
!ReferenceEquals(_streamInstanceWhenWrapped, _context.Request.Body))
{
_streamInstanceWhenWrapped = _context.Request.Body;
_internalPipeReader = PipeReader.Create(_context.Request.Body);
_context.Response.OnCompleted((self) =>
{
((PipeReader)self).Complete();
return Task.CompletedTask;
}, _internalPipeReader);
}
return _internalPipeReader;

}

// LOH be damned
if (bodyType == typeof(ReadOnlyMemory<byte>))
{
return (new ReadOnlyMemory<byte>(buffer.ToArray()), true);
}

return (buffer.ToArray(), true);
}

bodyReader.AdvanceTo(buffer.Start, buffer.End);
}
}

if (!httpContext.Request.HasJsonContentType())
{
Log.UnexpectedJsonContentType(httpContext, httpContext.Request.ContentType, throwOnBadRequest);
Expand Down Expand Up @@ -1157,7 +1200,7 @@ private static Expression BindParameterFromFormFile(

private static Expression BindParameterFromBody(ParameterInfo parameter, bool allowEmpty, FactoryContext factoryContext)
{
if (factoryContext.JsonRequestBodyParameter is not null)
if (factoryContext.RequestBodyParameter is not null)
{
factoryContext.HasMultipleBodyParameters = true;
var parameterName = parameter.Name;
Expand All @@ -1173,7 +1216,7 @@ private static Expression BindParameterFromBody(ParameterInfo parameter, bool al

var isOptional = IsOptionalParameter(parameter, factoryContext);

factoryContext.JsonRequestBodyParameter = parameter;
factoryContext.RequestBodyParameter = parameter;
factoryContext.AllowEmptyRequestBody = allowEmpty || isOptional;
factoryContext.Metadata.Add(new AcceptsMetadata(parameter.ParameterType, factoryContext.AllowEmptyRequestBody, DefaultAcceptsContentType));

Expand Down Expand Up @@ -1445,7 +1488,7 @@ private class FactoryContext
public bool DisableInferredFromBody { get; init; }

// Temporary State
public ParameterInfo? JsonRequestBodyParameter { get; set; }
public ParameterInfo? RequestBodyParameter { get; set; }
public bool AllowEmptyRequestBody { get; set; }

public bool UsingTempSourceString { get; set; }
Expand Down
69 changes: 69 additions & 0 deletions src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#nullable enable

using System.Buffers;
using System.Globalization;
using System.IO.Pipelines;
using System.Linq.Expressions;
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down