|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNet.Builder; |
| 7 | +using Microsoft.AspNet.Http; |
| 8 | + |
| 9 | +namespace Microsoft.AspNet.SpaServices.Webpack |
| 10 | +{ |
| 11 | + // Based on https://github.com/aspnet/Proxy/blob/dev/src/Microsoft.AspNetCore.Proxy/ProxyMiddleware.cs |
| 12 | + // Differs in that, if the proxied request returns a 404, we pass through to the next middleware in the chain |
| 13 | + // This is useful for Webpack middleware, because it lets you fall back on prebuilt files on disk for |
| 14 | + // chunks not exposed by the current Webpack config (e.g., DLL/vendor chunks). |
| 15 | + internal class ConditionalProxyMiddleware { |
| 16 | + private RequestDelegate next; |
| 17 | + private ConditionalProxyMiddlewareOptions options; |
| 18 | + private HttpClient httpClient; |
| 19 | + private string pathPrefix; |
| 20 | + |
| 21 | + public ConditionalProxyMiddleware(RequestDelegate next, string pathPrefix, ConditionalProxyMiddlewareOptions options) |
| 22 | + { |
| 23 | + this.next = next; |
| 24 | + this.pathPrefix = pathPrefix; |
| 25 | + this.options = options; |
| 26 | + this.httpClient = new HttpClient(new HttpClientHandler()); |
| 27 | + } |
| 28 | + |
| 29 | + public async Task Invoke(HttpContext context) |
| 30 | + { |
| 31 | + if (context.Request.Path.StartsWithSegments(this.pathPrefix)) { |
| 32 | + var didProxyRequest = await PerformProxyRequest(context); |
| 33 | + if (didProxyRequest) { |
| 34 | + return; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Not a request we can proxy |
| 39 | + await this.next.Invoke(context); |
| 40 | + } |
| 41 | + |
| 42 | + private async Task<bool> PerformProxyRequest(HttpContext context) { |
| 43 | + var requestMessage = new HttpRequestMessage(); |
| 44 | + |
| 45 | + // Copy the request headers |
| 46 | + foreach (var header in context.Request.Headers) { |
| 47 | + if (!requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()) && requestMessage.Content != null) { |
| 48 | + requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + requestMessage.Headers.Host = options.Host + ":" + options.Port; |
| 53 | + var uriString = $"{options.Scheme}://{options.Host}:{options.Port}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}"; |
| 54 | + requestMessage.RequestUri = new Uri(uriString); |
| 55 | + requestMessage.Method = new HttpMethod(context.Request.Method); |
| 56 | + |
| 57 | + using (var responseMessage = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted)) { |
| 58 | + if (responseMessage.StatusCode == HttpStatusCode.NotFound) { |
| 59 | + // Let some other middleware handle this |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + // We can handle this |
| 64 | + context.Response.StatusCode = (int)responseMessage.StatusCode; |
| 65 | + foreach (var header in responseMessage.Headers) { |
| 66 | + context.Response.Headers[header.Key] = header.Value.ToArray(); |
| 67 | + } |
| 68 | + |
| 69 | + foreach (var header in responseMessage.Content.Headers) { |
| 70 | + context.Response.Headers[header.Key] = header.Value.ToArray(); |
| 71 | + } |
| 72 | + |
| 73 | + // SendAsync removes chunking from the response. This removes the header so it doesn't expect a chunked response. |
| 74 | + context.Response.Headers.Remove("transfer-encoding"); |
| 75 | + await responseMessage.Content.CopyToAsync(context.Response.Body); |
| 76 | + return true; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + internal class ConditionalProxyMiddlewareOptions { |
| 82 | + public string Scheme { get; private set; } |
| 83 | + public string Host { get; private set; } |
| 84 | + public string Port { get; private set; } |
| 85 | + |
| 86 | + public ConditionalProxyMiddlewareOptions(string scheme, string host, string port) { |
| 87 | + this.Scheme = scheme; |
| 88 | + this.Host = host; |
| 89 | + this.Port = port; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments