| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -180,6 +180,18 @@ inline void Http2Settings::RefreshDefaults(Environment* env) { | |||
| 180 | 180 | (1 << IDX_SETTINGS_MAX_FRAME_SIZE); | |
| 181 | 181 | } | |
| 182 | 182 | ||
| 183 | + Http2Priority::Http2Priority(Environment* env, | ||
| 184 | + Local<Value> parent, | ||
| 185 | + Local<Value> weight, | ||
| 186 | + Local<Value> exclusive) { | ||
| 187 | + Local<Context> context = env->context(); | ||
| 188 | + int32_t parent_ = parent->Int32Value(context).ToChecked(); | ||
| 189 | + int32_t weight_ = weight->Int32Value(context).ToChecked(); | ||
| 190 | + bool exclusive_ = exclusive->BooleanValue(context).ToChecked(); | ||
| 191 | + DEBUG_HTTP2("Http2Priority: parent: %d, weight: %d, exclusive: %d\n", | ||
| 192 | + parent_, weight_, exclusive_); | ||
| 193 | + nghttp2_priority_spec_init(&spec, parent_, weight_, exclusive_ ? 1 : 0); | ||
| 194 | + } | ||
| 183 | 195 | ||
| 184 | 196 | Http2Session::Http2Session(Environment* env, | |
| 185 | 197 | Local<Object> wrap, | |
@@ -258,12 +270,8 @@ ssize_t Http2Session::OnCallbackPadding(size_t frameLen, | |||
| 258 | 270 | buffer[PADDING_BUF_RETURN_VALUE] = frameLen; | |
| 259 | 271 | MakeCallback(env()->ongetpadding_string(), 0, nullptr); | |
| 260 | 272 | uint32_t retval = buffer[PADDING_BUF_RETURN_VALUE]; | |
| 261 | - retval = retval <= maxPayloadLen ? retval : maxPayloadLen; | ||
| 262 | - retval = retval >= frameLen ? retval : frameLen; | ||
| 263 | - #if defined(DEBUG) && DEBUG | ||
| 264 | - CHECK_GE(retval, frameLen); | ||
| 265 | - CHECK_LE(retval, maxPayloadLen); | ||
| 266 | - #endif | ||
| 273 | + retval = std::min(retval, static_cast<uint32_t>(maxPayloadLen)); | ||
| 274 | + retval = std::max(retval, static_cast<uint32_t>(frameLen)); | ||
| 267 | 275 | return retval; | |
| 268 | 276 | } | |
| 269 | 277 | ||
@@ -445,30 +453,18 @@ void Http2Session::SubmitPriority(const FunctionCallbackInfo<Value>& args) { | |||
| 445 | 453 | ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); | |
| 446 | 454 | Local<Context> context = env->context(); | |
| 447 | 455 | ||
| 448 | - nghttp2_priority_spec spec; | ||
| 449 | 456 | int32_t id = args[0]->Int32Value(context).ToChecked(); | |
| 450 | - int32_t parent = args[1]->Int32Value(context).ToChecked(); | ||
| 451 | - int32_t weight = args[2]->Int32Value(context).ToChecked(); | ||
| 452 | - bool exclusive = args[3]->BooleanValue(context).ToChecked(); | ||
| 457 | + Http2Priority priority(env, args[1], args[2], args[3]); | ||
| 453 | 458 | bool silent = args[4]->BooleanValue(context).ToChecked(); | |
| 454 | - DEBUG_HTTP2("Http2Session: submitting priority for stream %d: " | ||
| 455 | - "parent: %d, weight: %d, exclusive: %d, silent: %d\n", | ||
| 456 | - id, parent, weight, exclusive, silent); | ||
| 457 | - | ||
| 458 | - #if defined(DEBUG) && DEBUG | ||
| 459 | - CHECK_GT(id, 0); | ||
| 460 | - CHECK_GE(parent, 0); | ||
| 461 | - CHECK_GE(weight, 0); | ||
| 462 | - #endif | ||
| 459 | + DEBUG_HTTP2("Http2Session: submitting priority for stream %d", id); | ||
| 463 | 460 | ||
| 464 | 461 | Nghttp2Stream* stream; | |
| 465 | 462 | if (!(stream = session->FindStream(id))) { | |
| 466 | 463 | // invalid stream | |
| 467 | 464 | return args.GetReturnValue().Set(NGHTTP2_ERR_INVALID_STREAM_ID); | |
| 468 | 465 | } | |
| 469 | - nghttp2_priority_spec_init(&spec, parent, weight, exclusive ? 1 : 0); | ||
| 470 | 466 | ||
| 471 | - args.GetReturnValue().Set(stream->SubmitPriority(&spec, silent)); | ||
| 467 | + args.GetReturnValue().Set(stream->SubmitPriority(*priority, silent)); | ||
| 472 | 468 | } | |
| 473 | 469 | ||
| 474 | 470 | void Http2Session::SubmitSettings(const FunctionCallbackInfo<Value>& args) { | |
@@ -524,20 +520,14 @@ void Http2Session::SubmitRequest(const FunctionCallbackInfo<Value>& args) { | |||
| 524 | 520 | ||
| 525 | 521 | Local<Array> headers = args[0].As<Array>(); | |
| 526 | 522 | int options = args[1]->IntegerValue(context).ToChecked(); | |
| 527 | - int32_t parent = args[2]->Int32Value(context).ToChecked(); | ||
| 528 | - int32_t weight = args[3]->Int32Value(context).ToChecked(); | ||
| 529 | - bool exclusive = args[4]->BooleanValue(context).ToChecked(); | ||
| 530 | - | ||
| 531 | - DEBUG_HTTP2("Http2Session: submitting request: headers: %d, options: %d, " | ||
| 532 | - "parent: %d, weight: %d, exclusive: %d\n", headers->Length(), | ||
| 533 | - options, parent, weight, exclusive); | ||
| 523 | + Http2Priority priority(env, args[2], args[3], args[4]); | ||
| 534 | 524 | ||
| 535 | - nghttp2_priority_spec prispec; | ||
| 536 | - nghttp2_priority_spec_init(&prispec, parent, weight, exclusive ? 1 : 0); | ||
| 525 | + DEBUG_HTTP2("Http2Session: submitting request: headers: %d, options: %d\n", | ||
| 526 | + headers->Length(), options); | ||
| 537 | 527 | ||
| 538 | 528 | Headers list(isolate, context, headers); | |
| 539 | 529 | ||
| 540 | - int32_t ret = session->Nghttp2Session::SubmitRequest(&prispec, | ||
| 530 | + int32_t ret = session->Nghttp2Session::SubmitRequest(*priority, | ||
| 541 | 531 | *list, list.length(), | |
| 542 | 532 | nullptr, options); | |
| 543 | 533 | DEBUG_HTTP2("Http2Session: request submitted, response: %d\n", ret); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -369,6 +369,20 @@ class Http2Settings { | |||
| 369 | 369 | MaybeStackBuffer<nghttp2_settings_entry, IDX_SETTINGS_COUNT> entries_; | |
| 370 | 370 | }; | |
| 371 | 371 | ||
| 372 | + class Http2Priority { | ||
| 373 | + public: | ||
| 374 | + Http2Priority(Environment* env, | ||
| 375 | + Local<Value> parent, | ||
| 376 | + Local<Value> weight, | ||
| 377 | + Local<Value> exclusive); | ||
| 378 | + | ||
| 379 | + nghttp2_priority_spec* operator*() { | ||
| 380 | + return &spec; | ||
| 381 | + } | ||
| 382 | + private: | ||
| 383 | + nghttp2_priority_spec spec; | ||
| 384 | + }; | ||
| 385 | + | ||
| 372 | 386 | class Http2Session : public AsyncWrap, | |
| 373 | 387 | public StreamBase, | |
| 374 | 388 | public Nghttp2Session { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,18 +28,21 @@ inline int Nghttp2Session::OnNghttpError(nghttp2_session* session, | |||
| 28 | 28 | } | |
| 29 | 29 | #endif | |
| 30 | 30 | ||
| 31 | + inline int32_t GetFrameID(const nghttp2_frame* frame) { | ||
| 32 | + // If this is a push promise, we want to grab the id of the promised stream | ||
| 33 | + return (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 34 | + frame->push_promise.promised_stream_id : | ||
| 35 | + frame->hd.stream_id; | ||
| 36 | + } | ||
| 37 | + | ||
| 31 | 38 | // nghttp2 calls this at the beginning a new HEADERS or PUSH_PROMISE frame. | |
| 32 | 39 | // We use it to ensure that an Nghttp2Stream instance is allocated to store | |
| 33 | 40 | // the state. | |
| 34 | 41 | inline int Nghttp2Session::OnBeginHeadersCallback(nghttp2_session* session, | |
| 35 | 42 | const nghttp2_frame* frame, | |
| 36 | 43 | void* user_data) { | |
| 37 | 44 | Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data); | |
| 38 | - // If this is a push promise frame, we want to grab the handle of | ||
| 39 | - // the promised stream. | ||
| 40 | - int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 41 | - frame->push_promise.promised_stream_id : | ||
| 42 | - frame->hd.stream_id; | ||
| 45 | + int32_t id = GetFrameID(frame); | ||
| 43 | 46 | DEBUG_HTTP2("Nghttp2Session %s: beginning headers for stream %d\n", | |
| 44 | 47 | handle->TypeName(), id); | |
| 45 | 48 | ||
@@ -62,11 +65,7 @@ inline int Nghttp2Session::OnHeaderCallback(nghttp2_session* session, | |||
| 62 | 65 | uint8_t flags, | |
| 63 | 66 | void* user_data) { | |
| 64 | 67 | Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data); | |
| 65 | - // If this is a push promise frame, we want to grab the handle of | ||
| 66 | - // the promised stream. | ||
| 67 | - int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 68 | - frame->push_promise.promised_stream_id : | ||
| 69 | - frame->hd.stream_id; | ||
| 68 | + int32_t id = GetFrameID(frame); | ||
| 70 | 69 | Nghttp2Stream* stream = handle->FindStream(id); | |
| 71 | 70 | // The header name and value are stored in a reference counted buffer | |
| 72 | 71 | // provided to us by nghttp2. We need to increment the reference counter | |
@@ -418,7 +417,7 @@ inline void Nghttp2Stream::FlushDataChunks() { | |||
| 418 | 417 | // see if the END_STREAM flag is set, and will flush the queued data chunks | |
| 419 | 418 | // to JS if the stream is flowing | |
| 420 | 419 | inline void Nghttp2Session::HandleDataFrame(const nghttp2_frame* frame) { | |
| 421 | - int32_t id = frame->hd.stream_id; | ||
| 420 | + int32_t id = GetFrameID(frame); | ||
| 422 | 421 | DEBUG_HTTP2("Nghttp2Session %s: handling data frame for stream %d\n", | |
| 423 | 422 | TypeName(), id); | |
| 424 | 423 | Nghttp2Stream* stream = this->FindStream(id); | |
@@ -436,8 +435,7 @@ inline void Nghttp2Session::HandleDataFrame(const nghttp2_frame* frame) { | |||
| 436 | 435 | // The headers are collected as the frame is being processed and sent out | |
| 437 | 436 | // to the JS side only when the frame is fully processed. | |
| 438 | 437 | inline void Nghttp2Session::HandleHeadersFrame(const nghttp2_frame* frame) { | |
| 439 | - int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 440 | - frame->push_promise.promised_stream_id : frame->hd.stream_id; | ||
| 438 | + int32_t id = GetFrameID(frame); | ||
| 441 | 439 | DEBUG_HTTP2("Nghttp2Session %s: handling headers frame for stream %d\n", | |
| 442 | 440 | TypeName(), id); | |
| 443 | 441 | Nghttp2Stream* stream = FindStream(id); | |
@@ -454,7 +452,7 @@ inline void Nghttp2Session::HandleHeadersFrame(const nghttp2_frame* frame) { | |||
| 454 | 452 | // Notifies the JS layer that a PRIORITY frame has been received | |
| 455 | 453 | inline void Nghttp2Session::HandlePriorityFrame(const nghttp2_frame* frame) { | |
| 456 | 454 | nghttp2_priority priority_frame = frame->priority; | |
| 457 | - int32_t id = frame->hd.stream_id; | ||
| 455 | + int32_t id = GetFrameID(frame); | ||
| 458 | 456 | DEBUG_HTTP2("Nghttp2Session %s: handling priority frame for stream %d\n", | |
| 459 | 457 | TypeName(), id); | |
| 460 | 458 | ||
@@ -548,39 +546,22 @@ inline int Nghttp2Session::Init(const nghttp2_session_type type, | |||
| 548 | 546 | session_type_ = type; | |
| 549 | 547 | DEBUG_HTTP2("Nghttp2Session %s: initializing session\n", TypeName()); | |
| 550 | 548 | destroying_ = false; | |
| 551 | - int ret = 0; | ||
| 552 | 549 | ||
| 553 | 550 | nghttp2_session_callbacks* callbacks | |
| 554 | 551 | = callback_struct_saved[HasGetPaddingCallback() ? 1 : 0].callbacks; | |
| 555 | 552 | ||
| 556 | - nghttp2_option* opts; | ||
| 557 | - if (options != nullptr) { | ||
| 558 | - opts = options; | ||
| 559 | - } else { | ||
| 560 | - nghttp2_option_new(&opts); | ||
| 561 | - } | ||
| 553 | + CHECK_NE(options, nullptr); | ||
| 562 | 554 | ||
| 563 | - switch (type) { | ||
| 564 | - case NGHTTP2_SESSION_SERVER: | ||
| 565 | - ret = nghttp2_session_server_new3(&session_, | ||
| 566 | - callbacks, | ||
| 567 | - this, | ||
| 568 | - opts, | ||
| 569 | - mem); | ||
| 570 | - break; | ||
| 571 | - case NGHTTP2_SESSION_CLIENT: | ||
| 572 | - ret = nghttp2_session_client_new3(&session_, | ||
| 573 | - callbacks, | ||
| 574 | - this, | ||
| 575 | - opts, | ||
| 576 | - mem); | ||
| 577 | - break; | ||
| 578 | - } | ||
| 579 | - if (opts != options) { | ||
| 580 | - nghttp2_option_del(opts); | ||
| 581 | - } | ||
| 555 | + typedef int (*init_fn)(nghttp2_session** session, | ||
| 556 | + const nghttp2_session_callbacks* callbacks, | ||
| 557 | + void* user_data, | ||
| 558 | + const nghttp2_option* options, | ||
| 559 | + nghttp2_mem* mem); | ||
| 560 | + init_fn fn = type == NGHTTP2_SESSION_SERVER ? | ||
| 561 | + nghttp2_session_server_new3 : | ||
| 562 | + nghttp2_session_client_new3; | ||
| 582 | 563 | ||
| 583 | - return ret; | ||
| 564 | + return fn(&session_, callbacks, this, options, mem); | ||
| 584 | 565 | } | |
| 585 | 566 | ||
| 586 | 567 | inline void Nghttp2Session::MarkDestroying() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments