Dairai/add http class descriptions (#17349)

This commit is contained in:
Andrew Stanton-Nurse 2019-12-05 09:28:29 -08:00 committed by GitHub
commit 06a85e5db2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 163 additions and 12 deletions

View File

@ -5,12 +5,15 @@ using System;
namespace Microsoft.AspNetCore.Http namespace Microsoft.AspNetCore.Http
{ {
/// <summary>
/// Contains methods to verify the request method of an HTTP request.
/// </summary>
public static class HttpMethods public static class HttpMethods
{ {
// We are intentionally using 'static readonly' here instead of 'const'. // We are intentionally using 'static readonly' here instead of 'const'.
// 'const' values would be embedded into each assembly that used them // 'const' values would be embedded into each assembly that used them
// and each consuming assembly would have a different 'string' instance. // and each consuming assembly would have a different 'string' instance.
// Using .'static readonly' means that all consumers get thee exact same // Using .'static readonly' means that all consumers get these exact same
// 'string' instance, which means the 'ReferenceEquals' checks below work // 'string' instance, which means the 'ReferenceEquals' checks below work
// and allow us to optimize comparisons when these constants are used. // and allow us to optimize comparisons when these constants are used.
@ -25,46 +28,109 @@ namespace Microsoft.AspNetCore.Http
public static readonly string Put = "PUT"; public static readonly string Put = "PUT";
public static readonly string Trace = "TRACE"; public static readonly string Trace = "TRACE";
/// <summary>
/// Returns a value that indicates if the HTTP request method is CONNECT.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is CONNECT; otherwise, <see langword="false" />.
/// </returns>
public static bool IsConnect(string method) public static bool IsConnect(string method)
{ {
return object.ReferenceEquals(Connect, method) || StringComparer.OrdinalIgnoreCase.Equals(Connect, method); return object.ReferenceEquals(Connect, method) || StringComparer.OrdinalIgnoreCase.Equals(Connect, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is DELETE.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is DELETE; otherwise, <see langword="false" />.
/// </returns>
public static bool IsDelete(string method) public static bool IsDelete(string method)
{ {
return object.ReferenceEquals(Delete, method) || StringComparer.OrdinalIgnoreCase.Equals(Delete, method); return object.ReferenceEquals(Delete, method) || StringComparer.OrdinalIgnoreCase.Equals(Delete, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is GET.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is GET; otherwise, <see langword="false" />.
/// </returns>
public static bool IsGet(string method) public static bool IsGet(string method)
{ {
return object.ReferenceEquals(Get, method) || StringComparer.OrdinalIgnoreCase.Equals(Get, method); return object.ReferenceEquals(Get, method) || StringComparer.OrdinalIgnoreCase.Equals(Get, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is HEAD.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is HEAD; otherwise, <see langword="false" />.
/// </returns>
public static bool IsHead(string method) public static bool IsHead(string method)
{ {
return object.ReferenceEquals(Head, method) || StringComparer.OrdinalIgnoreCase.Equals(Head, method); return object.ReferenceEquals(Head, method) || StringComparer.OrdinalIgnoreCase.Equals(Head, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is OPTIONS.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is OPTIONS; otherwise, <see langword="false" />.
/// </returns>
public static bool IsOptions(string method) public static bool IsOptions(string method)
{ {
return object.ReferenceEquals(Options, method) || StringComparer.OrdinalIgnoreCase.Equals(Options, method); return object.ReferenceEquals(Options, method) || StringComparer.OrdinalIgnoreCase.Equals(Options, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is PATCH.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is PATCH; otherwise, <see langword="false" />.
/// </returns>
public static bool IsPatch(string method) public static bool IsPatch(string method)
{ {
return object.ReferenceEquals(Patch, method) || StringComparer.OrdinalIgnoreCase.Equals(Patch, method); return object.ReferenceEquals(Patch, method) || StringComparer.OrdinalIgnoreCase.Equals(Patch, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is POST.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is POST; otherwise, <see langword="false" />.
/// </returns>
public static bool IsPost(string method) public static bool IsPost(string method)
{ {
return object.ReferenceEquals(Post, method) || StringComparer.OrdinalIgnoreCase.Equals(Post, method); return object.ReferenceEquals(Post, method) || StringComparer.OrdinalIgnoreCase.Equals(Post, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is PUT.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is PUT; otherwise, <see langword="false" />.
/// </returns>
public static bool IsPut(string method) public static bool IsPut(string method)
{ {
return object.ReferenceEquals(Put, method) || StringComparer.OrdinalIgnoreCase.Equals(Put, method); return object.ReferenceEquals(Put, method) || StringComparer.OrdinalIgnoreCase.Equals(Put, method);
} }
/// <summary>
/// Returns a value that indicates if the HTTP request method is TRACE.
/// </summary>
/// <param name="method">The HTTP request method.</param>
/// <returns>
/// <see langword="true" /> if the method is TRACE; otherwise, <see langword="false" />.
/// </returns>
public static bool IsTrace(string method) public static bool IsTrace(string method)
{ {
return object.ReferenceEquals(Trace, method) || StringComparer.OrdinalIgnoreCase.Equals(Trace, method); return object.ReferenceEquals(Trace, method) || StringComparer.OrdinalIgnoreCase.Equals(Trace, method);

View File

@ -13,6 +13,9 @@ using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Http namespace Microsoft.AspNetCore.Http
{ {
/// <summary>
/// Represents an implementation of the HTTP Context class.
/// </summary>
public sealed class DefaultHttpContext : HttpContext public sealed class DefaultHttpContext : HttpContext
{ {
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624 // Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624
@ -32,6 +35,9 @@ namespace Microsoft.AspNetCore.Http
private DefaultConnectionInfo _connection; private DefaultConnectionInfo _connection;
private DefaultWebSocketManager _websockets; private DefaultWebSocketManager _websockets;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultHttpContext"/> class.
/// </summary>
public DefaultHttpContext() public DefaultHttpContext()
: this(new FeatureCollection()) : this(new FeatureCollection())
{ {
@ -40,6 +46,10 @@ namespace Microsoft.AspNetCore.Http
Features.Set<IHttpResponseBodyFeature>(new StreamResponseBodyFeature(Stream.Null)); Features.Set<IHttpResponseBodyFeature>(new StreamResponseBodyFeature(Stream.Null));
} }
/// <summary>
/// Initializes a new instance of the <see cref="DefaultHttpContext"/> class with provided features.
/// </summary>
/// <param name="features">Initial set of features for the <see cref="DefaultHttpContext"/>.</param>
public DefaultHttpContext(IFeatureCollection features) public DefaultHttpContext(IFeatureCollection features)
{ {
_features.Initalize(features); _features.Initalize(features);
@ -47,6 +57,13 @@ namespace Microsoft.AspNetCore.Http
_response = new DefaultHttpResponse(this); _response = new DefaultHttpResponse(this);
} }
/// <summary>
/// Reinitialize the current instant of the class with features passed in.
/// </summary>
/// <remarks>
/// This method allows the consumer to re-use the <see cref="DefaultHttpContext" /> for another request, rather than having to allocate a new instance.
/// </remarks>
/// <param name="features">The new set of features for the <see cref="DefaultHttpContext" />.</param>
public void Initialize(IFeatureCollection features) public void Initialize(IFeatureCollection features)
{ {
var revision = features.Revision; var revision = features.Revision;
@ -57,6 +74,9 @@ namespace Microsoft.AspNetCore.Http
_websockets?.Initialize(features, revision); _websockets?.Initialize(features, revision);
} }
/// <summary>
/// Uninitialize all the features in the <see cref="DefaultHttpContext" />.
/// </summary>
public void Uninitialize() public void Uninitialize()
{ {
_features = default; _features = default;
@ -66,8 +86,20 @@ namespace Microsoft.AspNetCore.Http
_websockets?.Uninitialize(); _websockets?.Uninitialize();
} }
/// <summary>
/// Gets or set the <see cref="FormOptions" /> for this instance.
/// </summary>
/// <returns>
/// <see cref="FormOptions"/>
/// </returns>
public FormOptions FormOptions { get; set; } public FormOptions FormOptions { get; set; }
/// <summary>
/// Gets or sets the <see cref="IServiceScopeFactory" /> for this instance.
/// </summary>
/// <returns>
/// <see cref="IServiceScopeFactory"/>
/// </returns>
public IServiceScopeFactory ServiceScopeFactory { get; set; } public IServiceScopeFactory ServiceScopeFactory { get; set; }
private IItemsFeature ItemsFeature => private IItemsFeature ItemsFeature =>
@ -92,16 +124,22 @@ namespace Microsoft.AspNetCore.Http
private IHttpRequestIdentifierFeature RequestIdentifierFeature => private IHttpRequestIdentifierFeature RequestIdentifierFeature =>
_features.Fetch(ref _features.Cache.RequestIdentifier, _newHttpRequestIdentifierFeature); _features.Fetch(ref _features.Cache.RequestIdentifier, _newHttpRequestIdentifierFeature);
/// <inheritdoc/>
public override IFeatureCollection Features => _features.Collection ?? ContextDisposed(); public override IFeatureCollection Features => _features.Collection ?? ContextDisposed();
/// <inheritdoc/>
public override HttpRequest Request => _request; public override HttpRequest Request => _request;
/// <inheritdoc/>
public override HttpResponse Response => _response; public override HttpResponse Response => _response;
/// <inheritdoc/>
public override ConnectionInfo Connection => _connection ?? (_connection = new DefaultConnectionInfo(Features)); public override ConnectionInfo Connection => _connection ?? (_connection = new DefaultConnectionInfo(Features));
/// <inheritdoc/>
public override WebSocketManager WebSockets => _websockets ?? (_websockets = new DefaultWebSocketManager(Features)); public override WebSocketManager WebSockets => _websockets ?? (_websockets = new DefaultWebSocketManager(Features));
/// <inheritdoc/>
public override ClaimsPrincipal User public override ClaimsPrincipal User
{ {
get get
@ -117,30 +155,35 @@ namespace Microsoft.AspNetCore.Http
set { HttpAuthenticationFeature.User = value; } set { HttpAuthenticationFeature.User = value; }
} }
/// <inheritdoc/>
public override IDictionary<object, object> Items public override IDictionary<object, object> Items
{ {
get { return ItemsFeature.Items; } get { return ItemsFeature.Items; }
set { ItemsFeature.Items = value; } set { ItemsFeature.Items = value; }
} }
/// <inheritdoc/>
public override IServiceProvider RequestServices public override IServiceProvider RequestServices
{ {
get { return ServiceProvidersFeature.RequestServices; } get { return ServiceProvidersFeature.RequestServices; }
set { ServiceProvidersFeature.RequestServices = value; } set { ServiceProvidersFeature.RequestServices = value; }
} }
/// <inheritdoc/>
public override CancellationToken RequestAborted public override CancellationToken RequestAborted
{ {
get { return LifetimeFeature.RequestAborted; } get { return LifetimeFeature.RequestAborted; }
set { LifetimeFeature.RequestAborted = value; } set { LifetimeFeature.RequestAborted = value; }
} }
/// <inheritdoc/>
public override string TraceIdentifier public override string TraceIdentifier
{ {
get { return RequestIdentifierFeature.TraceIdentifier; } get { return RequestIdentifierFeature.TraceIdentifier; }
set { RequestIdentifierFeature.TraceIdentifier = value; } set { RequestIdentifierFeature.TraceIdentifier = value; }
} }
/// <inheritdoc/>
public override ISession Session public override ISession Session
{ {
get get
@ -166,6 +209,7 @@ namespace Microsoft.AspNetCore.Http
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]
public HttpContext HttpContext => this; public HttpContext HttpContext => this;
/// <inheritdoc/>
public override void Abort() public override void Abort()
{ {
LifetimeFeature.Abort(); LifetimeFeature.Abort();

View File

@ -5,10 +5,14 @@ using System.Threading;
namespace Microsoft.AspNetCore.Http namespace Microsoft.AspNetCore.Http
{ {
/// <summary>
/// Provides an implementation of <see cref="IHttpContextAccessor" /> based on the current execution context.
/// </summary>
public class HttpContextAccessor : IHttpContextAccessor public class HttpContextAccessor : IHttpContextAccessor
{ {
private static AsyncLocal<HttpContextHolder> _httpContextCurrent = new AsyncLocal<HttpContextHolder>(); private static AsyncLocal<HttpContextHolder> _httpContextCurrent = new AsyncLocal<HttpContextHolder>();
/// <inheritdoc/>
public HttpContext HttpContext public HttpContext HttpContext
{ {
get get

View File

@ -8,6 +8,9 @@ using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Http namespace Microsoft.AspNetCore.Http
{ {
/// <summary>
/// Represents methods used to create an HTTP context object.
/// </summary>
[Obsolete("This is obsolete and will be removed in a future version. Use DefaultHttpContextFactory instead.")] [Obsolete("This is obsolete and will be removed in a future version. Use DefaultHttpContextFactory instead.")]
public class HttpContextFactory : IHttpContextFactory public class HttpContextFactory : IHttpContextFactory
{ {
@ -15,21 +18,41 @@ namespace Microsoft.AspNetCore.Http
private readonly FormOptions _formOptions; private readonly FormOptions _formOptions;
private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IServiceScopeFactory _serviceScopeFactory;
/// <summary>
/// Initializes a new instance of the HttpContext class with options passed in.
/// </summary>
/// <param name="formOptions">Options to set when instantianting the HTTP context object.</param>
public HttpContextFactory(IOptions<FormOptions> formOptions) public HttpContextFactory(IOptions<FormOptions> formOptions)
: this(formOptions, serviceScopeFactory: null) : this(formOptions, serviceScopeFactory: null)
{ {
} }
/// <summary>
/// Initializes a new instance of the DefaultHttpContext class with options passed in.
/// </summary>
/// <param name="formOptions">Options to set when instantianting the HTTP context object.</param>
/// <param name="serviceScopeFactory">Factory object used to create the service scope for the HTTP context.</param>
public HttpContextFactory(IOptions<FormOptions> formOptions, IServiceScopeFactory serviceScopeFactory) public HttpContextFactory(IOptions<FormOptions> formOptions, IServiceScopeFactory serviceScopeFactory)
: this(formOptions, serviceScopeFactory, httpContextAccessor: null) : this(formOptions, serviceScopeFactory, httpContextAccessor: null)
{ {
} }
/// <summary>
/// Initializes a new instance of the DefaultHttpContext class with options passed in.
/// </summary>
/// <param name="formOptions">Options to set when instantianting the HTTP context object.</param>
/// <param name="httpContextAccessor">Object to be used to access the HTTP context instance.</param>
public HttpContextFactory(IOptions<FormOptions> formOptions, IHttpContextAccessor httpContextAccessor) public HttpContextFactory(IOptions<FormOptions> formOptions, IHttpContextAccessor httpContextAccessor)
: this(formOptions, serviceScopeFactory: null, httpContextAccessor: httpContextAccessor) : this(formOptions, serviceScopeFactory: null, httpContextAccessor: httpContextAccessor)
{ {
} }
/// <summary>
/// Initializes a new instance of the DefaultHttpContext class with options passed in.
/// </summary>
/// <param name="formOptions">Options to set when instantianting the HTTP context object.</param>
/// <param name="serviceScopeFactory">Factory object used to create the service scope for the HTTP context.</param>
/// <param name="httpContextAccessor">Options to set when instantianting the Default HTTP context object.</param>
public HttpContextFactory(IOptions<FormOptions> formOptions, IServiceScopeFactory serviceScopeFactory, IHttpContextAccessor httpContextAccessor) public HttpContextFactory(IOptions<FormOptions> formOptions, IServiceScopeFactory serviceScopeFactory, IHttpContextAccessor httpContextAccessor)
{ {
if (formOptions == null) if (formOptions == null)
@ -47,6 +70,10 @@ namespace Microsoft.AspNetCore.Http
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
} }
/// <summary>
/// Initializes a new instance of the DefaultHttpContext class with options passed in.
/// </summary>
/// <param name="featureCollection">Options to set when instantianting the Default HTTP context object.</param>
public HttpContext Create(IFeatureCollection featureCollection) public HttpContext Create(IFeatureCollection featureCollection)
{ {
if (featureCollection == null) if (featureCollection == null)
@ -66,6 +93,10 @@ namespace Microsoft.AspNetCore.Http
return httpContext; return httpContext;
} }
/// <summary>
/// Sets the HTTP context object to null for garbage collection.
/// </summary>
/// <param name="httpContext">HTTP context to dispose.</param>
public void Dispose(HttpContext httpContext) public void Dispose(HttpContext httpContext)
{ {
if (_httpContextAccessor != null) if (_httpContextAccessor != null)

View File

@ -13,6 +13,9 @@ using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.HttpsPolicy namespace Microsoft.AspNetCore.HttpsPolicy
{ {
/// <summary>
/// Middleware that redirects non-HTTPS requests to an HTTPS URL.
/// </summary>
public class HttpsRedirectionMiddleware public class HttpsRedirectionMiddleware
{ {
private const int PortNotFound = -1; private const int PortNotFound = -1;
@ -26,7 +29,7 @@ namespace Microsoft.AspNetCore.HttpsPolicy
private readonly ILogger _logger; private readonly ILogger _logger;
/// <summary> /// <summary>
/// Initializes the HttpsRedirectionMiddleware /// Initializes <see cref="HttpsRedirectionMiddleware" />.
/// </summary> /// </summary>
/// <param name="next"></param> /// <param name="next"></param>
/// <param name="options"></param> /// <param name="options"></param>
@ -56,13 +59,13 @@ namespace Microsoft.AspNetCore.HttpsPolicy
} }
/// <summary> /// <summary>
/// Initializes the HttpsRedirectionMiddleware /// Initializes <see cref="HttpsRedirectionMiddleware" />.
/// </summary> /// </summary>
/// <param name="next"></param> /// <param name="next"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="loggerFactory"></param> /// <param name="loggerFactory"></param>
/// <param name="serverAddressesFeature">The</param> /// <param name="serverAddressesFeature"></param>
public HttpsRedirectionMiddleware(RequestDelegate next, IOptions<HttpsRedirectionOptions> options, IConfiguration config, ILoggerFactory loggerFactory, public HttpsRedirectionMiddleware(RequestDelegate next, IOptions<HttpsRedirectionOptions> options, IConfiguration config, ILoggerFactory loggerFactory,
IServerAddressesFeature serverAddressesFeature) IServerAddressesFeature serverAddressesFeature)
: this(next, options, config, loggerFactory) : this(next, options, config, loggerFactory)
@ -71,7 +74,7 @@ namespace Microsoft.AspNetCore.HttpsPolicy
} }
/// <summary> /// <summary>
/// Invokes the HttpsRedirectionMiddleware /// Invokes the HttpsRedirectionMiddleware.
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
/// <returns></returns> /// <returns></returns>

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.HttpsPolicy namespace Microsoft.AspNetCore.HttpsPolicy
{ {
/// <summary> /// <summary>
/// Options for the HttpsRedirection middleware /// Options for the HTTPS Redirection Middleware.
/// </summary> /// </summary>
public class HttpsRedirectionOptions public class HttpsRedirectionOptions
{ {

View File

@ -9,16 +9,19 @@ using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Hosting namespace Microsoft.AspNetCore.Hosting
{ {
/// <summary>
/// Provides extensions method to use Http.sys as the server for the web host.
/// </summary>
public static class WebHostBuilderHttpSysExtensions public static class WebHostBuilderHttpSysExtensions
{ {
/// <summary> /// <summary>
/// Specify HttpSys as the server to be used by the web host. /// Specify Http.sys as the server to be used by the web host.
/// </summary> /// </summary>
/// <param name="hostBuilder"> /// <param name="hostBuilder">
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure. /// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.
/// </param> /// </param>
/// <returns> /// <returns>
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder. /// A reference to the <see cref="IWebHostBuilder" /> parameter object.
/// </returns> /// </returns>
public static IWebHostBuilder UseHttpSys(this IWebHostBuilder hostBuilder) public static IWebHostBuilder UseHttpSys(this IWebHostBuilder hostBuilder)
{ {
@ -38,16 +41,16 @@ namespace Microsoft.AspNetCore.Hosting
} }
/// <summary> /// <summary>
/// Specify HttpSys as the server to be used by the web host. /// Specify Http.sys as the server to be used by the web host.
/// </summary> /// </summary>
/// <param name="hostBuilder"> /// <param name="hostBuilder">
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure. /// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.
/// </param> /// </param>
/// <param name="options"> /// <param name="options">
/// A callback to configure HttpSys options. /// A callback to configure Http.sys options.
/// </param> /// </param>
/// <returns> /// <returns>
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder. /// A reference to the <see cref="IWebHostBuilder" /> parameter object.
/// </returns> /// </returns>
public static IWebHostBuilder UseHttpSys(this IWebHostBuilder hostBuilder, Action<HttpSysOptions> options) public static IWebHostBuilder UseHttpSys(this IWebHostBuilder hostBuilder, Action<HttpSysOptions> options)
{ {