This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
OverrideHeaderMiddleware.cs
89 lines (80 loc) · 3.56 KB
/
OverrideHeaderMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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.Net;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.HttpOverrides
{
public class OverrideHeaderMiddleware
{
private const string XForwardedForHeaderName = "X-Forwarded-For";
private const string XForwardedHostHeaderName = "X-Forwarded-Host";
private const string XForwardedProtoHeaderName = "X-Forwarded-Proto";
private const string XOriginalIPName = "X-Original-IP";
private const string XOriginalHostName = "X-Original-Host";
private const string XOriginalProtoName = "X-Original-Proto";
private readonly OverrideHeaderMiddlewareOptions _options;
private readonly RequestDelegate _next;
public OverrideHeaderMiddleware(RequestDelegate next, OverrideHeaderMiddlewareOptions options)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_options = options;
_next = next;
}
public Task Invoke(HttpContext context)
{
if ((_options.ForwardedOptions & ForwardedHeaders.XForwardedFor) != 0)
{
var xForwardedForHeaderValue = context.Request.Headers.GetCommaSeparatedValues(XForwardedForHeaderName);
if (xForwardedForHeaderValue != null && xForwardedForHeaderValue.Length > 0)
{
IPAddress ipFromHeader;
if (IPAddress.TryParse(xForwardedForHeaderValue[0], out ipFromHeader))
{
var remoteIPString = context.Connection.RemoteIpAddress?.ToString();
if (!string.IsNullOrEmpty(remoteIPString))
{
context.Request.Headers[XOriginalIPName] = remoteIPString;
}
context.Connection.RemoteIpAddress = ipFromHeader;
}
}
}
if ((_options.ForwardedOptions & ForwardedHeaders.XForwardedHost) != 0)
{
var xForwardHostHeaderValue = context.Request.Headers[XForwardedHostHeaderName];
if (!string.IsNullOrEmpty(xForwardHostHeaderValue))
{
var hostString = context.Request.Host.ToString();
if (!string.IsNullOrEmpty(hostString))
{
context.Request.Headers[XOriginalHostName] = hostString;
}
context.Request.Host = HostString.FromUriComponent(xForwardHostHeaderValue);
}
}
if ((_options.ForwardedOptions & ForwardedHeaders.XForwardedProto) != 0)
{
var xForwardProtoHeaderValue = context.Request.Headers[XForwardedProtoHeaderName];
if (!string.IsNullOrEmpty(xForwardProtoHeaderValue))
{
if (!string.IsNullOrEmpty(context.Request.Scheme))
{
context.Request.Headers[XOriginalProtoName] = context.Request.Scheme;
}
context.Request.Scheme = xForwardProtoHeaderValue;
}
}
return _next(context);
}
}
}