|
7 | 7 | using System.Net.Http; |
8 | 8 | using System.Net.Http.Headers; |
9 | 9 | using System.Runtime.CompilerServices; |
| 10 | +using System.Threading; |
10 | 11 | using System.Threading.Tasks; |
11 | 12 | using Microsoft.AspNetCore.Http; |
12 | 13 | using Microsoft.AspNetCore.Http.Features; |
@@ -53,6 +54,23 @@ private static bool IsBodylessStatusCode(HttpStatusCode statusCode) => |
53 | 54 | _ => false |
54 | 55 | }; |
55 | 56 |
|
| 57 | + /// <summary> |
| 58 | + /// A callback that is invoked prior to sending the proxied request. All HttpRequestMessage fields are |
| 59 | + /// initialized except RequestUri, which will be initialized after the callback if no value is provided. |
| 60 | + /// See <see cref="RequestUtilities.MakeDestinationAddress(string, PathString, QueryString)"/> for constructing a custom request Uri. |
| 61 | + /// The string parameter represents the destination URI prefix that should be used when constructing the RequestUri. |
| 62 | + /// The headers are copied by the base implementation, excluding some protocol headers like HTTP/2 pseudo headers (":authority"). |
| 63 | + /// This method may be overridden to conditionally produce a response, such as for error conditions, and prevent the request from |
| 64 | + /// being proxied. This is indicated by setting the `HttpResponse.StatusCode` to a value other than 200, or calling `HttpResponse.StartAsync()`, |
| 65 | + /// or writing to the `HttpResponse.Body` or `BodyWriter`. |
| 66 | + /// </summary> |
| 67 | + /// <param name="httpContext">The incoming request.</param> |
| 68 | + /// <param name="proxyRequest">The outgoing proxy request.</param> |
| 69 | + /// <param name="destinationPrefix">The uri prefix for the selected destination server which can be used to create the RequestUri.</param> |
| 70 | + /// <param name="cancellationToken">Indicates that the request is being canceled.</param> |
| 71 | + public virtual ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix, CancellationToken cancellationToken) |
| 72 | + => TransformRequestAsync(httpContext, proxyRequest, destinationPrefix); |
| 73 | + |
56 | 74 | /// <summary> |
57 | 75 | /// A callback that is invoked prior to sending the proxied request. All HttpRequestMessage fields are |
58 | 76 | /// initialized except RequestUri, which will be initialized after the callback if no value is provided. |
@@ -126,9 +144,24 @@ public virtual ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequ |
126 | 144 | /// </summary> |
127 | 145 | /// <param name="httpContext">The incoming request.</param> |
128 | 146 | /// <param name="proxyResponse">The response from the destination. This can be null if the destination did not respond.</param> |
| 147 | + /// <param name="cancellationToken">Indicates that the request is being canceled.</param> |
129 | 148 | /// <returns>A bool indicating if the response should be proxied to the client or not. A derived implementation |
130 | 149 | /// that returns false may send an alternate response inline or return control to the caller for it to retry, respond, |
131 | 150 | /// etc.</returns> |
| 151 | + public virtual ValueTask<bool> TransformResponseAsync(HttpContext httpContext, HttpResponseMessage? proxyResponse, CancellationToken cancellationToken) |
| 152 | + => TransformResponseAsync(httpContext, proxyResponse); |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// A callback that is invoked when the proxied response is received. The status code and reason phrase will be copied |
| 156 | + /// to the HttpContext.Response before the callback is invoked, but may still be modified there. The headers will be |
| 157 | + /// copied to HttpContext.Response.Headers by the base implementation, excludes certain protocol headers like |
| 158 | + /// `Transfer-Encoding: chunked`. |
| 159 | + /// </summary> |
| 160 | + /// <param name="httpContext">The incoming request.</param> |
| 161 | + /// <param name="proxyResponse">The response from the destination. This can be null if the destination did not respond.</param> |
| 162 | + /// <returns>A bool indicating if the response should be proxied to the client or not. A derived implementation |
| 163 | + /// that returns false may send an alternate response inline or return control to the caller for it to retry, respond, |
| 164 | + /// etc.</returns> |
132 | 165 | public virtual ValueTask<bool> TransformResponseAsync(HttpContext httpContext, HttpResponseMessage? proxyResponse) |
133 | 166 | { |
134 | 167 | if (proxyResponse is null) |
@@ -171,6 +204,16 @@ public virtual ValueTask<bool> TransformResponseAsync(HttpContext httpContext, H |
171 | 204 | return new ValueTask<bool>(true); |
172 | 205 | } |
173 | 206 |
|
| 207 | + /// <summary> |
| 208 | + /// A callback that is invoked after the response body to modify trailers, if supported. The trailers will be |
| 209 | + /// copied to the HttpContext.Response by the base implementation. |
| 210 | + /// </summary> |
| 211 | + /// <param name="httpContext">The incoming request.</param> |
| 212 | + /// <param name="proxyResponse">The response from the destination.</param> |
| 213 | + /// <param name="cancellationToken">Indicates that the request is being canceled.</param> |
| 214 | + public virtual ValueTask TransformResponseTrailersAsync(HttpContext httpContext, HttpResponseMessage proxyResponse, CancellationToken cancellationToken) |
| 215 | + => TransformResponseTrailersAsync(httpContext, proxyResponse); |
| 216 | + |
174 | 217 | /// <summary> |
175 | 218 | /// A callback that is invoked after the response body to modify trailers, if supported. The trailers will be |
176 | 219 | /// copied to the HttpContext.Response by the base implementation. |
|
0 commit comments