-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
codec_impl.cc
1454 lines (1261 loc) · 59 KB
/
codec_impl.cc
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
#include "common/http/http2/codec_impl.h"
#include <cstdint>
#include <memory>
#include <vector>
#include "envoy/event/dispatcher.h"
#include "envoy/http/codes.h"
#include "envoy/http/header_map.h"
#include "envoy/network/connection.h"
#include "common/common/assert.h"
#include "common/common/cleanup.h"
#include "common/common/enum_to_int.h"
#include "common/common/fmt.h"
#include "common/common/utility.h"
#include "common/http/codes.h"
#include "common/http/exception.h"
#include "common/http/header_utility.h"
#include "common/http/headers.h"
#include "common/http/http2/codec_stats.h"
#include "common/http/utility.h"
#include "absl/container/fixed_array.h"
namespace Envoy {
namespace Http {
namespace Http2 {
class Http2ResponseCodeDetailValues {
// Invalid HTTP header field was received and stream is going to be
// closed.
const absl::string_view ng_http2_err_http_header_ = "http2.invalid.header.field";
// Violation in HTTP messaging rule.
const absl::string_view ng_http2_err_http_messaging_ = "http2.violation.of.messaging.rule";
// none of the above
const absl::string_view ng_http2_err_unknown_ = "http2.unknown.nghttp2.error";
public:
const absl::string_view errorDetails(int error_code) const {
switch (error_code) {
case NGHTTP2_ERR_HTTP_HEADER:
return ng_http2_err_http_header_;
case NGHTTP2_ERR_HTTP_MESSAGING:
return ng_http2_err_http_messaging_;
default:
return ng_http2_err_unknown_;
}
}
};
using Http2ResponseCodeDetails = ConstSingleton<Http2ResponseCodeDetailValues>;
bool Utility::reconstituteCrumbledCookies(const HeaderString& key, const HeaderString& value,
HeaderString& cookies) {
if (key != Headers::get().Cookie.get().c_str()) {
return false;
}
if (!cookies.empty()) {
cookies.append("; ", 2);
}
const absl::string_view value_view = value.getStringView();
cookies.append(value_view.data(), value_view.size());
return true;
}
ConnectionImpl::Http2Callbacks ConnectionImpl::http2_callbacks_;
nghttp2_session* ProdNghttp2SessionFactory::create(const nghttp2_session_callbacks* callbacks,
ConnectionImpl* connection,
const nghttp2_option* options) {
nghttp2_session* session;
nghttp2_session_client_new2(&session, callbacks, connection, options);
return session;
}
void ProdNghttp2SessionFactory::init(nghttp2_session*, ConnectionImpl* connection,
const envoy::config::core::v3::Http2ProtocolOptions& options) {
connection->sendSettings(options, true);
}
/**
* Helper to remove const during a cast. nghttp2 takes non-const pointers for headers even though
* it copies them.
*/
template <typename T> static T* remove_const(const void* object) {
return const_cast<T*>(reinterpret_cast<const T*>(object));
}
ConnectionImpl::StreamImpl::StreamImpl(ConnectionImpl& parent, uint32_t buffer_limit)
: parent_(parent), local_end_stream_sent_(false), remote_end_stream_(false),
data_deferred_(false), waiting_for_non_informational_headers_(false),
pending_receive_buffer_high_watermark_called_(false),
pending_send_buffer_high_watermark_called_(false), reset_due_to_messaging_error_(false) {
parent_.stats_.streams_active_.inc();
if (buffer_limit > 0) {
setWriteBufferWatermarks(buffer_limit / 2, buffer_limit);
}
}
ConnectionImpl::StreamImpl::~StreamImpl() { ASSERT(stream_idle_timer_ == nullptr); }
void ConnectionImpl::StreamImpl::destroy() {
if (stream_idle_timer_ != nullptr) {
// To ease testing and the destructor assertion.
stream_idle_timer_->disableTimer();
stream_idle_timer_.reset();
}
parent_.stats_.streams_active_.dec();
parent_.stats_.pending_send_bytes_.sub(pending_send_data_.length());
}
static void insertHeader(std::vector<nghttp2_nv>& headers, const HeaderEntry& header) {
uint8_t flags = 0;
if (header.key().isReference()) {
flags |= NGHTTP2_NV_FLAG_NO_COPY_NAME;
}
if (header.value().isReference()) {
flags |= NGHTTP2_NV_FLAG_NO_COPY_VALUE;
}
const absl::string_view header_key = header.key().getStringView();
const absl::string_view header_value = header.value().getStringView();
headers.push_back({remove_const<uint8_t>(header_key.data()),
remove_const<uint8_t>(header_value.data()), header_key.size(),
header_value.size(), flags});
}
void ConnectionImpl::StreamImpl::buildHeaders(std::vector<nghttp2_nv>& final_headers,
const HeaderMap& headers) {
final_headers.reserve(headers.size());
headers.iterate(
[](const HeaderEntry& header, void* context) -> HeaderMap::Iterate {
std::vector<nghttp2_nv>* final_headers = static_cast<std::vector<nghttp2_nv>*>(context);
insertHeader(*final_headers, header);
return HeaderMap::Iterate::Continue;
},
&final_headers);
}
void ConnectionImpl::ServerStreamImpl::encode100ContinueHeaders(const ResponseHeaderMap& headers) {
ASSERT(headers.Status()->value() == "100");
encodeHeaders(headers, false);
}
void ConnectionImpl::StreamImpl::encodeHeadersBase(const std::vector<nghttp2_nv>& final_headers,
bool end_stream) {
nghttp2_data_provider provider;
if (!end_stream) {
provider.source.ptr = this;
provider.read_callback = [](nghttp2_session*, int32_t, uint8_t*, size_t length,
uint32_t* data_flags, nghttp2_data_source* source,
void*) -> ssize_t {
return static_cast<StreamImpl*>(source->ptr)->onDataSourceRead(length, data_flags);
};
}
local_end_stream_ = end_stream;
submitHeaders(final_headers, end_stream ? nullptr : &provider);
parent_.sendPendingFrames();
}
void ConnectionImpl::ClientStreamImpl::encodeHeaders(const RequestHeaderMap& headers,
bool end_stream) {
// This must exist outside of the scope of isUpgrade as the underlying memory is
// needed until encodeHeadersBase has been called.
std::vector<nghttp2_nv> final_headers;
Http::RequestHeaderMapPtr modified_headers;
if (Http::Utility::isUpgrade(headers)) {
modified_headers = createHeaderMap<RequestHeaderMapImpl>(headers);
upgrade_type_ = std::string(headers.getUpgradeValue());
Http::Utility::transformUpgradeRequestFromH1toH2(*modified_headers);
buildHeaders(final_headers, *modified_headers);
} else if (headers.Method() && headers.Method()->value() == "CONNECT") {
// If this is not an upgrade style connect (above branch) it is a bytestream
// connect and should have :path and :protocol set accordingly
// As HTTP/1.1 does not require a path for CONNECT, we may have to add one
// if shifting codecs. For now, default to "/" - this can be made
// configurable if necessary.
// https://tools.ietf.org/html/draft-kinnear-httpbis-http2-transport-02
modified_headers = createHeaderMap<RequestHeaderMapImpl>(headers);
modified_headers->setProtocol(Headers::get().ProtocolValues.Bytestream);
if (!headers.Path()) {
modified_headers->setPath("/");
}
buildHeaders(final_headers, *modified_headers);
} else {
buildHeaders(final_headers, headers);
}
encodeHeadersBase(final_headers, end_stream);
}
void ConnectionImpl::ServerStreamImpl::encodeHeaders(const ResponseHeaderMap& headers,
bool end_stream) {
// The contract is that client codecs must ensure that :status is present.
ASSERT(headers.Status() != nullptr);
// This must exist outside of the scope of isUpgrade as the underlying memory is
// needed until encodeHeadersBase has been called.
std::vector<nghttp2_nv> final_headers;
Http::ResponseHeaderMapPtr modified_headers;
if (Http::Utility::isUpgrade(headers)) {
modified_headers = createHeaderMap<ResponseHeaderMapImpl>(headers);
Http::Utility::transformUpgradeResponseFromH1toH2(*modified_headers);
buildHeaders(final_headers, *modified_headers);
} else {
buildHeaders(final_headers, headers);
}
encodeHeadersBase(final_headers, end_stream);
}
void ConnectionImpl::StreamImpl::encodeTrailersBase(const HeaderMap& trailers) {
ASSERT(!local_end_stream_);
local_end_stream_ = true;
if (pending_send_data_.length() > 0) {
// In this case we want trailers to come after we release all pending body data that is
// waiting on window updates. We need to save the trailers so that we can emit them later.
ASSERT(!pending_trailers_to_encode_);
pending_trailers_to_encode_ = cloneTrailers(trailers);
createPendingFlushTimer();
} else {
submitTrailers(trailers);
parent_.sendPendingFrames();
}
}
void ConnectionImpl::StreamImpl::encodeMetadata(const MetadataMapVector& metadata_map_vector) {
ASSERT(parent_.allow_metadata_);
MetadataEncoder& metadata_encoder = getMetadataEncoder();
if (!metadata_encoder.createPayload(metadata_map_vector)) {
return;
}
for (uint8_t flags : metadata_encoder.payloadFrameFlagBytes()) {
submitMetadata(flags);
}
parent_.sendPendingFrames();
}
void ConnectionImpl::StreamImpl::readDisable(bool disable) {
ENVOY_CONN_LOG(debug, "Stream {} {}, unconsumed_bytes {} read_disable_count {}",
parent_.connection_, stream_id_, (disable ? "disabled" : "enabled"),
unconsumed_bytes_, read_disable_count_);
if (disable) {
++read_disable_count_;
} else {
ASSERT(read_disable_count_ > 0);
--read_disable_count_;
if (!buffers_overrun()) {
nghttp2_session_consume(parent_.session_, stream_id_, unconsumed_bytes_);
unconsumed_bytes_ = 0;
parent_.sendPendingFrames();
}
}
}
void ConnectionImpl::StreamImpl::pendingRecvBufferHighWatermark() {
ENVOY_CONN_LOG(debug, "recv buffer over limit ", parent_.connection_);
ASSERT(!pending_receive_buffer_high_watermark_called_);
pending_receive_buffer_high_watermark_called_ = true;
readDisable(true);
}
void ConnectionImpl::StreamImpl::pendingRecvBufferLowWatermark() {
ENVOY_CONN_LOG(debug, "recv buffer under limit ", parent_.connection_);
ASSERT(pending_receive_buffer_high_watermark_called_);
pending_receive_buffer_high_watermark_called_ = false;
readDisable(false);
}
void ConnectionImpl::ClientStreamImpl::decodeHeaders(bool allow_waiting_for_informational_headers) {
auto& headers = absl::get<ResponseHeaderMapPtr>(headers_or_trailers_);
if (allow_waiting_for_informational_headers &&
CodeUtility::is1xx(Http::Utility::getResponseStatus(*headers))) {
waiting_for_non_informational_headers_ = true;
}
if (!upgrade_type_.empty() && headers->Status()) {
Http::Utility::transformUpgradeResponseFromH2toH1(*headers, upgrade_type_);
}
if (headers->Status()->value() == "100") {
ASSERT(!remote_end_stream_);
response_decoder_.decode100ContinueHeaders(std::move(headers));
} else {
response_decoder_.decodeHeaders(std::move(headers), remote_end_stream_);
}
}
void ConnectionImpl::ClientStreamImpl::decodeTrailers() {
response_decoder_.decodeTrailers(
std::move(absl::get<ResponseTrailerMapPtr>(headers_or_trailers_)));
}
void ConnectionImpl::ServerStreamImpl::decodeHeaders(bool allow_waiting_for_informational_headers) {
ASSERT(!allow_waiting_for_informational_headers);
auto& headers = absl::get<RequestHeaderMapPtr>(headers_or_trailers_);
if (Http::Utility::isH2UpgradeRequest(*headers)) {
Http::Utility::transformUpgradeRequestFromH2toH1(*headers);
}
request_decoder_->decodeHeaders(std::move(headers), remote_end_stream_);
}
void ConnectionImpl::ServerStreamImpl::decodeTrailers() {
request_decoder_->decodeTrailers(
std::move(absl::get<RequestTrailerMapPtr>(headers_or_trailers_)));
}
void ConnectionImpl::StreamImpl::pendingSendBufferHighWatermark() {
ENVOY_CONN_LOG(debug, "send buffer over limit ", parent_.connection_);
ASSERT(!pending_send_buffer_high_watermark_called_);
pending_send_buffer_high_watermark_called_ = true;
runHighWatermarkCallbacks();
}
void ConnectionImpl::StreamImpl::pendingSendBufferLowWatermark() {
ENVOY_CONN_LOG(debug, "send buffer under limit ", parent_.connection_);
ASSERT(pending_send_buffer_high_watermark_called_);
pending_send_buffer_high_watermark_called_ = false;
runLowWatermarkCallbacks();
}
void ConnectionImpl::StreamImpl::saveHeader(HeaderString&& name, HeaderString&& value) {
if (!Utility::reconstituteCrumbledCookies(name, value, cookies_)) {
headers().addViaMove(std::move(name), std::move(value));
}
}
void ConnectionImpl::StreamImpl::submitTrailers(const HeaderMap& trailers) {
std::vector<nghttp2_nv> final_headers;
buildHeaders(final_headers, trailers);
int rc = nghttp2_submit_trailer(parent_.session_, stream_id_, final_headers.data(),
final_headers.size());
ASSERT(rc == 0);
}
void ConnectionImpl::StreamImpl::submitMetadata(uint8_t flags) {
ASSERT(stream_id_ > 0);
const int result =
nghttp2_submit_extension(parent_.session_, METADATA_FRAME_TYPE, flags, stream_id_, nullptr);
ASSERT(result == 0);
}
ssize_t ConnectionImpl::StreamImpl::onDataSourceRead(uint64_t length, uint32_t* data_flags) {
if (pending_send_data_.length() == 0 && !local_end_stream_) {
ASSERT(!data_deferred_);
data_deferred_ = true;
return NGHTTP2_ERR_DEFERRED;
} else {
*data_flags |= NGHTTP2_DATA_FLAG_NO_COPY;
if (local_end_stream_ && pending_send_data_.length() <= length) {
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
if (pending_trailers_to_encode_) {
// We need to tell the library to not set end stream so that we can emit the trailers.
*data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
submitTrailers(*pending_trailers_to_encode_);
pending_trailers_to_encode_.reset();
}
}
return std::min(length, pending_send_data_.length());
}
}
int ConnectionImpl::StreamImpl::onDataSourceSend(const uint8_t* framehd, size_t length) {
// In this callback we are writing out a raw DATA frame without copying. nghttp2 assumes that we
// "just know" that the frame header is 9 bytes.
// https://nghttp2.org/documentation/types.html#c.nghttp2_send_data_callback
static const uint64_t FRAME_HEADER_SIZE = 9;
parent_.outbound_data_frames_++;
Buffer::OwnedImpl output;
if (!parent_.addOutboundFrameFragment(output, framehd, FRAME_HEADER_SIZE)) {
ENVOY_CONN_LOG(debug, "error sending data frame: Too many frames in the outbound queue",
parent_.connection_);
return NGHTTP2_ERR_FLOODED;
}
parent_.stats_.pending_send_bytes_.sub(length);
output.move(pending_send_data_, length);
parent_.connection_.write(output, false);
return 0;
}
void ConnectionImpl::ClientStreamImpl::submitHeaders(const std::vector<nghttp2_nv>& final_headers,
nghttp2_data_provider* provider) {
ASSERT(stream_id_ == -1);
stream_id_ = nghttp2_submit_request(parent_.session_, nullptr, final_headers.data(),
final_headers.size(), provider, base());
ASSERT(stream_id_ > 0);
}
void ConnectionImpl::ServerStreamImpl::submitHeaders(const std::vector<nghttp2_nv>& final_headers,
nghttp2_data_provider* provider) {
ASSERT(stream_id_ != -1);
int rc = nghttp2_submit_response(parent_.session_, stream_id_, final_headers.data(),
final_headers.size(), provider);
ASSERT(rc == 0);
}
void ConnectionImpl::ServerStreamImpl::createPendingFlushTimer() {
ASSERT(stream_idle_timer_ == nullptr);
if (stream_idle_timeout_.count() > 0) {
stream_idle_timer_ =
parent_.connection_.dispatcher().createTimer([this] { onPendingFlushTimer(); });
stream_idle_timer_->enableTimer(stream_idle_timeout_);
}
}
void ConnectionImpl::StreamImpl::onPendingFlushTimer() {
ENVOY_CONN_LOG(debug, "pending stream flush timeout", parent_.connection_);
stream_idle_timer_.reset();
parent_.stats_.tx_flush_timeout_.inc();
ASSERT(local_end_stream_ && !local_end_stream_sent_);
// This will emit a reset frame for this stream and close the stream locally. No reset callbacks
// will be run because higher layers think the stream is already finished.
resetStreamWorker(StreamResetReason::LocalReset);
parent_.sendPendingFrames();
}
void ConnectionImpl::StreamImpl::encodeData(Buffer::Instance& data, bool end_stream) {
ASSERT(!local_end_stream_);
local_end_stream_ = end_stream;
parent_.stats_.pending_send_bytes_.add(data.length());
pending_send_data_.move(data);
if (data_deferred_) {
int rc = nghttp2_session_resume_data(parent_.session_, stream_id_);
ASSERT(rc == 0);
data_deferred_ = false;
}
parent_.sendPendingFrames();
if (local_end_stream_ && pending_send_data_.length() > 0) {
createPendingFlushTimer();
}
}
void ConnectionImpl::StreamImpl::resetStream(StreamResetReason reason) {
// Higher layers expect calling resetStream() to immediately raise reset callbacks.
runResetCallbacks(reason);
// If we submit a reset, nghttp2 will cancel outbound frames that have not yet been sent.
// We want these frames to go out so we defer the reset until we send all of the frames that
// end the local stream.
if (local_end_stream_ && !local_end_stream_sent_) {
parent_.pending_deferred_reset_ = true;
deferred_reset_ = reason;
ENVOY_CONN_LOG(trace, "deferred reset stream", parent_.connection_);
} else {
resetStreamWorker(reason);
}
// We must still call sendPendingFrames() in both the deferred and not deferred path. This forces
// the cleanup logic to run which will reset the stream in all cases if all data frames could not
// be sent.
parent_.sendPendingFrames();
}
void ConnectionImpl::StreamImpl::resetStreamWorker(StreamResetReason reason) {
int rc = nghttp2_submit_rst_stream(parent_.session_, NGHTTP2_FLAG_NONE, stream_id_,
reason == StreamResetReason::LocalRefusedStreamReset
? NGHTTP2_REFUSED_STREAM
: NGHTTP2_NO_ERROR);
ASSERT(rc == 0);
}
MetadataEncoder& ConnectionImpl::StreamImpl::getMetadataEncoder() {
if (metadata_encoder_ == nullptr) {
metadata_encoder_ = std::make_unique<MetadataEncoder>();
}
return *metadata_encoder_;
}
MetadataDecoder& ConnectionImpl::StreamImpl::getMetadataDecoder() {
if (metadata_decoder_ == nullptr) {
auto cb = [this](MetadataMapPtr&& metadata_map_ptr) {
this->onMetadataDecoded(std::move(metadata_map_ptr));
};
metadata_decoder_ = std::make_unique<MetadataDecoder>(cb);
}
return *metadata_decoder_;
}
void ConnectionImpl::StreamImpl::onMetadataDecoded(MetadataMapPtr&& metadata_map_ptr) {
decoder().decodeMetadata(std::move(metadata_map_ptr));
}
ConnectionImpl::ConnectionImpl(Network::Connection& connection, CodecStats& stats,
const envoy::config::core::v3::Http2ProtocolOptions& http2_options,
const uint32_t max_headers_kb, const uint32_t max_headers_count)
: stats_(stats), connection_(connection), max_headers_kb_(max_headers_kb),
max_headers_count_(max_headers_count),
per_stream_buffer_limit_(http2_options.initial_stream_window_size().value()),
stream_error_on_invalid_http_messaging_(
http2_options.stream_error_on_invalid_http_messaging()),
flood_detected_(false), max_outbound_frames_(http2_options.max_outbound_frames().value()),
frame_buffer_releasor_([this]() { releaseOutboundFrame(); }),
max_outbound_control_frames_(http2_options.max_outbound_control_frames().value()),
control_frame_buffer_releasor_([this]() { releaseOutboundControlFrame(); }),
max_consecutive_inbound_frames_with_empty_payload_(
http2_options.max_consecutive_inbound_frames_with_empty_payload().value()),
max_inbound_priority_frames_per_stream_(
http2_options.max_inbound_priority_frames_per_stream().value()),
max_inbound_window_update_frames_per_data_frame_sent_(
http2_options.max_inbound_window_update_frames_per_data_frame_sent().value()),
dispatching_(false), raised_goaway_(false), pending_deferred_reset_(false) {}
ConnectionImpl::~ConnectionImpl() {
for (const auto& stream : active_streams_) {
stream->destroy();
}
nghttp2_session_del(session_);
}
Http::Status ConnectionImpl::dispatch(Buffer::Instance& data) {
// TODO(#10878): Remove this wrapper when exception removal is complete. innerDispatch may either
// throw an exception or return an error status. The utility wrapper catches exceptions and
// converts them to error statuses.
return Http::Utility::exceptionToStatus(
[&](Buffer::Instance& data) -> Http::Status { return innerDispatch(data); }, data);
}
Http::Status ConnectionImpl::innerDispatch(Buffer::Instance& data) {
ENVOY_CONN_LOG(trace, "dispatching {} bytes", connection_, data.length());
// Make sure that dispatching_ is set to false after dispatching, even when
// ConnectionImpl::dispatch returns early or throws an exception (consider removing if there is a
// single return after exception removal (#10878)).
Cleanup cleanup([this]() { dispatching_ = false; });
for (const Buffer::RawSlice& slice : data.getRawSlices()) {
dispatching_ = true;
ssize_t rc =
nghttp2_session_mem_recv(session_, static_cast<const uint8_t*>(slice.mem_), slice.len_);
if (rc == NGHTTP2_ERR_FLOODED || flood_detected_) {
throw FrameFloodException(
"Flooding was detected in this HTTP/2 session, and it must be closed");
}
if (rc != static_cast<ssize_t>(slice.len_)) {
throw CodecProtocolException(fmt::format("{}", nghttp2_strerror(rc)));
}
dispatching_ = false;
}
ENVOY_CONN_LOG(trace, "dispatched {} bytes", connection_, data.length());
data.drain(data.length());
// Decoding incoming frames can generate outbound frames so flush pending.
sendPendingFrames();
return Http::okStatus();
}
ConnectionImpl::StreamImpl* ConnectionImpl::getStream(int32_t stream_id) {
return static_cast<StreamImpl*>(nghttp2_session_get_stream_user_data(session_, stream_id));
}
int ConnectionImpl::onData(int32_t stream_id, const uint8_t* data, size_t len) {
StreamImpl* stream = getStream(stream_id);
// If this results in buffering too much data, the watermark buffer will call
// pendingRecvBufferHighWatermark, resulting in ++read_disable_count_
stream->pending_recv_data_.add(data, len);
// Update the window to the peer unless some consumer of this stream's data has hit a flow control
// limit and disabled reads on this stream
if (!stream->buffers_overrun()) {
nghttp2_session_consume(session_, stream_id, len);
} else {
stream->unconsumed_bytes_ += len;
}
return 0;
}
void ConnectionImpl::goAway() {
int rc = nghttp2_submit_goaway(session_, NGHTTP2_FLAG_NONE,
nghttp2_session_get_last_proc_stream_id(session_),
NGHTTP2_NO_ERROR, nullptr, 0);
ASSERT(rc == 0);
sendPendingFrames();
}
void ConnectionImpl::shutdownNotice() {
int rc = nghttp2_submit_shutdown_notice(session_);
ASSERT(rc == 0);
sendPendingFrames();
}
int ConnectionImpl::onBeforeFrameReceived(const nghttp2_frame_hd* hd) {
ENVOY_CONN_LOG(trace, "about to recv frame type={}, flags={}", connection_,
static_cast<uint64_t>(hd->type), static_cast<uint64_t>(hd->flags));
// Track all the frames without padding here, since this is the only callback we receive
// for some of them (e.g. CONTINUATION frame, frames sent on closed streams, etc.).
// HEADERS frame is tracked in onBeginHeaders(), DATA frame is tracked in onFrameReceived().
if (hd->type != NGHTTP2_HEADERS && hd->type != NGHTTP2_DATA) {
if (!trackInboundFrames(hd, 0)) {
return NGHTTP2_ERR_FLOODED;
}
}
return 0;
}
ABSL_MUST_USE_RESULT
enum GoAwayErrorCode ngHttp2ErrorCodeToErrorCode(uint32_t code) noexcept {
switch (code) {
case NGHTTP2_NO_ERROR:
return GoAwayErrorCode::NoError;
default:
return GoAwayErrorCode::Other;
}
}
int ConnectionImpl::onFrameReceived(const nghttp2_frame* frame) {
ENVOY_CONN_LOG(trace, "recv frame type={}", connection_, static_cast<uint64_t>(frame->hd.type));
// onFrameReceived() is called with a complete HEADERS frame assembled from all the HEADERS
// and CONTINUATION frames, but we track them separately: HEADERS frames in onBeginHeaders()
// and CONTINUATION frames in onBeforeFrameReceived().
ASSERT(frame->hd.type != NGHTTP2_CONTINUATION);
if (frame->hd.type == NGHTTP2_DATA) {
if (!trackInboundFrames(&frame->hd, frame->data.padlen)) {
return NGHTTP2_ERR_FLOODED;
}
}
// Only raise GOAWAY once, since we don't currently expose stream information. Shutdown
// notifications are the same as a normal GOAWAY.
// TODO: handle multiple GOAWAY frames.
if (frame->hd.type == NGHTTP2_GOAWAY && !raised_goaway_) {
ASSERT(frame->hd.stream_id == 0);
raised_goaway_ = true;
callbacks().onGoAway(ngHttp2ErrorCodeToErrorCode(frame->goaway.error_code));
return 0;
}
if (frame->hd.type == NGHTTP2_SETTINGS && frame->hd.flags == NGHTTP2_FLAG_NONE) {
onSettingsForTest(frame->settings);
}
StreamImpl* stream = getStream(frame->hd.stream_id);
if (!stream) {
return 0;
}
switch (frame->hd.type) {
case NGHTTP2_HEADERS: {
stream->remote_end_stream_ = frame->hd.flags & NGHTTP2_FLAG_END_STREAM;
if (!stream->cookies_.empty()) {
HeaderString key(Headers::get().Cookie);
stream->headers().addViaMove(std::move(key), std::move(stream->cookies_));
}
switch (frame->headers.cat) {
case NGHTTP2_HCAT_RESPONSE:
case NGHTTP2_HCAT_REQUEST: {
stream->decodeHeaders(frame->headers.cat == NGHTTP2_HCAT_RESPONSE);
break;
}
case NGHTTP2_HCAT_HEADERS: {
// It's possible that we are waiting to send a deferred reset, so only raise headers/trailers
// if local is not complete.
if (!stream->deferred_reset_) {
if (!stream->waiting_for_non_informational_headers_) {
if (!stream->remote_end_stream_) {
// This indicates we have received more headers frames than Envoy
// supports. Even if this is valid HTTP (something like 103 early hints) fail here
// rather than trying to push unexpected headers through the Envoy pipeline as that
// will likely result in Envoy crashing.
// It would be cleaner to reset the stream rather than reset the/ entire connection but
// it's also slightly more dangerous so currently we err on the side of safety.
stats_.too_many_header_frames_.inc();
throw CodecProtocolException("Unexpected 'trailers' with no end stream.");
} else {
stream->decodeTrailers();
}
} else {
ASSERT(!nghttp2_session_check_server_session(session_));
stream->waiting_for_non_informational_headers_ = false;
// Even if we have :status 100 in the client case in a response, when
// we received a 1xx to start out with, nghttp2 message checking
// guarantees proper flow here.
stream->decodeHeaders(false);
}
}
break;
}
default:
// We do not currently support push.
NOT_IMPLEMENTED_GCOVR_EXCL_LINE;
}
break;
}
case NGHTTP2_DATA: {
stream->remote_end_stream_ = frame->hd.flags & NGHTTP2_FLAG_END_STREAM;
// It's possible that we are waiting to send a deferred reset, so only raise data if local
// is not complete.
if (!stream->deferred_reset_) {
stream->decoder().decodeData(stream->pending_recv_data_, stream->remote_end_stream_);
}
stream->pending_recv_data_.drain(stream->pending_recv_data_.length());
break;
}
case NGHTTP2_RST_STREAM: {
ENVOY_CONN_LOG(trace, "remote reset: {}", connection_, frame->rst_stream.error_code);
stats_.rx_reset_.inc();
break;
}
}
return 0;
}
int ConnectionImpl::onFrameSend(const nghttp2_frame* frame) {
// The nghttp2 library does not cleanly give us a way to determine whether we received invalid
// data from our peer. Sometimes it raises the invalid frame callback, and sometimes it does not.
// In all cases however it will attempt to send a GOAWAY frame with an error status. If we see
// an outgoing frame of this type, we will return an error code so that we can abort execution.
ENVOY_CONN_LOG(trace, "sent frame type={}", connection_, static_cast<uint64_t>(frame->hd.type));
switch (frame->hd.type) {
case NGHTTP2_GOAWAY: {
ENVOY_CONN_LOG(debug, "sent goaway code={}", connection_, frame->goaway.error_code);
if (frame->goaway.error_code != NGHTTP2_NO_ERROR) {
return NGHTTP2_ERR_CALLBACK_FAILURE;
}
break;
}
case NGHTTP2_RST_STREAM: {
ENVOY_CONN_LOG(debug, "sent reset code={}", connection_, frame->rst_stream.error_code);
stats_.tx_reset_.inc();
break;
}
case NGHTTP2_HEADERS:
case NGHTTP2_DATA: {
StreamImpl* stream = getStream(frame->hd.stream_id);
stream->local_end_stream_sent_ = frame->hd.flags & NGHTTP2_FLAG_END_STREAM;
break;
}
}
return 0;
}
int ConnectionImpl::onInvalidFrame(int32_t stream_id, int error_code) {
ENVOY_CONN_LOG(debug, "invalid frame: {} on stream {}", connection_, nghttp2_strerror(error_code),
stream_id);
// Set details of error_code in the stream whenever we have one.
StreamImpl* stream = getStream(stream_id);
if (stream != nullptr) {
stream->setDetails(Http2ResponseCodeDetails::get().errorDetails(error_code));
}
if (error_code == NGHTTP2_ERR_HTTP_HEADER || error_code == NGHTTP2_ERR_HTTP_MESSAGING) {
stats_.rx_messaging_error_.inc();
if (stream_error_on_invalid_http_messaging_) {
// The stream is about to be closed due to an invalid header or messaging. Don't kill the
// entire connection if one stream has bad headers or messaging.
if (stream != nullptr) {
// See comment below in onStreamClose() for why we do this.
stream->reset_due_to_messaging_error_ = true;
}
return 0;
}
}
// Cause dispatch to return with an error code.
return NGHTTP2_ERR_CALLBACK_FAILURE;
}
int ConnectionImpl::onBeforeFrameSend(const nghttp2_frame* frame) {
ENVOY_CONN_LOG(trace, "about to send frame type={}, flags={}", connection_,
static_cast<uint64_t>(frame->hd.type), static_cast<uint64_t>(frame->hd.flags));
ASSERT(!is_outbound_flood_monitored_control_frame_);
// Flag flood monitored outbound control frames.
is_outbound_flood_monitored_control_frame_ =
((frame->hd.type == NGHTTP2_PING || frame->hd.type == NGHTTP2_SETTINGS) &&
frame->hd.flags & NGHTTP2_FLAG_ACK) ||
frame->hd.type == NGHTTP2_RST_STREAM;
return 0;
}
void ConnectionImpl::incrementOutboundFrameCount(bool is_outbound_flood_monitored_control_frame) {
++outbound_frames_;
if (is_outbound_flood_monitored_control_frame) {
++outbound_control_frames_;
}
checkOutboundQueueLimits();
}
bool ConnectionImpl::addOutboundFrameFragment(Buffer::OwnedImpl& output, const uint8_t* data,
size_t length) {
// Reset the outbound frame type (set in the onBeforeFrameSend callback) since the
// onBeforeFrameSend callback is not called for DATA frames.
bool is_outbound_flood_monitored_control_frame = false;
std::swap(is_outbound_flood_monitored_control_frame, is_outbound_flood_monitored_control_frame_);
try {
incrementOutboundFrameCount(is_outbound_flood_monitored_control_frame);
} catch (const FrameFloodException&) {
return false;
}
output.add(data, length);
output.addDrainTracker(is_outbound_flood_monitored_control_frame ? control_frame_buffer_releasor_
: frame_buffer_releasor_);
return true;
}
void ConnectionImpl::releaseOutboundFrame() {
ASSERT(outbound_frames_ >= 1);
--outbound_frames_;
}
void ConnectionImpl::releaseOutboundControlFrame() {
ASSERT(outbound_control_frames_ >= 1);
--outbound_control_frames_;
releaseOutboundFrame();
}
ssize_t ConnectionImpl::onSend(const uint8_t* data, size_t length) {
ENVOY_CONN_LOG(trace, "send data: bytes={}", connection_, length);
Buffer::OwnedImpl buffer;
if (!addOutboundFrameFragment(buffer, data, length)) {
ENVOY_CONN_LOG(debug, "error sending frame: Too many frames in the outbound queue.",
connection_);
return NGHTTP2_ERR_FLOODED;
}
// While the buffer is transient the fragment it contains will be moved into the
// write_buffer_ of the underlying connection_ by the write method below.
// This creates lifetime dependency between the write_buffer_ of the underlying connection
// and the codec object. Specifically the write_buffer_ MUST be either fully drained or
// deleted before the codec object is deleted. This is presently guaranteed by the
// destruction order of the Network::ConnectionImpl object where write_buffer_ is
// destroyed before the filter_manager_ which owns the codec through Http::ConnectionManagerImpl.
connection_.write(buffer, false);
return length;
}
int ConnectionImpl::onStreamClose(int32_t stream_id, uint32_t error_code) {
StreamImpl* stream = getStream(stream_id);
if (stream) {
ENVOY_CONN_LOG(debug, "stream closed: {}", connection_, error_code);
if (!stream->remote_end_stream_ || !stream->local_end_stream_) {
StreamResetReason reason;
if (stream->reset_due_to_messaging_error_) {
// Unfortunately, the nghttp2 API makes it incredibly difficult to clearly understand
// the flow of resets. I.e., did the reset originate locally? Was it remote? Here,
// we attempt to track cases in which we sent a reset locally due to an invalid frame
// received from the remote. We only do that in two cases currently (HTTP messaging layer
// errors from https://tools.ietf.org/html/rfc7540#section-8 which nghttp2 is very strict
// about). In other cases we treat invalid frames as a protocol error and just kill
// the connection.
reason = StreamResetReason::LocalReset;
} else {
reason = error_code == NGHTTP2_REFUSED_STREAM ? StreamResetReason::RemoteRefusedStreamReset
: StreamResetReason::RemoteReset;
}
stream->runResetCallbacks(reason);
}
stream->destroy();
connection_.dispatcher().deferredDelete(stream->removeFromList(active_streams_));
// Any unconsumed data must be consumed before the stream is deleted.
// nghttp2 does not appear to track this internally, and any stream deleted
// with outstanding window will contribute to a slow connection-window leak.
nghttp2_session_consume(session_, stream_id, stream->unconsumed_bytes_);
stream->unconsumed_bytes_ = 0;
nghttp2_session_set_stream_user_data(session_, stream->stream_id_, nullptr);
}
return 0;
}
int ConnectionImpl::onMetadataReceived(int32_t stream_id, const uint8_t* data, size_t len) {
ENVOY_CONN_LOG(trace, "recv {} bytes METADATA", connection_, len);
StreamImpl* stream = getStream(stream_id);
if (!stream) {
return 0;
}
bool success = stream->getMetadataDecoder().receiveMetadata(data, len);
return success ? 0 : NGHTTP2_ERR_CALLBACK_FAILURE;
}
int ConnectionImpl::onMetadataFrameComplete(int32_t stream_id, bool end_metadata) {
ENVOY_CONN_LOG(trace, "recv METADATA frame on stream {}, end_metadata: {}", connection_,
stream_id, end_metadata);
StreamImpl* stream = getStream(stream_id);
if (stream == nullptr) {
return 0;
}
bool result = stream->getMetadataDecoder().onMetadataFrameComplete(end_metadata);
return result ? 0 : NGHTTP2_ERR_CALLBACK_FAILURE;
}
ssize_t ConnectionImpl::packMetadata(int32_t stream_id, uint8_t* buf, size_t len) {
ENVOY_CONN_LOG(trace, "pack METADATA frame on stream {}", connection_, stream_id);
StreamImpl* stream = getStream(stream_id);
if (stream == nullptr) {
return 0;
}
MetadataEncoder& encoder = stream->getMetadataEncoder();
return encoder.packNextFramePayload(buf, len);
}
int ConnectionImpl::saveHeader(const nghttp2_frame* frame, HeaderString&& name,
HeaderString&& value) {
StreamImpl* stream = getStream(frame->hd.stream_id);
if (!stream) {
// We have seen 1 or 2 crashes where we get a headers callback but there is no associated
// stream data. I honestly am not sure how this can happen. However, from reading the nghttp2
// code it looks possible that inflate_header_block() can safely inflate headers for an already
// closed stream, but will still call the headers callback. Since that seems possible, we should
// ignore this case here.
// TODO(mattklein123): Figure out a test case that can hit this.
stats_.headers_cb_no_stream_.inc();
return 0;
}
auto should_return = checkHeaderNameForUnderscores(name.getStringView());
if (should_return) {
name.clear();
value.clear();
return should_return.value();
}
stream->saveHeader(std::move(name), std::move(value));
if (stream->headers().byteSize() > max_headers_kb_ * 1024 ||
stream->headers().size() > max_headers_count_) {
// This will cause the library to reset/close the stream.
stats_.header_overflow_.inc();
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
} else {
return 0;
}
}
void ConnectionImpl::sendPendingFrames() {
if (dispatching_ || connection_.state() == Network::Connection::State::Closed) {
return;
}
const int rc = nghttp2_session_send(session_);
if (rc != 0) {
ASSERT(rc == NGHTTP2_ERR_CALLBACK_FAILURE);
// For errors caused by the pending outbound frame flood the FrameFloodException has
// to be thrown. However the nghttp2 library returns only the generic error code for
// all failure types. Check queue limits and throw FrameFloodException if they were
// exceeded.
if (outbound_frames_ > max_outbound_frames_ ||
outbound_control_frames_ > max_outbound_control_frames_) {
throw FrameFloodException("Too many frames in the outbound queue.");
}
throw CodecProtocolException(std::string(nghttp2_strerror(rc)));
}
// See ConnectionImpl::StreamImpl::resetStream() for why we do this. This is an uncommon event,
// so iterating through every stream to find the ones that have a deferred reset is not a big
// deal. Furthermore, queueing a reset frame does not actually invoke the close stream callback.
// This is only done when the reset frame is sent. Thus, it's safe to work directly with the
// stream map.
// NOTE: The way we handle deferred reset is essentially best effort. If we intend to do a
// deferred reset, we try to finish the stream, including writing any pending data frames.
// If we cannot do this (potentially due to not enough window), we just reset the stream.
// In general this behavior occurs only when we are trying to send immediate error messages
// to short circuit requests. In the best effort case, we complete the stream before
// resetting. In other cases, we just do the reset now which will blow away pending data
// frames and release any memory associated with the stream.
if (pending_deferred_reset_) {
pending_deferred_reset_ = false;
for (auto& stream : active_streams_) {
if (stream->deferred_reset_) {
stream->resetStreamWorker(stream->deferred_reset_.value());
}
}
sendPendingFrames();