-
-
Notifications
You must be signed in to change notification settings - Fork 82
Stop Middleware shortcutting when filter is used #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ namespace AspNetCore.Proxy | |
| { | ||
| internal static class HttpExtensions | ||
| { | ||
| internal static async Task ExecuteHttpProxyOperationAsync(this HttpContext context, HttpProxy httpProxy) | ||
| internal static async Task<bool> ExecuteHttpProxyOperationAsync(this HttpContext context, HttpProxy httpProxy) | ||
| { | ||
| var uri = await context.GetEndpointFromComputerAsync(httpProxy.EndpointComputer).ConfigureAwait(false); | ||
| var options = httpProxy.Options; | ||
|
|
@@ -23,9 +23,14 @@ internal static async Task ExecuteHttpProxyOperationAsync(this HttpContext conte | |
| .GetService<IHttpClientFactory>() | ||
| .CreateClient(options?.HttpClientName ?? Helpers.HttpProxyClientName); | ||
|
|
||
| if (options?.Filter != null && !options.Filter(context)) | ||
| { | ||
| return false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably keep this, but I feel like, now that there are two filter options, this may call for an enum to be more clear than a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, I wanted to add it with boolean because it would be the simplest implementation. But the code is much more confusing because of that, an Enum absolutely solves this. I'll try to make a change later today. |
||
| } | ||
|
|
||
| // If `true`, this proxy call has been intercepted. | ||
| if(options?.Intercept != null && await options.Intercept(context).ConfigureAwait(false)) | ||
| return; | ||
| return true; | ||
|
|
||
| if(context.WebSockets.IsWebSocketRequest) | ||
| throw new InvalidOperationException("A WebSocket request cannot be routed as an HTTP proxy operation."); | ||
|
|
@@ -49,6 +54,7 @@ internal static async Task ExecuteHttpProxyOperationAsync(this HttpContext conte | |
| if(options?.AfterReceive != null) | ||
| await options.AfterReceive(context, proxiedResponse).ConfigureAwait(false); | ||
| await context.WriteProxiedHttpResponseAsync(proxiedResponse).ConfigureAwait(false); | ||
|
|
||
| } | ||
| catch (Exception e) | ||
| { | ||
|
|
@@ -59,12 +65,15 @@ internal static async Task ExecuteHttpProxyOperationAsync(this HttpContext conte | |
| // If the failures are not caught, then write a generic response. | ||
| context.Response.StatusCode = 502 /* BAD GATEWAY */; | ||
| await context.Response.WriteAsync($"Request could not be proxied.\n\n{e.Message}\n\n{e.StackTrace}").ConfigureAwait(false); | ||
| return; | ||
|
|
||
| } | ||
| else | ||
| { | ||
| await options.HandleFailure(context, e).ConfigureAwait(false); | ||
| } | ||
|
|
||
| await options.HandleFailure(context, e).ConfigureAwait(false); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static HttpRequestMessage CreateProxiedHttpRequest(this HttpContext context, string uriString, bool shouldAddForwardedHeaders) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to show the throwaway result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I wasn't sure on this one, didn't want to change the API but the user of this API can still choose to ignore the result and it should be their choice, an Enum would be the best output for them