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