GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "aliased_buffer.h" |
||
2 |
#include "allocated_buffer-inl.h" |
||
3 |
#include "aliased_struct-inl.h" |
||
4 |
#include "debug_utils-inl.h" |
||
5 |
#include "memory_tracker-inl.h" |
||
6 |
#include "node.h" |
||
7 |
#include "node_buffer.h" |
||
8 |
#include "node_http2.h" |
||
9 |
#include "node_http_common-inl.h" |
||
10 |
#include "node_mem-inl.h" |
||
11 |
#include "node_perf.h" |
||
12 |
#include "node_revert.h" |
||
13 |
#include "stream_base-inl.h" |
||
14 |
#include "util-inl.h" |
||
15 |
|||
16 |
#include <algorithm> |
||
17 |
|||
18 |
namespace node { |
||
19 |
|||
20 |
using v8::Array; |
||
21 |
using v8::ArrayBuffer; |
||
22 |
using v8::ArrayBufferView; |
||
23 |
using v8::Boolean; |
||
24 |
using v8::Context; |
||
25 |
using v8::EscapableHandleScope; |
||
26 |
using v8::Function; |
||
27 |
using v8::FunctionCallbackInfo; |
||
28 |
using v8::FunctionTemplate; |
||
29 |
using v8::HandleScope; |
||
30 |
using v8::Integer; |
||
31 |
using v8::Isolate; |
||
32 |
using v8::Local; |
||
33 |
using v8::MaybeLocal; |
||
34 |
using v8::Number; |
||
35 |
using v8::Object; |
||
36 |
using v8::ObjectTemplate; |
||
37 |
using v8::String; |
||
38 |
using v8::Uint8Array; |
||
39 |
using v8::Undefined; |
||
40 |
using v8::Value; |
||
41 |
|||
42 |
using node::performance::PerformanceEntry; |
||
43 |
namespace http2 { |
||
44 |
|||
45 |
namespace { |
||
46 |
|||
47 |
const char zero_bytes_256[256] = {}; |
||
48 |
|||
49 |
24326 |
bool HasHttp2Observer(Environment* env) { |
|
50 |
24326 |
AliasedUint32Array& observers = env->performance_state()->observers; |
|
51 |
24326 |
return observers[performance::NODE_PERFORMANCE_ENTRY_TYPE_HTTP2] != 0; |
|
52 |
} |
||
53 |
|||
54 |
} // anonymous namespace |
||
55 |
|||
56 |
// These configure the callbacks required by nghttp2 itself. There are |
||
57 |
// two sets of callback functions, one that is used if a padding callback |
||
58 |
// is set, and other that does not include the padding callback. |
||
59 |
✓✓ | 9382 |
const Http2Session::Callbacks Http2Session::callback_struct_saved[2] = { |
60 |
Callbacks(false), |
||
61 |
4691 |
Callbacks(true)}; |
|
62 |
|||
63 |
// The Http2Scope object is used to queue a write to the i/o stream. It is |
||
64 |
// used whenever any action is take on the underlying nghttp2 API that may |
||
65 |
// push data into nghttp2 outbound data queue. |
||
66 |
// |
||
67 |
// For example: |
||
68 |
// |
||
69 |
// Http2Scope h2scope(session); |
||
70 |
// nghttp2_submit_ping(session->session(), ... ); |
||
71 |
// |
||
72 |
// When the Http2Scope passes out of scope and is deconstructed, it will |
||
73 |
// call Http2Session::MaybeScheduleWrite(). |
||
74 |
63462 |
Http2Scope::Http2Scope(Http2Stream* stream) : Http2Scope(stream->session()) {} |
|
75 |
|||
76 |
107786 |
Http2Scope::Http2Scope(Http2Session* session) : session_(session) { |
|
77 |
✗✓ | 107786 |
if (!session_) return; |
78 |
|||
79 |
// If there is another scope further below on the stack, or |
||
80 |
// a write is already scheduled, there's nothing to do. |
||
81 |
✓✓✓✓ ✓✓ |
107786 |
if (session_->is_in_scope() || session_->is_write_scheduled()) { |
82 |
75035 |
session_.reset(); |
|
83 |
75035 |
return; |
|
84 |
} |
||
85 |
32751 |
session_->set_in_scope(); |
|
86 |
} |
||
87 |
|||
88 |
✓✓ | 215572 |
Http2Scope::~Http2Scope() { |
89 |
✓✓ | 107786 |
if (!session_) return; |
90 |
32751 |
session_->set_in_scope(false); |
|
91 |
✓✗ | 32751 |
if (!session_->is_write_scheduled()) |
92 |
32751 |
session_->MaybeScheduleWrite(); |
|
93 |
107786 |
} |
|
94 |
|||
95 |
// The Http2Options object is used during the construction of Http2Session |
||
96 |
// instances to configure an appropriate nghttp2_options struct. The class |
||
97 |
// uses a single TypedArray instance that is shared with the JavaScript side |
||
98 |
// to more efficiently pass values back and forth. |
||
99 |
660 |
Http2Options::Http2Options(Http2State* http2_state, SessionType type) { |
|
100 |
nghttp2_option* option; |
||
101 |
✗✓ | 660 |
CHECK_EQ(nghttp2_option_new(&option), 0); |
102 |
✗✓ | 660 |
CHECK_NOT_NULL(option); |
103 |
660 |
options_.reset(option); |
|
104 |
|||
105 |
// Make sure closed connections aren't kept around, taking up memory. |
||
106 |
// Note that this breaks the priority tree, which we don't use. |
||
107 |
660 |
nghttp2_option_set_no_closed_streams(option, 1); |
|
108 |
|||
109 |
// We manually handle flow control within a session in order to |
||
110 |
// implement backpressure -- that is, we only send WINDOW_UPDATE |
||
111 |
// frames to the remote peer as data is actually consumed by user |
||
112 |
// code. This ensures that the flow of data over the connection |
||
113 |
// does not move too quickly and limits the amount of data we |
||
114 |
// are required to buffer. |
||
115 |
660 |
nghttp2_option_set_no_auto_window_update(option, 1); |
|
116 |
|||
117 |
// Enable built in support for receiving ALTSVC and ORIGIN frames (but |
||
118 |
// only on client side sessions |
||
119 |
✓✓ | 660 |
if (type == NGHTTP2_SESSION_CLIENT) { |
120 |
321 |
nghttp2_option_set_builtin_recv_extension_type(option, NGHTTP2_ALTSVC); |
|
121 |
321 |
nghttp2_option_set_builtin_recv_extension_type(option, NGHTTP2_ORIGIN); |
|
122 |
} |
||
123 |
|||
124 |
660 |
AliasedUint32Array& buffer = http2_state->options_buffer; |
|
125 |
660 |
uint32_t flags = buffer[IDX_OPTIONS_FLAGS]; |
|
126 |
|||
127 |
✗✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_DEFLATE_DYNAMIC_TABLE_SIZE)) { |
128 |
nghttp2_option_set_max_deflate_dynamic_table_size( |
||
129 |
option, |
||
130 |
buffer[IDX_OPTIONS_MAX_DEFLATE_DYNAMIC_TABLE_SIZE]); |
||
131 |
} |
||
132 |
|||
133 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_RESERVED_REMOTE_STREAMS)) { |
134 |
2 |
nghttp2_option_set_max_reserved_remote_streams( |
|
135 |
option, |
||
136 |
1 |
buffer[IDX_OPTIONS_MAX_RESERVED_REMOTE_STREAMS]); |
|
137 |
} |
||
138 |
|||
139 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_SEND_HEADER_BLOCK_LENGTH)) { |
140 |
1 |
nghttp2_option_set_max_send_header_block_length( |
|
141 |
option, |
||
142 |
2 |
buffer[IDX_OPTIONS_MAX_SEND_HEADER_BLOCK_LENGTH]); |
|
143 |
} |
||
144 |
|||
145 |
// Recommended default |
||
146 |
660 |
nghttp2_option_set_peer_max_concurrent_streams(option, 100); |
|
147 |
✗✓ | 660 |
if (flags & (1 << IDX_OPTIONS_PEER_MAX_CONCURRENT_STREAMS)) { |
148 |
nghttp2_option_set_peer_max_concurrent_streams( |
||
149 |
option, |
||
150 |
buffer[IDX_OPTIONS_PEER_MAX_CONCURRENT_STREAMS]); |
||
151 |
} |
||
152 |
|||
153 |
// The padding strategy sets the mechanism by which we determine how much |
||
154 |
// additional frame padding to apply to DATA and HEADERS frames. Currently |
||
155 |
// this is set on a per-session basis, but eventually we may switch to |
||
156 |
// a per-stream setting, giving users greater control |
||
157 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_PADDING_STRATEGY)) { |
158 |
PaddingStrategy strategy = |
||
159 |
static_cast<PaddingStrategy>( |
||
160 |
2 |
buffer.GetValue(IDX_OPTIONS_PADDING_STRATEGY)); |
|
161 |
2 |
set_padding_strategy(strategy); |
|
162 |
} |
||
163 |
|||
164 |
// The max header list pairs option controls the maximum number of |
||
165 |
// header pairs the session may accept. This is a hard limit.. that is, |
||
166 |
// if the remote peer sends more than this amount, the stream will be |
||
167 |
// automatically closed with an RST_STREAM. |
||
168 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_HEADER_LIST_PAIRS)) |
169 |
1 |
set_max_header_pairs(buffer[IDX_OPTIONS_MAX_HEADER_LIST_PAIRS]); |
|
170 |
|||
171 |
// The HTTP2 specification places no limits on the number of HTTP2 |
||
172 |
// PING frames that can be sent. In order to prevent PINGS from being |
||
173 |
// abused as an attack vector, however, we place a strict upper limit |
||
174 |
// on the number of unacknowledged PINGS that can be sent at any given |
||
175 |
// time. |
||
176 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_OUTSTANDING_PINGS)) |
177 |
2 |
set_max_outstanding_pings(buffer[IDX_OPTIONS_MAX_OUTSTANDING_PINGS]); |
|
178 |
|||
179 |
// The HTTP2 specification places no limits on the number of HTTP2 |
||
180 |
// SETTINGS frames that can be sent. In order to prevent PINGS from being |
||
181 |
// abused as an attack vector, however, we place a strict upper limit |
||
182 |
// on the number of unacknowledged SETTINGS that can be sent at any given |
||
183 |
// time. |
||
184 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_OUTSTANDING_SETTINGS)) |
185 |
2 |
set_max_outstanding_settings(buffer[IDX_OPTIONS_MAX_OUTSTANDING_SETTINGS]); |
|
186 |
|||
187 |
// The HTTP2 specification places no limits on the amount of memory |
||
188 |
// that a session can consume. In order to prevent abuse, we place a |
||
189 |
// cap on the amount of memory a session can consume at any given time. |
||
190 |
// this is a credit based system. Existing streams may cause the limit |
||
191 |
// to be temporarily exceeded but once over the limit, new streams cannot |
||
192 |
// created. |
||
193 |
// Important: The maxSessionMemory option in javascript is expressed in |
||
194 |
// terms of MB increments (i.e. the value 1 == 1 MB) |
||
195 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_SESSION_MEMORY)) |
196 |
8 |
set_max_session_memory(buffer[IDX_OPTIONS_MAX_SESSION_MEMORY] * 1000000); |
|
197 |
|||
198 |
✓✓ | 660 |
if (flags & (1 << IDX_OPTIONS_MAX_SETTINGS)) { |
199 |
1 |
nghttp2_option_set_max_settings( |
|
200 |
option, |
||
201 |
2 |
static_cast<size_t>(buffer[IDX_OPTIONS_MAX_SETTINGS])); |
|
202 |
} |
||
203 |
660 |
} |
|
204 |
|||
205 |
#define GRABSETTING(entries, count, name) \ |
||
206 |
do { \ |
||
207 |
if (flags & (1 << IDX_SETTINGS_ ## name)) { \ |
||
208 |
uint32_t val = buffer[IDX_SETTINGS_ ## name]; \ |
||
209 |
entries[count++] = \ |
||
210 |
nghttp2_settings_entry {NGHTTP2_SETTINGS_ ## name, val}; \ |
||
211 |
} } while (0) |
||
212 |
|||
213 |
687 |
size_t Http2Settings::Init( |
|
214 |
Http2State* http2_state, |
||
215 |
nghttp2_settings_entry* entries) { |
||
216 |
687 |
AliasedUint32Array& buffer = http2_state->settings_buffer; |
|
217 |
687 |
uint32_t flags = buffer[IDX_SETTINGS_COUNT]; |
|
218 |
|||
219 |
687 |
size_t count = 0; |
|
220 |
|||
221 |
#define V(name) GRABSETTING(entries, count, name); |
||
222 |
✓✓ | 687 |
HTTP2_SETTINGS(V) |
223 |
✓✓ | 6 |
#undef V |
224 |
12 |
||
225 |
✓✓ | 699 |
return count; |
226 |
8 |
} |
|
227 |
✓✓ | 687 |
#undef GRABSETTING |
228 |
✓✓ | 18 |
|
229 |
6 |
// The Http2Settings class is used to configure a SETTINGS frame that is |
|
230 |
✓✓ | 6 |
// to be sent to the connected peer. The settings are set using a TypedArray |
231 |
10 |
// that is shared with the JavaScript side. |
|
232 |
✓✓ | 1357 |
Http2Settings::Http2Settings(Http2Session* session, |
233 |
6 |
Local<Object> obj, |
|
234 |
Local<Function> callback, |
||
235 |
670 |
uint64_t start_time) |
|
236 |
: AsyncWrap(session->env(), obj, PROVIDER_HTTP2SETTINGS), |
||
237 |
session_(session), |
||
238 |
1340 |
startTime_(start_time) { |
|
239 |
670 |
callback_.Reset(env()->isolate(), callback); |
|
240 |
670 |
count_ = Init(session->http2_state(), entries_); |
|
241 |
670 |
} |
|
242 |
|||
243 |
538 |
Local<Function> Http2Settings::callback() const { |
|
244 |
1076 |
return callback_.Get(env()->isolate()); |
|
245 |
} |
||
246 |
|||
247 |
4 |
void Http2Settings::MemoryInfo(MemoryTracker* tracker) const { |
|
248 |
4 |
tracker->TrackField("callback", callback_); |
|
249 |
4 |
} |
|
250 |
|||
251 |
// Generates a Buffer that contains the serialized payload of a SETTINGS |
||
252 |
// frame. This can be used, for instance, to create the Base64-encoded |
||
253 |
// content of an Http2-Settings header field. |
||
254 |
Local<Value> Http2Settings::Pack() { |
||
255 |
return Pack(session_->env(), count_, entries_); |
||
256 |
} |
||
257 |
|||
258 |
17 |
Local<Value> Http2Settings::Pack(Http2State* state) { |
|
259 |
nghttp2_settings_entry entries[IDX_SETTINGS_COUNT]; |
||
260 |
17 |
size_t count = Init(state, entries); |
|
261 |
17 |
return Pack(state->env(), count, entries); |
|
262 |
} |
||
263 |
|||
264 |
17 |
Local<Value> Http2Settings::Pack( |
|
265 |
Environment* env, |
||
266 |
size_t count, |
||
267 |
const nghttp2_settings_entry* entries) { |
||
268 |
17 |
EscapableHandleScope scope(env->isolate()); |
|
269 |
17 |
const size_t size = count * 6; |
|
270 |
34 |
AllocatedBuffer buffer = AllocatedBuffer::AllocateManaged(env, size); |
|
271 |
ssize_t ret = |
||
272 |
nghttp2_pack_settings_payload( |
||
273 |
17 |
reinterpret_cast<uint8_t*>(buffer.data()), |
|
274 |
size, |
||
275 |
entries, |
||
276 |
17 |
count); |
|
277 |
17 |
Local<Value> buf = Undefined(env->isolate()); |
|
278 |
✓✓ | 33 |
if (ret >= 0) buf = buffer.ToBuffer().ToLocalChecked(); |
279 |
17 |
return scope.Escape(buf); |
|
280 |
} |
||
281 |
|||
282 |
// Updates the shared TypedArray with the current remote or local settings for |
||
283 |
// the session. |
||
284 |
561 |
void Http2Settings::Update(Http2Session* session, get_setting fn) { |
|
285 |
561 |
AliasedUint32Array& buffer = session->http2_state()->settings_buffer; |
|
286 |
|||
287 |
#define V(name) \ |
||
288 |
buffer[IDX_SETTINGS_ ## name] = \ |
||
289 |
fn(session->session(), NGHTTP2_SETTINGS_ ## name); |
||
290 |
561 |
HTTP2_SETTINGS(V) |
|
291 |
1122 |
#undef V |
|
292 |
2244 |
} |
|
293 |
1122 |
||
294 |
561 |
// Initializes the shared TypedArray with the default settings values. |
|
295 |
567 |
void Http2Settings::RefreshDefaults(Http2State* http2_state) { |
|
296 |
6 |
AliasedUint32Array& buffer = http2_state->settings_buffer; |
|
297 |
6 |
uint32_t flags = 0; |
|
298 |
|||
299 |
#define V(name) \ |
||
300 |
do { \ |
||
301 |
buffer[IDX_SETTINGS_ ## name] = DEFAULT_SETTINGS_ ## name; \ |
||
302 |
flags |= 1 << IDX_SETTINGS_ ## name; \ |
||
303 |
} while (0); |
||
304 |
6 |
HTTP2_SETTINGS(V) |
|
305 |
6 |
#undef V |
|
306 |
6 |
||
307 |
12 |
buffer[IDX_SETTINGS_COUNT] = flags; |
|
308 |
18 |
} |
|
309 |
18 |
||
310 |
12 |
||
311 |
674 |
void Http2Settings::Send() { |
|
312 |
1336 |
Http2Scope h2scope(session_.get()); |
|
313 |
✗✓ | 668 |
CHECK_EQ(nghttp2_submit_settings( |
314 |
session_->session(), |
||
315 |
NGHTTP2_FLAG_NONE, |
||
316 |
&entries_[0], |
||
317 |
count_), 0); |
||
318 |
668 |
} |
|
319 |
|||
320 |
538 |
void Http2Settings::Done(bool ack) { |
|
321 |
538 |
uint64_t end = uv_hrtime(); |
|
322 |
538 |
double duration = (end - startTime_) / 1e6; |
|
323 |
|||
324 |
Local<Value> argv[] = { |
||
325 |
538 |
ack ? v8::True(env()->isolate()) : v8::False(env()->isolate()), |
|
326 |
Number::New(env()->isolate(), duration) |
||
327 |
✓✓ | 1614 |
}; |
328 |
538 |
MakeCallback(callback(), arraysize(argv), argv); |
|
329 |
538 |
} |
|
330 |
|||
331 |
// The Http2Priority class initializes an appropriate nghttp2_priority_spec |
||
332 |
// struct used when either creating a stream or updating its priority |
||
333 |
// settings. |
||
334 |
11786 |
Http2Priority::Http2Priority(Environment* env, |
|
335 |
Local<Value> parent, |
||
336 |
Local<Value> weight, |
||
337 |
11786 |
Local<Value> exclusive) { |
|
338 |
11786 |
Local<Context> context = env->context(); |
|
339 |
23572 |
int32_t parent_ = parent->Int32Value(context).ToChecked(); |
|
340 |
23572 |
int32_t weight_ = weight->Int32Value(context).ToChecked(); |
|
341 |
11786 |
bool exclusive_ = exclusive->IsTrue(); |
|
342 |
Debug(env, DebugCategory::HTTP2STREAM, |
||
343 |
"Http2Priority: parent: %d, weight: %d, exclusive: %s\n", |
||
344 |
✗✓ | 23572 |
parent_, weight_, exclusive_ ? "yes" : "no"); |
345 |
✗✓ | 11786 |
nghttp2_priority_spec_init(this, parent_, weight_, exclusive_ ? 1 : 0); |
346 |
11786 |
} |
|
347 |
|||
348 |
|||
349 |
118 |
const char* Http2Session::TypeName() const { |
|
350 |
✓✓✗ | 118 |
switch (session_type_) { |
351 |
59 |
case NGHTTP2_SESSION_SERVER: return "server"; |
|
352 |
59 |
case NGHTTP2_SESSION_CLIENT: return "client"; |
|
353 |
default: |
||
354 |
// This should never happen |
||
355 |
ABORT(); |
||
356 |
} |
||
357 |
} |
||
358 |
|||
359 |
5 |
Origins::Origins( |
|
360 |
Environment* env, |
||
361 |
Local<String> origin_string, |
||
362 |
5 |
size_t origin_count) |
|
363 |
5 |
: count_(origin_count) { |
|
364 |
5 |
int origin_string_len = origin_string->Length(); |
|
365 |
✗✓ | 5 |
if (count_ == 0) { |
366 |
CHECK_EQ(origin_string_len, 0); |
||
367 |
return; |
||
368 |
} |
||
369 |
|||
370 |
10 |
buf_ = AllocatedBuffer::AllocateManaged( |
|
371 |
env, |
||
372 |
(alignof(nghttp2_origin_entry) - 1) + |
||
373 |
5 |
count_ * sizeof(nghttp2_origin_entry) + |
|
374 |
5 |
origin_string_len); |
|
375 |
|||
376 |
// Make sure the start address is aligned appropriately for an nghttp2_nv*. |
||
377 |
5 |
char* start = AlignUp(buf_.data(), alignof(nghttp2_origin_entry)); |
|
378 |
5 |
char* origin_contents = start + (count_ * sizeof(nghttp2_origin_entry)); |
|
379 |
nghttp2_origin_entry* const nva = |
||
380 |
5 |
reinterpret_cast<nghttp2_origin_entry*>(start); |
|
381 |
|||
382 |
✗✓ | 5 |
CHECK_LE(origin_contents + origin_string_len, buf_.data() + buf_.size()); |
383 |
✗✓ | 10 |
CHECK_EQ(origin_string->WriteOneByte( |
384 |
env->isolate(), |
||
385 |
reinterpret_cast<uint8_t*>(origin_contents), |
||
386 |
0, |
||
387 |
origin_string_len, |
||
388 |
String::NO_NULL_TERMINATION), |
||
389 |
origin_string_len); |
||
390 |
|||
391 |
5 |
size_t n = 0; |
|
392 |
char* p; |
||
393 |
✓✓ | 14 |
for (p = origin_contents; p < origin_contents + origin_string_len; n++) { |
394 |
✗✓ | 9 |
if (n >= count_) { |
395 |
static uint8_t zero = '\0'; |
||
396 |
nva[0].origin = &zero; |
||
397 |
nva[0].origin_len = 1; |
||
398 |
count_ = 1; |
||
399 |
return; |
||
400 |
} |
||
401 |
|||
402 |
9 |
nva[n].origin = reinterpret_cast<uint8_t*>(p); |
|
403 |
9 |
nva[n].origin_len = strlen(p); |
|
404 |
9 |
p += nva[n].origin_len + 1; |
|
405 |
} |
||
406 |
} |
||
407 |
|||
408 |
// Sets the various callback functions that nghttp2 will use to notify us |
||
409 |
// about significant events while processing http2 stuff. |
||
410 |
9382 |
Http2Session::Callbacks::Callbacks(bool kHasGetPaddingCallback) { |
|
411 |
nghttp2_session_callbacks* callbacks_; |
||
412 |
✗✓ | 9382 |
CHECK_EQ(nghttp2_session_callbacks_new(&callbacks_), 0); |
413 |
9382 |
callbacks.reset(callbacks_); |
|
414 |
|||
415 |
nghttp2_session_callbacks_set_on_begin_headers_callback( |
||
416 |
9382 |
callbacks_, OnBeginHeadersCallback); |
|
417 |
nghttp2_session_callbacks_set_on_header_callback2( |
||
418 |
9382 |
callbacks_, OnHeaderCallback); |
|
419 |
nghttp2_session_callbacks_set_on_frame_recv_callback( |
||
420 |
9382 |
callbacks_, OnFrameReceive); |
|
421 |
nghttp2_session_callbacks_set_on_stream_close_callback( |
||
422 |
9382 |
callbacks_, OnStreamClose); |
|
423 |
nghttp2_session_callbacks_set_on_data_chunk_recv_callback( |
||
424 |
9382 |
callbacks_, OnDataChunkReceived); |
|
425 |
nghttp2_session_callbacks_set_on_frame_not_send_callback( |
||
426 |
9382 |
callbacks_, OnFrameNotSent); |
|
427 |
nghttp2_session_callbacks_set_on_invalid_header_callback2( |
||
428 |
9382 |
callbacks_, OnInvalidHeader); |
|
429 |
nghttp2_session_callbacks_set_error_callback( |
||
430 |
9382 |
callbacks_, OnNghttpError); |
|
431 |
nghttp2_session_callbacks_set_send_data_callback( |
||
432 |
9382 |
callbacks_, OnSendData); |
|
433 |
nghttp2_session_callbacks_set_on_invalid_frame_recv_callback( |
||
434 |
9382 |
callbacks_, OnInvalidFrame); |
|
435 |
nghttp2_session_callbacks_set_on_frame_send_callback( |
||
436 |
9382 |
callbacks_, OnFrameSent); |
|
437 |
|||
438 |
✓✓ | 9382 |
if (kHasGetPaddingCallback) { |
439 |
nghttp2_session_callbacks_set_select_padding_callback( |
||
440 |
4691 |
callbacks_, OnSelectPadding); |
|
441 |
} |
||
442 |
9382 |
} |
|
443 |
|||
444 |
void Http2Session::StopTrackingRcbuf(nghttp2_rcbuf* buf) { |
||
445 |
StopTrackingMemory(buf); |
||
446 |
} |
||
447 |
|||
448 |
202631 |
void Http2Session::CheckAllocatedSize(size_t previous_size) const { |
|
449 |
✗✓ | 202631 |
CHECK_GE(current_nghttp2_memory_, previous_size); |
450 |
202631 |
} |
|
451 |
|||
452 |
102265 |
void Http2Session::IncreaseAllocatedSize(size_t size) { |
|
453 |
102265 |
current_nghttp2_memory_ += size; |
|
454 |
102265 |
} |
|
455 |
|||
456 |
125512 |
void Http2Session::DecreaseAllocatedSize(size_t size) { |
|
457 |
125512 |
current_nghttp2_memory_ -= size; |
|
458 |
125512 |
} |
|
459 |
|||
460 |
660 |
Http2Session::Http2Session(Http2State* http2_state, |
|
461 |
Local<Object> wrap, |
||
462 |
660 |
SessionType type) |
|
463 |
: AsyncWrap(http2_state->env(), wrap, AsyncWrap::PROVIDER_HTTP2SESSION), |
||
464 |
js_fields_(http2_state->env()->isolate()), |
||
465 |
session_type_(type), |
||
466 |
1320 |
http2_state_(http2_state) { |
|
467 |
660 |
MakeWeak(); |
|
468 |
660 |
statistics_.start_time = uv_hrtime(); |
|
469 |
|||
470 |
// Capture the configuration options for this session |
||
471 |
1320 |
Http2Options opts(http2_state, type); |
|
472 |
|||
473 |
660 |
max_session_memory_ = opts.max_session_memory(); |
|
474 |
|||
475 |
660 |
uint32_t maxHeaderPairs = opts.max_header_pairs(); |
|
476 |
660 |
max_header_pairs_ = |
|
477 |
type == NGHTTP2_SESSION_SERVER |
||
478 |
339 |
? GetServerMaxHeaderPairs(maxHeaderPairs) |
|
479 |
✓✓ | 999 |
: GetClientMaxHeaderPairs(maxHeaderPairs); |
480 |
|||
481 |
660 |
max_outstanding_pings_ = opts.max_outstanding_pings(); |
|
482 |
660 |
max_outstanding_settings_ = opts.max_outstanding_settings(); |
|
483 |
|||
484 |
660 |
padding_strategy_ = opts.padding_strategy(); |
|
485 |
|||
486 |
bool hasGetPaddingCallback = |
||
487 |
660 |
padding_strategy_ != PADDING_STRATEGY_NONE; |
|
488 |
|||
489 |
✓✓ | 660 |
auto fn = type == NGHTTP2_SESSION_SERVER ? |
490 |
nghttp2_session_server_new3 : |
||
491 |
660 |
nghttp2_session_client_new3; |
|
492 |
|||
493 |
660 |
nghttp2_mem alloc_info = MakeAllocator(); |
|
494 |
|||
495 |
// This should fail only if the system is out of memory, which |
||
496 |
// is going to cause lots of other problems anyway, or if any |
||
497 |
// of the options are out of acceptable range, which we should |
||
498 |
// be catching before it gets this far. Either way, crash if this |
||
499 |
// fails. |
||
500 |
nghttp2_session* session; |
||
501 |
✓✓✗✓ |
660 |
CHECK_EQ(fn( |
502 |
&session, |
||
503 |
callback_struct_saved[hasGetPaddingCallback ? 1 : 0].callbacks.get(), |
||
504 |
this, |
||
505 |
*opts, |
||
506 |
&alloc_info), 0); |
||
507 |
660 |
session_.reset(session); |
|
508 |
|||
509 |
660 |
outgoing_storage_.reserve(1024); |
|
510 |
660 |
outgoing_buffers_.reserve(32); |
|
511 |
|||
512 |
Local<Uint8Array> uint8_arr = |
||
513 |
660 |
Uint8Array::New(js_fields_.GetArrayBuffer(), 0, kSessionUint8FieldCount); |
|
514 |
1980 |
USE(wrap->Set(env()->context(), env()->fields_string(), uint8_arr)); |
|
515 |
660 |
} |
|
516 |
|||
517 |
2640 |
Http2Session::~Http2Session() { |
|
518 |
✗✓ | 660 |
CHECK(!is_in_scope()); |
519 |
660 |
Debug(this, "freeing nghttp2 session"); |
|
520 |
// Explicitly reset session_ so the subsequent |
||
521 |
// current_nghttp2_memory_ check passes. |
||
522 |
660 |
session_.reset(); |
|
523 |
✗✓ | 660 |
CHECK_EQ(current_nghttp2_memory_, 0); |
524 |
1320 |
} |
|
525 |
|||
526 |
4 |
void Http2Session::MemoryInfo(MemoryTracker* tracker) const { |
|
527 |
4 |
tracker->TrackField("streams", streams_); |
|
528 |
4 |
tracker->TrackField("outstanding_pings", outstanding_pings_); |
|
529 |
4 |
tracker->TrackField("outstanding_settings", outstanding_settings_); |
|
530 |
4 |
tracker->TrackField("outgoing_buffers", outgoing_buffers_); |
|
531 |
4 |
tracker->TrackFieldWithSize("stream_buf", stream_buf_.len); |
|
532 |
4 |
tracker->TrackFieldWithSize("outgoing_storage", outgoing_storage_.size()); |
|
533 |
4 |
tracker->TrackFieldWithSize("pending_rst_streams", |
|
534 |
8 |
pending_rst_streams_.size() * sizeof(int32_t)); |
|
535 |
4 |
tracker->TrackFieldWithSize("nghttp2_memory", current_nghttp2_memory_); |
|
536 |
4 |
} |
|
537 |
|||
538 |
118 |
std::string Http2Session::diagnostic_name() const { |
|
539 |
236 |
return std::string("Http2Session ") + TypeName() + " (" + |
|
540 |
354 |
std::to_string(static_cast<int64_t>(get_async_id())) + ")"; |
|
541 |
} |
||
542 |
|||
543 |
23670 |
void Http2Stream::EmitStatistics() { |
|
544 |
✗✓ | 23670 |
CHECK_NOT_NULL(session()); |
545 |
✓✓ | 23670 |
if (!HasHttp2Observer(env())) |
546 |
23662 |
return; |
|
547 |
auto entry = |
||
548 |
std::make_unique<Http2StreamPerformanceEntry>( |
||
549 |
16 |
session()->http2_state(), id_, statistics_); |
|
550 |
48 |
env()->SetImmediate([entry = move(entry)](Environment* env) { |
|
551 |
✗✓ | 8 |
if (!HasHttp2Observer(env)) |
552 |
return; |
||
553 |
16 |
HandleScope handle_scope(env->isolate()); |
|
554 |
8 |
AliasedFloat64Array& buffer = entry->http2_state()->stream_stats_buffer; |
|
555 |
8 |
buffer[IDX_STREAM_STATS_ID] = entry->id(); |
|
556 |
✗✓ | 8 |
if (entry->first_byte() != 0) { |
557 |
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] = |
||
558 |
(entry->first_byte() - entry->startTimeNano()) / 1e6; |
||
559 |
} else { |
||
560 |
8 |
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTE] = 0; |
|
561 |
} |
||
562 |
✓✗ | 8 |
if (entry->first_header() != 0) { |
563 |
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] = |
||
564 |
8 |
(entry->first_header() - entry->startTimeNano()) / 1e6; |
|
565 |
} else { |
||
566 |
buffer[IDX_STREAM_STATS_TIMETOFIRSTHEADER] = 0; |
||
567 |
} |
||
568 |
✓✓ | 8 |
if (entry->first_byte_sent() != 0) { |
569 |
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] = |
||
570 |
4 |
(entry->first_byte_sent() - entry->startTimeNano()) / 1e6; |
|
571 |
} else { |
||
572 |
4 |
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] = 0; |
|
573 |
} |
||
574 |
buffer[IDX_STREAM_STATS_SENTBYTES] = |
||
575 |
8 |
static_cast<double>(entry->sent_bytes()); |
|
576 |
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = |
||
577 |
8 |
static_cast<double>(entry->received_bytes()); |
|
578 |
Local<Object> obj; |
||
579 |
✓✗ | 24 |
if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj); |
580 |
8 |
}); |
|
581 |
} |
||
582 |
|||
583 |
641 |
void Http2Session::EmitStatistics() { |
|
584 |
✓✓ | 641 |
if (!HasHttp2Observer(env())) |
585 |
634 |
return; |
|
586 |
auto entry = std::make_unique<Http2SessionPerformanceEntry>( |
||
587 |
14 |
http2_state(), statistics_, session_type_); |
|
588 |
42 |
env()->SetImmediate([entry = std::move(entry)](Environment* env) { |
|
589 |
✗✓ | 7 |
if (!HasHttp2Observer(env)) |
590 |
return; |
||
591 |
14 |
HandleScope handle_scope(env->isolate()); |
|
592 |
7 |
AliasedFloat64Array& buffer = entry->http2_state()->session_stats_buffer; |
|
593 |
7 |
buffer[IDX_SESSION_STATS_TYPE] = entry->type(); |
|
594 |
7 |
buffer[IDX_SESSION_STATS_PINGRTT] = entry->ping_rtt() / 1e6; |
|
595 |
7 |
buffer[IDX_SESSION_STATS_FRAMESRECEIVED] = entry->frame_count(); |
|
596 |
7 |
buffer[IDX_SESSION_STATS_FRAMESSENT] = entry->frame_sent(); |
|
597 |
7 |
buffer[IDX_SESSION_STATS_STREAMCOUNT] = entry->stream_count(); |
|
598 |
buffer[IDX_SESSION_STATS_STREAMAVERAGEDURATION] = |
||
599 |
7 |
entry->stream_average_duration(); |
|
600 |
buffer[IDX_SESSION_STATS_DATA_SENT] = |
||
601 |
7 |
static_cast<double>(entry->data_sent()); |
|
602 |
buffer[IDX_SESSION_STATS_DATA_RECEIVED] = |
||
603 |
7 |
static_cast<double>(entry->data_received()); |
|
604 |
buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] = |
||
605 |
7 |
static_cast<double>(entry->max_concurrent_streams()); |
|
606 |
Local<Object> obj; |
||
607 |
✓✗ | 21 |
if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj); |
608 |
7 |
}); |
|
609 |
} |
||
610 |
|||
611 |
// Closes the session and frees the associated resources |
||
612 |
641 |
void Http2Session::Close(uint32_t code, bool socket_closed) { |
|
613 |
641 |
Debug(this, "closing session"); |
|
614 |
|||
615 |
✗✓ | 641 |
if (is_closing()) |
616 |
return; |
||
617 |
641 |
set_closing(); |
|
618 |
|||
619 |
// Stop reading on the i/o stream |
||
620 |
✓✓ | 641 |
if (stream_ != nullptr) { |
621 |
631 |
set_reading_stopped(); |
|
622 |
631 |
stream_->ReadStop(); |
|
623 |
} |
||
624 |
|||
625 |
// If the socket is not closed, then attempt to send a closing GOAWAY |
||
626 |
// frame. There is no guarantee that this GOAWAY will be received by |
||
627 |
// the peer but the HTTP/2 spec recommends sending it anyway. We'll |
||
628 |
// make a best effort. |
||
629 |
✓✓ | 641 |
if (!socket_closed) { |
630 |
597 |
Debug(this, "terminating session with code %d", code); |
|
631 |
✗✓ | 597 |
CHECK_EQ(nghttp2_session_terminate_session(session_.get(), code), 0); |
632 |
597 |
SendPendingData(); |
|
633 |
✓✓ | 44 |
} else if (stream_ != nullptr) { |
634 |
34 |
stream_->RemoveStreamListener(this); |
|
635 |
} |
||
636 |
|||
637 |
641 |
set_destroyed(); |
|
638 |
|||
639 |
// If we are writing we will get to make the callback in OnStreamAfterWrite. |
||
640 |
✓✓ | 641 |
if (!is_write_in_progress()) { |
641 |
598 |
Debug(this, "make done session callback"); |
|
642 |
1196 |
HandleScope scope(env()->isolate()); |
|
643 |
598 |
MakeCallback(env()->ondone_string(), 0, nullptr); |
|
644 |
} |
||
645 |
|||
646 |
// If there are outstanding pings, those will need to be canceled, do |
||
647 |
// so on the next iteration of the event loop to avoid calling out into |
||
648 |
// javascript since this may be called during garbage collection. |
||
649 |
✓✓ | 643 |
while (BaseObjectPtr<Http2Ping> ping = PopPing()) { |
650 |
1 |
ping->DetachFromSession(); |
|
651 |
✓✓ | 2 |
env()->SetImmediate( |
652 |
5 |
[ping = std::move(ping)](Environment* env) { |
|
653 |
1 |
ping->Done(false); |
|
654 |
2 |
}); |
|
655 |
1 |
} |
|
656 |
|||
657 |
641 |
statistics_.end_time = uv_hrtime(); |
|
658 |
641 |
EmitStatistics(); |
|
659 |
} |
||
660 |
|||
661 |
// Locates an existing known stream by ID. nghttp2 has a similar method |
||
662 |
// but this is faster and does not fail if the stream is not found. |
||
663 |
246472 |
BaseObjectPtr<Http2Stream> Http2Session::FindStream(int32_t id) { |
|
664 |
246472 |
auto s = streams_.find(id); |
|
665 |
✓✓ | 246472 |
return s != streams_.end() ? s->second : BaseObjectPtr<Http2Stream>(); |
666 |
} |
||
667 |
|||
668 |
11992 |
bool Http2Session::CanAddStream() { |
|
669 |
uint32_t maxConcurrentStreams = |
||
670 |
11992 |
nghttp2_session_get_local_settings( |
|
671 |
11992 |
session_.get(), NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS); |
|
672 |
size_t maxSize = |
||
673 |
11992 |
std::min(streams_.max_size(), static_cast<size_t>(maxConcurrentStreams)); |
|
674 |
// We can add a new stream so long as we are less than the current |
||
675 |
// maximum on concurrent streams and there's enough available memory |
||
676 |
✓✗✓✓ |
23984 |
return streams_.size() < maxSize && |
677 |
23984 |
has_available_session_memory(sizeof(Http2Stream)); |
|
678 |
} |
||
679 |
|||
680 |
23779 |
void Http2Session::AddStream(Http2Stream* stream) { |
|
681 |
✗✓ | 23779 |
CHECK_GE(++statistics_.stream_count, 0); |
682 |
23779 |
streams_[stream->id()] = BaseObjectPtr<Http2Stream>(stream); |
|
683 |
23779 |
size_t size = streams_.size(); |
|
684 |
✓✓ | 23779 |
if (size > statistics_.max_concurrent_streams) |
685 |
1419 |
statistics_.max_concurrent_streams = size; |
|
686 |
23779 |
IncrementCurrentSessionMemory(sizeof(*stream)); |
|
687 |
23779 |
} |
|
688 |
|||
689 |
|||
690 |
23670 |
BaseObjectPtr<Http2Stream> Http2Session::RemoveStream(int32_t id) { |
|
691 |
23670 |
BaseObjectPtr<Http2Stream> stream; |
|
692 |
✗✓ | 23670 |
if (streams_.empty()) |
693 |
return stream; |
||
694 |
23670 |
stream = FindStream(id); |
|
695 |
✓✗ | 23670 |
if (stream) { |
696 |
23670 |
streams_.erase(id); |
|
697 |
23670 |
DecrementCurrentSessionMemory(sizeof(*stream)); |
|
698 |
} |
||
699 |
23670 |
return stream; |
|
700 |
} |
||
701 |
|||
702 |
// Used as one of the Padding Strategy functions. Will attempt to ensure |
||
703 |
// that the total frame size, including header bytes, are 8-byte aligned. |
||
704 |
// If maxPayloadLen is smaller than the number of bytes necessary to align, |
||
705 |
// will return maxPayloadLen instead. |
||
706 |
3 |
ssize_t Http2Session::OnDWordAlignedPadding(size_t frameLen, |
|
707 |
size_t maxPayloadLen) { |
||
708 |
3 |
size_t r = (frameLen + 9) % 8; |
|
709 |
✗✓ | 3 |
if (r == 0) return frameLen; // If already a multiple of 8, return. |
710 |
|||
711 |
3 |
size_t pad = frameLen + (8 - r); |
|
712 |
|||
713 |
// If maxPayloadLen happens to be less than the calculated pad length, |
||
714 |
// use the max instead, even tho this means the frame will not be |
||
715 |
// aligned. |
||
716 |
3 |
pad = std::min(maxPayloadLen, pad); |
|
717 |
3 |
Debug(this, "using frame size padding: %d", pad); |
|
718 |
3 |
return pad; |
|
719 |
} |
||
720 |
|||
721 |
// Used as one of the Padding Strategy functions. Uses the maximum amount |
||
722 |
// of padding allowed for the current frame. |
||
723 |
ssize_t Http2Session::OnMaxFrameSizePadding(size_t frameLen, |
||
724 |
size_t maxPayloadLen) { |
||
725 |
Debug(this, "using max frame size padding: %d", maxPayloadLen); |
||
726 |
return maxPayloadLen; |
||
727 |
} |
||
728 |
|||
729 |
// Write data received from the i/o stream to the underlying nghttp2_session. |
||
730 |
// On each call to nghttp2_session_mem_recv, nghttp2 will begin calling the |
||
731 |
// various callback functions. Each of these will typically result in a call |
||
732 |
// out to JavaScript so this particular function is rather hot and can be |
||
733 |
// quite expensive. This is a potential performance optimization target later. |
||
734 |
31527 |
ssize_t Http2Session::ConsumeHTTP2Data() { |
|
735 |
✗✓ | 31527 |
CHECK_NOT_NULL(stream_buf_.base); |
736 |
✗✓ | 31527 |
CHECK_LE(stream_buf_offset_, stream_buf_.len); |
737 |
31527 |
size_t read_len = stream_buf_.len - stream_buf_offset_; |
|
738 |
|||
739 |
// multiple side effects. |
||
740 |
31527 |
Debug(this, "receiving %d bytes [wants data? %d]", |
|
741 |
read_len, |
||
742 |
63054 |
nghttp2_session_want_read(session_.get())); |
|
743 |
31527 |
set_receive_paused(false); |
|
744 |
ssize_t ret = |
||
745 |
63054 |
nghttp2_session_mem_recv(session_.get(), |
|
746 |
31527 |
reinterpret_cast<uint8_t*>(stream_buf_.base) + |
|
747 |
31527 |
stream_buf_offset_, |
|
748 |
31527 |
read_len); |
|
749 |
✗✓ | 31527 |
CHECK_NE(ret, NGHTTP2_ERR_NOMEM); |
750 |
|||
751 |
✓✓ | 31527 |
if (is_receive_paused()) { |
752 |
✗✓ | 560 |
CHECK(is_reading_stopped()); |
753 |
|||
754 |
✗✓ | 560 |
CHECK_GT(ret, 0); |
755 |
✗✓ | 560 |
CHECK_LE(static_cast<size_t>(ret), read_len); |
756 |
|||
757 |
// Mark the remainder of the data as available for later consumption. |
||
758 |
// Even if all bytes were received, a paused stream may delay the |
||
759 |
// nghttp2_on_frame_recv_callback which may have an END_STREAM flag. |
||
760 |
560 |
stream_buf_offset_ += ret; |
|
761 |
560 |
return ret; |
|
762 |
} |
||
763 |
|||
764 |
// We are done processing the current input chunk. |
||
765 |
30967 |
DecrementCurrentSessionMemory(stream_buf_.len); |
|
766 |
30967 |
stream_buf_offset_ = 0; |
|
767 |
30967 |
stream_buf_ab_.Reset(); |
|
768 |
30967 |
stream_buf_allocation_.clear(); |
|
769 |
30967 |
stream_buf_ = uv_buf_init(nullptr, 0); |
|
770 |
|||
771 |
✓✓ | 30967 |
if (ret < 0) |
772 |
6 |
return ret; |
|
773 |
|||
774 |
// Send any data that was queued up while processing the received data. |
||
775 |
✓✓ | 30961 |
if (!is_destroyed()) { |
776 |
30437 |
SendPendingData(); |
|
777 |
} |
||
778 |
30961 |
return ret; |
|
779 |
} |
||
780 |
|||
781 |
|||
782 |
143218 |
int32_t GetFrameID(const nghttp2_frame* frame) { |
|
783 |
// If this is a push promise, we want to grab the id of the promised stream |
||
784 |
✓✓ | 143218 |
return (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? |
785 |
frame->push_promise.promised_stream_id : |
||
786 |
143218 |
frame->hd.stream_id; |
|
787 |
} |
||
788 |
|||
789 |
|||
790 |
// Called by nghttp2 at the start of receiving a HEADERS frame. We use this |
||
791 |
// callback to determine if a new stream is being created or if we are simply |
||
792 |
// adding a new block of headers to an existing stream. The header pairs |
||
793 |
// themselves are set in the OnHeaderCallback |
||
794 |
23695 |
int Http2Session::OnBeginHeadersCallback(nghttp2_session* handle, |
|
795 |
const nghttp2_frame* frame, |
||
796 |
void* user_data) { |
||
797 |
23695 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
798 |
23695 |
int32_t id = GetFrameID(frame); |
|
799 |
Debug(session, "beginning headers for stream %d", id); |
||
800 |
|||
801 |
47390 |
BaseObjectPtr<Http2Stream> stream = session->FindStream(id); |
|
802 |
// The common case is that we're creating a new stream. The less likely |
||
803 |
// case is that we're receiving a set of trailers |
||
804 |
✓✓ | 23695 |
if (LIKELY(!stream)) { |
805 |
✓✓✗✓ ✓✓ |
11992 |
if (UNLIKELY(!session->CanAddStream() || |
806 |
Http2Stream::New(session, id, frame->headers.cat) == |
||
807 |
nullptr)) { |
||
808 |
✗✓ | 2 |
if (session->rejected_stream_count_++ > |
809 |
1 |
session->js_fields_->max_rejected_streams) |
|
810 |
return NGHTTP2_ERR_CALLBACK_FAILURE; |
||
811 |
// Too many concurrent streams being opened |
||
812 |
1 |
nghttp2_submit_rst_stream( |
|
813 |
session->session(), |
||
814 |
NGHTTP2_FLAG_NONE, |
||
815 |
id, |
||
816 |
2 |
NGHTTP2_ENHANCE_YOUR_CALM); |
|
817 |
1 |
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
|
818 |
} |
||
819 |
|||
820 |
11991 |
session->rejected_stream_count_ = 0; |
|
821 |
✓✗ | 11703 |
} else if (!stream->is_destroyed()) { |
822 |
11703 |
stream->StartHeaders(frame->headers.cat); |
|
823 |
} |
||
824 |
23694 |
return 0; |
|
825 |
} |
||
826 |
|||
827 |
// Called by nghttp2 for each header name/value pair in a HEADERS block. |
||
828 |
// This had to have been preceded by a call to OnBeginHeadersCallback so |
||
829 |
// the Http2Stream is guaranteed to already exist. |
||
830 |
71882 |
int Http2Session::OnHeaderCallback(nghttp2_session* handle, |
|
831 |
const nghttp2_frame* frame, |
||
832 |
nghttp2_rcbuf* name, |
||
833 |
nghttp2_rcbuf* value, |
||
834 |
uint8_t flags, |
||
835 |
void* user_data) { |
||
836 |
71882 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
837 |
71882 |
int32_t id = GetFrameID(frame); |
|
838 |
143764 |
BaseObjectPtr<Http2Stream> stream = session->FindStream(id); |
|
839 |
// If stream is null at this point, either something odd has happened |
||
840 |
// or the stream was closed locally while header processing was occurring. |
||
841 |
// either way, do not proceed and close the stream. |
||
842 |
✗✓ | 71882 |
if (UNLIKELY(!stream)) |
843 |
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
||
844 |
|||
845 |
// If the stream has already been destroyed, ignore. |
||
846 |
✓✗✓✓ ✓✓ |
71882 |
if (!stream->is_destroyed() && !stream->AddHeader(name, value, flags)) { |
847 |
// This will only happen if the connected peer sends us more |
||
848 |
// than the allowed number of header items at any given time |
||
849 |
3 |
stream->SubmitRstStream(NGHTTP2_ENHANCE_YOUR_CALM); |
|
850 |
3 |
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
|
851 |
} |
||
852 |
71879 |
return 0; |
|
853 |
} |
||
854 |
|||
855 |
|||
856 |
// Called by nghttp2 when a complete HTTP2 frame has been received. There are |
||
857 |
// only a handful of frame types that we care about handling here. |
||
858 |
55703 |
int Http2Session::OnFrameReceive(nghttp2_session* handle, |
|
859 |
const nghttp2_frame* frame, |
||
860 |
void* user_data) { |
||
861 |
55703 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
862 |
55703 |
session->statistics_.frame_count++; |
|
863 |
55703 |
Debug(session, "complete frame received: type: %d", |
|
864 |
frame->hd.type); |
||
865 |
✓✓✓✓ ✓✓✓✓ ✓ |
55703 |
switch (frame->hd.type) { |
866 |
case NGHTTP2_DATA: |
||
867 |
24179 |
return session->HandleDataFrame(frame); |
|
868 |
case NGHTTP2_PUSH_PROMISE: |
||
869 |
// Intentional fall-through, handled just like headers frames |
||
870 |
case NGHTTP2_HEADERS: |
||
871 |
23453 |
session->HandleHeadersFrame(frame); |
|
872 |
23453 |
break; |
|
873 |
case NGHTTP2_SETTINGS: |
||
874 |
2136 |
session->HandleSettingsFrame(frame); |
|
875 |
2136 |
break; |
|
876 |
case NGHTTP2_PRIORITY: |
||
877 |
16 |
session->HandlePriorityFrame(frame); |
|
878 |
16 |
break; |
|
879 |
case NGHTTP2_GOAWAY: |
||
880 |
308 |
session->HandleGoawayFrame(frame); |
|
881 |
308 |
break; |
|
882 |
case NGHTTP2_PING: |
||
883 |
1020 |
session->HandlePingFrame(frame); |
|
884 |
1020 |
break; |
|
885 |
case NGHTTP2_ALTSVC: |
||
886 |
4 |
session->HandleAltSvcFrame(frame); |
|
887 |
4 |
break; |
|
888 |
case NGHTTP2_ORIGIN: |
||
889 |
5 |
session->HandleOriginFrame(frame); |
|
890 |
5 |
break; |
|
891 |
default: |
||
892 |
4582 |
break; |
|
893 |
} |
||
894 |
31524 |
return 0; |
|
895 |
} |
||
896 |
|||
897 |
242 |
int Http2Session::OnInvalidFrame(nghttp2_session* handle, |
|
898 |
const nghttp2_frame* frame, |
||
899 |
int lib_error_code, |
||
900 |
void* user_data) { |
||
901 |
242 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
902 |
|||
903 |
242 |
Debug(session, |
|
904 |
"invalid frame received (%u/%u), code: %d", |
||
905 |
session->invalid_frame_count_, |
||
906 |
242 |
session->js_fields_->max_invalid_frames, |
|
907 |
lib_error_code); |
||
908 |
✓✓ | 242 |
if (session->invalid_frame_count_++ > session->js_fields_->max_invalid_frames) |
909 |
2 |
return 1; |
|
910 |
|||
911 |
// If the error is fatal or if error code is ERR_STREAM_CLOSED... emit error |
||
912 |
✓✗✓✓ ✓✓ |
480 |
if (nghttp2_is_fatal(lib_error_code) || |
913 |
240 |
lib_error_code == NGHTTP2_ERR_STREAM_CLOSED) { |
|
914 |
1 |
Environment* env = session->env(); |
|
915 |
1 |
Isolate* isolate = env->isolate(); |
|
916 |
2 |
HandleScope scope(isolate); |
|
917 |
1 |
Local<Context> context = env->context(); |
|
918 |
Context::Scope context_scope(context); |
||
919 |
1 |
Local<Value> arg = Integer::New(isolate, lib_error_code); |
|
920 |
1 |
session->MakeCallback(env->http2session_on_error_function(), 1, &arg); |
|
921 |
} |
||
922 |
240 |
return 0; |
|
923 |
} |
||
924 |
|||
925 |
// If nghttp2 is unable to send a queued up frame, it will call this callback |
||
926 |
// to let us know. If the failure occurred because we are in the process of |
||
927 |
// closing down the session or stream, we go ahead and ignore it. We don't |
||
928 |
// really care about those and there's nothing we can reasonably do about it |
||
929 |
// anyway. Other types of failures are reported up to JavaScript. This should |
||
930 |
// be exceedingly rare. |
||
931 |
2188 |
int Http2Session::OnFrameNotSent(nghttp2_session* handle, |
|
932 |
const nghttp2_frame* frame, |
||
933 |
int error_code, |
||
934 |
void* user_data) { |
||
935 |
2188 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
936 |
2188 |
Environment* env = session->env(); |
|
937 |
2188 |
Debug(session, "frame type %d was not sent, code: %d", |
|
938 |
frame->hd.type, error_code); |
||
939 |
|||
940 |
// Do not report if the frame was not sent due to the session closing |
||
941 |
✓✓✓✓ |
4380 |
if (error_code == NGHTTP2_ERR_SESSION_CLOSING || |
942 |
✓✓ | 7 |
error_code == NGHTTP2_ERR_STREAM_CLOSED || |
943 |
✓✓✓✓ |
2193 |
error_code == NGHTTP2_ERR_STREAM_CLOSING || |
944 |
2 |
session->js_fields_->frame_error_listener_count == 0) { |
|
945 |
2187 |
return 0; |
|
946 |
} |
||
947 |
|||
948 |
1 |
Isolate* isolate = env->isolate(); |
|
949 |
2 |
HandleScope scope(isolate); |
|
950 |
1 |
Local<Context> context = env->context(); |
|
951 |
Context::Scope context_scope(context); |
||
952 |
|||
953 |
Local<Value> argv[3] = { |
||
954 |
1 |
Integer::New(isolate, frame->hd.stream_id), |
|
955 |
1 |
Integer::New(isolate, frame->hd.type), |
|
956 |
Integer::New(isolate, error_code) |
||
957 |
6 |
}; |
|
958 |
session->MakeCallback( |
||
959 |
env->http2session_on_frame_error_function(), |
||
960 |
1 |
arraysize(argv), argv); |
|
961 |
1 |
return 0; |
|
962 |
} |
||
963 |
|||
964 |
56039 |
int Http2Session::OnFrameSent(nghttp2_session* handle, |
|
965 |
const nghttp2_frame* frame, |
||
966 |
void* user_data) { |
||
967 |
56039 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
968 |
56039 |
session->statistics_.frame_sent += 1; |
|
969 |
56039 |
return 0; |
|
970 |
} |
||
971 |
|||
972 |
// Called by nghttp2 when a stream closes. |
||
973 |
23640 |
int Http2Session::OnStreamClose(nghttp2_session* handle, |
|
974 |
int32_t id, |
||
975 |
uint32_t code, |
||
976 |
void* user_data) { |
||
977 |
23640 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
978 |
23640 |
Environment* env = session->env(); |
|
979 |
23640 |
Isolate* isolate = env->isolate(); |
|
980 |
47280 |
HandleScope scope(isolate); |
|
981 |
23640 |
Local<Context> context = env->context(); |
|
982 |
Context::Scope context_scope(context); |
||
983 |
Debug(session, "stream %d closed with code: %d", id, code); |
||
984 |
47280 |
BaseObjectPtr<Http2Stream> stream = session->FindStream(id); |
|
985 |
// Intentionally ignore the callback if the stream does not exist or has |
||
986 |
// already been destroyed |
||
987 |
✓✓✗✓ ✓✓ |
23640 |
if (!stream || stream->is_destroyed()) |
988 |
55 |
return 0; |
|
989 |
|||
990 |
23585 |
stream->Close(code); |
|
991 |
|||
992 |
// It is possible for the stream close to occur before the stream is |
||
993 |
// ever passed on to the javascript side. If that happens, the callback |
||
994 |
// will return false. |
||
995 |
23585 |
Local<Value> arg = Integer::NewFromUnsigned(isolate, code); |
|
996 |
MaybeLocal<Value> answer = |
||
997 |
23585 |
stream->MakeCallback(env->http2session_on_stream_close_function(), |
|
998 |
47170 |
1, &arg); |
|
999 |
✓✗✓✓ ✓✓ |
70755 |
if (answer.IsEmpty() || answer.ToLocalChecked()->IsFalse()) { |
1000 |
// Skip to destroy |
||
1001 |
138 |
stream->Destroy(); |
|
1002 |
} |
||
1003 |
23585 |
return 0; |
|
1004 |
} |
||
1005 |
|||
1006 |
// Called by nghttp2 when an invalid header has been received. For now, we |
||
1007 |
// ignore these. If this callback was not provided, nghttp2 would handle |
||
1008 |
// invalid headers strictly and would shut down the stream. We are intentionally |
||
1009 |
// being more lenient here although we may want to revisit this choice later. |
||
1010 |
4 |
int Http2Session::OnInvalidHeader(nghttp2_session* session, |
|
1011 |
const nghttp2_frame* frame, |
||
1012 |
nghttp2_rcbuf* name, |
||
1013 |
nghttp2_rcbuf* value, |
||
1014 |
uint8_t flags, |
||
1015 |
void* user_data) { |
||
1016 |
// Ignore invalid header fields by default. |
||
1017 |
4 |
return 0; |
|
1018 |
} |
||
1019 |
|||
1020 |
// When nghttp2 receives a DATA frame, it will deliver the data payload to |
||
1021 |
// us in discrete chunks. We push these into a linked list stored in the |
||
1022 |
// Http2Sttream which is flushed out to JavaScript as quickly as possible. |
||
1023 |
// This can be a particularly hot path. |
||
1024 |
13793 |
int Http2Session::OnDataChunkReceived(nghttp2_session* handle, |
|
1025 |
uint8_t flags, |
||
1026 |
int32_t id, |
||
1027 |
const uint8_t* data, |
||
1028 |
size_t len, |
||
1029 |
void* user_data) { |
||
1030 |
13793 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
1031 |
Debug(session, "buffering data chunk for stream %d, size: " |
||
1032 |
"%d, flags: %d", id, len, flags); |
||
1033 |
13793 |
Environment* env = session->env(); |
|
1034 |
27586 |
HandleScope scope(env->isolate()); |
|
1035 |
|||
1036 |
// We should never actually get a 0-length chunk so this check is |
||
1037 |
// only a precaution at this point. |
||
1038 |
✗✓ | 13793 |
if (len == 0) |
1039 |
return 0; |
||
1040 |
|||
1041 |
// Notify nghttp2 that we've consumed a chunk of data on the connection |
||
1042 |
// so that it can send a WINDOW_UPDATE frame. This is a critical part of |
||
1043 |
// the flow control process in http2 |
||
1044 |
✗✓ | 13793 |
CHECK_EQ(nghttp2_session_consume_connection(handle, len), 0); |
1045 |
27586 |
BaseObjectPtr<Http2Stream> stream = session->FindStream(id); |
|
1046 |
|||
1047 |
// If the stream has been destroyed, ignore this chunk |
||
1048 |
✓✓✗✓ ✓✓ |
13793 |
if (!stream || stream->is_destroyed()) |
1049 |
1 |
return 0; |
|
1050 |
|||
1051 |
13792 |
stream->statistics_.received_bytes += len; |
|
1052 |
|||
1053 |
// Repeatedly ask the stream's owner for memory, and copy the read data |
||
1054 |
// into those buffers. |
||
1055 |
// The typical case is actually the exception here; Http2StreamListeners |
||
1056 |
// know about the HTTP2 session associated with this stream, so they know |
||
1057 |
// about the larger from-socket read buffer, so they do not require copying. |
||
1058 |
do { |
||
1059 |
13792 |
uv_buf_t buf = stream->EmitAlloc(len); |
|
1060 |
13792 |
ssize_t avail = len; |
|
1061 |
✗✓ | 13792 |
if (static_cast<ssize_t>(buf.len) < avail) |
1062 |
avail = buf.len; |
||
1063 |
|||
1064 |
// `buf.base == nullptr` is the default Http2StreamListener's way |
||
1065 |
// of saying that it wants a pointer to the raw original. |
||
1066 |
// Since it has access to the original socket buffer from which the data |
||
1067 |
// was read in the first place, it can use that to minimize ArrayBuffer |
||
1068 |
// allocations. |
||
1069 |
✓✗ | 13792 |
if (LIKELY(buf.base == nullptr)) |
1070 |
13792 |
buf.base = reinterpret_cast<char*>(const_cast<uint8_t*>(data)); |
|
1071 |
else |
||
1072 |
memcpy(buf.base, data, avail); |
||
1073 |
13792 |
data += avail; |
|
1074 |
13792 |
len -= avail; |
|
1075 |
13792 |
stream->EmitRead(avail, buf); |
|
1076 |
|||
1077 |
// If the stream owner (e.g. the JS Http2Stream) wants more data, just |
||
1078 |
// tell nghttp2 that all data has been consumed. Otherwise, defer until |
||
1079 |
// more data is being requested. |
||
1080 |
✓✓ | 13792 |
if (stream->is_reading()) |
1081 |
12683 |
nghttp2_session_consume_stream(handle, id, avail); |
|
1082 |
else |
||
1083 |
1109 |
stream->inbound_consumed_data_while_paused_ += avail; |
|
1084 |
|||
1085 |
// If we have a gathered a lot of data for output, try sending it now. |
||
1086 |
✓✗✓✓ ✓✓ |
27584 |
if (session->outgoing_length_ > 4096 || |
1087 |
13792 |
stream->available_outbound_length_ > 4096) { |
|
1088 |
4 |
session->SendPendingData(); |
|
1089 |
} |
||
1090 |
✗✓ | 13792 |
} while (len != 0); |
1091 |
|||
1092 |
// If we are currently waiting for a write operation to finish, we should |
||
1093 |
// tell nghttp2 that we want to wait before we process more input data. |
||
1094 |
✓✓ | 13792 |
if (session->is_write_in_progress()) { |
1095 |
✗✓ | 560 |
CHECK(session->is_reading_stopped()); |
1096 |
560 |
session->set_receive_paused(); |
|
1097 |
Debug(session, "receive paused"); |
||
1098 |
560 |
return NGHTTP2_ERR_PAUSE; |
|
1099 |
} |
||
1100 |
|||
1101 |
13232 |
return 0; |
|
1102 |
} |
||
1103 |
|||
1104 |
// Called by nghttp2 when it needs to determine how much padding to use in |
||
1105 |
// a DATA or HEADERS frame. |
||
1106 |
3 |
ssize_t Http2Session::OnSelectPadding(nghttp2_session* handle, |
|
1107 |
const nghttp2_frame* frame, |
||
1108 |
size_t maxPayloadLen, |
||
1109 |
void* user_data) { |
||
1110 |
3 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
1111 |
3 |
ssize_t padding = frame->hd.length; |
|
1112 |
|||
1113 |
✗✗✓✗ |
3 |
switch (session->padding_strategy_) { |
1114 |
case PADDING_STRATEGY_NONE: |
||
1115 |
// Fall-through |
||
1116 |
break; |
||
1117 |
case PADDING_STRATEGY_MAX: |
||
1118 |
padding = session->OnMaxFrameSizePadding(padding, maxPayloadLen); |
||
1119 |
break; |
||
1120 |
case PADDING_STRATEGY_ALIGNED: |
||
1121 |
3 |
padding = session->OnDWordAlignedPadding(padding, maxPayloadLen); |
|
1122 |
3 |
break; |
|
1123 |
} |
||
1124 |
3 |
return padding; |
|
1125 |
} |
||
1126 |
|||
1127 |
#define BAD_PEER_MESSAGE "Remote peer returned unexpected data while we " \ |
||
1128 |
"expected SETTINGS frame. Perhaps, peer does not " \ |
||
1129 |
"support HTTP/2 properly." |
||
1130 |
|||
1131 |
// We use this currently to determine when an attempt is made to use the http2 |
||
1132 |
// protocol with a non-http2 peer. |
||
1133 |
239 |
int Http2Session::OnNghttpError(nghttp2_session* handle, |
|
1134 |
const char* message, |
||
1135 |
size_t len, |
||
1136 |
void* user_data) { |
||
1137 |
// Unfortunately, this is currently the only way for us to know if |
||
1138 |
// the session errored because the peer is not an http2 peer. |
||
1139 |
239 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
1140 |
Debug(session, "Error '%s'", message); |
||
1141 |
✓✓ | 239 |
if (strncmp(message, BAD_PEER_MESSAGE, len) == 0) { |
1142 |
1 |
Environment* env = session->env(); |
|
1143 |
1 |
Isolate* isolate = env->isolate(); |
|
1144 |
2 |
HandleScope scope(isolate); |
|
1145 |
1 |
Local<Context> context = env->context(); |
|
1146 |
Context::Scope context_scope(context); |
||
1147 |
1 |
Local<Value> arg = Integer::New(isolate, NGHTTP2_ERR_PROTO); |
|
1148 |
1 |
session->MakeCallback(env->http2session_on_error_function(), 1, &arg); |
|
1149 |
} |
||
1150 |
239 |
return 0; |
|
1151 |
} |
||
1152 |
|||
1153 |
13792 |
uv_buf_t Http2StreamListener::OnStreamAlloc(size_t size) { |
|
1154 |
// See the comments in Http2Session::OnDataChunkReceived |
||
1155 |
// (which is the only possible call site for this method). |
||
1156 |
13792 |
return uv_buf_init(nullptr, size); |
|
1157 |
} |
||
1158 |
|||
1159 |
26454 |
void Http2StreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { |
|
1160 |
✓✗ | 26454 |
Http2Stream* stream = static_cast<Http2Stream*>(stream_); |
1161 |
26454 |
Http2Session* session = stream->session(); |
|
1162 |
26454 |
Environment* env = stream->env(); |
|
1163 |
40246 |
HandleScope handle_scope(env->isolate()); |
|
1164 |
✓✓ | 40246 |
Context::Scope context_scope(env->context()); |
1165 |
|||
1166 |
✓✓ | 26454 |
if (nread < 0) { |
1167 |
12662 |
PassReadErrorToPreviousListener(nread); |
|
1168 |
12662 |
return; |
|
1169 |
} |
||
1170 |
|||
1171 |
Local<ArrayBuffer> ab; |
||
1172 |
✓✓ | 27584 |
if (session->stream_buf_ab_.IsEmpty()) { |
1173 |
5635 |
ab = session->stream_buf_allocation_.ToArrayBuffer(); |
|
1174 |
5635 |
session->stream_buf_ab_.Reset(env->isolate(), ab); |
|
1175 |
} else { |
||
1176 |
8157 |
ab = PersistentToLocal::Strong(session->stream_buf_ab_); |
|
1177 |
} |
||
1178 |
|||
1179 |
// There is a single large array buffer for the entire data read from the |
||
1180 |
// network; create a slice of that array buffer and emit it as the |
||
1181 |
// received data buffer. |
||
1182 |
13792 |
size_t offset = buf.base - session->stream_buf_.base; |
|
1183 |
|||
1184 |
// Verify that the data offset is inside the current read buffer. |
||
1185 |
✗✓ | 13792 |
CHECK_GE(offset, session->stream_buf_offset_); |
1186 |
✗✓ | 13792 |
CHECK_LE(offset, session->stream_buf_.len); |
1187 |
✗✓ | 13792 |
CHECK_LE(offset + buf.len, session->stream_buf_.len); |
1188 |
|||
1189 |
13792 |
stream->CallJSOnreadMethod(nread, ab, offset); |
|
1190 |
} |
||
1191 |
|||
1192 |
|||
1193 |
// Called by OnFrameReceived to notify JavaScript land that a complete |
||
1194 |
// HEADERS frame has been received and processed. This method converts the |
||
1195 |
// received headers into a JavaScript array and pushes those out to JS. |
||
1196 |
23453 |
void Http2Session::HandleHeadersFrame(const nghttp2_frame* frame) { |
|
1197 |
23453 |
Isolate* isolate = env()->isolate(); |
|
1198 |
46906 |
HandleScope scope(isolate); |
|
1199 |
23453 |
Local<Context> context = env()->context(); |
|
1200 |
✓✗ | 23453 |
Context::Scope context_scope(context); |
1201 |
|||
1202 |
23453 |
int32_t id = GetFrameID(frame); |
|
1203 |
23453 |
Debug(this, "handle headers frame for stream %d", id); |
|
1204 |
46906 |
BaseObjectPtr<Http2Stream> stream = FindStream(id); |
|
1205 |
|||
1206 |
// If the stream has already been destroyed, ignore. |
||
1207 |
✓✗✗✓ ✗✓ |
23453 |
if (!stream || stream->is_destroyed()) |
1208 |
return; |
||
1209 |
|||
1210 |
// The headers are stored as a vector of Http2Header instances. |
||
1211 |
// The following converts that into a JS array with the structure: |
||
1212 |
// [name1, value1, name2, value2, name3, value3, name3, value4] and so on. |
||
1213 |
// That array is passed up to the JS layer and converted into an Object form |
||
1214 |
// like {name1: value1, name2: value2, name3: [value3, value4]}. We do it |
||
1215 |
// this way for performance reasons (it's faster to generate and pass an |
||
1216 |
// array than it is to generate and pass the object). |
||
1217 |
|||
1218 |
✓✗ | 46906 |
MaybeStackBuffer<Local<Value>, 64> headers_v(stream->headers_count() * 2); |
1219 |
46906 |
MaybeStackBuffer<Local<Value>, 32> sensitive_v(stream->headers_count()); |
|
1220 |
23453 |
size_t sensitive_count = 0; |
|
1221 |
|||
1222 |
118674 |
stream->TransferHeaders([&](const Http2Header& header, size_t i) { |
|
1223 |
287077 |
headers_v[i * 2] = header.GetName(this).ToLocalChecked(); |
|
1224 |
287072 |
headers_v[i * 2 + 1] = header.GetValue(this).ToLocalChecked(); |
|
1225 |
✓✓ | 71768 |
if (header.flags() & NGHTTP2_NV_FLAG_NO_INDEX) |
1226 |
10 |
sensitive_v[sensitive_count++] = headers_v[i * 2]; |
|
1227 |
95221 |
}); |
|
1228 |
✗✓ | 23453 |
CHECK_EQ(stream->headers_count(), 0); |
1229 |
|||
1230 |
23453 |
DecrementCurrentSessionMemory(stream->current_headers_length_); |
|
1231 |
23453 |
stream->current_headers_length_ = 0; |
|
1232 |
|||
1233 |
Local<Value> args[] = { |
||
1234 |
23453 |
stream->object(), |
|
1235 |
Integer::New(isolate, id), |
||
1236 |
23453 |
Integer::New(isolate, stream->headers_category()), |
|
1237 |
23453 |
Integer::New(isolate, frame->hd.flags), |
|
1238 |
Array::New(isolate, headers_v.out(), headers_v.length()), |
||
1239 |
Array::New(isolate, sensitive_v.out(), sensitive_count), |
||
1240 |
234530 |
}; |
|
1241 |
MakeCallback(env()->http2session_on_headers_function(), |
||
1242 |
23453 |
arraysize(args), args); |
|
1243 |
} |
||
1244 |
|||
1245 |
|||
1246 |
// Called by OnFrameReceived when a complete PRIORITY frame has been |
||
1247 |
// received. Notifies JS land about the priority change. Note that priorities |
||
1248 |
// are considered advisory only, so this has no real effect other than to |
||
1249 |
// simply let user code know that the priority has changed. |
||
1250 |
16 |
void Http2Session::HandlePriorityFrame(const nghttp2_frame* frame) { |
|
1251 |
✓✓ | 16 |
if (js_fields_->priority_listener_count == 0) return; |
1252 |
5 |
Isolate* isolate = env()->isolate(); |
|
1253 |
10 |
HandleScope scope(isolate); |
|
1254 |
5 |
Local<Context> context = env()->context(); |
|
1255 |
Context::Scope context_scope(context); |
||
1256 |
|||
1257 |
5 |
nghttp2_priority priority_frame = frame->priority; |
|
1258 |
5 |
int32_t id = GetFrameID(frame); |
|
1259 |
5 |
Debug(this, "handle priority frame for stream %d", id); |
|
1260 |
// Priority frame stream ID should never be <= 0. nghttp2 handles this for us |
||
1261 |
5 |
nghttp2_priority_spec spec = priority_frame.pri_spec; |
|
1262 |
|||
1263 |
Local<Value> argv[4] = { |
||
1264 |
Integer::New(isolate, id), |
||
1265 |
Integer::New(isolate, spec.stream_id), |
||
1266 |
Integer::New(isolate, spec.weight), |
||
1267 |
5 |
Boolean::New(isolate, spec.exclusive) |
|
1268 |
30 |
}; |
|
1269 |
MakeCallback(env()->http2session_on_priority_function(), |
||
1270 |
5 |
arraysize(argv), argv); |
|
1271 |
} |
||
1272 |
|||
1273 |
|||
1274 |
// Called by OnFrameReceived when a complete DATA frame has been received. |
||
1275 |
// If we know that this was the last DATA frame (because the END_STREAM flag |
||
1276 |
// is set), then we'll terminate the readable side of the StreamBase. |
||
1277 |
24179 |
int Http2Session::HandleDataFrame(const nghttp2_frame* frame) { |
|
1278 |
24179 |
int32_t id = GetFrameID(frame); |
|
1279 |
24179 |
Debug(this, "handling data frame for stream %d", id); |
|
1280 |
48358 |
BaseObjectPtr<Http2Stream> stream = FindStream(id); |
|
1281 |
|||
1282 |
✓✗✓✓ |
48358 |
if (stream && |
1283 |
✓✗✓✓ |
48358 |
!stream->is_destroyed() && |
1284 |
24179 |
frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { |
|
1285 |
12662 |
stream->EmitRead(UV_EOF); |
|
1286 |
✗✓ | 11517 |
} else if (frame->hd.length == 0) { |
1287 |
return 1; // Consider 0-length frame without END_STREAM an error. |
||
1288 |
} |
||
1289 |
24179 |
return 0; |
|
1290 |
} |
||
1291 |
|||
1292 |
|||
1293 |
// Called by OnFrameReceived when a complete GOAWAY frame has been received. |
||
1294 |
308 |
void Http2Session::HandleGoawayFrame(const nghttp2_frame* frame) { |
|
1295 |
308 |
Isolate* isolate = env()->isolate(); |
|
1296 |
616 |
HandleScope scope(isolate); |
|
1297 |
308 |
Local<Context> context = env()->context(); |
|
1298 |
Context::Scope context_scope(context); |
||
1299 |
|||
1300 |
308 |
nghttp2_goaway goaway_frame = frame->goaway; |
|
1301 |
308 |
Debug(this, "handling goaway frame"); |
|
1302 |
|||
1303 |
Local<Value> argv[3] = { |
||
1304 |
Integer::NewFromUnsigned(isolate, goaway_frame.error_code), |
||
1305 |
Integer::New(isolate, goaway_frame.last_stream_id), |
||
1306 |
Undefined(isolate) |
||
1307 |
1232 |
}; |
|
1308 |
|||
1309 |
308 |
size_t length = goaway_frame.opaque_data_len; |
|
1310 |
✓✓ | 308 |
if (length > 0) { |
1311 |
// If the copy fails for any reason here, we just ignore it. |
||
1312 |
// The additional goaway data is completely optional and we |
||
1313 |
// shouldn't fail if we're not able to process it. |
||
1314 |
9 |
argv[2] = Buffer::Copy(isolate, |
|
1315 |
3 |
reinterpret_cast<char*>(goaway_frame.opaque_data), |
|
1316 |
3 |
length).ToLocalChecked(); |
|
1317 |
} |
||
1318 |
|||
1319 |
MakeCallback(env()->http2session_on_goaway_data_function(), |
||
1320 |
308 |
arraysize(argv), argv); |
|
1321 |
308 |
} |
|
1322 |
|||
1323 |
// Called by OnFrameReceived when a complete ALTSVC frame has been received. |
||
1324 |
4 |
void Http2Session::HandleAltSvcFrame(const nghttp2_frame* frame) { |
|
1325 |
✗✓ | 4 |
if (!(js_fields_->bitfield & (1 << kSessionHasAltsvcListeners))) return; |
1326 |
4 |
Isolate* isolate = env()->isolate(); |
|
1327 |
8 |
HandleScope scope(isolate); |
|
1328 |
4 |
Local<Context> context = env()->context(); |
|
1329 |
Context::Scope context_scope(context); |
||
1330 |
|||
1331 |
4 |
int32_t id = GetFrameID(frame); |
|
1332 |
|||
1333 |
4 |
nghttp2_extension ext = frame->ext; |
|
1334 |
4 |
nghttp2_ext_altsvc* altsvc = static_cast<nghttp2_ext_altsvc*>(ext.payload); |
|
1335 |
4 |
Debug(this, "handling altsvc frame"); |
|
1336 |
|||
1337 |
Local<Value> argv[3] = { |
||
1338 |
Integer::New(isolate, id), |
||
1339 |
8 |
OneByteString(isolate, altsvc->origin, altsvc->origin_len), |
|
1340 |
8 |
OneByteString(isolate, altsvc->field_value, altsvc->field_value_len) |
|
1341 |
32 |
}; |
|
1342 |
|||
1343 |
MakeCallback(env()->http2session_on_altsvc_function(), |
||
1344 |
4 |
arraysize(argv), argv); |
|
1345 |
} |
||
1346 |
|||
1347 |
5 |
void Http2Session::HandleOriginFrame(const nghttp2_frame* frame) { |
|
1348 |
5 |
Isolate* isolate = env()->isolate(); |
|
1349 |
10 |
HandleScope scope(isolate); |
|
1350 |
5 |
Local<Context> context = env()->context(); |
|
1351 |
Context::Scope context_scope(context); |
||
1352 |
|||
1353 |
5 |
Debug(this, "handling origin frame"); |
|
1354 |
|||
1355 |
5 |
nghttp2_extension ext = frame->ext; |
|
1356 |
5 |
nghttp2_ext_origin* origin = static_cast<nghttp2_ext_origin*>(ext.payload); |
|
1357 |
|||
1358 |
5 |
size_t nov = origin->nov; |
|
1359 |
10 |
std::vector<Local<Value>> origin_v(nov); |
|
1360 |
|||
1361 |
✓✓ | 14 |
for (size_t i = 0; i < nov; ++i) { |
1362 |
9 |
const nghttp2_origin_entry& entry = origin->ov[i]; |
|
1363 |
18 |
origin_v[i] = OneByteString(isolate, entry.origin, entry.origin_len); |
|
1364 |
} |
||
1365 |
5 |
Local<Value> holder = Array::New(isolate, origin_v.data(), origin_v.size()); |
|
1366 |
5 |
MakeCallback(env()->http2session_on_origin_function(), 1, &holder); |
|
1367 |
5 |
} |
|
1368 |
|||
1369 |
// Called by OnFrameReceived when a complete PING frame has been received. |
||
1370 |
1020 |
void Http2Session::HandlePingFrame(const nghttp2_frame* frame) { |
|
1371 |
1020 |
Isolate* isolate = env()->isolate(); |
|
1372 |
1022 |
HandleScope scope(isolate); |
|
1373 |
1020 |
Local<Context> context = env()->context(); |
|
1374 |
✓✓ | 2 |
Context::Scope context_scope(context); |
1375 |
Local<Value> arg; |
||
1376 |
1020 |
bool ack = frame->hd.flags & NGHTTP2_FLAG_ACK; |
|
1377 |
✓✓ | 1020 |
if (ack) { |
1378 |
22 |
BaseObjectPtr<Http2Ping> ping = PopPing(); |
|
1379 |
|||
1380 |
✓✓ | 11 |
if (!ping) { |
1381 |
// PING Ack is unsolicited. Treat as a connection error. The HTTP/2 |
||
1382 |
// spec does not require this, but there is no legitimate reason to |
||
1383 |
// receive an unsolicited PING ack on a connection. Either the peer |
||
1384 |
// is buggy or malicious, and we're not going to tolerate such |
||
1385 |
// nonsense. |
||
1386 |
2 |
arg = Integer::New(isolate, NGHTTP2_ERR_PROTO); |
|
1387 |
1 |
MakeCallback(env()->http2session_on_error_function(), 1, &arg); |
|
1388 |
1 |
return; |
|
1389 |
} |
||
1390 |
|||
1391 |
10 |
ping->Done(true, frame->ping.opaque_data); |
|
1392 |
10 |
return; |
|
1393 |
} |
||
1394 |
|||
1395 |
✓✓ | 1009 |
if (!(js_fields_->bitfield & (1 << kSessionHasPingListeners))) return; |
1396 |
// Notify the session that a ping occurred |
||
1397 |
6 |
arg = Buffer::Copy( |
|
1398 |
env(), |
||
1399 |
reinterpret_cast<const char*>(frame->ping.opaque_data), |
||
1400 |
4 |
8).ToLocalChecked(); |
|
1401 |
2 |
MakeCallback(env()->http2session_on_ping_function(), 1, &arg); |
|
1402 |
} |
||
1403 |
|||
1404 |
// Called by OnFrameReceived when a complete SETTINGS frame has been received. |
||
1405 |
2136 |
void Http2Session::HandleSettingsFrame(const nghttp2_frame* frame) { |
|
1406 |
2136 |
bool ack = frame->hd.flags & NGHTTP2_FLAG_ACK; |
|
1407 |
✓✓ | 2136 |
if (!ack) { |
1408 |
1600 |
js_fields_->bitfield &= ~(1 << kSessionRemoteSettingsIsUpToDate); |
|
1409 |
✓✓ | 1600 |
if (!(js_fields_->bitfield & (1 << kSessionHasRemoteSettingsListeners))) |
1410 |
3723 |
return; |
|
1411 |
// This is not a SETTINGS acknowledgement, notify and return |
||
1412 |
13 |
MakeCallback(env()->http2session_on_settings_function(), 0, nullptr); |
|
1413 |
13 |
return; |
|
1414 |
} |
||
1415 |
|||
1416 |
// If this is an acknowledgement, we should have an Http2Settings |
||
1417 |
// object for it. |
||
1418 |
536 |
BaseObjectPtr<Http2Settings> settings = PopSettings(); |
|
1419 |
✓✗ | 536 |
if (settings) { |
1420 |
536 |
settings->Done(true); |
|
1421 |
536 |
return; |
|
1422 |
} |
||
1423 |
// SETTINGS Ack is unsolicited. Treat as a connection error. The HTTP/2 |
||
1424 |
// spec does not require this, but there is no legitimate reason to |
||
1425 |
// receive an unsolicited SETTINGS ack on a connection. Either the peer |
||
1426 |
// is buggy or malicious, and we're not going to tolerate such |
||
1427 |
// nonsense. |
||
1428 |
// Note that nghttp2 currently prevents this from happening for SETTINGS |
||
1429 |
// frames, so this block is purely defensive just in case that behavior |
||
1430 |
// changes. Specifically, unlike unsolicited PING acks, unsolicited |
||
1431 |
// SETTINGS acks should *never* make it this far. |
||
1432 |
Isolate* isolate = env()->isolate(); |
||
1433 |
HandleScope scope(isolate); |
||
1434 |
Local<Context> context = env()->context(); |
||
1435 |
Context::Scope context_scope(context); |
||
1436 |
Local<Value> arg = Integer::New(isolate, NGHTTP2_ERR_PROTO); |
||
1437 |
MakeCallback(env()->http2session_on_error_function(), 1, &arg); |
||
1438 |
} |
||
1439 |
|||
1440 |
// Callback used when data has been written to the stream. |
||
1441 |
1569 |
void Http2Session::OnStreamAfterWrite(WriteWrap* w, int status) { |
|
1442 |
1569 |
Debug(this, "write finished with status %d", status); |
|
1443 |
|||
1444 |
✗✓ | 1569 |
CHECK(is_write_in_progress()); |
1445 |
1569 |
set_write_in_progress(false); |
|
1446 |
|||
1447 |
// Inform all pending writes about their completion. |
||
1448 |
1569 |
ClearOutgoing(status); |
|
1449 |
|||
1450 |
✓✓✓✓ |
4704 |
if (is_reading_stopped() && |
1451 |
✓✓✓✓ |
3131 |
!is_write_in_progress() && |
1452 |
1562 |
nghttp2_session_want_read(session_.get())) { |
|
1453 |
1521 |
set_reading_stopped(false); |
|
1454 |
1521 |
stream_->ReadStart(); |
|
1455 |
} |
||
1456 |
|||
1457 |
✓✓ | 1569 |
if (is_destroyed()) { |
1458 |
84 |
HandleScope scope(env()->isolate()); |
|
1459 |
42 |
MakeCallback(env()->ondone_string(), 0, nullptr); |
|
1460 |
42 |
return; |
|
1461 |
} |
||
1462 |
|||
1463 |
// If there is more incoming data queued up, consume it. |
||
1464 |
✓✓ | 1527 |
if (stream_buf_offset_ > 0) { |
1465 |
301 |
ConsumeHTTP2Data(); |
|
1466 |
} |
||
1467 |
|||
1468 |
✓✓ | 1527 |
if (!is_write_scheduled()) { |
1469 |
// Schedule a new write if nghttp2 wants to send data. |
||
1470 |
1418 |
MaybeScheduleWrite(); |
|
1471 |
} |
||
1472 |
} |
||
1473 |
|||
1474 |
// If the underlying nghttp2_session struct has data pending in its outbound |
||
1475 |
// queue, MaybeScheduleWrite will schedule a SendPendingData() call to occur |
||
1476 |
// on the next iteration of the Node.js event loop (using the SetImmediate |
||
1477 |
// queue), but only if a write has not already been scheduled. |
||
1478 |
34169 |
void Http2Session::MaybeScheduleWrite() { |
|
1479 |
✗✓ | 34169 |
CHECK(!is_write_scheduled()); |
1480 |
✗✓ | 34169 |
if (UNLIKELY(!session_)) |
1481 |
return; |
||
1482 |
|||
1483 |
✓✓ | 34169 |
if (nghttp2_session_want_write(session_.get())) { |
1484 |
3560 |
HandleScope handle_scope(env()->isolate()); |
|
1485 |
1780 |
Debug(this, "scheduling write"); |
|
1486 |
1780 |
set_write_scheduled(); |
|
1487 |
3560 |
BaseObjectPtr<Http2Session> strong_ref{this}; |
|
1488 |
15424 |
env()->SetImmediate([this, strong_ref](Environment* env) { |
|
1489 |
✓✗✓✓ ✓✓ |
3560 |
if (!session_ || !is_write_scheduled()) { |
1490 |
// This can happen e.g. when a stream was reset before this turn |
||
1491 |
// of the event loop, in which case SendPendingData() is called early, |
||
1492 |
// or the session was destroyed in the meantime. |
||
1493 |
298 |
return; |
|
1494 |
} |
||
1495 |
|||
1496 |
// Sending data may call arbitrary JS code, so keep track of |
||
1497 |
// async context. |
||
1498 |
2964 |
HandleScope handle_scope(env->isolate()); |
|
1499 |
2964 |
InternalCallbackScope callback_scope(this); |
|
1500 |
1482 |
SendPendingData(); |
|
1501 |
1780 |
}); |
|
1502 |
} |
||
1503 |
} |
||
1504 |
|||
1505 |
62679 |
void Http2Session::MaybeStopReading() { |
|
1506 |
✓✓ | 62679 |
if (is_reading_stopped()) return; |
1507 |
60022 |
int want_read = nghttp2_session_want_read(session_.get()); |
|
1508 |
60022 |
Debug(this, "wants read? %d", want_read); |
|
1509 |
✓✓✓✓ ✓✓ |
60022 |
if (want_read == 0 || is_write_in_progress()) { |
1510 |
1532 |
set_reading_stopped(); |
|
1511 |
1532 |
stream_->ReadStop(); |
|
1512 |
} |
||
1513 |
} |
||
1514 |
|||
1515 |
// Unset the sending state, finish up all current writes, and reset |
||
1516 |
// storage for data and metadata that was associated with these writes. |
||
1517 |
32471 |
void Http2Session::ClearOutgoing(int status) { |
|
1518 |
✗✓ | 32471 |
CHECK(is_sending()); |
1519 |
|||
1520 |
32471 |
set_sending(false); |
|
1521 |
|||
1522 |
✓✓ | 32471 |
if (!outgoing_buffers_.empty()) { |
1523 |
31467 |
outgoing_storage_.clear(); |
|
1524 |
31467 |
outgoing_length_ = 0; |
|
1525 |
|||
1526 |
62934 |
std::vector<NgHttp2StreamWrite> current_outgoing_buffers_; |
|
1527 |
31467 |
current_outgoing_buffers_.swap(outgoing_buffers_); |
|
1528 |
✓✓ | 125613 |
for (const NgHttp2StreamWrite& wr : current_outgoing_buffers_) { |
1529 |
188292 |
BaseObjectPtr<AsyncWrap> wrap = std::move(wr.req_wrap); |
|
1530 |
✓✓ | 94146 |
if (wrap) { |
1531 |
// TODO(addaleax): Pass `status` instead of 0, so that we actually error |
||
1532 |
// out with the error from the write to the underlying protocol, |
||
1533 |
// if one occurred. |
||
1534 |
4030 |
WriteWrap::FromObject(wrap)->Done(0); |
|
1535 |
} |
||
1536 |
} |
||
1537 |
} |
||
1538 |
|||
1539 |
// Now that we've finished sending queued data, if there are any pending |
||
1540 |
// RstStreams we should try sending again and then flush them one by one. |
||
1541 |
✓✓ | 32471 |
if (!pending_rst_streams_.empty()) { |
1542 |
12 |
std::vector<int32_t> current_pending_rst_streams; |
|
1543 |
6 |
pending_rst_streams_.swap(current_pending_rst_streams); |
|
1544 |
|||
1545 |
6 |
SendPendingData(); |
|
1546 |
|||
1547 |
✓✓ | 14 |
for (int32_t stream_id : current_pending_rst_streams) { |
1548 |
16 |
BaseObjectPtr<Http2Stream> stream = FindStream(stream_id); |
|
1549 |
✓✓ | 8 |
if (LIKELY(stream)) |
1550 |
1 |
stream->FlushRstStream(); |
|
1551 |
} |
||
1552 |
} |
||
1553 |
32471 |
} |
|
1554 |
|||
1555 |
94158 |
void Http2Session::PushOutgoingBuffer(NgHttp2StreamWrite&& write) { |
|
1556 |
94158 |
outgoing_length_ += write.buf.len; |
|
1557 |
94158 |
outgoing_buffers_.emplace_back(std::move(write)); |
|
1558 |
94158 |
} |
|
1559 |
|||
1560 |
// Queue a given block of data for sending. This always creates a copy, |
||
1561 |
// so it is used for the cases in which nghttp2 requests sending of a |
||
1562 |
// small chunk of data. |
||
1563 |
56361 |
void Http2Session::CopyDataIntoOutgoing(const uint8_t* src, size_t src_length) { |
|
1564 |
56361 |
size_t offset = outgoing_storage_.size(); |
|
1565 |
56361 |
outgoing_storage_.resize(offset + src_length); |
|
1566 |
56361 |
memcpy(&outgoing_storage_[offset], src, src_length); |
|
1567 |
|||
1568 |
// Store with a base of `nullptr` initially, since future resizes |
||
1569 |
// of the outgoing_buffers_ vector may invalidate the pointer. |
||
1570 |
// The correct base pointers will be set later, before writing to the |
||
1571 |
// underlying socket. |
||
1572 |
112722 |
PushOutgoingBuffer(NgHttp2StreamWrite { |
|
1573 |
uv_buf_init(nullptr, src_length) |
||
1574 |
56361 |
}); |
|
1575 |
56361 |
} |
|
1576 |
|||
1577 |
// Prompts nghttp2 to begin serializing it's pending data and pushes each |
||
1578 |
// chunk out to the i/o socket to be sent. This is a particularly hot method |
||
1579 |
// that will generally be called at least twice be event loop iteration. |
||
1580 |
// This is a potential performance optimization target later. |
||
1581 |
// Returns non-zero value if a write is already in progress. |
||
1582 |
32646 |
uint8_t Http2Session::SendPendingData() { |
|
1583 |
32646 |
Debug(this, "sending pending data"); |
|
1584 |
// Do not attempt to send data on the socket if the destroying flag has |
||
1585 |
// been set. That means everything is shutting down and the socket |
||
1586 |
// will not be usable. |
||
1587 |
✓✓ | 32646 |
if (is_destroyed()) |
1588 |
40 |
return 0; |
|
1589 |
32606 |
set_write_scheduled(false); |
|
1590 |
|||
1591 |
// SendPendingData should not be called recursively. |
||
1592 |
✓✓ | 32606 |
if (is_sending()) |
1593 |
131 |
return 1; |
|
1594 |
// This is cleared by ClearOutgoing(). |
||
1595 |
32475 |
set_sending(); |
|
1596 |
|||
1597 |
ssize_t src_length; |
||
1598 |
const uint8_t* src; |
||
1599 |
|||
1600 |
✗✓ | 32475 |
CHECK(outgoing_buffers_.empty()); |
1601 |
✗✓ | 32475 |
CHECK(outgoing_storage_.empty()); |
1602 |
|||
1603 |
// Part One: Gather data from nghttp2 |
||
1604 |
|||
1605 |
✓✓ | 42316 |
while ((src_length = nghttp2_session_mem_send(session_.get(), &src)) > 0) { |
1606 |
42316 |
Debug(this, "nghttp2 has %d bytes to send", src_length); |
|
1607 |
42316 |
CopyDataIntoOutgoing(src, src_length); |
|
1608 |
} |
||
1609 |
|||
1610 |
✗✓ | 32475 |
CHECK_NE(src_length, NGHTTP2_ERR_NOMEM); |
1611 |
|||
1612 |
✓✓ | 32475 |
if (stream_ == nullptr) { |
1613 |
// It would seem nice to bail out earlier, but `nghttp2_session_mem_send()` |
||
1614 |
// does take care of things like closing the individual streams after |
||
1615 |
// a socket has been torn down, so we still need to call it. |
||
1616 |
12 |
ClearOutgoing(UV_ECANCELED); |
|
1617 |
12 |
return 0; |
|
1618 |
} |
||
1619 |
|||
1620 |
// Part Two: Pass Data to the underlying stream |
||
1621 |
|||
1622 |
32463 |
size_t count = outgoing_buffers_.size(); |
|
1623 |
✓✓ | 32463 |
if (count == 0) { |
1624 |
1004 |
ClearOutgoing(0); |
|
1625 |
1004 |
return 0; |
|
1626 |
} |
||
1627 |
62918 |
MaybeStackBuffer<uv_buf_t, 32> bufs; |
|
1628 |
31459 |
bufs.AllocateSufficientStorage(count); |
|
1629 |
|||
1630 |
// Set the buffer base pointers for copied data that ended up in the |
||
1631 |
// sessions's own storage since it might have shifted around during gathering. |
||
1632 |
// (Those are marked by having .base == nullptr.) |
||
1633 |
31459 |
size_t offset = 0; |
|
1634 |
31459 |
size_t i = 0; |
|
1635 |
✓✓ | 125600 |
for (const NgHttp2StreamWrite& write : outgoing_buffers_) { |
1636 |
94141 |
statistics_.data_sent += write.buf.len; |
|
1637 |
✓✓ | 94141 |
if (write.buf.base == nullptr) { |
1638 |
56344 |
bufs[i++] = uv_buf_init( |
|
1639 |
56344 |
reinterpret_cast<char*>(outgoing_storage_.data() + offset), |
|
1640 |
112688 |
write.buf.len); |
|
1641 |
56344 |
offset += write.buf.len; |
|
1642 |
} else { |
||
1643 |
37797 |
bufs[i++] = write.buf; |
|
1644 |
} |
||
1645 |
} |
||
1646 |
|||
1647 |
31459 |
chunks_sent_since_last_write_++; |
|
1648 |
|||
1649 |
✗✓ | 31459 |
CHECK(!is_write_in_progress()); |
1650 |
31459 |
set_write_in_progress(); |
|
1651 |
31459 |
StreamWriteResult res = underlying_stream()->Write(*bufs, count); |
|
1652 |
✓✓ | 31459 |
if (!res.async) { |
1653 |
29886 |
set_write_in_progress(false); |
|
1654 |
29886 |
ClearOutgoing(res.err); |
|
1655 |
} |
||
1656 |
|||
1657 |
31459 |
MaybeStopReading(); |
|
1658 |
|||
1659 |
31459 |
return 0; |
|
1660 |
} |
||
1661 |
|||
1662 |
|||
1663 |
// This callback is called from nghttp2 when it wants to send DATA frames for a |
||
1664 |
// given Http2Stream, when we set the `NGHTTP2_DATA_FLAG_NO_COPY` flag earlier |
||
1665 |
// in the Http2Stream::Provider::Stream::OnRead callback. |
||
1666 |
// We take the write information directly out of the stream's data queue. |
||
1667 |
14044 |
int Http2Session::OnSendData( |
|
1668 |
nghttp2_session* session_, |
||
1669 |
nghttp2_frame* frame, |
||
1670 |
const uint8_t* framehd, |
||
1671 |
size_t length, |
||
1672 |
nghttp2_data_source* source, |
||
1673 |
void* user_data) { |
||
1674 |
14044 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
1675 |
28088 |
BaseObjectPtr<Http2Stream> stream = session->FindStream(frame->hd.stream_id); |
|
1676 |
✗✓ | 14044 |
if (!stream) return 0; |
1677 |
|||
1678 |
// Send the frame header + a byte that indicates padding length. |
||
1679 |
14044 |
session->CopyDataIntoOutgoing(framehd, 9); |
|
1680 |
✓✓ | 14044 |
if (frame->data.padlen > 0) { |
1681 |
1 |
uint8_t padding_byte = frame->data.padlen - 1; |
|
1682 |
✗✓ | 1 |
CHECK_EQ(padding_byte, frame->data.padlen - 1); |
1683 |
1 |
session->CopyDataIntoOutgoing(&padding_byte, 1); |
|
1684 |
} |
||
1685 |
|||
1686 |
Debug(session, "nghttp2 has %d bytes to send directly", length); |
||
1687 |
✓✓ | 69612 |
while (length > 0) { |
1688 |
// nghttp2 thinks that there is data available (length > 0), which means |
||
1689 |
// we told it so, which means that we *should* have data available. |
||
1690 |
✗✓ | 37796 |
CHECK(!stream->queue_.empty()); |
1691 |
|||
1692 |
37796 |
NgHttp2StreamWrite& write = stream->queue_.front(); |
|
1693 |
✓✓ | 37796 |
if (write.buf.len <= length) { |
1694 |
// This write does not suffice by itself, so we can consume it completely. |
||
1695 |
27784 |
length -= write.buf.len; |
|
1696 |
27784 |
session->PushOutgoingBuffer(std::move(write)); |
|
1697 |
27784 |
stream->queue_.pop(); |
|
1698 |
27784 |
continue; |
|
1699 |
} |
||
1700 |
|||
1701 |
// Slice off `length` bytes of the first write in the queue. |
||
1702 |
20024 |
session->PushOutgoingBuffer(NgHttp2StreamWrite { |
|
1703 |
uv_buf_init(write.buf.base, length) |
||
1704 |
10012 |
}); |
|
1705 |
10012 |
write.buf.base += length; |
|
1706 |
10012 |
write.buf.len -= length; |
|
1707 |
10012 |
break; |
|
1708 |
} |
||
1709 |
|||
1710 |
✓✓ | 14044 |
if (frame->data.padlen > 0) { |
1711 |
// Send padding if that was requested. |
||
1712 |
2 |
session->PushOutgoingBuffer(NgHttp2StreamWrite { |
|
1713 |
1 |
uv_buf_init(const_cast<char*>(zero_bytes_256), frame->data.padlen - 1) |
|
1714 |
1 |
}); |
|
1715 |
} |
||
1716 |
|||
1717 |
14044 |
return 0; |
|
1718 |
} |
||
1719 |
|||
1720 |
// Creates a new Http2Stream and submits a new http2 request. |
||
1721 |
11780 |
Http2Stream* Http2Session::SubmitRequest( |
|
1722 |
const Http2Priority& priority, |
||
1723 |
const Http2Headers& headers, |
||
1724 |
int32_t* ret, |
||
1725 |
int options) { |
||
1726 |
11780 |
Debug(this, "submitting request"); |
|
1727 |
23560 |
Http2Scope h2scope(this); |
|
1728 |
11780 |
Http2Stream* stream = nullptr; |
|
1729 |
23560 |
Http2Stream::Provider::Stream prov(options); |
|
1730 |
11780 |
*ret = nghttp2_submit_request( |
|
1731 |
session_.get(), |
||
1732 |
&priority, |
||
1733 |
headers.data(), |
||
1734 |
headers.length(), |
||
1735 |
11780 |
*prov, |
|
1736 |
nullptr); |
||
1737 |
✗✓ | 11780 |
CHECK_NE(*ret, NGHTTP2_ERR_NOMEM); |
1738 |
✓✓ | 11780 |
if (LIKELY(*ret > 0)) |
1739 |
11779 |
stream = Http2Stream::New(this, *ret, NGHTTP2_HCAT_HEADERS, options); |
|
1740 |
23560 |
return stream; |
|
1741 |
} |
||
1742 |
|||
1743 |
31248 |
uv_buf_t Http2Session::OnStreamAlloc(size_t suggested_size) { |
|
1744 |
31248 |
return AllocatedBuffer::AllocateManaged(env(), suggested_size).release(); |
|
1745 |
} |
||
1746 |
|||
1747 |
// Callback used to receive inbound data from the i/o stream |
||
1748 |
31273 |
void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) { |
|
1749 |
62493 |
HandleScope handle_scope(env()->isolate()); |
|
1750 |
✓✓ | 62493 |
Context::Scope context_scope(env()->context()); |
1751 |
62493 |
Http2Scope h2scope(this); |
|
1752 |
✗✓ | 31273 |
CHECK_NOT_NULL(stream_); |
1753 |
31273 |
Debug(this, "receiving %d bytes, offset %d", nread, stream_buf_offset_); |
|
1754 |
✓✓ | 62493 |
AllocatedBuffer buf(env(), buf_); |
1755 |
|||
1756 |
// Only pass data on if nread > 0 |
||
1757 |
✓✓ | 31273 |
if (nread <= 0) { |
1758 |
✓✗ | 47 |
if (nread < 0) { |
1759 |
47 |
PassReadErrorToPreviousListener(nread); |
|
1760 |
} |
||
1761 |
47 |
return; |
|
1762 |
} |
||
1763 |
|||
1764 |
31226 |
statistics_.data_received += nread; |
|
1765 |
|||
1766 |
✓✓ | 31226 |
if (LIKELY(stream_buf_offset_ == 0)) { |
1767 |
// Shrink to the actual amount of used data. |
||
1768 |
30968 |
buf.Resize(nread); |
|
1769 |
} else { |
||
1770 |
// This is a very unlikely case, and should only happen if the ReadStart() |
||
1771 |
// call in OnStreamAfterWrite() immediately provides data. If that does |
||
1772 |
// happen, we concatenate the data we received with the already-stored |
||
1773 |
// pending input data, slicing off the already processed part. |
||
1774 |
258 |
size_t pending_len = stream_buf_.len - stream_buf_offset_; |
|
1775 |
AllocatedBuffer new_buf = |
||
1776 |
516 |
AllocatedBuffer::AllocateManaged(env(), pending_len + nread); |
|
1777 |
258 |
memcpy(new_buf.data(), stream_buf_.base + stream_buf_offset_, pending_len); |
|
1778 |
258 |
memcpy(new_buf.data() + pending_len, buf.data(), nread); |
|
1779 |
|||
1780 |
258 |
buf = std::move(new_buf); |
|
1781 |
258 |
nread = buf.size(); |
|
1782 |
258 |
stream_buf_offset_ = 0; |
|
1783 |
258 |
stream_buf_ab_.Reset(); |
|
1784 |
|||
1785 |
// We have now fully processed the stream_buf_ input chunk (by moving the |
||
1786 |
// remaining part into buf, which will be accounted for below). |
||
1787 |
258 |
DecrementCurrentSessionMemory(stream_buf_.len); |
|
1788 |
} |
||
1789 |
|||
1790 |
31226 |
IncrementCurrentSessionMemory(nread); |
|
1791 |
|||
1792 |
// Remember the current buffer, so that OnDataChunkReceived knows the |
||
1793 |
// offset of a DATA frame's data into the socket read buffer. |
||
1794 |
31226 |
stream_buf_ = uv_buf_init(buf.data(), static_cast<unsigned int>(nread)); |
|
1795 |
|||
1796 |
31226 |
Isolate* isolate = env()->isolate(); |
|
1797 |
|||
1798 |
// Store this so we can create an ArrayBuffer for read data from it. |
||
1799 |
// DATA frames will be emitted as slices of that ArrayBuffer to avoid having |
||
1800 |
// to copy memory. |
||
1801 |
31226 |
stream_buf_allocation_ = std::move(buf); |
|
1802 |
|||
1803 |
31226 |
ssize_t ret = ConsumeHTTP2Data(); |
|
1804 |
|||
1805 |
✓✓ | 31226 |
if (UNLIKELY(ret < 0)) { |
1806 |
6 |
Debug(this, "fatal error receiving data: %d", ret); |
|
1807 |
6 |
Local<Value> arg = Integer::New(isolate, static_cast<int32_t>(ret)); |
|
1808 |
6 |
MakeCallback(env()->http2session_on_error_function(), 1, &arg); |
|
1809 |
6 |
return; |
|
1810 |
} |
||
1811 |
|||
1812 |
✓✓ | 31220 |
MaybeStopReading(); |
1813 |
} |
||
1814 |
|||
1815 |
23660 |
bool Http2Session::HasWritesOnSocketForStream(Http2Stream* stream) { |
|
1816 |
✓✓ | 23934 |
for (const NgHttp2StreamWrite& wr : outgoing_buffers_) { |
1817 |
✓✓✓✗ ✓✓✓✓ |
335 |
if (wr.req_wrap && WriteWrap::FromObject(wr.req_wrap)->stream() == stream) |
1818 |
61 |
return true; |
|
1819 |
} |
||
1820 |
23599 |
return false; |
|
1821 |
} |
||
1822 |
|||
1823 |
// Every Http2Session session is tightly bound to a single i/o StreamBase |
||
1824 |
// (typically a net.Socket or tls.TLSSocket). The lifecycle of the two is |
||
1825 |
// tightly coupled with all data transfer between the two happening at the |
||
1826 |
// C++ layer via the StreamBase API. |
||
1827 |
660 |
void Http2Session::Consume(Local<Object> stream_obj) { |
|
1828 |
660 |
StreamBase* stream = StreamBase::FromObject(stream_obj); |
|
1829 |
660 |
stream->PushStreamListener(this); |
|
1830 |
660 |
Debug(this, "i/o stream consumed"); |
|
1831 |
660 |
} |
|
1832 |
|||
1833 |
// Allow injecting of data from JS |
||
1834 |
// This is used when the socket has already some data received |
||
1835 |
// before our listener was attached |
||
1836 |
// https://github.com/nodejs/node/issues/35475 |
||
1837 |
2 |
void Http2Session::Receive(const FunctionCallbackInfo<Value>& args) { |
|
1838 |
Http2Session* session; |
||
1839 |
✗✓ | 2 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
1840 |
✗✓ | 4 |
CHECK(args[0]->IsObject()); |
1841 |
|||
1842 |
2 |
ArrayBufferViewContents<char> buffer(args[0]); |
|
1843 |
2 |
const char* data = buffer.data(); |
|
1844 |
2 |
size_t len = buffer.length(); |
|
1845 |
2 |
Debug(session, "Receiving %zu bytes injected from JS", len); |
|
1846 |
|||
1847 |
// Copy given buffer |
||
1848 |
✓✓ | 6 |
while (len > 0) { |
1849 |
2 |
uv_buf_t buf = session->OnStreamAlloc(len); |
|
1850 |
✓✗ | 2 |
size_t copy = buf.len > len ? len : buf.len; |
1851 |
2 |
memcpy(buf.base, data, copy); |
|
1852 |
2 |
buf.len = copy; |
|
1853 |
2 |
session->OnStreamRead(copy, buf); |
|
1854 |
|||
1855 |
2 |
data += copy; |
|
1856 |
2 |
len -= copy; |
|
1857 |
} |
||
1858 |
} |
||
1859 |
|||
1860 |
23779 |
Http2Stream* Http2Stream::New(Http2Session* session, |
|
1861 |
int32_t id, |
||
1862 |
nghttp2_headers_category category, |
||
1863 |
int options) { |
||
1864 |
Local<Object> obj; |
||
1865 |
✗✓ | 47558 |
if (!session->env() |
1866 |
47558 |
->http2stream_constructor_template() |
|
1867 |
71337 |
->NewInstance(session->env()->context()) |
|
1868 |
23779 |
.ToLocal(&obj)) { |
|
1869 |
return nullptr; |
||
1870 |
} |
||
1871 |
23779 |
return new Http2Stream(session, obj, id, category, options); |
|
1872 |
} |
||
1873 |
|||
1874 |
23779 |
Http2Stream::Http2Stream(Http2Session* session, |
|
1875 |
Local<Object> obj, |
||
1876 |
int32_t id, |
||
1877 |
nghttp2_headers_category category, |
||
1878 |
23779 |
int options) |
|
1879 |
: AsyncWrap(session->env(), obj, AsyncWrap::PROVIDER_HTTP2STREAM), |
||
1880 |
StreamBase(session->env()), |
||
1881 |
session_(session), |
||
1882 |
id_(id), |
||
1883 |
23779 |
current_headers_category_(category) { |
|
1884 |
23779 |
MakeWeak(); |
|
1885 |
23779 |
StreamBase::AttachToObject(GetObject()); |
|
1886 |
23779 |
statistics_.start_time = uv_hrtime(); |
|
1887 |
|||
1888 |
// Limit the number of header pairs |
||
1889 |
23779 |
max_header_pairs_ = session->max_header_pairs(); |
|
1890 |
✗✓ | 23779 |
if (max_header_pairs_ == 0) { |
1891 |
max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS; |
||
1892 |
} |
||
1893 |
23779 |
current_headers_.reserve(std::min(max_header_pairs_, 12u)); |
|
1894 |
|||
1895 |
// Limit the number of header octets |
||
1896 |
23779 |
max_header_length_ = |
|
1897 |
23779 |
std::min( |
|
1898 |
47558 |
nghttp2_session_get_local_settings( |
|
1899 |
session->session(), |
||
1900 |
NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE), |
||
1901 |
71337 |
MAX_MAX_HEADER_LIST_SIZE); |
|
1902 |
|||
1903 |
✓✓ | 23779 |
if (options & STREAM_OPTION_GET_TRAILERS) |
1904 |
2 |
set_has_trailers(); |
|
1905 |
|||
1906 |
23779 |
PushStreamListener(&stream_listener_); |
|
1907 |
|||
1908 |
✓✓ | 23779 |
if (options & STREAM_OPTION_EMPTY_PAYLOAD) |
1909 |
605 |
Shutdown(); |
|
1910 |
23779 |
session->AddStream(this); |
|
1911 |
23779 |
} |
|
1912 |
|||
1913 |
71337 |
Http2Stream::~Http2Stream() { |
|
1914 |
23779 |
Debug(this, "tearing down stream"); |
|
1915 |
47558 |
} |
|
1916 |
|||
1917 |
void Http2Stream::MemoryInfo(MemoryTracker* tracker) const { |
||
1918 |
tracker->TrackField("current_headers", current_headers_); |
||
1919 |
tracker->TrackField("queue", queue_); |
||
1920 |
} |
||
1921 |
|||
1922 |
18 |
std::string Http2Stream::diagnostic_name() const { |
|
1923 |
36 |
return "HttpStream " + std::to_string(id()) + " (" + |
|
1924 |
72 |
std::to_string(static_cast<int64_t>(get_async_id())) + ") [" + |
|
1925 |
54 |
session()->diagnostic_name() + "]"; |
|
1926 |
} |
||
1927 |
|||
1928 |
// Notify the Http2Stream that a new block of HEADERS is being processed. |
||
1929 |
11703 |
void Http2Stream::StartHeaders(nghttp2_headers_category category) { |
|
1930 |
11703 |
Debug(this, "starting headers, category: %d", category); |
|
1931 |
✗✓ | 11703 |
CHECK(!this->is_destroyed()); |
1932 |
11703 |
session_->DecrementCurrentSessionMemory(current_headers_length_); |
|
1933 |
11703 |
current_headers_length_ = 0; |
|
1934 |
11703 |
current_headers_.clear(); |
|
1935 |
11703 |
current_headers_category_ = category; |
|
1936 |
11703 |
} |
|
1937 |
|||
1938 |
|||
1939 |
nghttp2_stream* Http2Stream::operator*() const { return stream(); } |
||
1940 |
|||
1941 |
11 |
nghttp2_stream* Http2Stream::stream() const { |
|
1942 |
11 |
return nghttp2_session_find_stream(session_->session(), id_); |
|
1943 |
} |
||
1944 |
|||
1945 |
23585 |
void Http2Stream::Close(int32_t code) { |
|
1946 |
✗✓ | 23585 |
CHECK(!this->is_destroyed()); |
1947 |
23585 |
set_closed(); |
|
1948 |
23585 |
code_ = code; |
|
1949 |
23585 |
Debug(this, "closed with code %d", code); |
|
1950 |
23585 |
} |
|
1951 |
|||
1952 |
24092 |
ShutdownWrap* Http2Stream::CreateShutdownWrap(v8::Local<v8::Object> object) { |
|
1953 |
// DoShutdown() always finishes synchronously, so there's no need to create |
||
1954 |
// a structure to store asynchronous context. |
||
1955 |
24092 |
return nullptr; |
|
1956 |
} |
||
1957 |
|||
1958 |
24092 |
int Http2Stream::DoShutdown(ShutdownWrap* req_wrap) { |
|
1959 |
✗✓ | 24092 |
if (is_destroyed()) |
1960 |
return UV_EPIPE; |
||
1961 |
|||
1962 |
{ |
||
1963 |
48184 |
Http2Scope h2scope(this); |
|
1964 |
24092 |
set_not_writable(); |
|
1965 |
✗✓ | 24092 |
CHECK_NE(nghttp2_session_resume_data( |
1966 |
session_->session(), id_), |
||
1967 |
NGHTTP2_ERR_NOMEM); |
||
1968 |
24092 |
Debug(this, "writable side shutdown"); |
|
1969 |
} |
||
1970 |
24092 |
return 1; |
|
1971 |
} |
||
1972 |
|||
1973 |
// Destroy the Http2Stream and render it unusable. Actual resources for the |
||
1974 |
// Stream will not be freed until the next tick of the Node.js event loop |
||
1975 |
// using the SetImmediate queue. |
||
1976 |
23671 |
void Http2Stream::Destroy() { |
|
1977 |
// Do nothing if this stream instance is already destroyed |
||
1978 |
✓✓ | 23671 |
if (is_destroyed()) |
1979 |
1 |
return; |
|
1980 |
✓✓ | 23670 |
if (session_->has_pending_rststream(id_)) |
1981 |
7 |
FlushRstStream(); |
|
1982 |
23670 |
set_destroyed(); |
|
1983 |
|||
1984 |
23670 |
Debug(this, "destroying stream"); |
|
1985 |
|||
1986 |
// Wait until the start of the next loop to delete because there |
||
1987 |
// may still be some pending operations queued for this stream. |
||
1988 |
47340 |
BaseObjectPtr<Http2Stream> strong_ref = session_->RemoveStream(id_); |
|
1989 |
✓✗ | 23670 |
if (strong_ref) { |
1990 |
118350 |
env()->SetImmediate([this, strong_ref = std::move(strong_ref)]( |
|
1991 |
94627 |
Environment* env) { |
|
1992 |
// Free any remaining outgoing data chunks here. This should be done |
||
1993 |
// here because it's possible for destroy to have been called while |
||
1994 |
// we still have queued outbound writes. |
||
1995 |
✓✓ | 23682 |
while (!queue_.empty()) { |
1996 |
6 |
NgHttp2StreamWrite& head = queue_.front(); |
|
1997 |
✓✗ | 6 |
if (head.req_wrap) |
1998 |
6 |
WriteWrap::FromObject(head.req_wrap)->Done(UV_ECANCELED); |
|
1999 |
6 |
queue_.pop(); |
|
2000 |
} |
||
2001 |
|||
2002 |
// We can destroy the stream now if there are no writes for it |
||
2003 |
// already on the socket. Otherwise, we'll wait for the garbage collector |
||
2004 |
// to take care of cleaning up. |
||
2005 |
✓✓✓✓ ✓✓ |
47330 |
if (session() == nullptr || |
2006 |
23660 |
!session()->HasWritesOnSocketForStream(this)) { |
|
2007 |
// Delete once strong_ref goes out of scope. |
||
2008 |
23609 |
Detach(); |
|
2009 |
} |
||
2010 |
47340 |
}); |
|
2011 |
} |
||
2012 |
|||
2013 |
23670 |
statistics_.end_time = uv_hrtime(); |
|
2014 |
47340 |
session_->statistics_.stream_average_duration = |
|
2015 |
47340 |
((statistics_.end_time - statistics_.start_time) / |
|
2016 |
47340 |
session_->statistics_.stream_count) / 1e6; |
|
2017 |
23670 |
EmitStatistics(); |
|
2018 |
} |
||
2019 |
|||
2020 |
|||
2021 |
// Initiates a response on the Http2Stream using data provided via the |
||
2022 |
// StreamBase Streams API. |
||
2023 |
11705 |
int Http2Stream::SubmitResponse(const Http2Headers& headers, int options) { |
|
2024 |
✗✓ | 11705 |
CHECK(!this->is_destroyed()); |
2025 |
23410 |
Http2Scope h2scope(this); |
|
2026 |
11705 |
Debug(this, "submitting response"); |
|
2027 |
✓✓ | 11705 |
if (options & STREAM_OPTION_GET_TRAILERS) |
2028 |
169 |
set_has_trailers(); |
|
2029 |
|||
2030 |
✓✓ | 11705 |
if (!is_writable()) |
2031 |
10143 |
options |= STREAM_OPTION_EMPTY_PAYLOAD; |
|
2032 |
|||
2033 |
23410 |
Http2Stream::Provider::Stream prov(this, options); |
|
2034 |
11705 |
int ret = nghttp2_submit_response( |
|
2035 |
session_->session(), |
||
2036 |
id_, |
||
2037 |
headers.data(), |
||
2038 |
headers.length(), |
||
2039 |
23410 |
*prov); |
|
2040 |
✗✓ | 11705 |
CHECK_NE(ret, NGHTTP2_ERR_NOMEM); |
2041 |
23410 |
return ret; |
|
2042 |
} |
||
2043 |
|||
2044 |
|||
2045 |
// Submit informational headers for a stream. |
||
2046 |
5 |
int Http2Stream::SubmitInfo(const Http2Headers& headers) { |
|
2047 |
✗✓ | 5 |
CHECK(!this->is_destroyed()); |
2048 |
10 |
Http2Scope h2scope(this); |
|
2049 |
10 |
Debug(this, "sending %d informational headers", headers.length()); |
|
2050 |
5 |
int ret = nghttp2_submit_headers( |
|
2051 |
session_->session(), |
||
2052 |
NGHTTP2_FLAG_NONE, |
||
2053 |
id_, |
||
2054 |
nullptr, |
||
2055 |
headers.data(), |
||
2056 |
headers.length(), |
||
2057 |
5 |
nullptr); |
|
2058 |
✗✓ | 5 |
CHECK_NE(ret, NGHTTP2_ERR_NOMEM); |
2059 |
10 |
return ret; |
|
2060 |
} |
||
2061 |
|||
2062 |
37 |
void Http2Stream::OnTrailers() { |
|
2063 |
37 |
Debug(this, "let javascript know we are ready for trailers"); |
|
2064 |
✗✓ | 37 |
CHECK(!this->is_destroyed()); |
2065 |
37 |
Isolate* isolate = env()->isolate(); |
|
2066 |
74 |
HandleScope scope(isolate); |
|
2067 |
37 |
Local<Context> context = env()->context(); |
|
2068 |
Context::Scope context_scope(context); |
||
2069 |
37 |
set_has_trailers(false); |
|
2070 |
37 |
MakeCallback(env()->http2session_on_stream_trailers_function(), 0, nullptr); |
|
2071 |
37 |
} |
|
2072 |
|||
2073 |
// Submit informational headers for a stream. |
||
2074 |
31 |
int Http2Stream::SubmitTrailers(const Http2Headers& headers) { |
|
2075 |
✗✓ | 31 |
CHECK(!this->is_destroyed()); |
2076 |
62 |
Http2Scope h2scope(this); |
|
2077 |
62 |
Debug(this, "sending %d trailers", headers.length()); |
|
2078 |
int ret; |
||
2079 |
// Sending an empty trailers frame poses problems in Safari, Edge & IE. |
||
2080 |
// Instead we can just send an empty data frame with NGHTTP2_FLAG_END_STREAM |
||
2081 |
// to indicate that the stream is ready to be closed. |
||
2082 |
✓✓ | 31 |
if (headers.length() == 0) { |
2083 |
52 |
Http2Stream::Provider::Stream prov(this, 0); |
|
2084 |
26 |
ret = nghttp2_submit_data( |
|
2085 |
session_->session(), |
||
2086 |
NGHTTP2_FLAG_END_STREAM, |
||
2087 |
id_, |
||
2088 |
26 |
*prov); |
|
2089 |
} else { |
||
2090 |
5 |
ret = nghttp2_submit_trailer( |
|
2091 |
session_->session(), |
||
2092 |
id_, |
||
2093 |
headers.data(), |
||
2094 |
headers.length()); |
||
2095 |
} |
||
2096 |
✗✓ | 31 |
CHECK_NE(ret, NGHTTP2_ERR_NOMEM); |
2097 |
62 |
return ret; |
|
2098 |
} |
||
2099 |
|||
2100 |
// Submit a PRIORITY frame to the connected peer. |
||
2101 |
6 |
int Http2Stream::SubmitPriority(const Http2Priority& priority, |
|
2102 |
bool silent) { |
||
2103 |
✗✓ | 6 |
CHECK(!this->is_destroyed()); |
2104 |
12 |
Http2Scope h2scope(this); |
|
2105 |
6 |
Debug(this, "sending priority spec"); |
|
2106 |
✗✓ | 12 |
int ret = silent ? |
2107 |
nghttp2_session_change_stream_priority( |
||
2108 |
session_->session(), |
||
2109 |
id_, |
||
2110 |
&priority) : |
||
2111 |
6 |
nghttp2_submit_priority( |
|
2112 |
session_->session(), |
||
2113 |
NGHTTP2_FLAG_NONE, |
||
2114 |
6 |
id_, &priority); |
|
2115 |
✗✓ | 6 |
CHECK_NE(ret, NGHTTP2_ERR_NOMEM); |
2116 |
12 |
return ret; |
|
2117 |
} |
||
2118 |
|||
2119 |
// Closes the Http2Stream by submitting an RST_STREAM frame to the connected |
||
2120 |
// peer. |
||
2121 |
120 |
void Http2Stream::SubmitRstStream(const uint32_t code) { |
|
2122 |
✗✓ | 120 |
CHECK(!this->is_destroyed()); |
2123 |
120 |
code_ = code; |
|
2124 |
// If possible, force a purge of any currently pending data here to make sure |
||
2125 |
// it is sent before closing the stream. If it returns non-zero then we need |
||
2126 |
// to wait until the current write finishes and try again to avoid nghttp2 |
||
2127 |
// behaviour where it prioritizes RstStream over everything else. |
||
2128 |
✓✓ | 120 |
if (session_->SendPendingData() != 0) { |
2129 |
8 |
session_->AddPendingRstStream(id_); |
|
2130 |
8 |
return; |
|
2131 |
} |
||
2132 |
|||
2133 |
112 |
FlushRstStream(); |
|
2134 |
} |
||
2135 |
|||
2136 |
120 |
void Http2Stream::FlushRstStream() { |
|
2137 |
✓✓ | 120 |
if (is_destroyed()) |
2138 |
5 |
return; |
|
2139 |
230 |
Http2Scope h2scope(this); |
|
2140 |
✗✓ | 115 |
CHECK_EQ(nghttp2_submit_rst_stream( |
2141 |
session_->session(), |
||
2142 |
NGHTTP2_FLAG_NONE, |
||
2143 |
id_, |
||
2144 |
code_), 0); |
||
2145 |
} |
||
2146 |
|||
2147 |
|||
2148 |
// Submit a push promise and create the associated Http2Stream if successful. |
||
2149 |
9 |
Http2Stream* Http2Stream::SubmitPushPromise(const Http2Headers& headers, |
|
2150 |
int32_t* ret, |
||
2151 |
int options) { |
||
2152 |
✗✓ | 9 |
CHECK(!this->is_destroyed()); |
2153 |
18 |
Http2Scope h2scope(this); |
|
2154 |
9 |
Debug(this, "sending push promise"); |
|
2155 |
9 |
*ret = nghttp2_submit_push_promise( |
|
2156 |
session_->session(), |
||
2157 |
NGHTTP2_FLAG_NONE, |
||
2158 |
id_, |
||
2159 |
headers.data(), |
||
2160 |
headers.length(), |
||
2161 |
nullptr); |
||
2162 |
✗✓ | 9 |
CHECK_NE(*ret, NGHTTP2_ERR_NOMEM); |
2163 |
9 |
Http2Stream* stream = nullptr; |
|
2164 |
✓✗ | 9 |
if (*ret > 0) { |
2165 |
9 |
stream = Http2Stream::New( |
|
2166 |
9 |
session_.get(), *ret, NGHTTP2_HCAT_HEADERS, options); |
|
2167 |
} |
||
2168 |
|||
2169 |
18 |
return stream; |
|
2170 |
} |
||
2171 |
|||
2172 |
// Switch the StreamBase into flowing mode to begin pushing chunks of data |
||
2173 |
// out to JS land. |
||
2174 |
23459 |
int Http2Stream::ReadStart() { |
|
2175 |
46918 |
Http2Scope h2scope(this); |
|
2176 |
✗✓ | 23459 |
CHECK(!this->is_destroyed()); |
2177 |
23459 |
set_reading(); |
|
2178 |
|||
2179 |
23459 |
Debug(this, "reading starting"); |
|
2180 |
|||
2181 |
// Tell nghttp2 about our consumption of the data that was handed |
||
2182 |
// off to JS land. |
||
2183 |
23459 |
nghttp2_session_consume_stream( |
|
2184 |
session_->session(), |
||
2185 |
id_, |
||
2186 |
23459 |
inbound_consumed_data_while_paused_); |
|
2187 |
23459 |
inbound_consumed_data_while_paused_ = 0; |
|
2188 |
|||
2189 |
46918 |
return 0; |
|
2190 |
} |
||
2191 |
|||
2192 |
// Switch the StreamBase into paused mode. |
||
2193 |
17756 |
int Http2Stream::ReadStop() { |
|
2194 |
✗✓ | 17756 |
CHECK(!this->is_destroyed()); |
2195 |
✓✓ | 17756 |
if (!is_reading()) |
2196 |
2236 |
return 0; |
|
2197 |
15520 |
set_paused(); |
|
2198 |
15520 |
Debug(this, "reading stopped"); |
|
2199 |
15520 |
return 0; |
|
2200 |
} |
||
2201 |
|||
2202 |
// The Http2Stream class is a subclass of StreamBase. The DoWrite method |
||
2203 |
// receives outbound chunks of data to send as outbound DATA frames. These |
||
2204 |
// are queued in an internal linked list of uv_buf_t structs that are sent |
||
2205 |
// when nghttp2 is ready to serialize the data frame. |
||
2206 |
// |
||
2207 |
// Queue the given set of uv_but_t handles for writing to an |
||
2208 |
// nghttp2_stream. The WriteWrap's Done callback will be invoked once the |
||
2209 |
// chunks of data have been flushed to the underlying nghttp2_session. |
||
2210 |
// Note that this does *not* mean that the data has been flushed |
||
2211 |
// to the socket yet. |
||
2212 |
4040 |
int Http2Stream::DoWrite(WriteWrap* req_wrap, |
|
2213 |
uv_buf_t* bufs, |
||
2214 |
size_t nbufs, |
||
2215 |
uv_stream_t* send_handle) { |
||
2216 |
✗✓ | 4040 |
CHECK_NULL(send_handle); |
2217 |
8080 |
Http2Scope h2scope(this); |
|
2218 |
✓✗✗✓ ✗✓ |
4040 |
if (!is_writable() || is_destroyed()) { |
2219 |
req_wrap->Done(UV_EOF); |
||
2220 |
return 0; |
||
2221 |
} |
||
2222 |
4040 |
Debug(this, "queuing %d buffers to send", nbufs); |
|
2223 |
✓✓ | 31836 |
for (size_t i = 0; i < nbufs; ++i) { |
2224 |
// Store the req_wrap on the last write info in the queue, so that it is |
||
2225 |
// only marked as finished once all buffers associated with it are finished. |
||
2226 |
55592 |
queue_.emplace(NgHttp2StreamWrite { |
|
2227 |
✓✓ | 59632 |
BaseObjectPtr<AsyncWrap>( |
2228 |
31836 |
i == nbufs - 1 ? req_wrap->GetAsyncWrap() : nullptr), |
|
2229 |
27796 |
bufs[i] |
|
2230 |
27796 |
}); |
|
2231 |
27796 |
IncrementAvailableOutboundLength(bufs[i].len); |
|
2232 |
} |
||
2233 |
✗✓ | 4040 |
CHECK_NE(nghttp2_session_resume_data( |
2234 |
session_->session(), |
||
2235 |
id_), NGHTTP2_ERR_NOMEM); |
||
2236 |
4040 |
return 0; |
|
2237 |
} |
||
2238 |
|||
2239 |
// Ads a header to the Http2Stream. Note that the header name and value are |
||
2240 |
// provided using a buffer structure provided by nghttp2 that allows us to |
||
2241 |
// avoid unnecessary memcpy's. Those buffers are ref counted. The ref count |
||
2242 |
// is incremented here and are decremented when the header name and values |
||
2243 |
// are garbage collected later. |
||
2244 |
71882 |
bool Http2Stream::AddHeader(nghttp2_rcbuf* name, |
|
2245 |
nghttp2_rcbuf* value, |
||
2246 |
uint8_t flags) { |
||
2247 |
✗✓ | 71882 |
CHECK(!this->is_destroyed()); |
2248 |
|||
2249 |
✗✓ | 71882 |
if (Http2RcBufferPointer::IsZeroLength(name)) |
2250 |
return true; // Ignore empty headers. |
||
2251 |
|||
2252 |
143764 |
Http2Header header(env(), name, value, flags); |
|
2253 |
71882 |
size_t length = header.length() + 32; |
|
2254 |
// A header can only be added if we have not exceeded the maximum number |
||
2255 |
// of headers and the session has memory available for it. |
||
2256 |
✓✓✓✓ |
215646 |
if (!session_->has_available_session_memory(length) || |
2257 |
✓✗✓✓ |
143763 |
current_headers_.size() == max_header_pairs_ || |
2258 |
71881 |
current_headers_length_ + length > max_header_length_) { |
|
2259 |
3 |
return false; |
|
2260 |
} |
||
2261 |
|||
2262 |
✓✓ | 71879 |
if (statistics_.first_header == 0) |
2263 |
23472 |
statistics_.first_header = uv_hrtime(); |
|
2264 |
|||
2265 |
71879 |
current_headers_.push_back(std::move(header)); |
|
2266 |
|||
2267 |
71879 |
current_headers_length_ += length; |
|
2268 |
71879 |
session_->IncrementCurrentSessionMemory(length); |
|
2269 |
71879 |
return true; |
|
2270 |
} |
||
2271 |
|||
2272 |
// A Provider is the thing that provides outbound DATA frame data. |
||
2273 |
11731 |
Http2Stream::Provider::Provider(Http2Stream* stream, int options) { |
|
2274 |
✗✓ | 11731 |
CHECK(!stream->is_destroyed()); |
2275 |
11731 |
provider_.source.ptr = stream; |
|
2276 |
11731 |
empty_ = options & STREAM_OPTION_EMPTY_PAYLOAD; |
|
2277 |
11731 |
} |
|
2278 |
|||
2279 |
11780 |
Http2Stream::Provider::Provider(int options) { |
|
2280 |
11780 |
provider_.source.ptr = nullptr; |
|
2281 |
11780 |
empty_ = options & STREAM_OPTION_EMPTY_PAYLOAD; |
|
2282 |
11780 |
} |
|
2283 |
|||
2284 |
47022 |
Http2Stream::Provider::~Provider() { |
|
2285 |
23511 |
provider_.source.ptr = nullptr; |
|
2286 |
23511 |
} |
|
2287 |
|||
2288 |
// The Stream Provider pulls data from a linked list of uv_buf_t structs |
||
2289 |
// built via the StreamBase API and the Streams js API. |
||
2290 |
11780 |
Http2Stream::Provider::Stream::Stream(int options) |
|
2291 |
11780 |
: Http2Stream::Provider(options) { |
|
2292 |
11780 |
provider_.read_callback = Http2Stream::Provider::Stream::OnRead; |
|
2293 |
11780 |
} |
|
2294 |
|||
2295 |
11731 |
Http2Stream::Provider::Stream::Stream(Http2Stream* stream, int options) |
|
2296 |
11731 |
: Http2Stream::Provider(stream, options) { |
|
2297 |
11731 |
provider_.read_callback = Http2Stream::Provider::Stream::OnRead; |
|
2298 |
11731 |
} |
|
2299 |
|||
2300 |
28108 |
ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle, |
|
2301 |
int32_t id, |
||
2302 |
uint8_t* buf, |
||
2303 |
size_t length, |
||
2304 |
uint32_t* flags, |
||
2305 |
nghttp2_data_source* source, |
||
2306 |
void* user_data) { |
||
2307 |
28108 |
Http2Session* session = static_cast<Http2Session*>(user_data); |
|
2308 |
Debug(session, "reading outbound data for stream %d", id); |
||
2309 |
56216 |
BaseObjectPtr<Http2Stream> stream = session->FindStream(id); |
|
2310 |
✗✓ | 28108 |
if (!stream) return 0; |
2311 |
✓✓ | 28108 |
if (stream->statistics_.first_byte_sent == 0) |
2312 |
12735 |
stream->statistics_.first_byte_sent = uv_hrtime(); |
|
2313 |
✗✓ | 28108 |
CHECK_EQ(id, stream->id()); |
2314 |
|||
2315 |
28108 |
size_t amount = 0; // amount of data being sent in this data frame. |
|
2316 |
|||
2317 |
// Remove all empty chunks from the head of the queue. |
||
2318 |
// This is done here so that .write('', cb) is still a meaningful way to |
||
2319 |
// find out when the HTTP2 stream wants to consume data, and because the |
||
2320 |
// StreamBase API allows empty input chunks. |
||
2321 |
✓✓✓✓ ✓✓ |
28118 |
while (!stream->queue_.empty() && stream->queue_.front().buf.len == 0) { |
2322 |
BaseObjectPtr<AsyncWrap> finished = |
||
2323 |
10 |
std::move(stream->queue_.front().req_wrap); |
|
2324 |
5 |
stream->queue_.pop(); |
|
2325 |
✓✓ | 5 |
if (finished) |
2326 |
3 |
WriteWrap::FromObject(finished)->Done(0); |
|
2327 |
} |
||
2328 |
|||
2329 |
✓✓ | 28108 |
if (!stream->queue_.empty()) { |
2330 |
Debug(session, "stream %d has pending outbound data", id); |
||
2331 |
14044 |
amount = std::min(stream->available_outbound_length_, length); |
|
2332 |
Debug(session, "sending %d bytes for data frame on stream %d", amount, id); |
||
2333 |
✓✗ | 14044 |
if (amount > 0) { |
2334 |
// Just return the length, let Http2Session::OnSendData take care of |
||
2335 |
// actually taking the buffers out of the queue. |
||
2336 |
14044 |
*flags |= NGHTTP2_DATA_FLAG_NO_COPY; |
|
2337 |
14044 |
stream->DecrementAvailableOutboundLength(amount); |
|
2338 |
} |
||
2339 |
} |
||
2340 |
|||
2341 |
✓✓✓✓ ✓✓ |
28108 |
if (amount == 0 && stream->is_writable()) { |
2342 |
✗✓ | 2701 |
CHECK(stream->queue_.empty()); |
2343 |
Debug(session, "deferring stream %d", id); |
||
2344 |
2701 |
stream->EmitWantsWrite(length); |
|
2345 |
✓✗✗✓ ✗✓ |
2701 |
if (stream->available_outbound_length_ > 0 || !stream->is_writable()) { |
2346 |
// EmitWantsWrite() did something interesting synchronously, restart: |
||
2347 |
return OnRead(handle, id, buf, length, flags, source, user_data); |
||
2348 |
} |
||
2349 |
2701 |
return NGHTTP2_ERR_DEFERRED; |
|
2350 |
} |
||
2351 |
|||
2352 |
✓✓✓✓ ✓✓ |
25407 |
if (stream->available_outbound_length_ == 0 && !stream->is_writable()) { |
2353 |
Debug(session, "no more data for stream %d", id); |
||
2354 |
12724 |
*flags |= NGHTTP2_DATA_FLAG_EOF; |
|
2355 |
✓✓ | 12724 |
if (stream->has_trailers()) { |
2356 |
37 |
*flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM; |
|
2357 |
37 |
stream->OnTrailers(); |
|
2358 |
} |
||
2359 |
} |
||
2360 |
|||
2361 |
25407 |
stream->statistics_.sent_bytes += amount; |
|
2362 |
25407 |
return amount; |
|
2363 |
} |
||
2364 |
|||
2365 |
27796 |
void Http2Stream::IncrementAvailableOutboundLength(size_t amount) { |
|
2366 |
27796 |
available_outbound_length_ += amount; |
|
2367 |
27796 |
session_->IncrementCurrentSessionMemory(amount); |
|
2368 |
27796 |
} |
|
2369 |
|||
2370 |
14044 |
void Http2Stream::DecrementAvailableOutboundLength(size_t amount) { |
|
2371 |
14044 |
available_outbound_length_ -= amount; |
|
2372 |
14044 |
session_->DecrementCurrentSessionMemory(amount); |
|
2373 |
14044 |
} |
|
2374 |
|||
2375 |
|||
2376 |
// Implementation of the JavaScript API |
||
2377 |
|||
2378 |
// Fetches the string description of a nghttp2 error code and passes that |
||
2379 |
// back to JS land |
||
2380 |
64 |
void HttpErrorString(const FunctionCallbackInfo<Value>& args) { |
|
2381 |
64 |
Environment* env = Environment::GetCurrent(args); |
|
2382 |
256 |
uint32_t val = args[0]->Uint32Value(env->context()).ToChecked(); |
|
2383 |
192 |
args.GetReturnValue().Set( |
|
2384 |
OneByteString( |
||
2385 |
env->isolate(), |
||
2386 |
64 |
reinterpret_cast<const uint8_t*>(nghttp2_strerror(val)))); |
|
2387 |
64 |
} |
|
2388 |
|||
2389 |
|||
2390 |
// Serializes the settings object into a Buffer instance that |
||
2391 |
// would be suitable, for instance, for creating the Base64 |
||
2392 |
// output for an HTTP2-Settings header field. |
||
2393 |
17 |
void PackSettings(const FunctionCallbackInfo<Value>& args) { |
|
2394 |
17 |
Http2State* state = Environment::GetBindingData<Http2State>(args); |
|
2395 |
51 |
args.GetReturnValue().Set(Http2Settings::Pack(state)); |
|
2396 |
17 |
} |
|
2397 |
|||
2398 |
// A TypedArray instance is shared between C++ and JS land to contain the |
||
2399 |
// default SETTINGS. RefreshDefaultSettings updates that TypedArray with the |
||
2400 |
// default values. |
||
2401 |
6 |
void RefreshDefaultSettings(const FunctionCallbackInfo<Value>& args) { |
|
2402 |
6 |
Http2State* state = Environment::GetBindingData<Http2State>(args); |
|
2403 |
6 |
Http2Settings::RefreshDefaults(state); |
|
2404 |
6 |
} |
|
2405 |
|||
2406 |
// Sets the next stream ID the Http2Session. If successful, returns true. |
||
2407 |
1 |
void Http2Session::SetNextStreamID(const FunctionCallbackInfo<Value>& args) { |
|
2408 |
1 |
Environment* env = Environment::GetCurrent(args); |
|
2409 |
Http2Session* session; |
||
2410 |
✗✓ | 1 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2411 |
4 |
int32_t id = args[0]->Int32Value(env->context()).ToChecked(); |
|
2412 |
✗✓ | 1 |
if (nghttp2_session_set_next_stream_id(session->session(), id) < 0) { |
2413 |
Debug(session, "failed to set next stream id to %d", id); |
||
2414 |
return args.GetReturnValue().Set(false); |
||
2415 |
} |
||
2416 |
2 |
args.GetReturnValue().Set(true); |
|
2417 |
1 |
Debug(session, "set next stream id to %d", id); |
|
2418 |
} |
||
2419 |
|||
2420 |
// Set local window size (local endpoints's window size) to the given |
||
2421 |
// window_size for the stream denoted by 0. |
||
2422 |
// This function returns 0 if it succeeds, or one of a negative codes |
||
2423 |
3 |
void Http2Session::SetLocalWindowSize( |
|
2424 |
const FunctionCallbackInfo<Value>& args) { |
||
2425 |
3 |
Environment* env = Environment::GetCurrent(args); |
|
2426 |
Http2Session* session; |
||
2427 |
✗✓ | 3 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2428 |
|||
2429 |
12 |
int32_t window_size = args[0]->Int32Value(env->context()).ToChecked(); |
|
2430 |
|||
2431 |
3 |
int result = nghttp2_session_set_local_window_size( |
|
2432 |
3 |
session->session(), NGHTTP2_FLAG_NONE, 0, window_size); |
|
2433 |
|||
2434 |
6 |
args.GetReturnValue().Set(result); |
|
2435 |
|||
2436 |
3 |
Debug(session, "set local window size to %d", window_size); |
|
2437 |
} |
||
2438 |
|||
2439 |
// A TypedArray instance is shared between C++ and JS land to contain the |
||
2440 |
// SETTINGS (either remote or local). RefreshSettings updates the current |
||
2441 |
// values established for each of the settings so those can be read in JS land. |
||
2442 |
template <get_setting fn> |
||
2443 |
561 |
void Http2Session::RefreshSettings(const FunctionCallbackInfo<Value>& args) { |
|
2444 |
Http2Session* session; |
||
2445 |
✗✓✗✓ |
561 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2446 |
561 |
Http2Settings::Update(session, fn); |
|
2447 |
561 |
Debug(session, "settings refreshed for session"); |
|
2448 |
} |
||
2449 |
|||
2450 |
// A TypedArray instance is shared between C++ and JS land to contain state |
||
2451 |
// information of the current Http2Session. This updates the values in the |
||
2452 |
// TypedArray so those can be read in JS land. |
||
2453 |
12 |
void Http2Session::RefreshState(const FunctionCallbackInfo<Value>& args) { |
|
2454 |
Http2Session* session; |
||
2455 |
✗✓ | 12 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2456 |
12 |
Debug(session, "refreshing state"); |
|
2457 |
|||
2458 |
12 |
AliasedFloat64Array& buffer = session->http2_state()->session_state_buffer; |
|
2459 |
|||
2460 |
12 |
nghttp2_session* s = session->session(); |
|
2461 |
|||
2462 |
buffer[IDX_SESSION_STATE_EFFECTIVE_LOCAL_WINDOW_SIZE] = |
||
2463 |
12 |
nghttp2_session_get_effective_local_window_size(s); |
|
2464 |
buffer[IDX_SESSION_STATE_EFFECTIVE_RECV_DATA_LENGTH] = |
||
2465 |
12 |
nghttp2_session_get_effective_recv_data_length(s); |
|
2466 |
buffer[IDX_SESSION_STATE_NEXT_STREAM_ID] = |
||
2467 |
12 |
nghttp2_session_get_next_stream_id(s); |
|
2468 |
buffer[IDX_SESSION_STATE_LOCAL_WINDOW_SIZE] = |
||
2469 |
12 |
nghttp2_session_get_local_window_size(s); |
|
2470 |
buffer[IDX_SESSION_STATE_LAST_PROC_STREAM_ID] = |
||
2471 |
12 |
nghttp2_session_get_last_proc_stream_id(s); |
|
2472 |
buffer[IDX_SESSION_STATE_REMOTE_WINDOW_SIZE] = |
||
2473 |
12 |
nghttp2_session_get_remote_window_size(s); |
|
2474 |
buffer[IDX_SESSION_STATE_OUTBOUND_QUEUE_SIZE] = |
||
2475 |
12 |
static_cast<double>(nghttp2_session_get_outbound_queue_size(s)); |
|
2476 |
buffer[IDX_SESSION_STATE_HD_DEFLATE_DYNAMIC_TABLE_SIZE] = |
||
2477 |
12 |
static_cast<double>(nghttp2_session_get_hd_deflate_dynamic_table_size(s)); |
|
2478 |
buffer[IDX_SESSION_STATE_HD_INFLATE_DYNAMIC_TABLE_SIZE] = |
||
2479 |
12 |
static_cast<double>(nghttp2_session_get_hd_inflate_dynamic_table_size(s)); |
|
2480 |
} |
||
2481 |
|||
2482 |
|||
2483 |
// Constructor for new Http2Session instances. |
||
2484 |
660 |
void Http2Session::New(const FunctionCallbackInfo<Value>& args) { |
|
2485 |
660 |
Http2State* state = Environment::GetBindingData<Http2State>(args); |
|
2486 |
660 |
Environment* env = state->env(); |
|
2487 |
✗✓ | 660 |
CHECK(args.IsConstructCall()); |
2488 |
SessionType type = |
||
2489 |
static_cast<SessionType>( |
||
2490 |
2640 |
args[0]->Int32Value(env->context()).ToChecked()); |
|
2491 |
660 |
Http2Session* session = new Http2Session(state, args.This(), type); |
|
2492 |
660 |
session->get_async_id(); // avoid compiler warning |
|
2493 |
Debug(session, "session created"); |
||
2494 |
660 |
} |
|
2495 |
|||
2496 |
|||
2497 |
// Binds the Http2Session with a StreamBase used for i/o |
||
2498 |
660 |
void Http2Session::Consume(const FunctionCallbackInfo<Value>& args) { |
|
2499 |
Http2Session* session; |
||
2500 |
✗✓ | 660 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2501 |
✗✓ | 1320 |
CHECK(args[0]->IsObject()); |
2502 |
1320 |
session->Consume(args[0].As<Object>()); |
|
2503 |
} |
||
2504 |
|||
2505 |
// Destroys the Http2Session instance and renders it unusable |
||
2506 |
641 |
void Http2Session::Destroy(const FunctionCallbackInfo<Value>& args) { |
|
2507 |
Http2Session* session; |
||
2508 |
✗✓ | 641 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2509 |
641 |
Debug(session, "destroying session"); |
|
2510 |
641 |
Environment* env = Environment::GetCurrent(args); |
|
2511 |
641 |
Local<Context> context = env->context(); |
|
2512 |
|||
2513 |
1923 |
uint32_t code = args[0]->Uint32Value(context).ToChecked(); |
|
2514 |
1282 |
session->Close(code, args[1]->IsTrue()); |
|
2515 |
} |
||
2516 |
|||
2517 |
// Submits a new request on the Http2Session and returns either an error code |
||
2518 |
// or the Http2Stream object. |
||
2519 |
11780 |
void Http2Session::Request(const FunctionCallbackInfo<Value>& args) { |
|
2520 |
Http2Session* session; |
||
2521 |
✗✓ | 11781 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2522 |
11780 |
Environment* env = session->env(); |
|
2523 |
|||
2524 |
23560 |
Local<Array> headers = args[0].As<Array>(); |
|
2525 |
47120 |
int32_t options = args[1]->Int32Value(env->context()).ToChecked(); |
|
2526 |
|||
2527 |
11780 |
Debug(session, "request submitted"); |
|
2528 |
|||
2529 |
11780 |
int32_t ret = 0; |
|
2530 |
Http2Stream* stream = |
||
2531 |
11780 |
session->Http2Session::SubmitRequest( |
|
2532 |
23560 |
Http2Priority(env, args[2], args[3], args[4]), |
|
2533 |
23560 |
Http2Headers(env, headers), |
|
2534 |
&ret, |
||
2535 |
11780 |
static_cast<int>(options)); |
|
2536 |
|||
2537 |
✓✓✗✓ |
11780 |
if (ret <= 0 || stream == nullptr) { |
2538 |
2 |
Debug(session, "could not submit request: %s", nghttp2_strerror(ret)); |
|
2539 |
3 |
return args.GetReturnValue().Set(ret); |
|
2540 |
} |
||
2541 |
|||
2542 |
23558 |
Debug(session, "request submitted, new stream id %d", stream->id()); |
|
2543 |
35337 |
args.GetReturnValue().Set(stream->object()); |
|
2544 |
} |
||
2545 |
|||
2546 |
// Submits a GOAWAY frame to signal that the Http2Session is in the process |
||
2547 |
// of shutting down. Note that this function does not actually alter the |
||
2548 |
// state of the Http2Session, it's simply a notification. |
||
2549 |
582 |
void Http2Session::Goaway(uint32_t code, |
|
2550 |
int32_t lastStreamID, |
||
2551 |
const uint8_t* data, |
||
2552 |
size_t len) { |
||
2553 |
✗✓ | 582 |
if (is_destroyed()) |
2554 |
return; |
||
2555 |
|||
2556 |
1164 |
Http2Scope h2scope(this); |
|
2557 |
// the last proc stream id is the most recently created Http2Stream. |
||
2558 |
✓✗ | 582 |
if (lastStreamID <= 0) |
2559 |
582 |
lastStreamID = nghttp2_session_get_last_proc_stream_id(session_.get()); |
|
2560 |
582 |
Debug(this, "submitting goaway"); |
|
2561 |
582 |
nghttp2_submit_goaway(session_.get(), NGHTTP2_FLAG_NONE, |
|
2562 |
582 |
lastStreamID, code, data, len); |
|
2563 |
} |
||
2564 |
|||
2565 |
// Submits a GOAWAY frame to signal that the Http2Session is in the process |
||
2566 |
// of shutting down. The opaque data argument is an optional TypedArray that |
||
2567 |
// can be used to send debugging data to the connected peer. |
||
2568 |
582 |
void Http2Session::Goaway(const FunctionCallbackInfo<Value>& args) { |
|
2569 |
582 |
Environment* env = Environment::GetCurrent(args); |
|
2570 |
582 |
Local<Context> context = env->context(); |
|
2571 |
Http2Session* session; |
||
2572 |
✗✓ | 582 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2573 |
|||
2574 |
1746 |
uint32_t code = args[0]->Uint32Value(context).ToChecked(); |
|
2575 |
1746 |
int32_t lastStreamID = args[1]->Int32Value(context).ToChecked(); |
|
2576 |
582 |
ArrayBufferViewContents<uint8_t> opaque_data; |
|
2577 |
|||
2578 |
✓✓ | 1164 |
if (args[2]->IsArrayBufferView()) { |
2579 |
2 |
opaque_data.Read(args[2].As<ArrayBufferView>()); |
|
2580 |
} |
||
2581 |
|||
2582 |
582 |
session->Goaway(code, lastStreamID, opaque_data.data(), opaque_data.length()); |
|
2583 |
} |
||
2584 |
|||
2585 |
// Update accounting of data chunks. This is used primarily to manage timeout |
||
2586 |
// logic when using the FD Provider. |
||
2587 |
10 |
void Http2Session::UpdateChunksSent(const FunctionCallbackInfo<Value>& args) { |
|
2588 |
10 |
Environment* env = Environment::GetCurrent(args); |
|
2589 |
10 |
Isolate* isolate = env->isolate(); |
|
2590 |
20 |
HandleScope scope(isolate); |
|
2591 |
Http2Session* session; |
||
2592 |
✗✓ | 10 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2593 |
|||
2594 |
10 |
uint32_t length = session->chunks_sent_since_last_write_; |
|
2595 |
|||
2596 |
30 |
session->object()->Set(env->context(), |
|
2597 |
env->chunks_sent_since_last_write_string(), |
||
2598 |
50 |
Integer::NewFromUnsigned(isolate, length)).Check(); |
|
2599 |
|||
2600 |
✓✗ | 20 |
args.GetReturnValue().Set(length); |
2601 |
} |
||
2602 |
|||
2603 |
// Submits an RST_STREAM frame effectively closing the Http2Stream. Note that |
||
2604 |
// this *WILL* alter the state of the stream, causing the OnStreamClose |
||
2605 |
// callback to the triggered. |
||
2606 |
117 |
void Http2Stream::RstStream(const FunctionCallbackInfo<Value>& args) { |
|
2607 |
117 |
Environment* env = Environment::GetCurrent(args); |
|
2608 |
117 |
Local<Context> context = env->context(); |
|
2609 |
Http2Stream* stream; |
||
2610 |
✗✓ | 117 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2611 |
351 |
uint32_t code = args[0]->Uint32Value(context).ToChecked(); |
|
2612 |
117 |
Debug(stream, "sending rst_stream with code %d", code); |
|
2613 |
117 |
stream->SubmitRstStream(code); |
|
2614 |
} |
||
2615 |
|||
2616 |
// Initiates a response on the Http2Stream using the StreamBase API to provide |
||
2617 |
// outbound DATA frames. |
||
2618 |
11705 |
void Http2Stream::Respond(const FunctionCallbackInfo<Value>& args) { |
|
2619 |
11705 |
Environment* env = Environment::GetCurrent(args); |
|
2620 |
Http2Stream* stream; |
||
2621 |
✗✓ | 11705 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2622 |
|||
2623 |
23410 |
Local<Array> headers = args[0].As<Array>(); |
|
2624 |
46820 |
int32_t options = args[1]->Int32Value(env->context()).ToChecked(); |
|
2625 |
|||
2626 |
35115 |
args.GetReturnValue().Set( |
|
2627 |
stream->SubmitResponse( |
||
2628 |
23410 |
Http2Headers(env, headers), |
|
2629 |
static_cast<int>(options))); |
||
2630 |
11705 |
Debug(stream, "response submitted"); |
|
2631 |
} |
||
2632 |
|||
2633 |
|||
2634 |
// Submits informational headers on the Http2Stream |
||
2635 |
5 |
void Http2Stream::Info(const FunctionCallbackInfo<Value>& args) { |
|
2636 |
5 |
Environment* env = Environment::GetCurrent(args); |
|
2637 |
Http2Stream* stream; |
||
2638 |
✗✓ | 5 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2639 |
|||
2640 |
10 |
Local<Array> headers = args[0].As<Array>(); |
|
2641 |
|||
2642 |
15 |
args.GetReturnValue().Set(stream->SubmitInfo(Http2Headers(env, headers))); |
|
2643 |
} |
||
2644 |
|||
2645 |
// Submits trailing headers on the Http2Stream |
||
2646 |
31 |
void Http2Stream::Trailers(const FunctionCallbackInfo<Value>& args) { |
|
2647 |
31 |
Environment* env = Environment::GetCurrent(args); |
|
2648 |
Http2Stream* stream; |
||
2649 |
✗✓ | 31 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2650 |
|||
2651 |
62 |
Local<Array> headers = args[0].As<Array>(); |
|
2652 |
|||
2653 |
93 |
args.GetReturnValue().Set( |
|
2654 |
62 |
stream->SubmitTrailers(Http2Headers(env, headers))); |
|
2655 |
} |
||
2656 |
|||
2657 |
// Grab the numeric id of the Http2Stream |
||
2658 |
11788 |
void Http2Stream::GetID(const FunctionCallbackInfo<Value>& args) { |
|
2659 |
Http2Stream* stream; |
||
2660 |
✗✓ | 11788 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2661 |
35364 |
args.GetReturnValue().Set(stream->id()); |
|
2662 |
} |
||
2663 |
|||
2664 |
// Destroy the Http2Stream, rendering it no longer usable |
||
2665 |
23533 |
void Http2Stream::Destroy(const FunctionCallbackInfo<Value>& args) { |
|
2666 |
Http2Stream* stream; |
||
2667 |
✗✓ | 23533 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2668 |
23533 |
Debug(stream, "destroying stream"); |
|
2669 |
23533 |
stream->Destroy(); |
|
2670 |
} |
||
2671 |
|||
2672 |
// Initiate a Push Promise and create the associated Http2Stream |
||
2673 |
9 |
void Http2Stream::PushPromise(const FunctionCallbackInfo<Value>& args) { |
|
2674 |
9 |
Environment* env = Environment::GetCurrent(args); |
|
2675 |
Http2Stream* parent; |
||
2676 |
✗✓ | 9 |
ASSIGN_OR_RETURN_UNWRAP(&parent, args.Holder()); |
2677 |
|||
2678 |
18 |
Local<Array> headers = args[0].As<Array>(); |
|
2679 |
36 |
int32_t options = args[1]->Int32Value(env->context()).ToChecked(); |
|
2680 |
|||
2681 |
9 |
Debug(parent, "creating push promise"); |
|
2682 |
|||
2683 |
9 |
int32_t ret = 0; |
|
2684 |
Http2Stream* stream = |
||
2685 |
9 |
parent->SubmitPushPromise( |
|
2686 |
18 |
Http2Headers(env, headers), |
|
2687 |
&ret, |
||
2688 |
9 |
static_cast<int>(options)); |
|
2689 |
|||
2690 |
✓✗✗✓ |
9 |
if (ret <= 0 || stream == nullptr) { |
2691 |
Debug(parent, "failed to create push stream: %d", ret); |
||
2692 |
return args.GetReturnValue().Set(ret); |
||
2693 |
} |
||
2694 |
18 |
Debug(parent, "push stream %d created", stream->id()); |
|
2695 |
27 |
args.GetReturnValue().Set(stream->object()); |
|
2696 |
} |
||
2697 |
|||
2698 |
// Send a PRIORITY frame |
||
2699 |
6 |
void Http2Stream::Priority(const FunctionCallbackInfo<Value>& args) { |
|
2700 |
6 |
Environment* env = Environment::GetCurrent(args); |
|
2701 |
Http2Stream* stream; |
||
2702 |
✗✓ | 6 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2703 |
|||
2704 |
✗✓ | 18 |
CHECK_EQ(stream->SubmitPriority( |
2705 |
Http2Priority(env, args[0], args[1], args[2]), |
||
2706 |
args[3]->IsTrue()), 0); |
||
2707 |
6 |
Debug(stream, "priority submitted"); |
|
2708 |
} |
||
2709 |
|||
2710 |
// A TypedArray shared by C++ and JS land is used to communicate state |
||
2711 |
// information about the Http2Stream. This updates the values in that |
||
2712 |
// TypedArray so that the state can be read by JS. |
||
2713 |
11 |
void Http2Stream::RefreshState(const FunctionCallbackInfo<Value>& args) { |
|
2714 |
Http2Stream* stream; |
||
2715 |
✗✓ | 11 |
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder()); |
2716 |
|||
2717 |
11 |
Debug(stream, "refreshing state"); |
|
2718 |
|||
2719 |
✗✓ | 11 |
CHECK_NOT_NULL(stream->session()); |
2720 |
AliasedFloat64Array& buffer = |
||
2721 |
11 |
stream->session()->http2_state()->stream_state_buffer; |
|
2722 |
|||
2723 |
11 |
nghttp2_stream* str = stream->stream(); |
|
2724 |
11 |
nghttp2_session* s = stream->session()->session(); |
|
2725 |
|||
2726 |
✓✓ | 11 |
if (str == nullptr) { |
2727 |
1 |
buffer[IDX_STREAM_STATE] = NGHTTP2_STREAM_STATE_IDLE; |
|
2728 |
buffer[IDX_STREAM_STATE_WEIGHT] = |
||
2729 |
buffer[IDX_STREAM_STATE_SUM_DEPENDENCY_WEIGHT] = |
||
2730 |
buffer[IDX_STREAM_STATE_LOCAL_CLOSE] = |
||
2731 |
buffer[IDX_STREAM_STATE_REMOTE_CLOSE] = |
||
2732 |
1 |
buffer[IDX_STREAM_STATE_LOCAL_WINDOW_SIZE] = 0; |
|
2733 |
} else { |
||
2734 |
buffer[IDX_STREAM_STATE] = |
||
2735 |
10 |
nghttp2_stream_get_state(str); |
|
2736 |
buffer[IDX_STREAM_STATE_WEIGHT] = |
||
2737 |
10 |
nghttp2_stream_get_weight(str); |
|
2738 |
buffer[IDX_STREAM_STATE_SUM_DEPENDENCY_WEIGHT] = |
||
2739 |
10 |
nghttp2_stream_get_sum_dependency_weight(str); |
|
2740 |
buffer[IDX_STREAM_STATE_LOCAL_CLOSE] = |
||
2741 |
10 |
nghttp2_session_get_stream_local_close(s, stream->id()); |
|
2742 |
buffer[IDX_STREAM_STATE_REMOTE_CLOSE] = |
||
2743 |
10 |
nghttp2_session_get_stream_remote_close(s, stream->id()); |
|
2744 |
buffer[IDX_STREAM_STATE_LOCAL_WINDOW_SIZE] = |
||
2745 |
10 |
nghttp2_session_get_stream_local_window_size(s, stream->id()); |
|
2746 |
} |
||
2747 |
} |
||
2748 |
|||
2749 |
5 |
void Http2Session::AltSvc(int32_t id, |
|
2750 |
uint8_t* origin, |
||
2751 |
size_t origin_len, |
||
2752 |
uint8_t* value, |
||
2753 |
size_t value_len) { |
||
2754 |
10 |
Http2Scope h2scope(this); |
|
2755 |
✗✓ | 5 |
CHECK_EQ(nghttp2_submit_altsvc(session_.get(), NGHTTP2_FLAG_NONE, id, |
2756 |
origin, origin_len, value, value_len), 0); |
||
2757 |
5 |
} |
|
2758 |
|||
2759 |
5 |
void Http2Session::Origin(const Origins& origins) { |
|
2760 |
10 |
Http2Scope h2scope(this); |
|
2761 |
✗✓ | 5 |
CHECK_EQ(nghttp2_submit_origin( |
2762 |
session_.get(), |
||
2763 |
NGHTTP2_FLAG_NONE, |
||
2764 |
*origins, |
||
2765 |
origins.length()), 0); |
||
2766 |
5 |
} |
|
2767 |
|||
2768 |
// Submits an AltSvc frame to be sent to the connected peer. |
||
2769 |
5 |
void Http2Session::AltSvc(const FunctionCallbackInfo<Value>& args) { |
|
2770 |
5 |
Environment* env = Environment::GetCurrent(args); |
|
2771 |
Http2Session* session; |
||
2772 |
✗✓ | 5 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2773 |
|||
2774 |
20 |
int32_t id = args[0]->Int32Value(env->context()).ToChecked(); |
|
2775 |
|||
2776 |
// origin and value are both required to be ASCII, handle them as such. |
||
2777 |
20 |
Local<String> origin_str = args[1]->ToString(env->context()).ToLocalChecked(); |
|
2778 |
20 |
Local<String> value_str = args[2]->ToString(env->context()).ToLocalChecked(); |
|
2779 |
|||
2780 |
✓✗✗✓ ✗✓ |
10 |
if (origin_str.IsEmpty() || value_str.IsEmpty()) |
2781 |
return; |
||
2782 |
|||
2783 |
5 |
size_t origin_len = origin_str->Length(); |
|
2784 |
5 |
size_t value_len = value_str->Length(); |
|
2785 |
|||
2786 |
✗✓ | 5 |
CHECK_LE(origin_len + value_len, 16382); // Max permitted for ALTSVC |
2787 |
// Verify that origin len != 0 if stream id == 0, or |
||
2788 |
// that origin len == 0 if stream id != 0 |
||
2789 |
✓✓✗✓ ✓✓✓✗ ✗✓✗✓ |
5 |
CHECK((origin_len != 0 && id == 0) || (origin_len == 0 && id != 0)); |
2790 |
|||
2791 |
10 |
MaybeStackBuffer<uint8_t> origin(origin_len); |
|
2792 |
10 |
MaybeStackBuffer<uint8_t> value(value_len); |
|
2793 |
10 |
origin_str->WriteOneByte(env->isolate(), *origin); |
|
2794 |
10 |
value_str->WriteOneByte(env->isolate(), *value); |
|
2795 |
|||
2796 |
5 |
session->AltSvc(id, *origin, origin_len, *value, value_len); |
|
2797 |
} |
||
2798 |
|||
2799 |
5 |
void Http2Session::Origin(const FunctionCallbackInfo<Value>& args) { |
|
2800 |
5 |
Environment* env = Environment::GetCurrent(args); |
|
2801 |
5 |
Local<Context> context = env->context(); |
|
2802 |
Http2Session* session; |
||
2803 |
✗✓ | 5 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2804 |
|||
2805 |
10 |
Local<String> origin_string = args[0].As<String>(); |
|
2806 |
15 |
size_t count = args[1]->Int32Value(context).ToChecked(); |
|
2807 |
|||
2808 |
5 |
session->Origin(Origins(env, origin_string, count)); |
|
2809 |
} |
||
2810 |
|||
2811 |
// Submits a PING frame to be sent to the connected peer. |
||
2812 |
13 |
void Http2Session::Ping(const FunctionCallbackInfo<Value>& args) { |
|
2813 |
Http2Session* session; |
||
2814 |
✗✓ | 13 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2815 |
|||
2816 |
// A PING frame may have exactly 8 bytes of payload data. If not provided, |
||
2817 |
// then the current hrtime will be used as the payload. |
||
2818 |
13 |
ArrayBufferViewContents<uint8_t, 8> payload; |
|
2819 |
✓✓ | 26 |
if (args[0]->IsArrayBufferView()) { |
2820 |
12 |
payload.Read(args[0].As<ArrayBufferView>()); |
|
2821 |
✗✓ | 6 |
CHECK_EQ(payload.length(), 8); |
2822 |
} |
||
2823 |
|||
2824 |
✗✓ | 26 |
CHECK(args[1]->IsFunction()); |
2825 |
39 |
args.GetReturnValue().Set( |
|
2826 |
39 |
session->AddPing(payload.data(), args[1].As<Function>())); |
|
2827 |
} |
||
2828 |
|||
2829 |
// Submits a SETTINGS frame for the Http2Session |
||
2830 |
670 |
void Http2Session::Settings(const FunctionCallbackInfo<Value>& args) { |
|
2831 |
Http2Session* session; |
||
2832 |
✗✓ | 670 |
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); |
2833 |
✗✓ | 1340 |
CHECK(args[0]->IsFunction()); |
2834 |
2680 |
args.GetReturnValue().Set(session->AddSettings(args[0].As<Function>())); |
|
2835 |
} |
||
2836 |
|||
2837 |
653 |
BaseObjectPtr<Http2Ping> Http2Session::PopPing() { |
|
2838 |
653 |
BaseObjectPtr<Http2Ping> ping; |
|
2839 |
✓✓ | 653 |
if (!outstanding_pings_.empty()) { |
2840 |
11 |
ping = std::move(outstanding_pings_.front()); |
|
2841 |
11 |
outstanding_pings_.pop(); |
|
2842 |
11 |
DecrementCurrentSessionMemory(sizeof(*ping)); |
|
2843 |
} |
||
2844 |
653 |
return ping; |
|
2845 |
} |
||
2846 |
|||
2847 |
13 |
bool Http2Session::AddPing(const uint8_t* payload, Local<Function> callback) { |
|
2848 |
Local<Object> obj; |
||
2849 |
✗✓ | 39 |
if (!env()->http2ping_constructor_template() |
2850 |
39 |
->NewInstance(env()->context()) |
|
2851 |
13 |
.ToLocal(&obj)) { |
|
2852 |
return false; |
||
2853 |
} |
||
2854 |
|||
2855 |
BaseObjectPtr<Http2Ping> ping = |
||
2856 |
26 |
MakeDetachedBaseObject<Http2Ping>(this, obj, callback); |
|
2857 |
✗✓ | 13 |
if (!ping) |
2858 |
return false; |
||
2859 |
|||
2860 |
✓✓ | 13 |
if (outstanding_pings_.size() == max_outstanding_pings_) { |
2861 |
2 |
ping->Done(false); |
|
2862 |
2 |
return false; |
|
2863 |
} |
||
2864 |
|||
2865 |
11 |
IncrementCurrentSessionMemory(sizeof(*ping)); |
|
2866 |
// The Ping itself is an Async resource. When the acknowledgement is received, |
||
2867 |
// the callback will be invoked and a notification sent out to JS land. The |
||
2868 |
// notification will include the duration of the ping, allowing the round |
||
2869 |
// trip to be measured. |
||
2870 |
11 |
ping->Send(payload); |
|
2871 |
|||
2872 |
11 |
outstanding_pings_.emplace(std::move(ping)); |
|
2873 |
11 |
return true; |
|
2874 |
} |
||
2875 |
|||
2876 |
536 |
BaseObjectPtr<Http2Settings> Http2Session::PopSettings() { |
|
2877 |
536 |
BaseObjectPtr<Http2Settings> settings; |
|
2878 |
✓✗ | 536 |
if (!outstanding_settings_.empty()) { |
2879 |
536 |
settings = std::move(outstanding_settings_.front()); |
|
2880 |
536 |
outstanding_settings_.pop(); |
|
2881 |
536 |
DecrementCurrentSessionMemory(sizeof(*settings)); |
|
2882 |
} |
||
2883 |
536 |
return settings; |
|
2884 |
} |
||
2885 |
|||
2886 |
670 |
bool Http2Session::AddSettings(Local<Function> callback) { |
|
2887 |
Local<Object> obj; |
||
2888 |
✗✓ | 2010 |
if (!env()->http2settings_constructor_template() |
2889 |
2010 |
->NewInstance(env()->context()) |
|
2890 |
670 |
.ToLocal(&obj)) { |
|
2891 |
return false; |
||
2892 |
} |
||
2893 |
|||
2894 |
BaseObjectPtr<Http2Settings> settings = |
||
2895 |
1340 |
MakeDetachedBaseObject<Http2Settings>(this, obj, callback, 0); |
|
2896 |
✗✓ | 670 |
if (!settings) |
2897 |
return false; |
||
2898 |
|||
2899 |
✓✓ | 670 |
if (outstanding_settings_.size() == max_outstanding_settings_) { |
2900 |
2 |
settings->Done(false); |
|
2901 |
2 |
return false; |
|
2902 |
} |
||
2903 |
|||
2904 |
668 |
IncrementCurrentSessionMemory(sizeof(*settings)); |
|
2905 |
668 |
settings->Send(); |
|
2906 |
668 |
outstanding_settings_.emplace(std::move(settings)); |
|
2907 |
668 |
return true; |
|
2908 |
} |
||
2909 |
|||
2910 |
13 |
Http2Ping::Http2Ping( |
|
2911 |
Http2Session* session, |
||
2912 |
Local<Object> obj, |
||
2913 |
13 |
Local<Function> callback) |
|
2914 |
: AsyncWrap(session->env(), obj, AsyncWrap::PROVIDER_HTTP2PING), |
||
2915 |
session_(session), |
||
2916 |
26 |
startTime_(uv_hrtime()) { |
|
2917 |
13 |
callback_.Reset(env()->isolate(), callback); |
|
2918 |
13 |
} |
|
2919 |
|||
2920 |
void Http2Ping::MemoryInfo(MemoryTracker* tracker) const { |
||
2921 |
tracker->TrackField("callback", callback_); |
||
2922 |
} |
||
2923 |
|||
2924 |
13 |
Local<Function> Http2Ping::callback() const { |
|
2925 |
26 |
return callback_.Get(env()->isolate()); |
|
2926 |
} |
||
2927 |
|||
2928 |
11 |
void Http2Ping::Send(const uint8_t* payload) { |
|
2929 |
✗✓ | 11 |
CHECK(session_); |
2930 |
uint8_t data[8]; |
||
2931 |
✓✓ | 11 |
if (payload == nullptr) { |
2932 |
5 |
memcpy(&data, &startTime_, arraysize(data)); |
|
2933 |
5 |
payload = data; |
|
2934 |
} |
||
2935 |
22 |
Http2Scope h2scope(session_.get()); |
|
2936 |
✗✓ | 11 |
CHECK_EQ(nghttp2_submit_ping( |
2937 |
session_->session(), |
||
2938 |
NGHTTP2_FLAG_NONE, |
||
2939 |
payload), 0); |
||
2940 |
11 |
} |
|
2941 |
|||
2942 |
13 |
void Http2Ping::Done(bool ack, const uint8_t* payload) { |
|
2943 |
13 |
uint64_t duration_ns = uv_hrtime() - startTime_; |
|
2944 |
13 |
double duration_ms = duration_ns / 1e6; |
|
2945 |
✓✓ | 13 |
if (session_) session_->statistics_.ping_rtt = duration_ns; |
2946 |
|||
2947 |
13 |
Isolate* isolate = env()->isolate(); |
|
2948 |
26 |
HandleScope handle_scope(isolate); |
|
2949 |
13 |
Context::Scope context_scope(env()->context()); |
|
2950 |
|||
2951 |
Local<Value> buf = Undefined(isolate); |
||
2952 |
✓✓ | 13 |
if (payload != nullptr) { |
2953 |
30 |
buf = Buffer::Copy(isolate, |
|
2954 |
reinterpret_cast<const char*>(payload), |
||
2955 |
10 |
8).ToLocalChecked(); |
|
2956 |
} |
||
2957 |
|||
2958 |
Local<Value> argv[] = { |
||
2959 |
ack ? v8::True(isolate) : v8::False(isolate), |
||
2960 |
Number::New(isolate, duration_ms), |
||
2961 |
buf |
||
2962 |
✓✓ | 39 |
}; |
2963 |
13 |
MakeCallback(callback(), arraysize(argv), argv); |
|
2964 |
13 |
} |
|
2965 |
|||
2966 |
1 |
void Http2Ping::DetachFromSession() { |
|
2967 |
1 |
session_.reset(); |
|
2968 |
1 |
} |
|
2969 |
|||
2970 |
void NgHttp2StreamWrite::MemoryInfo(MemoryTracker* tracker) const { |
||
2971 |
if (req_wrap) |
||
2972 |
tracker->TrackField("req_wrap", req_wrap); |
||
2973 |
tracker->TrackField("buf", buf); |
||
2974 |
} |
||
2975 |
|||
2976 |
242 |
void SetCallbackFunctions(const FunctionCallbackInfo<Value>& args) { |
|
2977 |
242 |
Environment* env = Environment::GetCurrent(args); |
|
2978 |
✗✓ | 242 |
CHECK_EQ(args.Length(), 11); |
2979 |
|||
2980 |
#define SET_FUNCTION(arg, name) \ |
||
2981 |
CHECK(args[arg]->IsFunction()); \ |
||
2982 |
env->set_http2session_on_ ## name ## _function(args[arg].As<Function>()); |
||
2983 |
|||
2984 |
✗✓ | 968 |
SET_FUNCTION(0, error) |
2985 |
✗✓ | 968 |
SET_FUNCTION(1, priority) |
2986 |
✗✓ | 968 |
SET_FUNCTION(2, settings) |
2987 |
✗✓ | 968 |
SET_FUNCTION(3, ping) |
2988 |
✗✓ | 968 |
SET_FUNCTION(4, headers) |
2989 |
✗✓ | 968 |
SET_FUNCTION(5, frame_error) |
2990 |
✗✓ | 968 |
SET_FUNCTION(6, goaway_data) |
2991 |
✗✓ | 968 |
SET_FUNCTION(7, altsvc) |
2992 |
✗✓ | 968 |
SET_FUNCTION(8, origin) |
2993 |
✗✓ | 968 |
SET_FUNCTION(9, stream_trailers) |
2994 |
✗✓ | 968 |
SET_FUNCTION(10, stream_close) |
2995 |
|||
2996 |
#undef SET_FUNCTION |
||
2997 |
242 |
} |
|
2998 |
|||
2999 |
2 |
void Http2State::MemoryInfo(MemoryTracker* tracker) const { |
|
3000 |
2 |
tracker->TrackField("root_buffer", root_buffer); |
|
3001 |
2 |
} |
|
3002 |
|||
3003 |
// TODO(addaleax): Remove once we're on C++17. |
||
3004 |
constexpr FastStringKey Http2State::binding_data_name; |
||
3005 |
|||
3006 |
// Set up the process.binding('http2') binding. |
||
3007 |
247 |
void Initialize(Local<Object> target, |
|
3008 |
Local<Value> unused, |
||
3009 |
Local<Context> context, |
||
3010 |
void* priv) { |
||
3011 |
247 |
Environment* env = Environment::GetCurrent(context); |
|
3012 |
247 |
Isolate* isolate = env->isolate(); |
|
3013 |
494 |
HandleScope handle_scope(isolate); |
|
3014 |
|||
3015 |
247 |
Http2State* const state = env->AddBindingData<Http2State>(context, target); |
|
3016 |
✗✓ | 247 |
if (state == nullptr) return; |
3017 |
|||
3018 |
#define SET_STATE_TYPEDARRAY(name, field) \ |
||
3019 |
target->Set(context, \ |
||
3020 |
FIXED_ONE_BYTE_STRING(isolate, (name)), \ |
||
3021 |
(field)).FromJust() |
||
3022 |
|||
3023 |
// Initialize the buffer used to store the session state |
||
3024 |
988 |
SET_STATE_TYPEDARRAY( |
|
3025 |
"sessionState", state->session_state_buffer.GetJSArray()); |
||
3026 |
// Initialize the buffer used to store the stream state |
||
3027 |
988 |
SET_STATE_TYPEDARRAY( |
|
3028 |
"streamState", state->stream_state_buffer.GetJSArray()); |
||
3029 |
988 |
SET_STATE_TYPEDARRAY( |
|
3030 |
"settingsBuffer", state->settings_buffer.GetJSArray()); |
||
3031 |
988 |
SET_STATE_TYPEDARRAY( |
|
3032 |
"optionsBuffer", state->options_buffer.GetJSArray()); |
||
3033 |
988 |
SET_STATE_TYPEDARRAY( |
|
3034 |
"streamStats", state->stream_stats_buffer.GetJSArray()); |
||
3035 |
988 |
SET_STATE_TYPEDARRAY( |
|
3036 |
"sessionStats", state->session_stats_buffer.GetJSArray()); |
||
3037 |
#undef SET_STATE_TYPEDARRAY |
||
3038 |
|||
3039 |
988 |
NODE_DEFINE_CONSTANT(target, kBitfield); |
|
3040 |
247 |
NODE_DEFINE_CONSTANT(target, kSessionPriorityListenerCount); |
|
3041 |
741 |
NODE_DEFINE_CONSTANT(target, kSessionFrameErrorListenerCount); |
|
3042 |
988 |
NODE_DEFINE_CONSTANT(target, kSessionMaxInvalidFrames); |
|
3043 |
1729 |
NODE_DEFINE_CONSTANT(target, kSessionMaxRejectedStreams); |
|
3044 |
1976 |
NODE_DEFINE_CONSTANT(target, kSessionUint8FieldCount); |
|
3045 |
1729 |
||
3046 |
1482 |
NODE_DEFINE_CONSTANT(target, kSessionHasRemoteSettingsListeners); |
|
3047 |
1482 |
NODE_DEFINE_CONSTANT(target, kSessionRemoteSettingsIsUpToDate); |
|
3048 |
1482 |
NODE_DEFINE_CONSTANT(target, kSessionHasPingListeners); |
|
3049 |
1235 |
NODE_DEFINE_CONSTANT(target, kSessionHasAltsvcListeners); |
|
3050 |
1729 |
||
3051 |
1235 |
// Method to fetch the nghttp2 string description of an nghttp2 error code |
|
3052 |
988 |
env->SetMethod(target, "nghttp2ErrorString", HttpErrorString); |
|
3053 |
741 |
env->SetMethod(target, "refreshDefaultSettings", RefreshDefaultSettings); |
|
3054 |
247 |
env->SetMethod(target, "packSettings", PackSettings); |
|
3055 |
247 |
env->SetMethod(target, "setCallbackFunctions", SetCallbackFunctions); |
|
3056 |
|||
3057 |
Local<String> http2SessionClassName = |
||
3058 |
247 |
FIXED_ONE_BYTE_STRING(isolate, "Http2Session"); |
|
3059 |
|||
3060 |
247 |
Local<FunctionTemplate> ping = FunctionTemplate::New(env->isolate()); |
|
3061 |
494 |
ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping")); |
|
3062 |
494 |
ping->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
3063 |
247 |
Local<ObjectTemplate> pingt = ping->InstanceTemplate(); |
|
3064 |
247 |
pingt->SetInternalFieldCount(Http2Ping::kInternalFieldCount); |
|
3065 |
247 |
env->set_http2ping_constructor_template(pingt); |
|
3066 |
|||
3067 |
247 |
Local<FunctionTemplate> setting = FunctionTemplate::New(env->isolate()); |
|
3068 |
494 |
setting->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Setting")); |
|
3069 |
494 |
setting->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
3070 |
247 |
Local<ObjectTemplate> settingt = setting->InstanceTemplate(); |
|
3071 |
247 |
settingt->SetInternalFieldCount(AsyncWrap::kInternalFieldCount); |
|
3072 |
247 |
env->set_http2settings_constructor_template(settingt); |
|
3073 |
|||
3074 |
247 |
Local<FunctionTemplate> stream = FunctionTemplate::New(env->isolate()); |
|
3075 |
494 |
stream->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream")); |
|
3076 |
247 |
env->SetProtoMethod(stream, "id", Http2Stream::GetID); |
|
3077 |
247 |
env->SetProtoMethod(stream, "destroy", Http2Stream::Destroy); |
|
3078 |
247 |
env->SetProtoMethod(stream, "priority", Http2Stream::Priority); |
|
3079 |
247 |
env->SetProtoMethod(stream, "pushPromise", Http2Stream::PushPromise); |
|
3080 |
247 |
env->SetProtoMethod(stream, "info", Http2Stream::Info); |
|
3081 |
247 |
env->SetProtoMethod(stream, "trailers", Http2Stream::Trailers); |
|
3082 |
247 |
env->SetProtoMethod(stream, "respond", Http2Stream::Respond); |
|
3083 |
247 |
env->SetProtoMethod(stream, "rstStream", Http2Stream::RstStream); |
|
3084 |
247 |
env->SetProtoMethod(stream, "refreshState", Http2Stream::RefreshState); |
|
3085 |
494 |
stream->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
3086 |
247 |
StreamBase::AddMethods(env, stream); |
|
3087 |
247 |
Local<ObjectTemplate> streamt = stream->InstanceTemplate(); |
|
3088 |
247 |
streamt->SetInternalFieldCount(StreamBase::kInternalFieldCount); |
|
3089 |
247 |
env->set_http2stream_constructor_template(streamt); |
|
3090 |
494 |
target->Set(context, |
|
3091 |
FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"), |
||
3092 |
1235 |
stream->GetFunction(env->context()).ToLocalChecked()).Check(); |
|
3093 |
|||
3094 |
Local<FunctionTemplate> session = |
||
3095 |
247 |
env->NewFunctionTemplate(Http2Session::New); |
|
3096 |
247 |
session->SetClassName(http2SessionClassName); |
|
3097 |
741 |
session->InstanceTemplate()->SetInternalFieldCount( |
|
3098 |
247 |
Http2Session::kInternalFieldCount); |
|
3099 |
494 |
session->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
3100 |
247 |
env->SetProtoMethod(session, "origin", Http2Session::Origin); |
|
3101 |
247 |
env->SetProtoMethod(session, "altsvc", Http2Session::AltSvc); |
|
3102 |
247 |
env->SetProtoMethod(session, "ping", Http2Session::Ping); |
|
3103 |
247 |
env->SetProtoMethod(session, "consume", Http2Session::Consume); |
|
3104 |
247 |
env->SetProtoMethod(session, "receive", Http2Session::Receive); |
|
3105 |
247 |
env->SetProtoMethod(session, "destroy", Http2Session::Destroy); |
|
3106 |
247 |
env->SetProtoMethod(session, "goaway", Http2Session::Goaway); |
|
3107 |
247 |
env->SetProtoMethod(session, "settings", Http2Session::Settings); |
|
3108 |
247 |
env->SetProtoMethod(session, "request", Http2Session::Request); |
|
3109 |
env->SetProtoMethod(session, "setNextStreamID", |
||
3110 |
247 |
Http2Session::SetNextStreamID); |
|
3111 |
env->SetProtoMethod(session, "setLocalWindowSize", |
||
3112 |
247 |
Http2Session::SetLocalWindowSize); |
|
3113 |
env->SetProtoMethod(session, "updateChunksSent", |
||
3114 |
247 |
Http2Session::UpdateChunksSent); |
|
3115 |
247 |
env->SetProtoMethod(session, "refreshState", Http2Session::RefreshState); |
|
3116 |
env->SetProtoMethod( |
||
3117 |
session, "localSettings", |
||
3118 |
247 |
Http2Session::RefreshSettings<nghttp2_session_get_local_settings>); |
|
3119 |
env->SetProtoMethod( |
||
3120 |
session, "remoteSettings", |
||
3121 |
247 |
Http2Session::RefreshSettings<nghttp2_session_get_remote_settings>); |
|
3122 |
494 |
target->Set(context, |
|
3123 |
http2SessionClassName, |
||
3124 |
988 |
session->GetFunction(env->context()).ToLocalChecked()).Check(); |
|
3125 |
|||
3126 |
247 |
Local<Object> constants = Object::New(isolate); |
|
3127 |
|||
3128 |
// This does alocate one more slot than needed but it's not used. |
||
3129 |
#define V(name) FIXED_ONE_BYTE_STRING(isolate, #name), |
||
3130 |
Local<Value> error_code_names[] = { |
||
3131 |
HTTP2_ERROR_CODES(V) |
||
3132 |
3705 |
}; |
|
3133 |
#undef V |
||
3134 |
|||
3135 |
Local<Array> name_for_error_code = |
||
3136 |
Array::New( |
||
3137 |
isolate, |
||
3138 |
error_code_names, |
||
3139 |
247 |
arraysize(error_code_names)); |
|
3140 |
|||
3141 |
494 |
target->Set(context, |
|
3142 |
FIXED_ONE_BYTE_STRING(isolate, "nameForErrorCode"), |
||
3143 |
741 |
name_for_error_code).Check(); |
|
3144 |
|||
3145 |
#define V(constant) NODE_DEFINE_HIDDEN_CONSTANT(constants, constant); |
||
3146 |
6916 |
HTTP2_HIDDEN_CONSTANTS(V) |
|
3147 |
494 |
#undef V |
|
3148 |
494 |
||
3149 |
247 |
#define V(constant) NODE_DEFINE_CONSTANT(constants, constant); |
|
3150 |
50882 |
HTTP2_CONSTANTS(V) |
|
3151 |
247 |
#undef V |
|
3152 |
494 |
||
3153 |
247 |
// NGHTTP2_DEFAULT_WEIGHT is a macro and not a regular define |
|
3154 |
741 |
// it won't be set properly on the constants object if included |
|
3155 |
741 |
// in the HTTP2_CONSTANTS macro. |
|
3156 |
741 |
NODE_DEFINE_CONSTANT(constants, NGHTTP2_DEFAULT_WEIGHT); |
|
3157 |
741 |
||
3158 |
741 |
#define V(NAME, VALUE) \ |
|
3159 |
1482 |
NODE_DEFINE_STRING_CONSTANT(constants, "HTTP2_HEADER_" # NAME, VALUE); |
|
3160 |
116584 |
HTTP_KNOWN_HEADERS(V) |
|
3161 |
1235 |
#undef V |
|
3162 |
741 |
||
3163 |
988 |
#define V(NAME, VALUE) \ |
|
3164 |
1235 |
NODE_DEFINE_STRING_CONSTANT(constants, "HTTP2_METHOD_" # NAME, VALUE); |
|
3165 |
48659 |
HTTP_KNOWN_METHODS(V) |
|
3166 |
1729 |
#undef V |
|
3167 |
1482 |
||
3168 |
1482 |
#define V(name, _) NODE_DEFINE_CONSTANT(constants, HTTP_STATUS_##name); |
|
3169 |
58045 |
HTTP_STATUS_CODES(V) |
|
3170 |
1729 |
#undef V |
|
3171 |
2223 |
||
3172 |
✗✓ | 2717 |
target->Set(context, env->constants_string(), constants).Check(); |
3173 |
1729 |
} |
|
3174 |
1729 |
} // namespace http2 |
|
3175 |
1976 |
} // namespace node |
|
3176 |
1976 |
||
3177 |
✓✗✓✗ |
20732 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(http2, node::http2::Initialize) |
Generated by: GCOVR (Version 3.4) |