forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp3.cc
More file actions
1440 lines (1296 loc) · 51.8 KB
/
Copy pathhttp3.cc
File metadata and controls
1440 lines (1296 loc) · 51.8 KB
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 "nghttp3/lib/nghttp3_conn.h"
#if HAVE_OPENSSL && HAVE_QUIC
#include "guard.h"
#ifndef OPENSSL_NO_QUIC
#include <async_wrap-inl.h>
#include <base_object-inl.h>
#include <debug_utils-inl.h>
#include <env-inl.h>
#include <memory_tracker-inl.h>
#include <nghttp3/nghttp3.h>
#include <ngtcp2/ngtcp2.h>
#include <node_http_common-inl.h>
#include <node_sockaddr-inl.h>
#include <util-inl.h>
#include <zlib.h>
#include "application.h"
#include "bindingdata.h"
#include "defs.h"
#include "http3.h"
#include "session.h"
#include "sessionticket.h"
namespace node {
using v8::Array;
using v8::Local;
namespace quic {
namespace {
constexpr uint8_t kSessionTicketAppDataVersion = 1;
// Layout: [type(1)][version(1)][crc(4)][payload(34)] = 40 bytes
constexpr size_t kSessionTicketAppDataSize = 40;
constexpr size_t kSessionTicketAppDataHeaderSize = 6; // type + version + crc
constexpr size_t kSessionTicketAppDataPayloadSize =
kSessionTicketAppDataSize - kSessionTicketAppDataHeaderSize;
inline void WriteBE32(uint8_t* buf, uint32_t val) {
buf[0] = static_cast<uint8_t>((val >> 24) & 0xff);
buf[1] = static_cast<uint8_t>((val >> 16) & 0xff);
buf[2] = static_cast<uint8_t>((val >> 8) & 0xff);
buf[3] = static_cast<uint8_t>(val & 0xff);
}
inline uint32_t ReadBE32(const uint8_t* buf) {
return (static_cast<uint32_t>(buf[0]) << 24) |
(static_cast<uint32_t>(buf[1]) << 16) |
(static_cast<uint32_t>(buf[2]) << 8) | static_cast<uint32_t>(buf[3]);
}
inline void WriteBE64(uint8_t* buf, uint64_t val) {
buf[0] = static_cast<uint8_t>((val >> 56) & 0xff);
buf[1] = static_cast<uint8_t>((val >> 48) & 0xff);
buf[2] = static_cast<uint8_t>((val >> 40) & 0xff);
buf[3] = static_cast<uint8_t>((val >> 32) & 0xff);
buf[4] = static_cast<uint8_t>((val >> 24) & 0xff);
buf[5] = static_cast<uint8_t>((val >> 16) & 0xff);
buf[6] = static_cast<uint8_t>((val >> 8) & 0xff);
buf[7] = static_cast<uint8_t>(val & 0xff);
}
inline uint64_t ReadBE64(const uint8_t* buf) {
return (static_cast<uint64_t>(buf[0]) << 56) |
(static_cast<uint64_t>(buf[1]) << 48) |
(static_cast<uint64_t>(buf[2]) << 40) |
(static_cast<uint64_t>(buf[3]) << 32) |
(static_cast<uint64_t>(buf[4]) << 24) |
(static_cast<uint64_t>(buf[5]) << 16) |
(static_cast<uint64_t>(buf[6]) << 8) | static_cast<uint64_t>(buf[7]);
}
// Serialize an nghttp3_pri into an RFC 9218 priority field value
// (e.g., "u=3" or "u=0, i"). Returns the number of bytes written.
// This is used only for setting the priority field of HTTP/3 streams on
// the client side.
inline size_t FormatPriority(char* buf, size_t buflen, const nghttp3_pri& pri) {
int len;
if (pri.inc) {
len = snprintf(buf, buflen, "u=%d, i", pri.urgency);
} else {
len = snprintf(buf, buflen, "u=%d", pri.urgency);
}
return static_cast<size_t>(len);
}
} // namespace
struct Http3HeadersTraits {
using nv_t = nghttp3_nv;
};
struct Http3RcBufferPointerTraits {
using rcbuf_t = nghttp3_rcbuf;
using vector_t = nghttp3_vec;
static void inc(rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
nghttp3_rcbuf_incref(buf);
}
static void dec(rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
nghttp3_rcbuf_decref(buf);
}
static vector_t get_vec(rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
return nghttp3_rcbuf_get_buf(buf);
}
static bool is_static(const rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
return nghttp3_rcbuf_is_static(buf);
}
};
using Http3ConnectionPointer = DeleteFnPtr<nghttp3_conn, nghttp3_conn_del>;
using Http3Headers = NgHeaders<Http3HeadersTraits>;
using Http3RcBufferPointer = NgRcBufPointer<Http3RcBufferPointerTraits>;
struct Http3HeaderTraits {
typedef Http3RcBufferPointer rcbufferpointer_t;
typedef BindingData allocator_t;
static const char* ToHttpHeaderName(int32_t token) {
switch (token) {
case -1:
return nullptr;
#define V(key, name) \
case NGHTTP3_QPACK_TOKEN__##key: \
return name;
HTTP_SPECIAL_HEADERS(V)
#undef V
#define V(key, name) \
case NGHTTP3_QPACK_TOKEN_##key: \
return name;
HTTP_REGULAR_HEADERS(V)
#undef V
}
return nullptr;
}
};
using Http3Header = NgHeader<Http3HeaderTraits>;
// Implements the low-level HTTP/3 Application semantics.
class Http3ApplicationImpl final : public Session::Application {
public:
Http3ApplicationImpl(Session* session, const Options& options)
: Application(session, options),
allocator_(BindingData::Get(env()).nghttp3_allocator()),
options_(options),
conn_(nullptr) {
// Build the ORIGIN frame payload from the SNI configuration before
// creating the nghttp3 connection, since InitializeConnection needs
// the origin_vec_ to be ready for settings.origin_list.
if (session->is_server()) {
BuildOriginPayload();
}
conn_ = InitializeConnection();
session->set_priority_supported();
}
const Options& options() const override { return options_; }
Session::Application::Type type() const override {
return Session::Application::Type::HTTP3;
}
error_code GetNoErrorCode() const override { return NGHTTP3_H3_NO_ERROR; }
// HTTP/3 defines H3_INTERNAL_ERROR (0x102) for non-specific failures
// initiated by the implementation; this is the right code to send
// on RESET_STREAM when a stream is being aborted without an
// application-supplied code.
error_code GetInternalErrorCode() const override {
return NGHTTP3_H3_INTERNAL_ERROR;
}
void EarlyDataRejected() override {
// When 0-RTT is rejected, destroy the nghttp3 connection and all
// open streams — ngtcp2 has discarded their internal state.
// Reset started_ so Start() is called again via on_receive_rx_key
// at 1RTT to recreate the nghttp3 connection. Use the
// application's internal error code since this is an error
// condition (code 0 would be treated as a clean close).
conn_.reset();
started_ = false;
session().DestroyAllStreams(
QuicError::ForApplication(GetInternalErrorCode()));
if (!session().is_destroyed()) {
session().EmitEarlyDataRejected();
}
}
bool ReceiveStreamOpen(stream_id id) override {
// In HTTP/3, only create Stream objects for bidirectional streams.
// Unidirectional streams (control, QPACK encoder/decoder) are
// managed internally by nghttp3 and should not be exposed to JS.
if (!ngtcp2_is_bidi_stream(id)) return true;
auto stream = session().CreateStream(id);
if (!stream || session().is_destroyed()) [[unlikely]] {
return !session().is_destroyed();
}
return true;
}
bool SupportsHeaders() const override { return true; }
bool is_started() const override { return started_; }
bool Start() override {
if (started_) return true;
started_ = true;
Debug(&session(), "Starting HTTP/3 application.");
auto params = ngtcp2_conn_get_remote_transport_params(session());
if (params == nullptr) [[unlikely]] {
// The params are not available yet. Cannot start.
Debug(&session(),
"Cannot start HTTP/3 application yet. No remote transport params");
return false;
}
if (params->initial_max_streams_uni < 3) {
// HTTP3 requires 3 unidirectional control streams to be opened in each
// direction in additional to the bidirectional streams that are used to
// actually carry request and response payload back and forth.
// See:
// https://nghttp2.org/nghttp3/programmers-guide.html#binding-control-streams
Debug(&session(),
"Cannot start HTTP/3 application. Initial max "
"unidirectional streams [%zu] is too low. Must be at least 3",
params->initial_max_streams_uni);
return false;
}
// If this is a server session, then set the maximum number of
// bidirectional streams that can be created. This determines the number
// of requests that the client can actually created.
if (session().is_server()) {
nghttp3_conn_set_max_client_streams_bidi(
*this, params->initial_max_streams_bidi);
}
Debug(&session(), "Creating and binding HTTP/3 control streams");
bool ret =
ngtcp2_conn_open_uni_stream(session(), &control_stream_id_, nullptr) ==
0 &&
ngtcp2_conn_open_uni_stream(
session(), &qpack_enc_stream_id_, nullptr) == 0 &&
ngtcp2_conn_open_uni_stream(
session(), &qpack_dec_stream_id_, nullptr) == 0 &&
nghttp3_conn_bind_control_stream(*this, control_stream_id_) == 0 &&
nghttp3_conn_bind_qpack_streams(
*this, qpack_enc_stream_id_, qpack_dec_stream_id_) == 0;
if (env()->enabled_debug_list()->enabled(DebugCategory::QUIC) && ret) {
Debug(&session(),
"Created and bound control stream %" PRIi64,
control_stream_id_);
Debug(&session(),
"Created and bound qpack enc stream %" PRIi64,
qpack_enc_stream_id_);
Debug(&session(),
"Created and bound qpack dec streams %" PRIi64,
qpack_dec_stream_id_);
}
return ret;
}
void BeginShutdown() override {
// Only submit a shutdown notice if the H3 connection was fully
// started (control streams bound). If the TLS handshake failed
// before Start() was called, conn_ exists but its control streams
// are unbound, and nghttp3_conn_submit_shutdown_notice would crash.
if (conn_ && started_) nghttp3_conn_submit_shutdown_notice(*this);
}
void CompleteShutdown() override {
// Same guard as BeginShutdown — nghttp3_conn_shutdown asserts
// that the control stream is bound (conn->tx.ctrl != NULL).
if (conn_ && started_) nghttp3_conn_shutdown(*this);
}
bool ReceiveStreamData(stream_id id,
const uint8_t* data,
size_t datalen,
const Stream::ReceiveDataFlags& flags,
void* unused) override {
Debug(&session(),
"HTTP/3 application received %zu bytes of data "
"on stream %" PRIi64 ". Is final? %d. Is early? %d",
datalen,
id,
flags.fin,
flags.early);
auto nread = nghttp3_conn_read_stream2(
*this, id, data, datalen, flags.fin ? 1 : 0, uv_hrtime());
if (nread < 0) {
Debug(&session(),
"HTTP/3 application failed to read stream data: %s",
nghttp3_strerror(nread));
return false;
}
if (nread > 0) {
Debug(&session(),
"Extending stream and connection offset by %zd bytes",
nread);
session().ExtendStreamOffset(id, nread);
session().ExtendOffset(nread);
}
// If this data arrived as 0-RTT, mark the stream. We set it after
// nghttp3_conn_read_stream2 because the stream may not exist until
// nghttp3 processes the headers (via on_begin_headers).
if (flags.early) {
if (auto stream = session().FindStream(id)) {
stream->set_early();
}
}
return true;
}
bool AcknowledgeStreamData(stream_id id, size_t datalen) override {
Debug(&session(),
"HTTP/3 application received acknowledgement for %zu bytes of data "
"on stream %" PRIi64,
datalen,
id);
return nghttp3_conn_add_ack_offset(*this, id, datalen) == 0;
}
bool CanAddHeader(size_t current_count,
size_t current_headers_length,
size_t this_header_length) override {
// We cannot add the header if we've either reached
// * the max number of header pairs or
// * the max number of header bytes (name + value combined)
return (current_count < options_.max_header_pairs) &&
(current_headers_length + this_header_length) <=
options_.max_header_length;
}
bool stream_fin_managed_by_application() const override { return true; }
void StreamWriteShut(stream_id id) override {
nghttp3_conn_shutdown_stream_write(*this, id);
}
void BlockStream(stream_id id) override {
nghttp3_conn_block_stream(*this, id);
Application::BlockStream(id);
}
void ResumeStream(stream_id id) override {
nghttp3_conn_resume_stream(*this, id);
Application::ResumeStream(id);
}
void ExtendMaxStreams(EndpointLabel label,
Direction direction,
uint64_t max_streams) override {
switch (label) {
case EndpointLabel::LOCAL:
return;
case EndpointLabel::REMOTE: {
switch (direction) {
case Direction::BIDIRECTIONAL: {
Debug(&session(),
"HTTP/3 application extending max bidi streams by %" PRIu64,
max_streams);
ngtcp2_conn_extend_max_streams_bidi(
session(), static_cast<size_t>(max_streams));
break;
}
case Direction::UNIDIRECTIONAL: {
Debug(&session(),
"HTTP/3 application extending max uni streams by %" PRIu64,
max_streams);
ngtcp2_conn_extend_max_streams_uni(
session(), static_cast<size_t>(max_streams));
break;
}
}
}
}
}
void ExtendMaxStreamData(Stream* stream, uint64_t max_data) override {
Debug(&session(),
"HTTP/3 application extending max stream data to %" PRIu64,
max_data);
nghttp3_conn_unblock_stream(*this, stream->id());
}
void CollectSessionTicketAppData(
SessionTicket::AppData* app_data) const override {
uint8_t buf[kSessionTicketAppDataSize];
buf[0] = static_cast<uint8_t>(Type::HTTP3);
buf[1] = kSessionTicketAppDataVersion;
uint8_t* payload = buf + kSessionTicketAppDataHeaderSize;
WriteBE64(payload, options_.max_field_section_size);
WriteBE64(payload + 8, options_.qpack_max_dtable_capacity);
WriteBE64(payload + 16, options_.qpack_encoder_max_dtable_capacity);
WriteBE64(payload + 24, options_.qpack_blocked_streams);
payload[32] = options_.enable_connect_protocol ? 1 : 0;
payload[33] = options_.enable_datagrams ? 1 : 0;
uLong crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, payload, kSessionTicketAppDataPayloadSize);
WriteBE32(buf + 2, static_cast<uint32_t>(crc));
app_data->Set(
uv_buf_init(reinterpret_cast<char*>(buf), kSessionTicketAppDataSize));
}
SessionTicket::AppData::Status ExtractSessionTicketAppData(
const SessionTicket::AppData& app_data,
SessionTicket::AppData::Source::Flag flag) override {
auto data = app_data.Get();
if (!data || data->len != kSessionTicketAppDataSize) {
return SessionTicket::AppData::Status::TICKET_IGNORE_RENEW;
}
const uint8_t* buf = reinterpret_cast<const uint8_t*>(data->base);
// buf[0] is the application type byte, buf[1] is the version.
if (buf[0] != static_cast<uint8_t>(Type::HTTP3) ||
buf[1] != kSessionTicketAppDataVersion) {
Debug(&session(),
"Ticket app data rejected: type=%d version=%d "
"(expected type=%d version=%d)",
buf[0],
buf[1],
static_cast<uint8_t>(Type::HTTP3),
kSessionTicketAppDataVersion);
return SessionTicket::AppData::Status::TICKET_IGNORE_RENEW;
}
const uint8_t* payload = buf + kSessionTicketAppDataHeaderSize;
uint32_t stored_crc = ReadBE32(buf + 2);
uLong computed_crc = crc32(0L, Z_NULL, 0);
computed_crc =
crc32(computed_crc, payload, kSessionTicketAppDataPayloadSize);
if (stored_crc != static_cast<uint32_t>(computed_crc)) {
Debug(&session(),
"Ticket app data rejected: CRC mismatch "
"(stored=%u computed=%u)",
stored_crc,
static_cast<uint32_t>(computed_crc));
return SessionTicket::AppData::Status::TICKET_IGNORE_RENEW;
}
uint64_t stored_max_field_section_size = ReadBE64(payload);
uint64_t stored_qpack_max_dtable_capacity = ReadBE64(payload + 8);
uint64_t stored_qpack_encoder_max_dtable_capacity = ReadBE64(payload + 16);
uint64_t stored_qpack_blocked_streams = ReadBE64(payload + 24);
bool stored_enable_connect_protocol = payload[32] != 0;
bool stored_enable_datagrams = payload[33] != 0;
Debug(&session(),
"Ticket app data: stored mfss=%" PRIu64 " qmdc=%" PRIu64
" qemdc=%" PRIu64 " qbs=%" PRIu64 " ecp=%d ed=%d",
stored_max_field_section_size,
stored_qpack_max_dtable_capacity,
stored_qpack_encoder_max_dtable_capacity,
stored_qpack_blocked_streams,
stored_enable_connect_protocol,
stored_enable_datagrams);
Debug(&session(),
"Current opts: mfss=%" PRIu64 " qmdc=%" PRIu64 " qemdc=%" PRIu64
" qbs=%" PRIu64 " ecp=%d ed=%d",
options_.max_field_section_size,
options_.qpack_max_dtable_capacity,
options_.qpack_encoder_max_dtable_capacity,
options_.qpack_blocked_streams,
options_.enable_connect_protocol,
options_.enable_datagrams);
if (options_.max_field_section_size < stored_max_field_section_size ||
options_.qpack_max_dtable_capacity < stored_qpack_max_dtable_capacity ||
options_.qpack_encoder_max_dtable_capacity <
stored_qpack_encoder_max_dtable_capacity ||
options_.qpack_blocked_streams < stored_qpack_blocked_streams ||
(stored_enable_connect_protocol && !options_.enable_connect_protocol) ||
(stored_enable_datagrams && !options_.enable_datagrams)) {
Debug(&session(), "Ticket app data REJECTED");
return SessionTicket::AppData::Status::TICKET_IGNORE_RENEW;
}
Debug(&session(), "Ticket app data ACCEPTED");
return flag == SessionTicket::AppData::Source::Flag::STATUS_RENEW
? SessionTicket::AppData::Status::TICKET_USE_RENEW
: SessionTicket::AppData::Status::TICKET_USE;
}
bool ApplySessionTicketData(const PendingTicketAppData& data) override {
if (!std::holds_alternative<Http3TicketData>(data)) return false;
const auto& ticket = std::get<Http3TicketData>(data);
// Validate that current settings are >= stored settings.
return options_.max_field_section_size >= ticket.max_field_section_size &&
options_.qpack_max_dtable_capacity >=
ticket.qpack_max_dtable_capacity &&
options_.qpack_encoder_max_dtable_capacity >=
ticket.qpack_encoder_max_dtable_capacity &&
options_.qpack_blocked_streams >= ticket.qpack_blocked_streams &&
(!ticket.enable_connect_protocol ||
options_.enable_connect_protocol) &&
(!ticket.enable_datagrams || options_.enable_datagrams);
}
void ReceiveStreamClose(Stream* stream,
QuicError&& error = QuicError()) override {
Debug(
&session(), "HTTP/3 application closing stream %" PRIi64, stream->id());
error_code code = NGHTTP3_H3_NO_ERROR;
if (error.type() == QuicError::Type::APPLICATION) {
code = error.code();
}
int rv = nghttp3_conn_close_stream(*this, stream->id(), code);
// If the call is successful, Http3Application::OnStreamClose callback will
// be invoked when the stream is ready to be closed. We'll handle destroying
// the actual Stream object there.
if (rv == 0) return;
if (rv == NGHTTP3_ERR_STREAM_NOT_FOUND) {
ExtendMaxStreams(EndpointLabel::REMOTE, stream->direction(), 1);
return;
}
session().SetLastError(
QuicError::ForApplication(nghttp3_err_infer_quic_app_error_code(rv)));
session().Close();
}
void ReceiveStreamReset(Stream* stream,
uint64_t final_size,
QuicError&& error = QuicError()) override {
// We are shutting down the readable side of the local stream here.
Debug(&session(),
"HTTP/3 application resetting stream %" PRIi64,
stream->id());
int rv = nghttp3_conn_shutdown_stream_read(*this, stream->id());
if (rv == 0) {
stream->ReceiveStreamReset(final_size, std::move(error));
return;
}
session().SetLastError(
QuicError::ForApplication(nghttp3_err_infer_quic_app_error_code(rv)));
session().Close();
}
void ReceiveStreamStopSending(Stream* stream,
QuicError&& error = QuicError()) override {
Application::ReceiveStreamStopSending(stream, std::move(error));
}
bool SendHeaders(const Stream& stream,
HeadersKind kind,
const Local<Array>& headers,
HeadersFlags flags = HeadersFlags::NONE) override {
Session::SendPendingDataScope send_scope(&session());
Http3Headers nva(env(), headers);
switch (kind) {
case HeadersKind::HINTS: {
if (!session().is_server()) {
// Client side cannot send hints
return false;
}
Debug(&session(),
"Submitting %" PRIu64 " early hints for stream %" PRIu64,
stream.id());
return nghttp3_conn_submit_info(
*this, stream.id(), nva.data(), nva.length()) == 0;
break;
}
case HeadersKind::INITIAL: {
static constexpr nghttp3_data_reader reader = {on_read_data_callback};
const nghttp3_data_reader* reader_ptr = nullptr;
// If the terminal flag is set, that means that we know we're only
// sending headers and no body and the stream writable side should be
// closed immediately because there is no nghttp3_data_reader provided.
if (flags != HeadersFlags::TERMINAL) {
reader_ptr = &reader;
}
if (session().is_server()) {
// If this is a server, we're submitting a response...
Debug(&session(),
"Submitting %" PRIu64 " response headers for stream %" PRIu64,
nva.length(),
stream.id());
return nghttp3_conn_submit_response(*this,
stream.id(),
nva.data(),
nva.length(),
reader_ptr) == 0;
} else {
// Otherwise we're submitting a request...
Debug(&session(),
"Submitting %" PRIu64 " request headers for stream %" PRIu64,
nva.length(),
stream.id());
return nghttp3_conn_submit_request(*this,
stream.id(),
nva.data(),
nva.length(),
reader_ptr,
const_cast<Stream*>(&stream)) == 0;
}
break;
}
case HeadersKind::TRAILING: {
Debug(&session(),
"Submitting %" PRIu64 " trailing headers for stream %" PRIu64,
nva.length(),
stream.id());
return nghttp3_conn_submit_trailers(
*this, stream.id(), nva.data(), nva.length()) == 0;
break;
}
}
return false;
}
void SetStreamPriority(const Stream& stream,
StreamPriority priority,
StreamPriorityFlags flags) override {
nghttp3_pri pri;
pri.inc = (flags == StreamPriorityFlags::INCREMENTAL) ? 1 : 0;
switch (priority) {
case StreamPriority::HIGH:
pri.urgency = NGHTTP3_URGENCY_HIGH;
break;
case StreamPriority::LOW:
pri.urgency = NGHTTP3_URGENCY_LOW;
break;
default:
pri.urgency = NGHTTP3_DEFAULT_URGENCY;
break;
}
if (session().is_server()) {
nghttp3_conn_set_server_stream_priority(*this, stream.id(), &pri);
} else {
// The client API takes a serialized RFC 9218 priority field value
// (e.g., "u=0, i") rather than an nghttp3_pri struct.
char buf[8];
size_t len = FormatPriority(buf, sizeof(buf), pri);
nghttp3_conn_set_client_stream_priority(
*this, stream.id(), reinterpret_cast<const uint8_t*>(buf), len);
}
}
StreamPriorityResult GetStreamPriority(const Stream& stream) override {
// nghttp3_conn_get_stream_priority is only available on the server
// side, where it reflects the peer's requested priority (e.g., from
// PRIORITY_UPDATE frames). Client-side priority is tracked by the
// Stream itself and returned directly from GetPriority in streams.cc.
if (!session().is_server()) {
auto& stored = stream.stored_priority();
return {stored.priority, stored.flags};
}
nghttp3_pri pri;
if (nghttp3_conn_get_stream_priority(*this, &pri, stream.id()) == 0) {
StreamPriority level;
switch (pri.urgency) {
case NGHTTP3_URGENCY_HIGH:
level = StreamPriority::HIGH;
break;
case NGHTTP3_URGENCY_LOW:
level = StreamPriority::LOW;
break;
default:
level = StreamPriority::DEFAULT;
break;
}
return {level,
pri.inc ? StreamPriorityFlags::INCREMENTAL
: StreamPriorityFlags::NON_INCREMENTAL};
}
return {StreamPriority::DEFAULT, StreamPriorityFlags::NON_INCREMENTAL};
}
int GetStreamData(StreamData* data) override {
data->count = kMaxVectorCount;
ssize_t ret = 0;
Debug(&session(), "HTTP/3 application getting stream data");
if (conn_ && session().max_data_left()) {
ret = nghttp3_conn_writev_stream(
*this, &data->id, &data->fin, *data, data->count);
// A negative return value indicates an error.
if (ret < 0) {
return static_cast<int>(ret);
}
data->count = static_cast<size_t>(ret);
if (data->id >= 0 && data->id != control_stream_id_ &&
data->id != qpack_dec_stream_id_ &&
data->id != qpack_enc_stream_id_) {
data->stream = session().FindStream(data->id);
}
}
return 0;
}
bool StreamCommit(StreamData* data, size_t datalen) override {
Debug(&session(),
"HTTP/3 application committing stream %" PRIi64 " data %zu",
data->id,
datalen);
// datalen is the total framed bytes consumed by ngtcp2, which includes
// H3 frame overhead (HEADERS frame bytes, DATA frame type/length).
// nghttp3 tracks its own offset via add_write_offset.
int err = nghttp3_conn_add_write_offset(*this, data->id, datalen);
if (err != 0) {
session().SetLastError(QuicError::ForApplication(
nghttp3_err_infer_quic_app_error_code(err)));
return false;
}
// Raw application bytes are committed to the stream's outbound
// immediately in on_read_data_callback (so that re-entrant
// fill_outq calls see the advanced position). We only need to
// propagate the fin flag here.
if (data->stream && data->fin) {
data->stream->Commit(0, true);
}
// After body data is committed, if on_read_data_callback signaled
// EOF+NO_END_STREAM (trailers pending), emit the want-trailers
// event to JS. This runs outside the NgHttp3CallbackScope so it's
// safe to call into JS. The JS handler calls sendTrailers() which
// calls nghttp3_conn_submit_trailers, queuing the TRAILERS frame
// for the next writev_stream in the send loop.
if (pending_trailers_stream_ == data->id) {
pending_trailers_stream_ = -1;
if (data->stream) data->stream->EmitWantTrailers();
}
return true;
}
SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(Http3ApplicationImpl)
SET_SELF_SIZE(Http3ApplicationImpl)
private:
inline operator nghttp3_conn*() const {
DCHECK_NOT_NULL(conn_.get());
return conn_.get();
}
inline bool is_control_stream(stream_id id) const {
return id == control_stream_id_ || id == qpack_dec_stream_id_ ||
id == qpack_enc_stream_id_;
}
void BuildOriginPayload() {
// Build the serialized ORIGIN frame payload from the SNI configuration.
// Each origin entry is: 2-byte BE length + origin string.
// Wildcard ('*') entries and entries with authoritative=false are skipped.
auto& sni = session().config().options.sni;
for (auto& [hostname, opts] : sni) {
if (hostname == "*" || !opts.authoritative) continue;
std::string origin = "https://";
origin += hostname;
if (opts.port != 443) {
origin += ":";
origin += std::to_string(opts.port);
}
// 2-byte BE length prefix
uint16_t len = static_cast<uint16_t>(origin.size());
origin_payload_.push_back(static_cast<uint8_t>((len >> 8) & 0xff));
origin_payload_.push_back(static_cast<uint8_t>(len & 0xff));
// Origin string bytes
origin_payload_.insert(
origin_payload_.end(), origin.begin(), origin.end());
}
if (!origin_payload_.empty()) {
origin_vec_ = {origin_payload_.data(), origin_payload_.size()};
}
}
Http3ConnectionPointer InitializeConnection() {
nghttp3_conn* conn = nullptr;
nghttp3_settings settings = options_;
if (!origin_payload_.empty()) {
settings.origin_list = &origin_vec_;
}
if (session().is_server()) {
CHECK_EQ(nghttp3_conn_server_new(
&conn, &kCallbacks, &settings, allocator_, this),
0);
} else {
CHECK_EQ(nghttp3_conn_client_new(
&conn, &kCallbacks, &settings, allocator_, this),
0);
}
return Http3ConnectionPointer(conn);
}
void OnStreamClose(Stream* stream, error_code app_error_code) {
if (app_error_code != NGHTTP3_H3_NO_ERROR) {
Debug(&session(),
"HTTP/3 application received stream close for stream %" PRIi64
" with code %" PRIu64,
stream->id(),
app_error_code);
}
auto direction = stream->direction();
stream->Destroy(QuicError::ForApplication(app_error_code));
ExtendMaxStreams(EndpointLabel::REMOTE, direction, 1);
}
void OnBeginHeaders(stream_id id) {
auto stream = FindOrCreateStream(conn_.get(), &session(), id);
if (!stream) [[unlikely]]
return;
Debug(&session(),
"HTTP/3 application beginning initial block of headers for stream "
"%" PRIi64,
id);
stream->BeginHeaders(HeadersKind::INITIAL);
}
void OnReceiveHeader(stream_id id, std::unique_ptr<Http3Header> header) {
auto stream = session().FindStream(id);
if (!stream) [[unlikely]]
return;
if (header->name() == ":status" && header->value()[0] == '1') {
Debug(&session(),
"HTTP/3 application switching to hints headers for stream %" PRIi64,
stream->id());
stream->set_headers_kind(HeadersKind::HINTS);
}
IF_QUIC_DEBUG(env()) {
Debug(&session(),
"Received header \"%s: %s\"",
header->name(),
header->value());
}
stream->AddHeader(std::move(header));
}
void OnEndHeaders(stream_id id, int fin) {
auto stream = session().FindStream(id);
if (!stream) [[unlikely]]
return;
Debug(&session(),
"HTTP/3 application received end of headers for stream %" PRIi64,
id);
stream->EmitHeaders();
if (fin) {
// The stream is done. There's no more data to receive!
Debug(&session(), "Headers are final for stream %" PRIi64, id);
Stream::ReceiveDataFlags flags{
.fin = true,
.early = false,
};
stream->ReceiveData(nullptr, 0, flags);
}
}
void OnBeginTrailers(stream_id id) {
auto stream = FindOrCreateStream(conn_.get(), &session(), id);
if (!stream) [[unlikely]]
return;
Debug(&session(),
"HTTP/3 application beginning block of trailers for stream %" PRIi64,
id);
stream->BeginHeaders(HeadersKind::TRAILING);
}
void OnReceiveTrailer(stream_id id, std::unique_ptr<Http3Header> header) {
auto stream = session().FindStream(id);
if (!stream) [[unlikely]]
return;
IF_QUIC_DEBUG(env()) {
Debug(&session(),
"Received header \"%s: %s\"",
header->name(),
header->value());
}
stream->AddHeader(std::move(header));
}
void OnEndTrailers(stream_id id, int fin) {
auto stream = session().FindStream(id);
if (!stream) [[unlikely]]
return;
Debug(&session(),
"HTTP/3 application received end of trailers for stream %" PRIi64,
id);
stream->EmitHeaders();
if (fin) {
Debug(&session(), "Trailers are final for stream %" PRIi64, id);
Stream::ReceiveDataFlags flags{
.fin = true,
.early = false,
};
stream->ReceiveData(nullptr, 0, flags);
}
}
void OnEndStream(stream_id id) {
auto stream = session().FindStream(id);
if (!stream) [[unlikely]]
return;
Debug(&session(),
"HTTP/3 application received end of stream for stream %" PRIi64,
id);
Stream::ReceiveDataFlags flags{
.fin = true,
.early = false,
};
stream->ReceiveData(nullptr, 0, flags);
}
void OnStopSending(stream_id id, error_code app_error_code) {
auto stream = session().FindStream(id);
if (!stream) [[unlikely]]
return;
Debug(&session(),
"HTTP/3 application received stop sending for stream %" PRIi64,
id);
stream->ReceiveStopSending(QuicError::ForApplication(app_error_code));
}
void OnResetStream(stream_id id, error_code app_error_code) {
auto stream = session().FindStream(id);
if (!stream) [[unlikely]]
return;
Debug(&session(),
"HTTP/3 application received reset stream for stream %" PRIi64,
id);
stream->ReceiveStreamReset(0, QuicError::ForApplication(app_error_code));
}
void OnShutdown(stream_id id) {
// The peer has sent a GOAWAY frame. This callback fires inside
// NgHttp3CallbackScope, so we cannot call into JS, destroy streams,
// or enter Close(GRACEFUL) here (which could trigger FinishClose and
// deferred destroy, preventing PostReceive from running).
//
// Store the GOAWAY stream ID — PostReceive() handles everything
// outside all callback scopes. For the shutdown notice (first phase,
// sentinel ID), we still store it so PostReceive knows to enter
// graceful close mode. For the final GOAWAY (real stream ID), we
// overwrite with the lower value.
Debug(&session(), "HTTP/3 received GOAWAY (id=%" PRIi64 ")", id);
pending_goaway_id_ = id;
}
void PostReceive() override {
if (pending_goaway_id_ < 0) return;
stream_id goaway_id = pending_goaway_id_;
pending_goaway_id_ = -1;
bool is_notice =
static_cast<uint64_t>(goaway_id) >= NGHTTP3_SHUTDOWN_NOTICE_STREAM_ID;
// For the shutdown notice, replace the sentinel stream ID with -1
// so JS sees a clean marker instead of a huge implementation detail.
stream_id emit_id = is_notice ? -1 : goaway_id;
if (!is_notice) {
// Final GOAWAY: destroy client-initiated bidi streams with
// IDs > goaway_id. These were not processed by the peer and
// can be retried. Copy the map because Destroy modifies it.
auto streams = session().streams();
for (auto& [id, stream] : streams) {
if (session().is_destroyed()) return;
if (ngtcp2_is_bidi_stream(id) && id > goaway_id) {
stream->Destroy(
QuicError::ForApplication(NGHTTP3_H3_REQUEST_REJECTED));
}
}
if (session().is_destroyed()) return;
}
// Notify JS for both notice and final GOAWAY. The notice uses
// -1 to signal "server is shutting down, stop new requests" without
// implying any specific stream boundary. The final GOAWAY (if it
// arrives separately) provides the exact stream ID for retry decisions.
//
// We do NOT call Close(GRACEFUL) here. The JS ongoaway handler sets
// isPendingClose (preventing new streams). The session closes naturally
// when the peer sends CONNECTION_CLOSE after all streams finish.
// Calling Close(GRACEFUL) would send a GOAWAY back and trigger
// BeginShutdown, which can interfere with in-progress streams.
session().EmitGoaway(emit_id);
}
void OnReceiveSettings(const nghttp3_proto_settings* settings) {