Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 1f35e54

Browse files
Simplify 404 handling in new SPA proxying code
1 parent 3aa1451 commit 1f35e54

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

src/Microsoft.AspNetCore.SpaServices.Extensions/Proxying/ConditionalProxy.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public static async Task<bool> PerformProxyRequest(
4343
HttpContext context,
4444
HttpClient httpClient,
4545
Task<Uri> baseUriTask,
46-
CancellationToken applicationStoppingToken)
46+
CancellationToken applicationStoppingToken,
47+
bool proxy404s)
4748
{
4849
// Stop proxying if either the server or client wants to disconnect
4950
var proxyCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(
@@ -71,7 +72,18 @@ public static async Task<bool> PerformProxyRequest(
7172
using (var requestMessage = CreateProxyHttpRequest(context, targetUri))
7273
using (var responseMessage = await SendProxyHttpRequest(context, httpClient, requestMessage, proxyCancellationToken))
7374
{
74-
return await CopyProxyHttpResponse(context, responseMessage, proxyCancellationToken);
75+
if (!proxy404s)
76+
{
77+
if (responseMessage.StatusCode == HttpStatusCode.NotFound)
78+
{
79+
// We're not proxying 404s, i.e., we want to resume the middleware pipeline
80+
// and let some other middleware handle this.
81+
return false;
82+
}
83+
}
84+
85+
await CopyProxyHttpResponse(context, responseMessage, proxyCancellationToken);
86+
return true;
7587
}
7688
}
7789
}
@@ -139,15 +151,8 @@ private static Task<HttpResponseMessage> SendProxyHttpRequest(HttpContext contex
139151
return httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
140152
}
141153

142-
private static async Task<bool> CopyProxyHttpResponse(HttpContext context, HttpResponseMessage responseMessage, CancellationToken cancellationToken)
154+
private static async Task CopyProxyHttpResponse(HttpContext context, HttpResponseMessage responseMessage, CancellationToken cancellationToken)
143155
{
144-
if (responseMessage.StatusCode == HttpStatusCode.NotFound)
145-
{
146-
// Let some other middleware handle this
147-
return false;
148-
}
149-
150-
// We can handle this
151156
context.Response.StatusCode = (int)responseMessage.StatusCode;
152157
foreach (var header in responseMessage.Headers)
153158
{
@@ -166,8 +171,6 @@ private static async Task<bool> CopyProxyHttpResponse(HttpContext context, HttpR
166171
{
167172
await responseStream.CopyToAsync(context.Response.Body, StreamCopyBufferSize, cancellationToken);
168173
}
169-
170-
return true;
171174
}
172175

173176
private static Uri ToWebSocketScheme(Uri uri)

src/Microsoft.AspNetCore.SpaServices.Extensions/Proxying/ConditionalProxyMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task Invoke(HttpContext context)
4848
if (context.Request.Path.StartsWithSegments(_pathPrefix) || _pathPrefixIsRoot)
4949
{
5050
var didProxyRequest = await ConditionalProxy.PerformProxyRequest(
51-
context, _httpClient, _baseUriTask, _applicationStoppingToken);
51+
context, _httpClient, _baseUriTask, _applicationStoppingToken, proxy404s: false);
5252
if (didProxyRequest)
5353
{
5454
return;

src/Microsoft.AspNetCore.SpaServices.Extensions/Proxying/SpaProxyingExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,8 @@ public static void UseProxyToSpaDevelopmentServer(
5454
applicationBuilder.Use(async (context, next) =>
5555
{
5656
var didProxyRequest = await ConditionalProxy.PerformProxyRequest(
57-
context, neverTimeOutHttpClient, baseUriTask, applicationStoppingToken);
58-
59-
// Since we are proxying everything, this is the end of the middleware pipeline.
60-
// We won't call next().
61-
if (!didProxyRequest)
62-
{
63-
context.Response.StatusCode = 404;
64-
}
57+
context, neverTimeOutHttpClient, baseUriTask, applicationStoppingToken,
58+
proxy404s: true);
6559
});
6660
}
6761

0 commit comments

Comments
 (0)