Skip to content

Commit ce2e3bc

Browse files
John Luopranavkm
andauthored
Api docs for HttpOverrides (#26530)
* Api docs for HttpOverrides * Update ForwardedHeadersExtensions.cs * Update ForwardedHeadersOptions.cs * Feedback * Add more details Co-authored-by: Pranav K <[email protected]>
1 parent 0a52bb7 commit ce2e3bc

10 files changed

+115
-22
lines changed

src/Middleware/HttpOverrides/src/ForwardedHeaders.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,31 @@
55

66
namespace Microsoft.AspNetCore.HttpOverrides
77
{
8+
/// <summary>
9+
/// Flags for controlling which forwarders are processed.
10+
/// </summary>
811
[Flags]
912
public enum ForwardedHeaders
1013
{
14+
/// <summary>
15+
/// Do not process any forwarders
16+
/// </summary>
1117
None = 0,
18+
/// <summary>
19+
/// Process X-Forwarded-For, which identifies the originating IP address of the client.
20+
/// </summary>
1221
XForwardedFor = 1 << 0,
22+
/// <summary>
23+
/// Process X-Forwarded-Host, which identifies the original host requested by the client.
24+
/// </summary>
1325
XForwardedHost = 1 << 1,
26+
/// <summary>
27+
/// Process X-Forwarded-Proto, which identifies the protocol (HTTP or HTTPS) the client used to connect.
28+
/// </summary>
1429
XForwardedProto = 1 << 2,
30+
/// <summary>
31+
/// Process X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto.
32+
/// </summary>
1533
All = XForwardedFor | XForwardedHost | XForwardedProto
1634
}
1735
}

src/Middleware/HttpOverrides/src/ForwardedHeadersExtensions.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77

88
namespace Microsoft.AspNetCore.Builder
99
{
10+
/// <summary>
11+
/// Extension methods for enabling <see cref="ForwardedHeadersMiddleware"/>.
12+
/// </summary>
1013
public static class ForwardedHeadersExtensions
1114
{
1215
private const string ForwardedHeadersAdded = "ForwardedHeadersAdded";
1316

1417
/// <summary>
15-
/// Forwards proxied headers onto current request
18+
/// Applies forwarded headers to their matching fields on the current request.
19+
/// <para>
20+
/// By convention, HTTP proxies forward information from the client in well-known HTTP headers.
21+
/// The <see cref="ForwardedHeadersMiddleware"/> reads these headers and fills in the associated fields on HttpContext.
22+
/// </para>
1623
/// </summary>
17-
/// <param name="builder"></param>
18-
/// <returns></returns>
24+
/// <param name="builder">The <see cref="IApplicationBuilder" />.</param>
25+
/// <returns>A reference to <paramref name="builder" /> after the operation has completed.</returns>
1926
public static IApplicationBuilder UseForwardedHeaders(this IApplicationBuilder builder)
2027
{
2128
if (builder == null)
@@ -35,11 +42,15 @@ public static IApplicationBuilder UseForwardedHeaders(this IApplicationBuilder b
3542
}
3643

3744
/// <summary>
38-
/// Forwards proxied headers onto current request
45+
/// Applies forwarded headers to their matching fields on the current request.
46+
/// <para>
47+
/// By convention, HTTP proxies forward information from the client in well-known HTTP headers.
48+
/// The <see cref="ForwardedHeadersMiddleware"/> reads these headers and fills in the associated fields on HttpContext.
49+
/// </para>
3950
/// </summary>
40-
/// <param name="builder"></param>
51+
/// <param name="builder">The <see cref="IApplicationBuilder" />.</param>
4152
/// <param name="options">Enables the different forwarding options.</param>
42-
/// <returns></returns>
53+
/// <returns>A reference to <paramref name="builder" /> after the operation has completed.</returns>
4354
public static IApplicationBuilder UseForwardedHeaders(this IApplicationBuilder builder, ForwardedHeadersOptions options)
4455
{
4556
if (builder == null)

src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace Microsoft.AspNetCore.HttpOverrides
1717
{
18+
/// <summary>
19+
/// A middleware for forwarding proxied headers onto the current request.
20+
/// </summary>
1821
public class ForwardedHeadersMiddleware
1922
{
2023
private static readonly bool[] HostCharValidity = new bool[127];
@@ -62,6 +65,12 @@ static ForwardedHeadersMiddleware()
6265
}
6366
}
6467

68+
/// <summary>
69+
/// Create a new <see cref="ForwardedHeadersMiddleware"/>.
70+
/// </summary>
71+
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
72+
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
73+
/// <param name="options">The <see cref="ForwardedHeadersOptions"/> for configuring the middleware.</param>
6574
public ForwardedHeadersMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions<ForwardedHeadersOptions> options)
6675
{
6776
if (next == null)
@@ -137,12 +146,20 @@ private bool IsTopLevelWildcard(string host)
137146
|| string.Equals("0.0.0.0", host, StringComparison.Ordinal)); // IPv4 Any
138147
}
139148

149+
/// <summary>
150+
/// Executes the middleware.
151+
/// </summary>
152+
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
140153
public Task Invoke(HttpContext context)
141154
{
142155
ApplyForwarders(context);
143156
return _next(context);
144157
}
145158

159+
/// <summary>
160+
/// Forward the proxied headers to the given <see cref="HttpContext"/>.
161+
/// </summary>
162+
/// <param name="context">The <see cref="HttpContext"/>.</param>
146163
public void ApplyForwarders(HttpContext context)
147164
{
148165
// Gather expected headers.

src/Middleware/HttpOverrides/src/ForwardedHeadersOptions.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
@@ -7,40 +7,46 @@
77

88
namespace Microsoft.AspNetCore.Builder
99
{
10+
/// <summary>
11+
/// Options for <see cref="ForwardedHeadersMiddleware"/>
12+
/// </summary>
1013
public class ForwardedHeadersOptions
1114
{
1215
/// <summary>
13-
/// Use this header instead of <see cref="ForwardedHeadersDefaults.XForwardedForHeaderName"/>
16+
/// Gets or sets the header used to retrieve the originating client IP. Defaults to the value specified by
17+
/// <see cref="ForwardedHeadersDefaults.XForwardedForHeaderName"/>.
1418
/// </summary>
15-
/// <seealso cref="Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersDefaults"/>
1619
public string ForwardedForHeaderName { get; set; } = ForwardedHeadersDefaults.XForwardedForHeaderName;
1720

1821
/// <summary>
19-
/// Use this header instead of <see cref="ForwardedHeadersDefaults.XForwardedHostHeaderName"/>
22+
/// Gets or sets the header used to retrieve the original value of the Host header field.
23+
/// Defaults to the value specified by <see cref="ForwardedHeadersDefaults.XForwardedHostHeaderName"/>
2024
/// </summary>
21-
/// <seealso cref="ForwardedHeadersDefaults"/>
2225
public string ForwardedHostHeaderName { get; set; } = ForwardedHeadersDefaults.XForwardedHostHeaderName;
2326

2427
/// <summary>
25-
/// Use this header instead of <see cref="ForwardedHeadersDefaults.XForwardedProtoHeaderName"/>
28+
/// Gets or sets the header used to retrieve the value for the originating scheme (HTTP/HTTPS).
29+
/// Defaults to the value specified by <see cref="ForwardedHeadersDefaults.XForwardedProtoHeaderName"/>
2630
/// </summary>
27-
/// <seealso cref="ForwardedHeadersDefaults"/>
2831
public string ForwardedProtoHeaderName { get; set; } = ForwardedHeadersDefaults.XForwardedProtoHeaderName;
2932

3033
/// <summary>
31-
/// Use this header instead of <see cref="ForwardedHeadersDefaults.XOriginalForHeaderName"/>
34+
/// Gets or sets the header used to store the original value of client IP before applying forwarded headers.
35+
/// Defaults to the value specified by <see cref="ForwardedHeadersDefaults.XOriginalForHeaderName"/>
3236
/// </summary>
3337
/// <seealso cref="ForwardedHeadersDefaults"/>
3438
public string OriginalForHeaderName { get; set; } = ForwardedHeadersDefaults.XOriginalForHeaderName;
3539

3640
/// <summary>
37-
/// Use this header instead of <see cref="ForwardedHeadersDefaults.XOriginalHostHeaderName"/>
41+
/// Gets or sets the header used to store the original value of the Host header field before applying forwarded headers.
42+
/// Defaults to the value specified by <see cref="ForwardedHeadersDefaults.XOriginalHostHeaderName"/>
3843
/// </summary>
3944
/// <seealso cref="ForwardedHeadersDefaults"/>
4045
public string OriginalHostHeaderName { get; set; } = ForwardedHeadersDefaults.XOriginalHostHeaderName;
4146

4247
/// <summary>
43-
/// Use this header instead of <see cref="ForwardedHeadersDefaults.XOriginalProtoHeaderName"/>
48+
/// Gets or sets the header used to store the original scheme (HTTP/HTTPS) before applying forwarded headers.
49+
/// Defaults to the value specified by <see cref="ForwardedHeadersDefaults.XOriginalProtoHeaderName"/>
4450
/// </summary>
4551
/// <seealso cref="ForwardedHeadersDefaults"/>
4652
public string OriginalProtoHeaderName { get; set; } = ForwardedHeadersDefaults.XOriginalProtoHeaderName;

src/Middleware/HttpOverrides/src/HttpMethodOverrideExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77

88
namespace Microsoft.AspNetCore.Builder
99
{
10+
/// <summary>
11+
/// Extension methods for enabling <see cref="HttpMethodOverrideMiddleware"/>.
12+
/// </summary>
1013
public static class HttpMethodOverrideExtensions
1114
{
1215
/// <summary>
13-
/// Allows incoming POST request to override method type with type specified in header.
16+
/// Allows incoming POST request to override method type with type specified in header. This middleware
17+
/// is used when a client is limited to sending GET or POST methods but wants to invoke other HTTP methods.
18+
/// By default, the X-HTTP-Method-Override request header is used to specify the HTTP method being tunneled.
1419
/// </summary>
1520
/// <param name="builder">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
1621
public static IApplicationBuilder UseHttpMethodOverride(this IApplicationBuilder builder)
@@ -24,10 +29,13 @@ public static IApplicationBuilder UseHttpMethodOverride(this IApplicationBuilder
2429
}
2530

2631
/// <summary>
27-
/// Allows incoming POST request to override method type with type specified in form.
32+
/// Allows incoming POST request to override method type with type specified in form. This middleware
33+
/// is used when a client is limited to sending GET or POST methods but wants to invoke other HTTP methods.
2834
/// </summary>
2935
/// <param name="builder">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
30-
/// <param name="options">The <see cref="HttpMethodOverrideOptions"/>.</param>
36+
/// <param name="options">
37+
/// The <see cref="HttpMethodOverrideOptions"/> which indicates which form type specifies the override method.
38+
/// </param>
3139
public static IApplicationBuilder UseHttpMethodOverride(this IApplicationBuilder builder, HttpMethodOverrideOptions options)
3240
{
3341
if (builder == null)

src/Middleware/HttpOverrides/src/HttpMethodOverrideMiddleware.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@
99

1010
namespace Microsoft.AspNetCore.HttpOverrides
1111
{
12+
/// <summary>
13+
/// A middleware for overriding the HTTP method of an incoming POST request.
14+
/// </summary>
1215
public class HttpMethodOverrideMiddleware
1316
{
1417
private const string xHttpMethodOverride = "X-Http-Method-Override";
1518
private readonly RequestDelegate _next;
1619
private readonly HttpMethodOverrideOptions _options;
1720

21+
/// <summary>
22+
/// Create a new <see cref="HttpMethodOverrideMiddleware"/>.
23+
/// </summary>
24+
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
25+
/// <param name="options">The <see cref="HttpMethodOverrideOptions"/> for configuring the middleware.</param>
1826
public HttpMethodOverrideMiddleware(RequestDelegate next, IOptions<HttpMethodOverrideOptions> options)
1927
{
2028
if (next == null)
@@ -29,6 +37,10 @@ public HttpMethodOverrideMiddleware(RequestDelegate next, IOptions<HttpMethodOve
2937
_options = options.Value;
3038
}
3139

40+
/// <summary>
41+
/// Executes the middleware.
42+
/// </summary>
43+
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
3244
public async Task Invoke(HttpContext context)
3345
{
3446
if (HttpMethods.IsPost(context.Request.Method))

src/Middleware/HttpOverrides/src/HttpMethodOverrideOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using Microsoft.AspNetCore.HttpOverrides;
5+
46
namespace Microsoft.AspNetCore.Builder
57
{
8+
/// <summary>
9+
/// Options for configuring <see cref="HttpMethodOverrideMiddleware"/>
10+
/// </summary>
611
public class HttpMethodOverrideOptions
712
{
813
/// <summary>

src/Middleware/HttpOverrides/src/IPNetwork.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Net;
55

66
namespace Microsoft.AspNetCore.HttpOverrides
77
{
8+
/// <summary>
9+
/// A representation of an IP network based on CIDR notation.
10+
/// </summary>
811
public class IPNetwork
912
{
13+
/// <summary>
14+
/// Create a new <see cref="IPNetwork"/> with the specified <see cref="IPAddress"/> and prefix length.
15+
/// </summary>
16+
/// <param name="prefix">The <see cref="IPAddress"/>.</param>
17+
/// <param name="prefixLength">The prefix length.</param>
1018
public IPNetwork(IPAddress prefix, int prefixLength)
1119
{
1220
Prefix = prefix;
@@ -15,6 +23,9 @@ public IPNetwork(IPAddress prefix, int prefixLength)
1523
Mask = CreateMask();
1624
}
1725

26+
/// <summary>
27+
/// Get the <see cref="IPAddress"/> that represents the prefix for the network.
28+
/// </summary>
1829
public IPAddress Prefix { get; }
1930

2031
private byte[] PrefixBytes { get; }
@@ -26,6 +37,11 @@ public IPNetwork(IPAddress prefix, int prefixLength)
2637

2738
private byte[] Mask { get; }
2839

40+
/// <summary>
41+
/// Determine whether a given The <see cref="IPAddress"/> is part of the IP network.
42+
/// </summary>
43+
/// <param name="address">The <see cref="IPAddress"/>.</param>
44+
/// <returns><see langword="true"/> if the <see cref="IPAddress"/> is part of the IP network. Otherwise, <see langword="false"/>.</returns>
2945
public bool Contains(IPAddress address)
3046
{
3147
if (Prefix.AddressFamily != address.AddressFamily)

src/Middleware/HttpOverrides/src/Microsoft.AspNetCore.HttpOverrides.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* HTTP method override header.</Description>
77
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
88
<IsAspNetCoreApp>true</IsAspNetCoreApp>
9-
<NoWarn>$(NoWarn);CS1591</NoWarn>
9+
<NoWarn>$(NoWarn.Replace('1591', ''))</NoWarn>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<PackageTags>aspnetcore;proxy;headers;xforwarded</PackageTags>
1212
<IsPackable>false</IsPackable>

src/Middleware/HttpOverrides/test/IPNetworkTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
using System.Net;
44
using Xunit;

0 commit comments

Comments
 (0)