-
Notifications
You must be signed in to change notification settings - Fork 838
/
HttpForwarder.cs
1086 lines (963 loc) · 54 KB
/
HttpForwarder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Yarp.ReverseProxy.Utilities;
namespace Yarp.ReverseProxy.Forwarder;
/// <summary>
/// Default implementation of <see cref="IHttpForwarder"/>.
/// </summary>
internal sealed class HttpForwarder : IHttpForwarder
{
private static readonly string WebSocketName = "websocket";
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(100);
private static readonly Version DefaultVersion = HttpVersion.Version20;
private static readonly HttpVersionPolicy DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
private readonly ILogger _logger;
private readonly IClock _clock;
public HttpForwarder(ILogger<HttpForwarder> logger, IClock clock)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
}
/// <summary>
/// Proxies the incoming request to the destination server, and the response back to the client.
/// </summary>
/// <remarks>
/// In what follows, as well as throughout in Reverse Proxy, we consider
/// the following picture as illustrative of the Proxy.
/// <code>
/// +-------------------+
/// | Destination +
/// +-------------------+
/// ▲ |
/// (b) | | (c)
/// | ▼
/// +-------------------+
/// | Proxy +
/// +-------------------+
/// ▲ |
/// (a) | | (d)
/// | ▼
/// +-------------------+
/// | Client +
/// +-------------------+
/// </code>
///
/// (a) and (b) show the *request* path, going from the client to the target.
/// (c) and (d) show the *response* path, going from the destination back to the client.
///
/// Normal proxying comprises the following steps:
/// (0) Disable ASP .NET Core limits for streaming requests
/// (1) Create outgoing HttpRequestMessage
/// (2) Setup copy of request body (background) Client --► Proxy --► Destination
/// (3) Copy request headers Client --► Proxy --► Destination
/// (4) Send the outgoing request using HttpMessageInvoker Client --► Proxy --► Destination
/// (5) Copy response status line Client ◄-- Proxy ◄-- Destination
/// (6) Copy response headers Client ◄-- Proxy ◄-- Destination
/// (7-A) Check for a 101 upgrade response, this takes care of WebSockets as well as any other upgradeable protocol.
/// (7-A-1) Upgrade client channel Client ◄--- Proxy ◄--- Destination
/// (7-A-2) Copy duplex streams and return Client ◄--► Proxy ◄--► Destination
/// (7-B) Copy (normal) response body Client ◄-- Proxy ◄-- Destination
/// (8) Copy response trailer headers and finish response Client ◄-- Proxy ◄-- Destination
/// (9) Wait for completion of step 2: copying request body Client --► Proxy --► Destination
///
/// ASP .NET Core (Kestrel) will finally send response trailers (if any)
/// after we complete the steps above and relinquish control.
/// </remarks>
public ValueTask<ForwarderError> SendAsync(
HttpContext context,
string destinationPrefix,
HttpMessageInvoker httpClient,
ForwarderRequestConfig requestConfig,
HttpTransformer transformer)
=> SendAsync(context, destinationPrefix, httpClient, requestConfig, transformer, CancellationToken.None);
public async ValueTask<ForwarderError> SendAsync(
HttpContext context,
string destinationPrefix,
HttpMessageInvoker httpClient,
ForwarderRequestConfig requestConfig,
HttpTransformer transformer,
CancellationToken cancellationToken)
{
_ = context ?? throw new ArgumentNullException(nameof(context));
_ = destinationPrefix ?? throw new ArgumentNullException(nameof(destinationPrefix));
_ = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_ = requestConfig ?? throw new ArgumentNullException(nameof(requestConfig));
_ = transformer ?? throw new ArgumentNullException(nameof(transformer));
if (RequestUtilities.IsResponseSet(context.Response))
{
throw new InvalidOperationException("The request cannot be forwarded, the response has already started");
}
// HttpClient overload for SendAsync changes response behavior to fully buffered which impacts performance
// See discussion in https://github.com/microsoft/reverse-proxy/issues/458
if (httpClient is HttpClient)
{
throw new ArgumentException($"The http client must be of type HttpMessageInvoker, not HttpClient", nameof(httpClient));
}
// "http://a".Length = 8
if (destinationPrefix is null || destinationPrefix.Length < 8)
{
throw new ArgumentException("Invalid destination prefix.", nameof(destinationPrefix));
}
ForwarderTelemetry.Log.ForwarderStart(destinationPrefix);
var activityCancellationSource = ActivityCancellationTokenSource.Rent(requestConfig?.ActivityTimeout ?? DefaultTimeout, context.RequestAborted, cancellationToken);
try
{
var isClientHttp2OrGreater = ProtocolHelper.IsHttp2OrGreater(context.Request.Protocol);
// NOTE: We heuristically assume gRPC-looking requests may require streaming semantics.
// See https://github.com/microsoft/reverse-proxy/issues/118 for design discussion.
var isStreamingRequest = isClientHttp2OrGreater && ProtocolHelper.IsGrpcContentType(context.Request.ContentType);
HttpRequestMessage? destinationRequest = null;
StreamCopyHttpContent? requestContent = null;
HttpResponseMessage destinationResponse;
try
{
// :: Step 1-3: Create outgoing HttpRequestMessage
bool tryDowngradingH2WsOnFailure;
(destinationRequest, requestContent, tryDowngradingH2WsOnFailure) = await CreateRequestMessageAsync(
context, destinationPrefix, transformer, requestConfig, isStreamingRequest, activityCancellationSource);
// Transforms generated a response, do not proxy.
if (RequestUtilities.IsResponseSet(context.Response))
{
Log.NotProxying(_logger, context.Response.StatusCode);
return ForwarderError.None;
}
Log.Proxying(_logger, destinationRequest, isStreamingRequest);
// :: Step 4: Send the outgoing request using HttpClient
ForwarderTelemetry.Log.ForwarderStage(ForwarderStage.SendAsyncStart);
try
{
destinationResponse = await httpClient.SendAsync(destinationRequest, activityCancellationSource.Token);
}
catch (HttpRequestException hre) when (tryDowngradingH2WsOnFailure)
{
Debug.Assert(requestContent is null);
// This is how SocketsHttpHandler communicates to us that we tried a HTTP/2 extension that wasn't
// enabled by the server. We should retry on HTTP/1.1.
if (hre.Data.Contains("SETTINGS_ENABLE_CONNECT_PROTOCOL"))
{
Debug.Assert(false == (bool?)hre.Data["SETTINGS_ENABLE_CONNECT_PROTOCOL"]);
Log.RetryingWebSocketDowngradeNoConnect(_logger);
}
// This is how SocketsHttpHandler communicates to us that we tried HTTP/2, but the server only supports
// HTTP/1.x (as determined by ALPN). We'll only get this when using TLS/https. Retry on HTTP/1.1.
// We don't let SocketsHttpHandler downgrade automatically for us because we need to send different headers.
else if (hre.Data.Contains("HTTP2_ENABLED"))
{
Debug.Assert(false == (bool?)hre.Data["HTTP2_ENABLED"]);
Log.RetryingWebSocketDowngradeNoHttp2(_logger);
}
else
{
throw;
}
// Trying again
activityCancellationSource.ResetTimeout();
var config = requestConfig! with
{
Version = HttpVersion.Version11,
VersionPolicy = HttpVersionPolicy.RequestVersionExact
};
// Set the request back to null while we call into CreateRequestMessageAsync so that
// potential exceptions are correctly treated as 'RequestCreation'.
destinationRequest = null;
(destinationRequest, requestContent, _) = await CreateRequestMessageAsync(
context, destinationPrefix, transformer, config, isStreamingRequest, activityCancellationSource);
destinationResponse = await httpClient.SendAsync(destinationRequest, activityCancellationSource.Token);
}
}
catch (Exception requestException)
{
return await HandleRequestFailureAsync(context, requestContent, requestException, transformer, activityCancellationSource,
failedDuringRequestCreation: destinationRequest is null);
}
ForwarderTelemetry.Log.ForwarderStage(ForwarderStage.SendAsyncStop);
// Reset the timeout since we received the response headers.
activityCancellationSource.ResetTimeout();
Log.ResponseReceived(_logger, destinationResponse);
try
{
// :: Step 5: Copy response status line Client ◄-- Proxy ◄-- Destination
// :: Step 6: Copy response headers Client ◄-- Proxy ◄-- Destination
var copyBody = await CopyResponseStatusAndHeadersAsync(destinationResponse, context, transformer, activityCancellationSource.Token);
if (!copyBody)
{
// The transforms callback decided that the response body should be discarded.
destinationResponse.Dispose();
if (requestContent is not null && requestContent.InProgress)
{
activityCancellationSource.Cancel();
await requestContent.ConsumptionTask;
}
return ForwarderError.None;
}
}
catch (Exception ex)
{
destinationResponse.Dispose();
if (requestContent is not null && requestContent.InProgress)
{
activityCancellationSource.Cancel();
await requestContent.ConsumptionTask;
}
ReportProxyError(context, ForwarderError.ResponseHeaders, ex);
// Clear the response since status code, reason and some headers might have already been copied and we want clean 502 response.
context.Response.Clear();
context.Response.StatusCode = StatusCodes.Status502BadGateway;
return ForwarderError.ResponseHeaders;
}
// :: Step 7-A: Check for a 101 upgrade response, this takes care of WebSockets as well as any other upgradeable protocol.
// Also check for HTTP/2 CONNECT 200 responses, they function similarly.
if (destinationResponse.StatusCode == HttpStatusCode.SwitchingProtocols
#if NET7_0_OR_GREATER
|| (destinationResponse.StatusCode == HttpStatusCode.OK
&& destinationResponse.Version == HttpVersion.Version20
&& destinationRequest.Headers.Protocol is not null
&& destinationRequest.Method.Equals(HttpMethod.Connect))
#endif
)
{
Debug.Assert(requestContent?.Started != true);
return await HandleUpgradedResponse(context, destinationResponse, activityCancellationSource);
}
// NOTE: it may *seem* wise to call `context.Response.StartAsync()` at this point
// since it looks like we are ready to send back response headers
// (and this might help reduce extra delays while we wait to receive the body from the destination).
// HOWEVER, this would produce the wrong result if it turns out that there is no content
// from the destination -- instead of sending headers and terminating the stream at once,
// we would send headers thinking a body may be coming, and there is none.
// This is problematic on gRPC connections when the destination server encounters an error,
// in which case it immediately returns the response headers and trailing headers, but no content,
// and clients misbehave if the initial headers response does not indicate stream end.
// :: Step 7-B: Copy response body Client ◄-- Proxy ◄-- Destination
var (responseBodyCopyResult, responseBodyException) = await CopyResponseBodyAsync(destinationResponse.Content, context.Response.Body, activityCancellationSource);
if (responseBodyCopyResult != StreamCopyResult.Success)
{
return await HandleResponseBodyErrorAsync(context, requestContent, responseBodyCopyResult, responseBodyException!, activityCancellationSource);
}
// :: Step 8: Copy response trailer headers and finish response Client ◄-- Proxy ◄-- Destination
await CopyResponseTrailingHeadersAsync(destinationResponse, context, transformer, activityCancellationSource.Token);
if (isStreamingRequest)
{
// NOTE: We must call `CompleteAsync` so that Kestrel will flush all bytes to the client.
// In the case where there was no response body,
// this is also when headers and trailing headers are sent to the client.
// Without this, the client might wait forever waiting for response bytes,
// while we might wait forever waiting for request bytes,
// leading to a stuck connection and no way to make progress.
await context.Response.CompleteAsync();
}
// :: Step 9: Wait for completion of step 2: copying request body Client --► Proxy --► Destination
// NOTE: It is possible for the request body to NOT be copied even when there was an incoming requet body,
// e.g. when the request includes header `Expect: 100-continue` and the destination produced a non-1xx response.
// We must only wait for the request body to complete if it actually started,
// otherwise we run the risk of waiting indefinitely for a task that will never complete.
if (requestContent is not null && requestContent.Started)
{
var (requestBodyCopyResult, requestBodyException) = await requestContent.ConsumptionTask;
if (requestBodyCopyResult != StreamCopyResult.Success)
{
// The response succeeded. If there was a request body error then it was probably because the client or destination decided
// to cancel it. Report as low severity.
var error = requestBodyCopyResult switch
{
StreamCopyResult.InputError => ForwarderError.RequestBodyClient,
StreamCopyResult.OutputError => ForwarderError.RequestBodyDestination,
StreamCopyResult.Canceled => ForwarderError.RequestBodyCanceled,
_ => throw new NotImplementedException(requestBodyCopyResult.ToString())
};
ReportProxyError(context, error, requestBodyException!);
return error;
}
}
}
finally
{
activityCancellationSource.Return();
ForwarderTelemetry.Log.ForwarderStop(context.Response.StatusCode);
}
return ForwarderError.None;
}
private async ValueTask<(HttpRequestMessage, StreamCopyHttpContent?, bool)> CreateRequestMessageAsync(HttpContext context, string destinationPrefix,
HttpTransformer transformer, ForwarderRequestConfig? requestConfig, bool isStreamingRequest, ActivityCancellationTokenSource activityToken)
{
var destinationRequest = new HttpRequestMessage();
var upgradeFeature = context.Features.Get<IHttpUpgradeFeature>();
var upgradeHeader = context.Request.Headers[HeaderNames.Upgrade].ToString();
var isSpdyRequest = (upgradeFeature?.IsUpgradableRequest ?? false)
&& upgradeHeader.StartsWith("SPDY/", StringComparison.OrdinalIgnoreCase);
var isH1WsRequest = (upgradeFeature?.IsUpgradableRequest ?? false)
&& string.Equals(WebSocketName, upgradeHeader, StringComparison.OrdinalIgnoreCase);
var incomingUpgrade = isSpdyRequest || isH1WsRequest;
var isH2WsRequest = false;
#if NET7_0_OR_GREATER
var connectFeature = context.Features.Get<IHttpExtendedConnectFeature>();
var connectProtocol = connectFeature?.Protocol;
isH2WsRequest = (connectFeature?.IsExtendedConnect ?? false)
&& string.Equals(WebSocketName, connectProtocol, StringComparison.OrdinalIgnoreCase);
#endif
var outgoingHttps = destinationPrefix.StartsWith("https://");
var outgoingVersion = requestConfig?.Version ?? DefaultVersion;
var outgoingPolicy = requestConfig?.VersionPolicy ?? DefaultVersionPolicy;
var outgoingUpgrade = false;
var outgoingConnect = false;
var tryDowngradingH2WsOnFailure = false;
if (isSpdyRequest)
{
// Can only be done on HTTP/1.1, force regardless of options.
outgoingUpgrade = true;
}
else if (isH1WsRequest || isH2WsRequest)
{
switch (outgoingVersion.Major, outgoingPolicy, outgoingHttps)
{
#if NET7_0_OR_GREATER
case (2, HttpVersionPolicy.RequestVersionExact, _):
case (2, HttpVersionPolicy.RequestVersionOrHigher, true):
outgoingConnect = true;
break;
case (1, HttpVersionPolicy.RequestVersionOrHigher, true):
case (2, HttpVersionPolicy.RequestVersionOrLower, true):
case (3, HttpVersionPolicy.RequestVersionOrLower, true):
// Try H2WS, downgrade if needed.
outgoingConnect = true;
tryDowngradingH2WsOnFailure = true;
break;
#endif
// 1.x Lower or Exact, regardless of HTTPS
// Anything else without HTTPS except 2 Exact
default:
// Override to use HTTP/1.1, nothing else is supported.
outgoingUpgrade = true;
break;
}
}
if (outgoingUpgrade)
{
// Can only be done on HTTP/1.1, force regardless of options.
destinationRequest.Version = HttpVersion.Version11;
destinationRequest.VersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
destinationRequest.Method = HttpMethod.Get;
}
#if NET7_0_OR_GREATER
else if (outgoingConnect)
{
// HTTP/2 only (for now).
destinationRequest.Version = HttpVersion.Version20;
destinationRequest.VersionPolicy = HttpVersionPolicy.RequestVersionExact;
destinationRequest.Method = HttpMethod.Connect;
destinationRequest.Headers.Protocol = connectProtocol ?? WebSocketName;
}
#endif
else
{
destinationRequest.Method = RequestUtilities.GetHttpMethod(context.Request.Method);
destinationRequest.Version = outgoingVersion;
destinationRequest.VersionPolicy = outgoingPolicy;
}
// :: Step 2: Setup copy of request body (background) Client --► Proxy --► Destination
// Note that we must do this before step (3) because step (3) may also add headers to the HttpContent that we set up here.
var requestContent = SetupRequestBodyCopy(context.Request, isStreamingRequest, activityToken);
destinationRequest.Content = requestContent;
// :: Step 3: Copy request headers Client --► Proxy --► Destination
await transformer.TransformRequestAsync(context, destinationRequest, destinationPrefix, activityToken.Token);
// The transformer generated a response, do not forward.
if (RequestUtilities.IsResponseSet(context.Response))
{
return (destinationRequest, requestContent, false);
}
// Transforms may have taken a while, especially if they buffered the body, they count as forward progress.
activityToken.ResetTimeout();
FixupUpgradeRequestHeaders(context, destinationRequest, outgoingUpgrade, outgoingConnect);
// Allow someone to custom build the request uri, otherwise provide a default for them.
var request = context.Request;
destinationRequest.RequestUri ??= RequestUtilities.MakeDestinationAddress(destinationPrefix, request.Path, request.QueryString);
if (requestConfig?.AllowResponseBuffering != true)
{
context.Features.Get<IHttpResponseBodyFeature>()?.DisableBuffering();
}
// TODO: What if they replace the HttpContent object? That would mess with our tracking and error handling.
return (destinationRequest, requestContent, tryDowngradingH2WsOnFailure);
}
// Connection and Upgrade headers were not copied with the rest of the headers.
private void FixupUpgradeRequestHeaders(HttpContext context, HttpRequestMessage request, bool outgoingUpgrade, bool outgoingConnect)
{
if (outgoingUpgrade)
{
// H2->H1, add Connection, Upgrade, Sec-WebSocket-Key
if (HttpProtocol.IsHttp2(context.Request.Protocol))
{
request.Headers.TryAddWithoutValidation(HeaderNames.Connection, HeaderNames.Upgrade);
request.Headers.TryAddWithoutValidation(HeaderNames.Upgrade, WebSocketName);
var key = ProtocolHelper.CreateSecWebSocketKey();
request.Headers.TryAddWithoutValidation(HeaderNames.SecWebSocketKey, key);
}
// H1->H1, re-add the original Connection, Upgrade headers.
else
{
var connectionValues = context.Request.Headers.GetCommaSeparatedValues(HeaderNames.Connection);
string? connectionUpgradeValue = null;
foreach (var headerValue in connectionValues)
{
if (headerValue.Equals(HeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase))
{
// Preserve original value, case
connectionUpgradeValue = headerValue;
break;
}
}
if (connectionUpgradeValue is not null && context.Request.Headers.TryGetValue(HeaderNames.Upgrade, out var upgradeValue))
{
request.Headers.TryAddWithoutValidation(HeaderNames.Connection, connectionUpgradeValue);
request.Headers.TryAddWithoutValidation(HeaderNames.Upgrade, (IEnumerable<string>)upgradeValue);
}
}
}
// H1->H2, remove Sec-WebSocket-Key
else if (outgoingConnect && !HttpProtocol.IsHttp2(context.Request.Protocol))
{
var key = context.Request.Headers[HeaderNames.SecWebSocketKey];
if (!ProtocolHelper.CheckSecWebSocketKey(key))
{
Log.InvalidSecWebSocketKeyHeader(_logger, key);
// The request will not be forwarded if we change the status code.
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
request.Headers.Remove(HeaderNames.SecWebSocketKey);
}
// else not an upgrade, or H2->H2, no changes needed
}
private StreamCopyHttpContent? SetupRequestBodyCopy(HttpRequest request, bool isStreamingRequest, ActivityCancellationTokenSource activityToken)
{
// If we generate an HttpContent without a Content-Length then for HTTP/1.1 HttpClient will add a Transfer-Encoding: chunked header
// even if it's a GET request. Some servers reject requests containing a Transfer-Encoding header if they're not expecting a body.
// Try to be as specific as possible about the client's intent to send a body. The one thing we don't want to do is to start
// reading the body early because that has side-effects like 100-continue.
var hasBody = true;
var contentLength = request.Headers.ContentLength;
var method = request.Method;
var canHaveBodyFeature = request.HttpContext.Features.Get<IHttpRequestBodyDetectionFeature>();
if (canHaveBodyFeature is not null)
{
// 5.0 servers provide a definitive answer for us.
hasBody = canHaveBodyFeature.CanHaveBody;
// TODO: Kestrel bug, this shouldn't be true for ExtendedConnect.
#if NET7_0_OR_GREATER
var connectFeature = request.HttpContext.Features.Get<IHttpExtendedConnectFeature>();
if (connectFeature?.Protocol != null)
{
hasBody = false;
}
#endif
}
// https://tools.ietf.org/html/rfc7230#section-3.3.3
// All HTTP/1.1 requests should have Transfer-Encoding or Content-Length.
// Http.Sys/IIS will even add a Transfer-Encoding header to HTTP/2 requests with bodies for back-compat.
// HTTP/1.0 Connection: close bodies are only allowed on responses, not requests.
// https://tools.ietf.org/html/rfc1945#section-7.2.2
//
// Transfer-Encoding overrides Content-Length per spec
else if (request.Headers.TryGetValue(HeaderNames.TransferEncoding, out var transferEncoding)
&& transferEncoding.Count == 1
&& string.Equals("chunked", transferEncoding.ToString(), StringComparison.OrdinalIgnoreCase))
{
hasBody = true;
}
else if (contentLength.HasValue)
{
hasBody = contentLength > 0;
}
// Kestrel HTTP/2: There are no required headers that indicate if there is a request body so we need to sniff other fields.
else if (!ProtocolHelper.IsHttp2OrGreater(request.Protocol))
{
hasBody = false;
}
// https://tools.ietf.org/html/rfc7231#section-4.3.1
// A payload within a GET/HEAD/DELETE/CONNECT request message has no defined semantics; sending a payload body on a
// GET/HEAD/DELETE/CONNECT request might cause some existing implementations to reject the request.
// https://tools.ietf.org/html/rfc7231#section-4.3.8
// A client MUST NOT send a message body in a TRACE request.
else if (HttpMethods.IsGet(method)
|| HttpMethods.IsHead(method)
|| HttpMethods.IsDelete(method)
|| HttpMethods.IsConnect(method)
|| HttpMethods.IsTrace(method))
{
hasBody = false;
}
// else hasBody defaults to true
if (hasBody)
{
if (isStreamingRequest)
{
DisableMinRequestBodyDataRateAndMaxRequestBodySize(request.HttpContext);
}
// Note on `autoFlushHttpClientOutgoingStream: isStreamingRequest`:
// The.NET Core HttpClient stack keeps its own buffers on top of the underlying outgoing connection socket.
// We flush those buffers down to the socket on every write when this is set,
// but it does NOT result in calls to flush on the underlying socket.
// This is necessary because we proxy http2 transparently,
// and we are deliberately unaware of packet structure used e.g. in gRPC duplex channels.
// Because the sockets aren't flushed, the perf impact of this choice is expected to be small.
// Future: It may be wise to set this to true for *all* http2 incoming requests,
// but for now, out of an abundance of caution, we only do it for requests that look like gRPC.
return new StreamCopyHttpContent(
request: request,
autoFlushHttpClientOutgoingStream: isStreamingRequest,
clock: _clock,
activityToken);
}
return null;
}
private ForwarderError HandleRequestBodyFailure(HttpContext context, StreamCopyResult requestBodyCopyResult, Exception requestBodyException, Exception additionalException)
{
ForwarderError requestBodyError;
int statusCode;
switch (requestBodyCopyResult)
{
// Failed while trying to copy the request body from the client. It's ambiguous if the request or response failed first.
case StreamCopyResult.InputError:
requestBodyError = ForwarderError.RequestBodyClient;
statusCode = StatusCodes.Status400BadRequest;
break;
// Failed while trying to copy the request body to the destination. It's ambiguous if the request or response failed first.
case StreamCopyResult.OutputError:
requestBodyError = ForwarderError.RequestBodyDestination;
statusCode = StatusCodes.Status502BadGateway;
break;
// Canceled while trying to copy the request body, either due to a client disconnect or a timeout. This probably caused the response to fail as a secondary error.
case StreamCopyResult.Canceled:
requestBodyError = ForwarderError.RequestBodyCanceled;
// Timeouts (504s) are handled at the SendAsync call site.
// The request body should only be canceled by the RequestAborted token.
statusCode = StatusCodes.Status502BadGateway;
break;
default:
throw new NotImplementedException(requestBodyCopyResult.ToString());
}
ReportProxyError(context, requestBodyError, new AggregateException(requestBodyException, additionalException));
// We don't know if the client is still around to see this error, but set it for diagnostics to see.
if (!context.Response.HasStarted)
{
// Nothing has been sent to the client yet, we can still send a good error response.
context.Response.Clear();
context.Response.StatusCode = statusCode;
return requestBodyError;
}
ResetOrAbort(context, isCancelled: requestBodyCopyResult == StreamCopyResult.Canceled);
return requestBodyError;
}
private async ValueTask<ForwarderError> HandleRequestFailureAsync(HttpContext context, StreamCopyHttpContent? requestContent, Exception requestException,
HttpTransformer transformer, ActivityCancellationTokenSource requestCancellationSource, bool failedDuringRequestCreation)
{
if (requestException is OperationCanceledException)
{
if (requestCancellationSource.CancelledByLinkedToken)
{
// Either the client went away (HttpContext.RequestAborted) or the CancellationToken provided to SendAsync was signaled.
return await ReportErrorAsync(ForwarderError.RequestCanceled, StatusCodes.Status502BadGateway);
}
else
{
Debug.Assert(requestCancellationSource.IsCancellationRequested || requestException.ToString().Contains("ConnectTimeout"), requestException.ToString());
return await ReportErrorAsync(ForwarderError.RequestTimedOut, StatusCodes.Status504GatewayTimeout);
}
}
// Check for request body errors, these may have triggered the response error.
if (requestContent?.ConsumptionTask.IsCompleted == true)
{
var (requestBodyCopyResult, requestBodyException) = requestContent.ConsumptionTask.Result;
if (requestBodyCopyResult != StreamCopyResult.Success)
{
var error = HandleRequestBodyFailure(context, requestBodyCopyResult, requestBodyException!, requestException);
await transformer.TransformResponseAsync(context, proxyResponse: null, requestCancellationSource.Token);
return error;
}
}
// We couldn't communicate with the destination.
return await ReportErrorAsync(failedDuringRequestCreation ? ForwarderError.RequestCreation : ForwarderError.Request, StatusCodes.Status502BadGateway);
async ValueTask<ForwarderError> ReportErrorAsync(ForwarderError error, int statusCode)
{
ReportProxyError(context, error, requestException);
context.Response.StatusCode = statusCode;
if (requestContent is not null && requestContent.InProgress)
{
requestCancellationSource.Cancel();
await requestContent.ConsumptionTask;
}
await transformer.TransformResponseAsync(context, null, requestCancellationSource.Token);
return error;
}
}
private static ValueTask<bool> CopyResponseStatusAndHeadersAsync(HttpResponseMessage source, HttpContext context, HttpTransformer transformer, CancellationToken cancellationToken)
{
context.Response.StatusCode = (int)source.StatusCode;
if (!ProtocolHelper.IsHttp2OrGreater(context.Request.Protocol))
{
// Don't explicitly set the field if the default reason phrase is used
if (source.ReasonPhrase != ReasonPhrases.GetReasonPhrase((int)source.StatusCode))
{
context.Features.Get<IHttpResponseFeature>()!.ReasonPhrase = source.ReasonPhrase;
}
}
// Copies headers
return transformer.TransformResponseAsync(context, source, cancellationToken);
}
private async ValueTask<ForwarderError> HandleUpgradedResponse(HttpContext context, HttpResponseMessage destinationResponse,
ActivityCancellationTokenSource activityCancellationSource)
{
ForwarderTelemetry.Log.ForwarderStage(ForwarderStage.ResponseUpgrade);
var isHttp2Request = HttpProtocol.IsHttp2(context.Request.Protocol);
var headerError = FixupUpgradeResponseHeaders(context, destinationResponse, isHttp2Request);
if (headerError != ForwarderError.None)
{
destinationResponse.Dispose();
return headerError;
}
// :: Step 7-A-1: Upgrade the client channel. This will also send response headers.
Stream upgradeResult;
try
{
#if NET7_0_OR_GREATER
if (isHttp2Request)
{
var connectFeature = context.Features.Get<IHttpExtendedConnectFeature>();
Debug.Assert(connectFeature != null);
upgradeResult = await connectFeature.AcceptAsync();
}
else
#endif
{
var upgradeFeature = context.Features.Get<IHttpUpgradeFeature>();
Debug.Assert(upgradeFeature != null);
upgradeResult = await upgradeFeature.UpgradeAsync();
}
}
catch (Exception ex)
{
destinationResponse.Dispose();
ReportProxyError(context, ForwarderError.UpgradeResponseClient, ex);
return ForwarderError.UpgradeResponseClient;
}
using var clientStream = upgradeResult;
// :: Step 7-A-2: Copy duplex streams
using var destinationStream = await destinationResponse.Content.ReadAsStreamAsync(activityCancellationSource.Token);
var requestTask = StreamCopier.CopyAsync(isRequest: true, clientStream, destinationStream, StreamCopier.UnknownLength, _clock, activityCancellationSource,
// HTTP/2 HttpClient request streams buffer by default.
autoFlush: destinationResponse.Version == HttpVersion.Version20, activityCancellationSource.Token).AsTask();
var responseTask = StreamCopier.CopyAsync(isRequest: false, destinationStream, clientStream, StreamCopier.UnknownLength, _clock, activityCancellationSource, activityCancellationSource.Token).AsTask();
// Make sure we report the first failure.
var firstTask = await Task.WhenAny(requestTask, responseTask);
var requestFinishedFirst = firstTask == requestTask;
var secondTask = requestFinishedFirst ? responseTask : requestTask;
ForwarderError error;
var (firstResult, firstException) = await firstTask;
if (firstResult != StreamCopyResult.Success)
{
error = ReportResult(context, requestFinishedFirst, firstResult, firstException);
// Cancel the other direction
activityCancellationSource.Cancel();
// Wait for this to finish before exiting so the resources get cleaned up properly.
await secondTask;
}
else
{
var cancelReads = !requestFinishedFirst && !secondTask.IsCompleted;
if (cancelReads)
{
// The response is finished, unblock the incoming reads
activityCancellationSource.Cancel();
}
var (secondResult, secondException) = await secondTask;
if (!cancelReads && secondResult != StreamCopyResult.Success)
{
error = ReportResult(context, !requestFinishedFirst, secondResult, secondException!);
}
else
{
error = ForwarderError.None;
}
}
return error;
ForwarderError ReportResult(HttpContext context, bool request, StreamCopyResult result, Exception exception)
{
var error = result switch
{
StreamCopyResult.InputError => request ? ForwarderError.UpgradeRequestClient : ForwarderError.UpgradeResponseDestination,
StreamCopyResult.OutputError => request ? ForwarderError.UpgradeRequestDestination : ForwarderError.UpgradeResponseClient,
StreamCopyResult.Canceled => request ? ForwarderError.UpgradeRequestCanceled : ForwarderError.UpgradeResponseCanceled,
_ => throw new NotImplementedException(result.ToString()),
};
ReportProxyError(context, error, exception);
return error;
}
}
// The Connection and Upgrade headers were not copied by default
private ForwarderError FixupUpgradeResponseHeaders(HttpContext context, HttpResponseMessage response, bool isHttp2Request)
{
if (isHttp2Request)
{
// H2 <- H1 Validate & remove the Sec-WebSocket-Accept header.
if (response.Version != HttpVersion.Version20)
{
var success = RequestUtilities.TryGetValues(response.RequestMessage!.Headers, HeaderNames.SecWebSocketKey, out var key);
Debug.Assert(success);
var accept = context.Response.Headers[HeaderNames.SecWebSocketAccept];
var expectedAccept = ProtocolHelper.CreateSecWebSocketAccept(key.ToString());
if (!string.Equals(expectedAccept, accept, StringComparison.Ordinal)) // Base64 is case sensitive
{
context.Response.Clear();
context.Response.StatusCode = StatusCodes.Status502BadGateway;
ReportProxyError(context, ForwarderError.ResponseHeaders, new InvalidOperationException("The Sec-WebSocket-Accept header does not match the expected value."));
return ForwarderError.ResponseHeaders;
}
context.Response.Headers.Remove(HeaderNames.SecWebSocketAccept);
context.Response.StatusCode = StatusCodes.Status200OK;
}
// else H2 <- H2, no changes needed
return ForwarderError.None;
}
// H1 <- H2
if (response.Version == HttpVersion.Version20)
{
// Generate and add the Sec-WebSocket-Accept header, and the Connection and Upgrade headers
var key = context.Request.Headers[HeaderNames.SecWebSocketKey];
var accept = ProtocolHelper.CreateSecWebSocketAccept(key);
context.Response.Headers.TryAdd(HeaderNames.SecWebSocketAccept, accept);
context.Response.Headers.TryAdd(HeaderNames.Connection, HeaderNames.Upgrade);
context.Response.Headers.TryAdd(HeaderNames.Upgrade, WebSocketName);
return ForwarderError.None;
}
// H1 <- H1
// Restore the Connection and Upgrade headers
// We don't use NonValidated for the Connection header as we do want value validation.
// HttpHeaders.TryGetValues will handle the parsing and split the values for us.
if (RequestUtilities.TryGetValues(response.Headers, HeaderNames.Upgrade, out var upgradeValues)
&& response.Headers.TryGetValues(HeaderNames.Connection, out var connectionValues))
{
foreach (var value in connectionValues)
{
if (value.Equals(HeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase))
{
context.Response.Headers.TryAdd(HeaderNames.Connection, value);
context.Response.Headers.TryAdd(HeaderNames.Upgrade, upgradeValues);
break;
}
}
}
return ForwarderError.None;
}
private async ValueTask<(StreamCopyResult, Exception?)> CopyResponseBodyAsync(HttpContent destinationResponseContent, Stream clientResponseStream,
ActivityCancellationTokenSource activityCancellationSource)
{
// SocketHttpHandler and similar transports always provide an HttpContent object, even if it's empty.
// In 3.1 this is only likely to return null in tests.
// As of 5.0 HttpResponse.Content never returns null.
// https://github.com/dotnet/runtime/blame/8fc68f626a11d646109a758cb0fc70a0aa7826f1/src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs#L46
if (destinationResponseContent is not null)
{
using var destinationResponseStream = await destinationResponseContent.ReadAsStreamAsync(activityCancellationSource.Token);
// The response content-length is enforced by the server.
return await StreamCopier.CopyAsync(isRequest: false, destinationResponseStream, clientResponseStream, StreamCopier.UnknownLength, _clock, activityCancellationSource, activityCancellationSource.Token);
}
return (StreamCopyResult.Success, null);
}
private async ValueTask<ForwarderError> HandleResponseBodyErrorAsync(HttpContext context, StreamCopyHttpContent? requestContent, StreamCopyResult responseBodyCopyResult, Exception responseBodyException, CancellationTokenSource requestCancellationSource)
{
if (requestContent is not null && requestContent.Started)
{
var alreadyFinished = requestContent.ConsumptionTask.IsCompleted == true;
if (!alreadyFinished)
{
requestCancellationSource.Cancel();
}
var (requestBodyCopyResult, requestBodyError) = await requestContent.ConsumptionTask;
// Check for request body errors, these may have triggered the response error.
if (alreadyFinished && requestBodyCopyResult != StreamCopyResult.Success)
{
return HandleRequestBodyFailure(context, requestBodyCopyResult, requestBodyError!, responseBodyException);
}
}
var error = responseBodyCopyResult switch
{
StreamCopyResult.InputError => ForwarderError.ResponseBodyDestination,
StreamCopyResult.OutputError => ForwarderError.ResponseBodyClient,
StreamCopyResult.Canceled => ForwarderError.ResponseBodyCanceled,
_ => throw new NotImplementedException(responseBodyCopyResult.ToString()),
};
ReportProxyError(context, error, responseBodyException);
if (!context.Response.HasStarted)
{
// Nothing has been sent to the client yet, we can still send a good error response.
context.Response.Clear();
context.Response.StatusCode = StatusCodes.Status502BadGateway;
return error;
}
// The response has already started, we must forcefully terminate it so the client doesn't get the
// the mistaken impression that the truncated response is complete.
ResetOrAbort(context, isCancelled: responseBodyCopyResult == StreamCopyResult.Canceled);
return error;
}
private static ValueTask CopyResponseTrailingHeadersAsync(HttpResponseMessage source, HttpContext context, HttpTransformer transformer, CancellationToken cancellationToken)
{
// Copies trailers
return transformer.TransformResponseTrailersAsync(context, source, cancellationToken);
}
/// <summary>
/// Disable some ASP .NET Core server limits so that we can handle long-running gRPC requests unconstrained.
/// Note that the gRPC server implementation on ASP .NET Core does the same for client-streaming and duplex methods.
/// Since in Gateway we have no way to determine if the current request requires client-streaming or duplex comm,
/// we do this for *all* incoming requests that look like they might be gRPC.
/// </summary>
/// <remarks>
/// Inspired on
/// <see href="https://github.com/grpc/grpc-dotnet/blob/3ce9b104524a4929f5014c13cd99ba9a1c2431d4/src/Grpc.AspNetCore.Server/Internal/CallHandlers/ServerCallHandlerBase.cs#L127"/>.
/// </remarks>
private void DisableMinRequestBodyDataRateAndMaxRequestBodySize(HttpContext httpContext)
{
var minRequestBodyDataRateFeature = httpContext.Features.Get<IHttpMinRequestBodyDataRateFeature>();
if (minRequestBodyDataRateFeature is not null)
{
minRequestBodyDataRateFeature.MinDataRate = null;
}
var maxRequestBodySizeFeature = httpContext.Features.Get<IHttpMaxRequestBodySizeFeature>();
if (maxRequestBodySizeFeature is not null)
{
if (!maxRequestBodySizeFeature.IsReadOnly)
{
maxRequestBodySizeFeature.MaxRequestBodySize = null;
}
else
{
// IsReadOnly could be true if middleware has already started reading the request body
// In that case we can't disable the max request body size for the request stream
_logger.LogWarning("Unable to disable max request body size.");
}
}
}
private void ReportProxyError(HttpContext context, ForwarderError error, Exception ex)
{
context.Features.Set<IForwarderErrorFeature>(new ForwarderErrorFeature(error, ex));
Log.ErrorProxying(_logger, error, ex);
ForwarderTelemetry.Log.ForwarderFailed(error);
}
private static void ResetOrAbort(HttpContext context, bool isCancelled)
{
var resetFeature = context.Features.Get<IHttpResetFeature>();
if (resetFeature is not null)
{
// https://tools.ietf.org/html/rfc7540#section-7
const int Cancelled = 2;
const int InternalError = 8;
resetFeature.Reset(isCancelled ? Cancelled : InternalError);
return;
}
context.Abort();
}
private static class Log
{
private static readonly Action<ILogger, Version, int, Exception?> _responseReceived = LoggerMessage.Define<Version, int>(
LogLevel.Information,
EventIds.ResponseReceived,
"Received HTTP/{version} response {statusCode}.");
private static readonly Action<ILogger, string, string, string, string, Exception?> _proxying = LoggerMessage.Define<string, string, string, string>(
LogLevel.Information,
EventIds.Forwarding,
"Proxying to {targetUrl} {version} {versionPolicy} {isStreaming}");
private static readonly Action<ILogger, ForwarderError, string, Exception> _proxyError = LoggerMessage.Define<ForwarderError, string>(
LogLevel.Information,
EventIds.ForwardingError,
"{error}: {message}");
private static readonly Action<ILogger, int, Exception?> _notProxying = LoggerMessage.Define<int>(
LogLevel.Information,
EventIds.NotForwarding,
"Not Proxying, a {statusCode} response was set by the transforms.");