-
Notifications
You must be signed in to change notification settings - Fork 68
Add request feature to support buffered/bufferless streams #170
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
9b77786
3f12072
4b4a610
532b5f2
f1d667a
9520a8c
3264ad1
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,196 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Web; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Http.Features; | ||
| using Microsoft.AspNetCore.WebUtilities; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| internal class HttpRequestAdapterFeature : IHttpRequestAdapterFeature, IHttpRequestFeature, IDisposable | ||
| { | ||
| private readonly int _bufferThreshold; | ||
| private readonly long? _bufferLimit; | ||
| private readonly IHttpRequestFeature _other; | ||
|
|
||
| private Stream? _bufferedStream; | ||
|
|
||
| public HttpRequestAdapterFeature(IHttpRequestFeature other, int bufferThreshold, long? bufferLimit) | ||
| { | ||
| _bufferThreshold = bufferThreshold; | ||
| _bufferLimit = bufferLimit; | ||
| _other = other; | ||
| } | ||
|
|
||
| public ReadEntityBodyMode Mode { get; private set; } | ||
|
|
||
| public Stream GetBufferedInputStream() | ||
| { | ||
| if (Mode is ReadEntityBodyMode.Buffered) | ||
| { | ||
| Debug.Assert(_bufferedStream is not null); | ||
| return _bufferedStream; | ||
| } | ||
|
|
||
| if (Mode is ReadEntityBodyMode.None) | ||
| { | ||
| Mode = ReadEntityBodyMode.Buffered; | ||
|
|
||
| return _bufferedStream = new FileBufferingReadStream(_other.Body, _bufferThreshold, _bufferLimit, AspNetCoreTempDirectory.TempDirectoryFactory); | ||
| } | ||
|
|
||
| throw new InvalidOperationException("GetBufferlessInputStream cannot be called after other stream access"); | ||
| } | ||
|
|
||
| Stream IHttpRequestAdapterFeature.GetBufferlessInputStream() | ||
| { | ||
| if (Mode is ReadEntityBodyMode.Bufferless or ReadEntityBodyMode.None) | ||
| { | ||
| Mode = ReadEntityBodyMode.Bufferless; | ||
| return GetBody(); | ||
| } | ||
|
|
||
| throw new InvalidOperationException("GetBufferlessInputStream cannot be called after other stream access"); | ||
| } | ||
|
|
||
| Stream IHttpRequestAdapterFeature.InputStream | ||
| { | ||
| get | ||
| { | ||
| if (Mode is ReadEntityBodyMode.Classic && _bufferedStream is not null) | ||
| { | ||
| return _bufferedStream; | ||
| } | ||
|
|
||
| throw new InvalidOperationException("InputStream must be prebuffered"); | ||
| } | ||
| } | ||
|
|
||
| async Task<Stream> IHttpRequestAdapterFeature.GetInputStreamAsync(CancellationToken token) | ||
| { | ||
| await BufferInputStreamAsync(token); | ||
| return GetBody(); | ||
| } | ||
|
|
||
| public async Task BufferInputStreamAsync(CancellationToken token) | ||
| { | ||
| if (Mode is ReadEntityBodyMode.Classic) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (Mode is not ReadEntityBodyMode.None) | ||
| { | ||
| throw new InvalidOperationException("InputStream cannot be called after other stream access"); | ||
| } | ||
|
|
||
| var stream = GetBufferedInputStream(); | ||
| await stream.DrainAsync(token); | ||
| stream.Position = 0; | ||
|
|
||
| Mode = ReadEntityBodyMode.Classic; | ||
| } | ||
|
|
||
| public void Dispose() => _bufferedStream?.Dispose(); | ||
|
|
||
| string IHttpRequestFeature.Protocol | ||
| { | ||
| get => _other.Protocol; | ||
| set => _other.Protocol = value; | ||
| } | ||
|
|
||
| string IHttpRequestFeature.Scheme | ||
| { | ||
| get => _other.Scheme; | ||
| set => _other.Scheme = value; | ||
| } | ||
|
|
||
| string IHttpRequestFeature.Method | ||
| { | ||
| get => _other.Method; | ||
| set => _other.Method = value; | ||
| } | ||
|
|
||
| string IHttpRequestFeature.PathBase | ||
| { | ||
| get => _other.PathBase; | ||
| set => _other.PathBase = value; | ||
| } | ||
|
|
||
| string IHttpRequestFeature.Path | ||
| { | ||
| get => _other.Path; | ||
| set => _other.Path = value; | ||
| } | ||
|
|
||
| string IHttpRequestFeature.QueryString | ||
| { | ||
| get => _other.QueryString; | ||
| set => _other.QueryString = value; | ||
| } | ||
|
|
||
| string IHttpRequestFeature.RawTarget | ||
| { | ||
| get => _other.RawTarget; | ||
| set => _other.RawTarget = value; | ||
| } | ||
|
|
||
| IHeaderDictionary IHttpRequestFeature.Headers | ||
| { | ||
| get => _other.Headers; | ||
| set => _other.Headers = value; | ||
| } | ||
|
|
||
| Stream IHttpRequestFeature.Body | ||
| { | ||
| get | ||
| { | ||
| var body = GetBody(); | ||
|
|
||
| if (Mode is ReadEntityBodyMode.None) | ||
| { | ||
| Mode = body.CanSeek ? ReadEntityBodyMode.Buffered : ReadEntityBodyMode.Bufferless; | ||
| } | ||
|
|
||
| return body; | ||
| } | ||
| set => _other.Body = value; | ||
|
Member
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. Should Mode be updated if the stream is replaced?
Member
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. _bufferedStream as well?
Member
Author
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.
Member
Author
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. I had thought about it, but wasn't sure how necessary it would be. Pushed a PR to reset in that case |
||
| } | ||
|
|
||
| private Stream GetBody() => _bufferedStream ?? _other.Body; | ||
|
|
||
| internal static class AspNetCoreTempDirectory | ||
| { | ||
| private static string? _tempDirectory; | ||
|
|
||
| public static string TempDirectory | ||
| { | ||
| get | ||
| { | ||
| if (_tempDirectory == null) | ||
| { | ||
| // Look for folders in the following order. | ||
| var temp = Environment.GetEnvironmentVariable("ASPNETCORE_TEMP") ?? // ASPNETCORE_TEMP - User set temporary location. | ||
| Path.GetTempPath(); // Fall back. | ||
|
|
||
| if (!Directory.Exists(temp)) | ||
| { | ||
| throw new DirectoryNotFoundException(temp); | ||
| } | ||
|
|
||
| _tempDirectory = temp; | ||
| } | ||
|
|
||
| return _tempDirectory; | ||
| } | ||
| } | ||
|
|
||
| public static Func<string> TempDirectoryFactory => () => TempDirectory; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #if NETCOREAPP | ||
|
|
||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Web; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| internal interface IHttpRequestAdapterFeature | ||
| { | ||
| ReadEntityBodyMode Mode { get; } | ||
|
|
||
| Task<Stream> GetInputStreamAsync(CancellationToken token); | ||
|
|
||
| Stream InputStream { get; } | ||
|
|
||
| Stream GetBufferedInputStream(); | ||
|
|
||
| Stream GetBufferlessInputStream(); | ||
| } | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Web | ||
| { | ||
| public enum ReadEntityBodyMode | ||
| { | ||
| None, | ||
| Classic, // BinaryRead, Form, Files, InputStream | ||
| Bufferless, // GetBufferlessInputStream | ||
| Buffered // GetBufferedInputStream | ||
| } | ||
| } |
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.
We could remove this requirement and provide an API similar to what is described in #164 for
HttpRequest.GetInputStreamAsync()that would do the awaiting there. This then could block and wait with the recommendation people move to the async versionThere 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.
For reference, this blocks on framework so is not a real change. Non-blocking APIs are the now available
GetBufferedStreamandGetBufferlessStream