-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseProxyMiddleware.cs
More file actions
170 lines (132 loc) · 5.87 KB
/
ReverseProxyMiddleware.cs
File metadata and controls
170 lines (132 loc) · 5.87 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ReverseProxyApplication
{
public class ReverseProxyMiddleware
{
private static readonly HttpClient _httpClient = new HttpClient();
private readonly RequestDelegate _nextMiddleware;
private string EarthmineURL;
public ReverseProxyMiddleware(RequestDelegate nextMiddleware, IOptions<EarthmineConfig> options)
{
EarthmineURL = options.Value.EarthmineURL;
_nextMiddleware = nextMiddleware;
}
public async Task Invoke(HttpContext context)
{
var targetUri = BuildTargetUri(context.Request);
if (targetUri != null)
{
var targetRequestMessage = CreateTargetMessage(context, targetUri);
using (var responseMessage = await _httpClient.SendAsync(targetRequestMessage, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted))
{
context.Response.StatusCode = (int)responseMessage.StatusCode;
CopyFromTargetResponseHeaders(context, responseMessage);
await ProcessResponseContent(context, responseMessage);
}
return;
}
await _nextMiddleware(context);
}
private async Task ProcessResponseContent(HttpContext context, HttpResponseMessage responseMessage)
{
var content = await responseMessage.Content.ReadAsByteArrayAsync();
if (IsContentOfType(responseMessage, "text/html") || IsContentOfType(responseMessage, "text/javascript"))
{
var stringContent = Encoding.UTF8.GetString(content);
await context.Response.WriteAsync(stringContent, Encoding.UTF8);
} else
{
await context.Response.Body.WriteAsync(content);
}
}
private bool IsContentOfType(HttpResponseMessage responseMessage, string type)
{
var result = false;
if (responseMessage.Content?.Headers?.ContentType != null)
{
result = responseMessage.Content.Headers.ContentType.MediaType == type;
}
return result;
}
private HttpRequestMessage CreateTargetMessage(HttpContext context, Uri targetUri)
{
var requestMessage = new HttpRequestMessage();
CopyFromOriginalRequestContentAndHeaders(context, requestMessage);
requestMessage.RequestUri = targetUri;
requestMessage.Headers.Host = targetUri.Host;
requestMessage.Method = GetMethod(context.Request.Method);
return requestMessage;
}
private void CopyFromOriginalRequestContentAndHeaders(HttpContext context, HttpRequestMessage requestMessage)
{
var requestMethod = context.Request.Method;
if (!HttpMethods.IsGet(requestMethod) &&
!HttpMethods.IsHead(requestMethod) &&
!HttpMethods.IsDelete(requestMethod) &&
!HttpMethods.IsTrace(requestMethod))
{
var streamContent = new StreamContent(context.Request.Body);
requestMessage.Content = streamContent;
}
foreach (var header in context.Request.Headers)
{
requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray());
}
}
private void CopyFromTargetResponseHeaders(HttpContext context, HttpResponseMessage responseMessage)
{
foreach (var header in responseMessage.Headers)
{
context.Response.Headers[header.Key] = header.Value.ToArray();
}
foreach (var header in responseMessage.Content.Headers)
{
context.Response.Headers[header.Key] = header.Value.ToArray();
}
context.Response.Headers.Remove("transfer-encoding");
}
private static HttpMethod GetMethod(string method)
{
if (HttpMethods.IsDelete(method)) return HttpMethod.Delete;
if (HttpMethods.IsGet(method)) return HttpMethod.Get;
if (HttpMethods.IsHead(method)) return HttpMethod.Head;
if (HttpMethods.IsOptions(method)) return HttpMethod.Options;
if (HttpMethods.IsPost(method)) return HttpMethod.Post;
if (HttpMethods.IsPut(method)) return HttpMethod.Put;
if (HttpMethods.IsTrace(method)) return HttpMethod.Trace;
return new HttpMethod(method);
}
private Uri BuildTargetUri(HttpRequest request)
{
Uri targetUri = null;
PathString remainingPath;
if (request.Path.StartsWithSegments("/crossdomain.xml", out remainingPath))
{
targetUri = new Uri(EarthmineURL + "/crossdomain.xml" + remainingPath);
}
if (request.Path.StartsWithSegments("/www", out remainingPath))
{
targetUri = new Uri(EarthmineURL + "/www" + remainingPath + request.QueryString);
}
if (request.Path.StartsWithSegments("/map", out remainingPath))
{
targetUri = new Uri(EarthmineURL + "/map" + remainingPath + request.QueryString);
}
if (request.Path.StartsWithSegments("/service", out remainingPath))
{
targetUri = new Uri(EarthmineURL + "/service" + remainingPath + request.QueryString );
}
return targetUri;
}
}
}