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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
|
#ifndef SRC_NODE_HTTP2_H_
#define SRC_NODE_HTTP2_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
// FIXME(joyeecheung): nghttp2.h needs stdint.h to compile on Windows
#include <cstdint>
#include "nghttp2/nghttp2.h"
#include "allocated_buffer.h"
#include "aliased_struct.h"
#include "node_http2_state.h"
#include "node_http_common.h"
#include "node_mem.h"
#include "node_perf.h"
#include "stream_base.h"
#include "string_bytes.h"
#include <algorithm>
#include <queue>
namespace node {
namespace http2 {
// Constants in all caps are exported as user-facing constants
// in JavaScript. Constants using the kName pattern are internal
// only.
// We strictly limit the number of outstanding unacknowledged PINGS a user
// may send in order to prevent abuse. The current default cap is 10. The
// user may set a different limit using a per Http2Session configuration
// option.
constexpr size_t kDefaultMaxPings = 10;
// Also strictly limit the number of outstanding SETTINGS frames a user sends
constexpr size_t kDefaultMaxSettings = 10;
// Default maximum total memory cap for Http2Session.
constexpr uint64_t kDefaultMaxSessionMemory = 10000000;
// These are the standard HTTP/2 defaults as specified by the RFC
constexpr uint32_t DEFAULT_SETTINGS_HEADER_TABLE_SIZE = 4096;
constexpr uint32_t DEFAULT_SETTINGS_ENABLE_PUSH = 1;
constexpr uint32_t DEFAULT_SETTINGS_MAX_CONCURRENT_STREAMS = 0xffffffffu;
constexpr uint32_t DEFAULT_SETTINGS_INITIAL_WINDOW_SIZE = 65535;
constexpr uint32_t DEFAULT_SETTINGS_MAX_FRAME_SIZE = 16384;
constexpr uint32_t DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE = 65535;
constexpr uint32_t DEFAULT_SETTINGS_ENABLE_CONNECT_PROTOCOL = 0;
constexpr uint32_t MAX_MAX_FRAME_SIZE = 16777215;
constexpr uint32_t MIN_MAX_FRAME_SIZE = DEFAULT_SETTINGS_MAX_FRAME_SIZE;
constexpr uint32_t MAX_INITIAL_WINDOW_SIZE = 2147483647;
// Stream is not going to have any DATA frames
constexpr int STREAM_OPTION_EMPTY_PAYLOAD = 0x1;
// Stream might have trailing headers
constexpr int STREAM_OPTION_GET_TRAILERS = 0x2;
// Http2Stream internal states
constexpr int kStreamStateNone = 0x0;
constexpr int kStreamStateShut = 0x1;
constexpr int kStreamStateReadStart = 0x2;
constexpr int kStreamStateReadPaused = 0x4;
constexpr int kStreamStateClosed = 0x8;
constexpr int kStreamStateDestroyed = 0x10;
constexpr int kStreamStateTrailers = 0x20;
// Http2Session internal states
constexpr int kSessionStateNone = 0x0;
constexpr int kSessionStateHasScope = 0x1;
constexpr int kSessionStateWriteScheduled = 0x2;
constexpr int kSessionStateClosed = 0x4;
constexpr int kSessionStateClosing = 0x8;
constexpr int kSessionStateSending = 0x10;
constexpr int kSessionStateWriteInProgress = 0x20;
constexpr int kSessionStateReadingStopped = 0x40;
constexpr int kSessionStateReceivePaused = 0x80;
// The Padding Strategy determines the method by which extra padding is
// selected for HEADERS and DATA frames. These are configurable via the
// options passed in to a Http2Session object.
enum PaddingStrategy {
// No padding strategy. This is the default.
PADDING_STRATEGY_NONE,
// Attempts to ensure that the frame is 8-byte aligned
PADDING_STRATEGY_ALIGNED,
// Padding will ensure all data frames are maxFrameSize
PADDING_STRATEGY_MAX,
// Removed and turned into an alias because it is unreasonably expensive for
// very little benefit.
PADDING_STRATEGY_CALLBACK = PADDING_STRATEGY_ALIGNED
};
enum SessionType {
NGHTTP2_SESSION_SERVER,
NGHTTP2_SESSION_CLIENT
};
template <typename T, void(*fn)(T*)>
struct Nghttp2Deleter {
void operator()(T* ptr) const noexcept { fn(ptr); }
};
using Nghttp2OptionPointer =
std::unique_ptr<nghttp2_option,
Nghttp2Deleter<nghttp2_option, nghttp2_option_del>>;
using Nghttp2SessionPointer =
std::unique_ptr<nghttp2_session,
Nghttp2Deleter<nghttp2_session, nghttp2_session_del>>;
using Nghttp2SessionCallbacksPointer =
std::unique_ptr<nghttp2_session_callbacks,
Nghttp2Deleter<nghttp2_session_callbacks,
nghttp2_session_callbacks_del>>;
struct Http2HeadersTraits {
typedef nghttp2_nv nv_t;
};
struct Http2RcBufferPointerTraits {
typedef nghttp2_rcbuf rcbuf_t;
typedef nghttp2_vec vector_t;
static void inc(rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
nghttp2_rcbuf_incref(buf);
}
static void dec(rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
nghttp2_rcbuf_decref(buf);
}
static vector_t get_vec(rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
return nghttp2_rcbuf_get_buf(buf);
}
static bool is_static(const rcbuf_t* buf) {
CHECK_NOT_NULL(buf);
return nghttp2_rcbuf_is_static(buf);
}
};
using Http2Headers = NgHeaders<Http2HeadersTraits>;
using Http2RcBufferPointer = NgRcBufPointer<Http2RcBufferPointerTraits>;
struct NgHttp2StreamWrite : public MemoryRetainer {
BaseObjectPtr<AsyncWrap> req_wrap;
uv_buf_t buf;
inline explicit NgHttp2StreamWrite(uv_buf_t buf_) : buf(buf_) {}
inline NgHttp2StreamWrite(BaseObjectPtr<AsyncWrap> req_wrap, uv_buf_t buf_) :
req_wrap(std::move(req_wrap)), buf(buf_) {}
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(NgHttp2StreamWrite)
SET_SELF_SIZE(NgHttp2StreamWrite)
};
typedef uint32_t(*get_setting)(nghttp2_session* session,
nghttp2_settings_id id);
class Http2Ping;
class Http2Session;
class Http2Settings;
class Http2Stream;
class Origins;
// This scope should be present when any call into nghttp2 that may schedule
// data to be written to the underlying transport is made, and schedules
// such a write automatically once the scope is exited.
class Http2Scope {
public:
explicit Http2Scope(Http2Stream* stream);
explicit Http2Scope(Http2Session* session);
~Http2Scope();
private:
BaseObjectPtr<Http2Session> session_;
};
// The Http2Options class is used to parse the options object passed in to
// a Http2Session object and convert those into an appropriate nghttp2_option
// struct. This is the primary mechanism by which the Http2Session object is
// configured.
class Http2Options {
public:
Http2Options(Http2State* http2_state,
SessionType type);
~Http2Options() = default;
nghttp2_option* operator*() const {
return options_.get();
}
void set_max_header_pairs(uint32_t max) {
max_header_pairs_ = max;
}
uint32_t max_header_pairs() const {
return max_header_pairs_;
}
void set_padding_strategy(PaddingStrategy val) {
padding_strategy_ = val;
}
PaddingStrategy padding_strategy() const {
return padding_strategy_;
}
void set_max_outstanding_pings(size_t max) {
max_outstanding_pings_ = max;
}
size_t max_outstanding_pings() const {
return max_outstanding_pings_;
}
void set_max_outstanding_settings(size_t max) {
max_outstanding_settings_ = max;
}
size_t max_outstanding_settings() const {
return max_outstanding_settings_;
}
void set_max_session_memory(uint64_t max) {
max_session_memory_ = max;
}
uint64_t max_session_memory() const {
return max_session_memory_;
}
private:
Nghttp2OptionPointer options_;
uint64_t max_session_memory_ = kDefaultMaxSessionMemory;
uint32_t max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS;
PaddingStrategy padding_strategy_ = PADDING_STRATEGY_NONE;
size_t max_outstanding_pings_ = kDefaultMaxPings;
size_t max_outstanding_settings_ = kDefaultMaxSettings;
};
struct Http2Priority : public nghttp2_priority_spec {
Http2Priority(Environment* env,
v8::Local<v8::Value> parent,
v8::Local<v8::Value> weight,
v8::Local<v8::Value> exclusive);
};
class Http2StreamListener : public StreamListener {
public:
uv_buf_t OnStreamAlloc(size_t suggested_size) override;
void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
};
struct Http2HeaderTraits {
typedef Http2RcBufferPointer rcbufferpointer_t;
typedef Http2Session allocator_t;
// HTTP/2 does not support identifying header names by token id.
// HTTP/3 will, however, so we prepare for that now.
static const char* ToHttpHeaderName(int32_t token) { return nullptr; }
};
using Http2Header = NgHeader<Http2HeaderTraits>;
class Http2Stream : public AsyncWrap,
public StreamBase {
public:
static Http2Stream* New(
Http2Session* session,
int32_t id,
nghttp2_headers_category category = NGHTTP2_HCAT_HEADERS,
int options = 0);
~Http2Stream() override;
nghttp2_stream* operator*() const;
nghttp2_stream* stream() const;
Http2Session* session() { return session_.get(); }
const Http2Session* session() const { return session_.get(); }
// Required for StreamBase
int ReadStart() override;
// Required for StreamBase
int ReadStop() override;
// Required for StreamBase
ShutdownWrap* CreateShutdownWrap(v8::Local<v8::Object> object) override;
int DoShutdown(ShutdownWrap* req_wrap) override;
bool HasWantsWrite() const override { return true; }
// Initiate a response on this stream.
int SubmitResponse(const Http2Headers& headers, int options);
// Submit informational headers for this stream
int SubmitInfo(const Http2Headers& headers);
// Submit trailing headers for this stream
int SubmitTrailers(const Http2Headers& headers);
void OnTrailers();
// Submit a PRIORITY frame for this stream
int SubmitPriority(const Http2Priority& priority, bool silent = false);
// Submits an RST_STREAM frame using the given code
void SubmitRstStream(const uint32_t code);
void FlushRstStream();
// Submits a PUSH_PROMISE frame with this stream as the parent.
Http2Stream* SubmitPushPromise(
const Http2Headers& headers,
int32_t* ret,
int options = 0);
void Close(int32_t code);
// Destroy this stream instance and free all held memory.
void Destroy();
bool is_destroyed() const {
return flags_ & kStreamStateDestroyed;
}
bool is_writable() const {
return !(flags_ & kStreamStateShut);
}
bool is_paused() const {
return flags_ & kStreamStateReadPaused;
}
bool is_closed() const {
return flags_ & kStreamStateClosed;
}
bool has_trailers() const {
return flags_ & kStreamStateTrailers;
}
void set_has_trailers(bool on = true) {
if (on)
flags_ |= kStreamStateTrailers;
else
flags_ &= ~kStreamStateTrailers;
}
void set_closed() {
flags_ |= kStreamStateClosed;
}
void set_destroyed() {
flags_ |= kStreamStateDestroyed;
}
void set_not_writable() {
flags_ |= kStreamStateShut;
}
void set_reading(bool on = true) {
if (on) {
flags_ |= kStreamStateReadStart;
set_paused(false);
} else {}
}
void set_paused(bool on = true) {
if (on)
flags_ |= kStreamStateReadPaused;
else
flags_ &= ~kStreamStateReadPaused;
}
// Returns true if this stream is in the reading state, which occurs when
// the kStreamStateReadStart flag has been set and the
// kStreamStateReadPaused flag is *not* set.
bool is_reading() const {
return flags_ & kStreamStateReadStart && !is_paused();
}
// Returns the RST_STREAM code used to close this stream
int32_t code() const { return code_; }
// Returns the stream identifier for this stream
int32_t id() const { return id_; }
void IncrementAvailableOutboundLength(size_t amount);
void DecrementAvailableOutboundLength(size_t amount);
bool AddHeader(nghttp2_rcbuf* name, nghttp2_rcbuf* value, uint8_t flags);
template <typename Fn>
void TransferHeaders(Fn&& fn) {
size_t i = 0;
for (const auto& header : current_headers_ )
fn(header, i++);
current_headers_.clear();
}
size_t headers_count() const {
return current_headers_.size();
}
nghttp2_headers_category headers_category() const {
return current_headers_category_;
}
void StartHeaders(nghttp2_headers_category category);
// Required for StreamBase
bool IsAlive() override {
return true;
}
// Required for StreamBase
bool IsClosing() override {
return false;
}
AsyncWrap* GetAsyncWrap() override { return this; }
int DoWrite(WriteWrap* w, uv_buf_t* bufs, size_t count,
uv_stream_t* send_handle) override;
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Http2Stream)
SET_SELF_SIZE(Http2Stream)
std::string diagnostic_name() const override;
// JavaScript API
static void GetID(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Destroy(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Priority(const v8::FunctionCallbackInfo<v8::Value>& args);
static void PushPromise(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RefreshState(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Info(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Trailers(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Respond(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RstStream(const v8::FunctionCallbackInfo<v8::Value>& args);
class Provider;
struct Statistics {
uint64_t start_time;
uint64_t end_time;
uint64_t first_header; // Time first header was received
uint64_t first_byte; // Time first DATA frame byte was received
uint64_t first_byte_sent; // Time first DATA frame byte was sent
uint64_t sent_bytes;
uint64_t received_bytes;
};
Statistics statistics_ = {};
private:
Http2Stream(Http2Session* session,
v8::Local<v8::Object> obj,
int32_t id,
nghttp2_headers_category category,
int options);
void EmitStatistics();
BaseObjectWeakPtr<Http2Session> session_; // The Parent HTTP/2 Session
int32_t id_ = 0; // The Stream Identifier
int32_t code_ = NGHTTP2_NO_ERROR; // The RST_STREAM code (if any)
int flags_ = kStreamStateNone; // Internal state flags
uint32_t max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS;
uint32_t max_header_length_ = DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE;
// The Current Headers block... As headers are received for this stream,
// they are temporarily stored here until the OnFrameReceived is called
// signalling the end of the HEADERS frame
nghttp2_headers_category current_headers_category_ = NGHTTP2_HCAT_HEADERS;
uint32_t current_headers_length_ = 0; // total number of octets
std::vector<Http2Header> current_headers_;
// This keeps track of the amount of data read from the socket while the
// socket was in paused mode. When `ReadStart()` is called (and not before
// then), we tell nghttp2 that we consumed that data to get proper
// backpressure handling.
size_t inbound_consumed_data_while_paused_ = 0;
// Outbound Data... This is the data written by the JS layer that is
// waiting to be written out to the socket.
std::queue<NgHttp2StreamWrite> queue_;
size_t available_outbound_length_ = 0;
Http2StreamListener stream_listener_;
friend class Http2Session;
};
class Http2Stream::Provider {
public:
Provider(Http2Stream* stream, int options);
explicit Provider(int options);
virtual ~Provider();
nghttp2_data_provider* operator*() {
return !empty_ ? &provider_ : nullptr;
}
class FD;
class Stream;
protected:
nghttp2_data_provider provider_;
private:
bool empty_ = false;
};
class Http2Stream::Provider::Stream : public Http2Stream::Provider {
public:
Stream(Http2Stream* stream, int options);
explicit Stream(int options);
static ssize_t OnRead(nghttp2_session* session,
int32_t id,
uint8_t* buf,
size_t length,
uint32_t* flags,
nghttp2_data_source* source,
void* user_data);
};
struct SessionJSFields {
uint8_t bitfield;
uint8_t priority_listener_count;
uint8_t frame_error_listener_count;
uint32_t max_invalid_frames = 1000;
uint32_t max_rejected_streams = 100;
};
// Indices for js_fields_, which serves as a way to communicate data with JS
// land fast. In particular, we store information about the number/presence
// of certain event listeners in JS, and skip calls from C++ into JS if they
// are missing.
enum SessionUint8Fields {
kBitfield = offsetof(SessionJSFields, bitfield), // See below
kSessionPriorityListenerCount =
offsetof(SessionJSFields, priority_listener_count),
kSessionFrameErrorListenerCount =
offsetof(SessionJSFields, frame_error_listener_count),
kSessionMaxInvalidFrames = offsetof(SessionJSFields, max_invalid_frames),
kSessionMaxRejectedStreams = offsetof(SessionJSFields, max_rejected_streams),
kSessionUint8FieldCount = sizeof(SessionJSFields)
};
enum SessionBitfieldFlags {
kSessionHasRemoteSettingsListeners,
kSessionRemoteSettingsIsUpToDate,
kSessionHasPingListeners,
kSessionHasAltsvcListeners
};
class Http2Session : public AsyncWrap,
public StreamListener,
public mem::NgLibMemoryManager<Http2Session, nghttp2_mem> {
public:
Http2Session(Http2State* http2_state,
v8::Local<v8::Object> wrap,
SessionType type = NGHTTP2_SESSION_SERVER);
~Http2Session() override;
StreamBase* underlying_stream() {
return static_cast<StreamBase*>(stream_);
}
void Close(uint32_t code = NGHTTP2_NO_ERROR,
bool socket_closed = false);
void Consume(v8::Local<v8::Object> stream);
void Goaway(uint32_t code, int32_t lastStreamID,
const uint8_t* data, size_t len);
void AltSvc(int32_t id,
uint8_t* origin,
size_t origin_len,
uint8_t* value,
size_t value_len);
void Origin(const Origins& origins);
uint8_t SendPendingData();
// Submits a new request. If the request is a success, assigned
// will be a pointer to the Http2Stream instance assigned.
// This only works if the session is a client session.
Http2Stream* SubmitRequest(
const Http2Priority& priority,
const Http2Headers& headers,
int32_t* ret,
int options = 0);
SessionType type() const { return session_type_; }
nghttp2_session* session() const { return session_.get(); }
nghttp2_session* operator*() { return session_.get(); }
uint32_t max_header_pairs() const { return max_header_pairs_; }
const char* TypeName() const;
bool is_destroyed() {
return (flags_ & kSessionStateClosed) || session_ == nullptr;
}
void set_destroyed() {
flags_ |= kSessionStateClosed;
}
#define IS_FLAG(name, flag) \
bool is_##name() const { return flags_ & flag; } \
void set_##name(bool on = true) { \
if (on) \
flags_ |= flag; \
else \
flags_ &= ~flag; \
}
IS_FLAG(in_scope, kSessionStateHasScope)
IS_FLAG(write_scheduled, kSessionStateWriteScheduled)
IS_FLAG(closing, kSessionStateClosing)
IS_FLAG(sending, kSessionStateSending)
IS_FLAG(write_in_progress, kSessionStateWriteInProgress)
IS_FLAG(reading_stopped, kSessionStateReadingStopped)
IS_FLAG(receive_paused, kSessionStateReceivePaused)
#undef IS_FLAG
// Schedule a write if nghttp2 indicates it wants to write to the socket.
void MaybeScheduleWrite();
// Stop reading if nghttp2 doesn't want to anymore.
void MaybeStopReading();
// Returns pointer to the stream, or nullptr if stream does not exist
BaseObjectPtr<Http2Stream> FindStream(int32_t id);
bool CanAddStream();
// Adds a stream instance to this session
void AddStream(Http2Stream* stream);
// Removes a stream instance from this session
BaseObjectPtr<Http2Stream> RemoveStream(int32_t id);
// Indicates whether there currently exist outgoing buffers for this stream.
bool HasWritesOnSocketForStream(Http2Stream* stream);
// Write data from stream_buf_ to the session
ssize_t ConsumeHTTP2Data();
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Http2Session)
SET_SELF_SIZE(Http2Session)
std::string diagnostic_name() const override;
// Schedule an RstStream for after the current write finishes.
void AddPendingRstStream(int32_t stream_id) {
pending_rst_streams_.emplace_back(stream_id);
}
bool has_pending_rststream(int32_t stream_id) {
return pending_rst_streams_.end() !=
std::find(pending_rst_streams_.begin(),
pending_rst_streams_.end(),
stream_id);
}
// Handle reads/writes from the underlying network transport.
uv_buf_t OnStreamAlloc(size_t suggested_size) override;
void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
void OnStreamAfterWrite(WriteWrap* w, int status) override;
// Implementation for mem::NgLibMemoryManager
void CheckAllocatedSize(size_t previous_size) const;
void IncreaseAllocatedSize(size_t size);
void DecreaseAllocatedSize(size_t size);
// The JavaScript API
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Consume(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Destroy(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Settings(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Request(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetNextStreamID(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Goaway(const v8::FunctionCallbackInfo<v8::Value>& args);
static void UpdateChunksSent(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RefreshState(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Ping(const v8::FunctionCallbackInfo<v8::Value>& args);
static void AltSvc(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Origin(const v8::FunctionCallbackInfo<v8::Value>& args);
template <get_setting fn>
static void RefreshSettings(const v8::FunctionCallbackInfo<v8::Value>& args);
uv_loop_t* event_loop() const {
return env()->event_loop();
}
Http2State* http2_state() const { return http2_state_.get(); }
BaseObjectPtr<Http2Ping> PopPing();
bool AddPing(const uint8_t* data, v8::Local<v8::Function> callback);
BaseObjectPtr<Http2Settings> PopSettings();
bool AddSettings(v8::Local<v8::Function> callback);
void IncrementCurrentSessionMemory(uint64_t amount) {
current_session_memory_ += amount;
}
void DecrementCurrentSessionMemory(uint64_t amount) {
DCHECK_LE(amount, current_session_memory_);
current_session_memory_ -= amount;
}
// Tell our custom memory allocator that this rcbuf is independent of
// this session now, and may outlive it.
void StopTrackingRcbuf(nghttp2_rcbuf* buf);
// Returns the current session memory including memory allocated by nghttp2,
// the current outbound storage queue, and pending writes.
uint64_t current_session_memory() const {
uint64_t total = current_session_memory_ + sizeof(Http2Session);
total += current_nghttp2_memory_;
total += outgoing_storage_.size();
return total;
}
// Return true if current_session_memory + amount is less than the max
bool has_available_session_memory(uint64_t amount) const {
return current_session_memory() + amount <= max_session_memory_;
}
struct Statistics {
uint64_t start_time;
uint64_t end_time;
uint64_t ping_rtt;
uint64_t data_sent;
uint64_t data_received;
uint32_t frame_count;
uint32_t frame_sent;
int32_t stream_count;
size_t max_concurrent_streams;
double stream_average_duration;
};
Statistics statistics_ = {};
private:
void EmitStatistics();
// Frame Padding Strategies
ssize_t OnDWordAlignedPadding(size_t frameLength,
size_t maxPayloadLen);
ssize_t OnMaxFrameSizePadding(size_t frameLength,
size_t maxPayloadLen);
// Frame Handler
int HandleDataFrame(const nghttp2_frame* frame);
void HandleGoawayFrame(const nghttp2_frame* frame);
void HandleHeadersFrame(const nghttp2_frame* frame);
void HandlePriorityFrame(const nghttp2_frame* frame);
void HandleSettingsFrame(const nghttp2_frame* frame);
void HandlePingFrame(const nghttp2_frame* frame);
void HandleAltSvcFrame(const nghttp2_frame* frame);
void HandleOriginFrame(const nghttp2_frame* frame);
// nghttp2 callbacks
static int OnBeginHeadersCallback(
nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data);
static int OnHeaderCallback(
nghttp2_session* session,
const nghttp2_frame* frame,
nghttp2_rcbuf* name,
nghttp2_rcbuf* value,
uint8_t flags,
void* user_data);
static int OnFrameReceive(
nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data);
static int OnFrameNotSent(
nghttp2_session* session,
const nghttp2_frame* frame,
int error_code,
void* user_data);
static int OnFrameSent(
nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data);
static int OnStreamClose(
nghttp2_session* session,
int32_t id,
uint32_t code,
void* user_data);
static int OnInvalidHeader(
nghttp2_session* session,
const nghttp2_frame* frame,
nghttp2_rcbuf* name,
nghttp2_rcbuf* value,
uint8_t flags,
void* user_data);
static int OnDataChunkReceived(
nghttp2_session* session,
uint8_t flags,
int32_t id,
const uint8_t* data,
size_t len,
void* user_data);
static ssize_t OnSelectPadding(
nghttp2_session* session,
const nghttp2_frame* frame,
size_t maxPayloadLen,
void* user_data);
static int OnNghttpError(
nghttp2_session* session,
const char* message,
size_t len,
void* user_data);
static int OnSendData(
nghttp2_session* session,
nghttp2_frame* frame,
const uint8_t* framehd,
size_t length,
nghttp2_data_source* source,
void* user_data);
static int OnInvalidFrame(
nghttp2_session* session,
const nghttp2_frame* frame,
int lib_error_code,
void* user_data);
struct Callbacks {
explicit Callbacks(bool kHasGetPaddingCallback);
Nghttp2SessionCallbacksPointer callbacks;
};
/* Use callback_struct_saved[kHasGetPaddingCallback ? 1 : 0] */
static const Callbacks callback_struct_saved[2];
// The underlying nghttp2_session handle
Nghttp2SessionPointer session_;
// JS-accessible numeric fields, as indexed by SessionUint8Fields.
AliasedStruct<SessionJSFields> js_fields_;
// The session type: client or server
SessionType session_type_;
// The maximum number of header pairs permitted for streams on this session
uint32_t max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS;
// The maximum amount of memory allocated for this session
uint64_t max_session_memory_ = kDefaultMaxSessionMemory;
uint64_t current_session_memory_ = 0;
// The amount of memory allocated by nghttp2 internals
uint64_t current_nghttp2_memory_ = 0;
// The collection of active Http2Streams associated with this session
std::unordered_map<int32_t, BaseObjectPtr<Http2Stream>> streams_;
int flags_ = kSessionStateNone;
// The StreamBase instance being used for i/o
PaddingStrategy padding_strategy_ = PADDING_STRATEGY_NONE;
// use this to allow timeout tracking during long-lasting writes
uint32_t chunks_sent_since_last_write_ = 0;
uv_buf_t stream_buf_ = uv_buf_init(nullptr, 0);
// When processing input data, either stream_buf_ab_ or stream_buf_allocation_
// will be set. stream_buf_ab_ is lazily created from stream_buf_allocation_.
v8::Global<v8::ArrayBuffer> stream_buf_ab_;
AllocatedBuffer stream_buf_allocation_;
size_t stream_buf_offset_ = 0;
size_t max_outstanding_pings_ = kDefaultMaxPings;
std::queue<BaseObjectPtr<Http2Ping>> outstanding_pings_;
size_t max_outstanding_settings_ = kDefaultMaxSettings;
std::queue<BaseObjectPtr<Http2Settings>> outstanding_settings_;
std::vector<NgHttp2StreamWrite> outgoing_buffers_;
std::vector<uint8_t> outgoing_storage_;
size_t outgoing_length_ = 0;
std::vector<int32_t> pending_rst_streams_;
// Count streams that have been rejected while being opened. Exceeding a fixed
// limit will result in the session being destroyed, as an indication of a
// misbehaving peer. This counter is reset once new streams are being
// accepted again.
uint32_t rejected_stream_count_ = 0;
// Also use the invalid frame count as a measure for rejecting input frames.
uint32_t invalid_frame_count_ = 0;
void PushOutgoingBuffer(NgHttp2StreamWrite&& write);
BaseObjectPtr<Http2State> http2_state_;
void CopyDataIntoOutgoing(const uint8_t* src, size_t src_length);
void ClearOutgoing(int status);
friend class Http2Scope;
friend class Http2StreamListener;
};
class Http2SessionPerformanceEntry : public performance::PerformanceEntry {
public:
Http2SessionPerformanceEntry(
Http2State* http2_state,
const Http2Session::Statistics& stats,
SessionType type) :
performance::PerformanceEntry(
http2_state->env(), "Http2Session", "http2",
stats.start_time,
stats.end_time),
ping_rtt_(stats.ping_rtt),
data_sent_(stats.data_sent),
data_received_(stats.data_received),
frame_count_(stats.frame_count),
frame_sent_(stats.frame_sent),
stream_count_(stats.stream_count),
max_concurrent_streams_(stats.max_concurrent_streams),
stream_average_duration_(stats.stream_average_duration),
session_type_(type),
http2_state_(http2_state) { }
uint64_t ping_rtt() const { return ping_rtt_; }
uint64_t data_sent() const { return data_sent_; }
uint64_t data_received() const { return data_received_; }
uint32_t frame_count() const { return frame_count_; }
uint32_t frame_sent() const { return frame_sent_; }
int32_t stream_count() const { return stream_count_; }
size_t max_concurrent_streams() const { return max_concurrent_streams_; }
double stream_average_duration() const { return stream_average_duration_; }
SessionType type() const { return session_type_; }
Http2State* http2_state() const { return http2_state_.get(); }
void Notify(v8::Local<v8::Value> obj) {
performance::PerformanceEntry::Notify(env(), kind(), obj);
}
private:
uint64_t ping_rtt_;
uint64_t data_sent_;
uint64_t data_received_;
uint32_t frame_count_;
uint32_t frame_sent_;
int32_t stream_count_;
size_t max_concurrent_streams_;
double stream_average_duration_;
SessionType session_type_;
BaseObjectPtr<Http2State> http2_state_;
};
class Http2StreamPerformanceEntry
: public performance::PerformanceEntry {
public:
Http2StreamPerformanceEntry(
Http2State* http2_state,
int32_t id,
const Http2Stream::Statistics& stats) :
performance::PerformanceEntry(
http2_state->env(), "Http2Stream", "http2",
stats.start_time,
stats.end_time),
id_(id),
first_header_(stats.first_header),
first_byte_(stats.first_byte),
first_byte_sent_(stats.first_byte_sent),
sent_bytes_(stats.sent_bytes),
received_bytes_(stats.received_bytes),
http2_state_(http2_state) { }
int32_t id() const { return id_; }
uint64_t first_header() const { return first_header_; }
uint64_t first_byte() const { return first_byte_; }
uint64_t first_byte_sent() const { return first_byte_sent_; }
uint64_t sent_bytes() const { return sent_bytes_; }
uint64_t received_bytes() const { return received_bytes_; }
Http2State* http2_state() const { return http2_state_.get(); }
void Notify(v8::Local<v8::Value> obj) {
performance::PerformanceEntry::Notify(env(), kind(), obj);
}
private:
int32_t id_;
uint64_t first_header_;
uint64_t first_byte_;
uint64_t first_byte_sent_;
uint64_t sent_bytes_;
uint64_t received_bytes_;
BaseObjectPtr<Http2State> http2_state_;
};
class Http2Ping : public AsyncWrap {
public:
explicit Http2Ping(
Http2Session* session,
v8::Local<v8::Object> obj,
v8::Local<v8::Function> callback);
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Http2Ping)
SET_SELF_SIZE(Http2Ping)
void Send(const uint8_t* payload);
void Done(bool ack, const uint8_t* payload = nullptr);
void DetachFromSession();
v8::Local<v8::Function> callback() const;
private:
BaseObjectWeakPtr<Http2Session> session_;
v8::Global<v8::Function> callback_;
uint64_t startTime_;
};
// The Http2Settings class is used to parse the settings passed in for
// an Http2Session, converting those into an array of nghttp2_settings_entry
// structs.
class Http2Settings : public AsyncWrap {
public:
Http2Settings(Http2Session* session,
v8::Local<v8::Object> obj,
v8::Local<v8::Function> callback,
uint64_t start_time = uv_hrtime());
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Http2Settings)
SET_SELF_SIZE(Http2Settings)
void Send();
void Done(bool ack);
v8::Local<v8::Function> callback() const;
// Returns a Buffer instance with the serialized SETTINGS payload
v8::Local<v8::Value> Pack();
static v8::Local<v8::Value> Pack(Http2State* state);
// Resets the default values in the settings buffer
static void RefreshDefaults(Http2State* http2_state);
// Update the local or remote settings for the given session
static void Update(Http2Session* session,
get_setting fn);
private:
static size_t Init(
Http2State* http2_state,
nghttp2_settings_entry* entries);
static v8::Local<v8::Value> Pack(
Environment* env,
size_t count,
const nghttp2_settings_entry* entries);
BaseObjectWeakPtr<Http2Session> session_;
v8::Global<v8::Function> callback_;
uint64_t startTime_;
size_t count_ = 0;
nghttp2_settings_entry entries_[IDX_SETTINGS_COUNT];
};
class Origins {
public:
Origins(Environment* env,
v8::Local<v8::String> origin_string,
size_t origin_count);
~Origins() = default;
const nghttp2_origin_entry* operator*() const {
return reinterpret_cast<const nghttp2_origin_entry*>(buf_.data());
}
size_t length() const {
return count_;
}
private:
size_t count_;
AllocatedBuffer buf_;
};
#define HTTP2_HIDDEN_CONSTANTS(V) \
V(NGHTTP2_HCAT_REQUEST) \
V(NGHTTP2_HCAT_RESPONSE) \
V(NGHTTP2_HCAT_PUSH_RESPONSE) \
V(NGHTTP2_HCAT_HEADERS) \
V(NGHTTP2_NV_FLAG_NONE) \
V(NGHTTP2_NV_FLAG_NO_INDEX) \
V(NGHTTP2_ERR_DEFERRED) \
V(NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE) \
V(NGHTTP2_ERR_INVALID_ARGUMENT) \
V(NGHTTP2_ERR_STREAM_CLOSED) \
V(STREAM_OPTION_EMPTY_PAYLOAD) \
V(STREAM_OPTION_GET_TRAILERS)
#define HTTP2_ERROR_CODES(V) \
V(NGHTTP2_NO_ERROR) \
V(NGHTTP2_PROTOCOL_ERROR) \
V(NGHTTP2_INTERNAL_ERROR) \
V(NGHTTP2_FLOW_CONTROL_ERROR) \
V(NGHTTP2_SETTINGS_TIMEOUT) \
V(NGHTTP2_STREAM_CLOSED) \
V(NGHTTP2_FRAME_SIZE_ERROR) \
V(NGHTTP2_REFUSED_STREAM) \
V(NGHTTP2_CANCEL) \
V(NGHTTP2_COMPRESSION_ERROR) \
V(NGHTTP2_CONNECT_ERROR) \
V(NGHTTP2_ENHANCE_YOUR_CALM) \
V(NGHTTP2_INADEQUATE_SECURITY) \
V(NGHTTP2_HTTP_1_1_REQUIRED) \
#define HTTP2_CONSTANTS(V) \
V(NGHTTP2_ERR_FRAME_SIZE_ERROR) \
V(NGHTTP2_SESSION_SERVER) \
V(NGHTTP2_SESSION_CLIENT) \
V(NGHTTP2_STREAM_STATE_IDLE) \
V(NGHTTP2_STREAM_STATE_OPEN) \
V(NGHTTP2_STREAM_STATE_RESERVED_LOCAL) \
V(NGHTTP2_STREAM_STATE_RESERVED_REMOTE) \
V(NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL) \
V(NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE) \
V(NGHTTP2_STREAM_STATE_CLOSED) \
V(NGHTTP2_FLAG_NONE) \
V(NGHTTP2_FLAG_END_STREAM) \
V(NGHTTP2_FLAG_END_HEADERS) \
V(NGHTTP2_FLAG_ACK) \
V(NGHTTP2_FLAG_PADDED) \
V(NGHTTP2_FLAG_PRIORITY) \
V(DEFAULT_SETTINGS_HEADER_TABLE_SIZE) \
V(DEFAULT_SETTINGS_ENABLE_PUSH) \
V(DEFAULT_SETTINGS_MAX_CONCURRENT_STREAMS) \
V(DEFAULT_SETTINGS_INITIAL_WINDOW_SIZE) \
V(DEFAULT_SETTINGS_MAX_FRAME_SIZE) \
V(DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE) \
V(DEFAULT_SETTINGS_ENABLE_CONNECT_PROTOCOL) \
V(MAX_MAX_FRAME_SIZE) \
V(MIN_MAX_FRAME_SIZE) \
V(MAX_INITIAL_WINDOW_SIZE) \
V(NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) \
V(NGHTTP2_SETTINGS_ENABLE_PUSH) \
V(NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS) \
V(NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE) \
V(NGHTTP2_SETTINGS_MAX_FRAME_SIZE) \
V(NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE) \
V(NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL) \
V(PADDING_STRATEGY_NONE) \
V(PADDING_STRATEGY_ALIGNED) \
V(PADDING_STRATEGY_MAX) \
V(PADDING_STRATEGY_CALLBACK) \
HTTP2_ERROR_CODES(V)
#define HTTP2_SETTINGS(V) \
V(HEADER_TABLE_SIZE) \
V(ENABLE_PUSH) \
V(MAX_CONCURRENT_STREAMS) \
V(INITIAL_WINDOW_SIZE) \
V(MAX_FRAME_SIZE) \
V(MAX_HEADER_LIST_SIZE) \
V(ENABLE_CONNECT_PROTOCOL) \
} // namespace http2
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_HTTP2_H_
|