-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Implement PipeBody Features and add to HttpContext #6394
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
Changes from all commits
73d4df5
42c4d08
43c9582
50faea0
1e7a380
a9929a0
158c6a3
709a371
5b7ee32
4733d88
c026818
1c5eb9c
fc2b31e
9f1969e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.IO.Pipelines; | ||
|
||
namespace Microsoft.AspNetCore.Http.Features | ||
{ | ||
/// <summary> | ||
/// Represents the HttpRequestBody as a PipeReader. | ||
/// </summary> | ||
public interface IRequestBodyPipeFeature | ||
{ | ||
/// <summary> | ||
/// A <see cref="PipeWriter"/> representing the request body, if any. | ||
/// </summary> | ||
PipeReader RequestBodyPipe { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO.Pipelines; | ||
|
||
namespace Microsoft.AspNetCore.Http.Features | ||
{ | ||
/// <summary> | ||
/// Represents the HttpResponseBody as a PipeWriter | ||
/// </summary> | ||
public interface IResponseBodyPipeFeature | ||
{ | ||
/// <summary> | ||
/// A <see cref="PipeWriter"/> representing the response body, if any. | ||
/// </summary> | ||
PipeWriter ResponseBodyPipe { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO.Pipelines; | ||
|
||
namespace Microsoft.AspNetCore.Http.Features | ||
{ | ||
public class RequestBodyPipeFeature : IRequestBodyPipeFeature | ||
davidfowl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private StreamPipeReader _internalPipeReader; | ||
private PipeReader _userSetPipeReader; | ||
private HttpContext _context; | ||
|
||
public RequestBodyPipeFeature(HttpContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
_context = context; | ||
} | ||
|
||
public PipeReader RequestBodyPipe | ||
{ | ||
get | ||
{ | ||
if (_userSetPipeReader != null) | ||
{ | ||
return _userSetPipeReader; | ||
} | ||
|
||
if (_internalPipeReader == null || | ||
!object.ReferenceEquals(_internalPipeReader.InnerStream, _context.Request.Body)) | ||
{ | ||
_internalPipeReader = new StreamPipeReader(_context.Request.Body); | ||
_context.Response.RegisterForDispose(_internalPipeReader); | ||
} | ||
|
||
return _internalPipeReader; | ||
} | ||
set | ||
{ | ||
_userSetPipeReader = value ?? throw new ArgumentNullException(nameof(value)); | ||
// TODO set the request body Stream to an adapted pipe https://github.com/aspnet/AspNetCore/issues/3971 | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO.Pipelines; | ||
|
||
namespace Microsoft.AspNetCore.Http.Features | ||
{ | ||
public class ResponseBodyPipeFeature : IResponseBodyPipeFeature | ||
{ | ||
private StreamPipeWriter _internalPipeWriter; | ||
private PipeWriter _userSetPipeWriter; | ||
private HttpContext _context; | ||
|
||
public ResponseBodyPipeFeature(HttpContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
_context = context; | ||
} | ||
|
||
public PipeWriter ResponseBodyPipe | ||
{ | ||
get | ||
{ | ||
if (_userSetPipeWriter != null) | ||
{ | ||
return _userSetPipeWriter; | ||
} | ||
|
||
jkotalik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (_internalPipeWriter == null || | ||
!object.ReferenceEquals(_internalPipeWriter.InnerStream, _context.Response.Body)) | ||
{ | ||
_internalPipeWriter = new StreamPipeWriter(_context.Response.Body); | ||
_context.Response.RegisterForDispose(_internalPipeWriter); | ||
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. We have to call RegisterForDispose here because by default the DefaultHttpContext doesn't call Uninitialize on the Request and Response features. https://github.com/aspnet/AspNetCore/blob/master/src/Http/Http/src/DefaultHttpContext.cs#L197. I'm still thinking about how to write a test for this. |
||
} | ||
|
||
jkotalik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return _internalPipeWriter; | ||
} | ||
set | ||
{ | ||
_userSetPipeWriter = value ?? throw new ArgumentNullException(nameof(value)); | ||
// TODO set the response body Stream to an adapted pipe https://github.com/aspnet/AspNetCore/issues/3971 | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.