-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Addresses #9466] API to configure TempDirectory used by HttpBuffering #11569
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
933c935
HttpFileBufferingFactory and buffering options.
cjaliaga 6c51f97
EnableRewind with HttpFileBufferingFactory.
cjaliaga 5f89991
Registered IOptions<HttpBufferingOptions> in FormFeatureTests.
cjaliaga 77e4bcb
XmlDataContractSerializarOutputFormatter using HttpFileBufferingStrea…
cjaliaga ec0d270
Update refs
cjaliaga a33df59
Clean TempDirectory checks.
cjaliaga ff104ac
HttpBuffering extensions and configuration.
cjaliaga 50f46c2
HttpBuffering factory, Xml formatters, ServiceCollection extensions.
cjaliaga 9831e36
Current EnableRewind for MultipartSection obsolete
cjaliaga 25d232d
NewtonsoftJson using IFileBufferingStreamFactory.
cjaliaga d6c629a
IBufferedWriteStream disposable. ViewComponent with factory.
cjaliaga 8f8659c
Update refs.
cjaliaga 8211084
Simplified fileBufferingWriteStream dispose.
cjaliaga a4a08a6
Removed EnableRewind without factory as it's internal and no longer u…
cjaliaga 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
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// 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. | ||
|
||
namespace Microsoft.AspNetCore.Http | ||
{ | ||
public class HttpBufferingOptions | ||
{ | ||
public string TempFileDirectory { get; set; } | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Http/Http/src/HttpBufferingServiceCollectionExtensions.cs
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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 Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
/// <summary> | ||
/// Extension methods for setting up Http Buffering services in an <see cref="IServiceCollection" />. | ||
/// </summary> | ||
public static class HttpBufferingServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds services required to enable Http Bufering to the specified <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||
public static IServiceCollection AddHttpBuffering(this IServiceCollection services) | ||
{ | ||
if (services == null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
services.AddOptions(); | ||
|
||
services.TryAddSingleton<IFileBufferingStreamFactory, HttpFileBufferingStreamFactory>(); | ||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<HttpBufferingOptions>, PostConfigureHttpBufferingOptions>()); | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Adds services required to enable Http Bufering to the specified <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||
/// <param name="configureOptions">An <see cref="Action{HttpBufferingOptions}"/> to configure the provided <see cref="HttpBufferingOptions"/>.</param> | ||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||
public static IServiceCollection AddHttpBuffering(this IServiceCollection services, Action<HttpBufferingOptions> configureOptions) | ||
{ | ||
if (services == null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
if (configureOptions == null) | ||
{ | ||
throw new ArgumentNullException(nameof(configureOptions)); | ||
} | ||
|
||
services.AddHttpBuffering(); | ||
services.Configure(configureOptions); | ||
|
||
return services; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.Http | ||
{ | ||
public class HttpFileBufferingStreamFactory : IFileBufferingStreamFactory | ||
{ | ||
private readonly HttpBufferingOptions _bufferingOptions; | ||
private readonly Func<string> _getTempDirectory; | ||
|
||
public HttpFileBufferingStreamFactory(IOptions<HttpBufferingOptions> bufferingOptions) | ||
{ | ||
if (bufferingOptions == null) | ||
{ | ||
throw new ArgumentNullException(nameof(bufferingOptions)); | ||
} | ||
|
||
_bufferingOptions = bufferingOptions.Value; | ||
_getTempDirectory = () => _tempDirectory; | ||
} | ||
|
||
public Stream CreateReadStream(Stream inner, int bufferThreshold, long? bufferLimit = null) | ||
{ | ||
return new FileBufferingReadStream(inner, bufferThreshold, bufferLimit, _getTempDirectory); | ||
} | ||
|
||
public IBufferedWriteStream CreateWriteStream() | ||
{ | ||
return new FileBufferingWriteStream(tempFileDirectoryAccessor: _getTempDirectory); | ||
} | ||
|
||
public IBufferedWriteStream CreateWriteStream(int memoryThreshold, long? bufferLimit = null) | ||
{ | ||
return new FileBufferingWriteStream(memoryThreshold, bufferLimit, _getTempDirectory); | ||
} | ||
|
||
private string _tempDirectory | ||
{ | ||
get | ||
{ | ||
// TempFileDirectory defaults to Path.GetTempPath() if it's not modified | ||
if (!Directory.Exists(_bufferingOptions.TempFileDirectory)) | ||
{ | ||
throw new DirectoryNotFoundException(_bufferingOptions.TempFileDirectory); | ||
} | ||
|
||
return _bufferingOptions.TempFileDirectory; | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
|
||
namespace Microsoft.AspNetCore.Http | ||
{ | ||
public interface IFileBufferingStreamFactory | ||
{ | ||
Stream CreateReadStream(Stream inner, int bufferThreshold, long? bufferLimit = null); | ||
IBufferedWriteStream CreateWriteStream(); | ||
IBufferedWriteStream CreateWriteStream(int memoryThreshold, long? bufferLimit = null); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.Http | ||
{ | ||
public class PostConfigureHttpBufferingOptions : IPostConfigureOptions<HttpBufferingOptions> | ||
{ | ||
public void PostConfigure(string name, HttpBufferingOptions options) | ||
{ | ||
if (string.IsNullOrEmpty(options.TempFileDirectory)) | ||
{ | ||
// Look for folders in the following order. | ||
options.TempFileDirectory = | ||
Environment.GetEnvironmentVariable("ASPNETCORE_TEMP") ?? // ASPNETCORE_TEMP - User set temporary location. | ||
Path.GetTempPath(); // Fall back. | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
This would likely go in the Http.Abstractions package.
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.
I was thinking on putting
IBufferedWriteStream
on WebUtilities but in that case Http.Abstractions would have a dependency on it, is that OK?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, tradeoffs. Let's not change the dependencies right now. That means IFileBufferingStreamFactory stays in Http and IBufferedWriteStream in WebUtilities.