|
2 | 2 |
|
3 | 3 | using Microsoft.AspNetCore.Http; |
4 | 4 | using Yarp.ReverseProxy.Forwarder; |
| 5 | +using HttpVersion = System.Net.HttpVersion; |
5 | 6 |
|
6 | 7 | // ReSharper disable once CheckNamespace |
7 | 8 | namespace BD.WTTS.Services.Implementation; |
@@ -30,6 +31,27 @@ public HttpReverseProxyMiddleware( |
30 | 31 | this.logger = logger; |
31 | 32 | } |
32 | 33 |
|
| 34 | + static ArgumentOutOfRangeException GetUnknownHttpVersionException(string? actualValue, [CallerArgumentExpression(nameof(actualValue))] string? paramName = null) => new( |
| 35 | +$""" |
| 36 | +Version doesn't map to a known HTTP protocol. (Parameter '{paramName}') |
| 37 | +Actual value was {actualValue}. |
| 38 | +"""); |
| 39 | + |
| 40 | + static Version GetHttpVersion(string requestProtocol) => |
| 41 | + (requestProtocol != null && requestProtocol.Length >= 6) ? requestProtocol[5] switch |
| 42 | + { |
| 43 | + // 参考 Microsoft.AspNetCore.Http.HttpProtocol.GetHttpProtocol |
| 44 | + '1' => requestProtocol.Length >= 8 ? requestProtocol[7] switch |
| 45 | + { |
| 46 | + '0' => HttpVersion.Version10, |
| 47 | + '1' => HttpVersion.Version11, |
| 48 | + _ => throw GetUnknownHttpVersionException(requestProtocol), |
| 49 | + } : throw GetUnknownHttpVersionException(requestProtocol), |
| 50 | + '2' => HttpVersion.Version20, |
| 51 | + '3' => HttpVersion.Version30, |
| 52 | + _ => throw GetUnknownHttpVersionException(requestProtocol), |
| 53 | + } : throw GetUnknownHttpVersionException(requestProtocol); |
| 54 | + |
33 | 55 | /// <summary> |
34 | 56 | /// 处理请求 |
35 | 57 | /// </summary> |
@@ -60,12 +82,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
60 | 82 | var destinationPrefix = GetDestinationPrefix(context.Request.Scheme, context.Request.Host, null); |
61 | 83 | var forwarderRequestConfig = new ForwarderRequestConfig() |
62 | 84 | { |
63 | | - Version = context.Request.Protocol switch |
64 | | - { |
65 | | - var protocol when protocol.StartsWith("HTTP/2") => System.Net.HttpVersion.Version20, |
66 | | - var protocol when protocol.StartsWith("HTTP/3") => System.Net.HttpVersion.Version30, |
67 | | - _ => System.Net.HttpVersion.Version11, |
68 | | - }, |
| 85 | + Version = GetHttpVersion(context.Request.Protocol), |
69 | 86 | }; |
70 | 87 | var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, forwarderRequestConfig, HttpTransformer.Empty); |
71 | 88 | if (error != ForwarderError.None) |
@@ -119,19 +136,23 @@ var protocol when protocol.StartsWith("HTTP/3") => System.Net.HttpVersion.Versio |
119 | 136 | context.Request.Headers.UserAgent = domainConfig.UserAgent.Replace("${origin}", context.Request.Headers.UserAgent, StringComparison.OrdinalIgnoreCase); |
120 | 137 | } |
121 | 138 |
|
122 | | - var forwarderRequestConfig = new ForwarderRequestConfig() |
123 | | - { |
124 | | - Version = context.Request.Protocol switch |
125 | | - { |
126 | | - var protocol when protocol.StartsWith("HTTP/2") => System.Net.HttpVersion.Version20, |
127 | | - var protocol when protocol.StartsWith("HTTP/3") => System.Net.HttpVersion.Version30, |
128 | | - _ => System.Net.HttpVersion.Version11, |
129 | | - }, |
130 | | - }; |
| 139 | + ForwarderRequestConfig forwarderRequestConfig; |
131 | 140 |
|
132 | 141 | if (domainConfig.IsServerSideProxy) |
133 | 142 | { |
134 | 143 | SetWattHeaders(context, reverseProxyConfig.Service.ServerSideProxyToken); |
| 144 | + forwarderRequestConfig = new ForwarderRequestConfig() |
| 145 | + { |
| 146 | + Version = GetHttpVersion(context.Request.Protocol), |
| 147 | + VersionPolicy = HttpVersionPolicy.RequestVersionOrHigher |
| 148 | + }; |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + forwarderRequestConfig = new ForwarderRequestConfig() |
| 153 | + { |
| 154 | + Version = GetHttpVersion(context.Request.Protocol), |
| 155 | + }; |
135 | 156 | } |
136 | 157 |
|
137 | 158 | var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, forwarderRequestConfig, HttpTransformer.Empty); |
|
0 commit comments