| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -186,6 +186,18 @@ inline void Http2Settings::RefreshDefaults(Environment* env) { | |||
| 186 | 186 | (1 << IDX_SETTINGS_MAX_HEADER_LIST_SIZE); | |
| 187 | 187 | } | |
| 188 | 188 | ||
| 189 | + Http2Priority::Http2Priority(Environment* env, | ||
| 190 | + Local<Value> parent, | ||
| 191 | + Local<Value> weight, | ||
| 192 | + Local<Value> exclusive) { | ||
| 193 | + Local<Context> context = env->context(); | ||
| 194 | + int32_t parent_ = parent->Int32Value(context).ToChecked(); | ||
| 195 | + int32_t weight_ = weight->Int32Value(context).ToChecked(); | ||
| 196 | + bool exclusive_ = exclusive->BooleanValue(context).ToChecked(); | ||
| 197 | + DEBUG_HTTP2("Http2Priority: parent: %d, weight: %d, exclusive: %d\n", | ||
| 198 | + parent_, weight_, exclusive_); | ||
| 199 | + nghttp2_priority_spec_init(&spec, parent_, weight_, exclusive_ ? 1 : 0); | ||
| 200 | + } | ||
| 189 | 201 | ||
| 190 | 202 | Http2Session::Http2Session(Environment* env, | |
| 191 | 203 | Local<Object> wrap, | |
@@ -267,12 +279,8 @@ ssize_t Http2Session::OnCallbackPadding(size_t frameLen, | |||
| 267 | 279 | buffer[PADDING_BUF_RETURN_VALUE] = frameLen; | |
| 268 | 280 | MakeCallback(env()->ongetpadding_string(), 0, nullptr); | |
| 269 | 281 | uint32_t retval = buffer[PADDING_BUF_RETURN_VALUE]; | |
| 270 | - retval = retval <= maxPayloadLen ? retval : maxPayloadLen; | ||
| 271 | - retval = retval >= frameLen ? retval : frameLen; | ||
| 272 | - #if defined(DEBUG) && DEBUG | ||
| 273 | - CHECK_GE(retval, frameLen); | ||
| 274 | - CHECK_LE(retval, maxPayloadLen); | ||
| 275 | - #endif | ||
| 282 | + retval = std::min(retval, static_cast<uint32_t>(maxPayloadLen)); | ||
| 283 | + retval = std::max(retval, static_cast<uint32_t>(frameLen)); | ||
| 276 | 284 | return retval; | |
| 277 | 285 | } | |
| 278 | 286 | ||
@@ -454,30 +462,18 @@ void Http2Session::SubmitPriority(const FunctionCallbackInfo<Value>& args) { | |||
| 454 | 462 | ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); | |
| 455 | 463 | Local<Context> context = env->context(); | |
| 456 | 464 | ||
| 457 | - nghttp2_priority_spec spec; | ||
| 458 | 465 | int32_t id = args[0]->Int32Value(context).ToChecked(); | |
| 459 | - int32_t parent = args[1]->Int32Value(context).ToChecked(); | ||
| 460 | - int32_t weight = args[2]->Int32Value(context).ToChecked(); | ||
| 461 | - bool exclusive = args[3]->BooleanValue(context).ToChecked(); | ||
| 466 | + Http2Priority priority(env, args[1], args[2], args[3]); | ||
| 462 | 467 | bool silent = args[4]->BooleanValue(context).ToChecked(); | |
| 463 | - DEBUG_HTTP2("Http2Session: submitting priority for stream %d: " | ||
| 464 | - "parent: %d, weight: %d, exclusive: %d, silent: %d\n", | ||
| 465 | - id, parent, weight, exclusive, silent); | ||
| 466 | - | ||
| 467 | - #if defined(DEBUG) && DEBUG | ||
| 468 | - CHECK_GT(id, 0); | ||
| 469 | - CHECK_GE(parent, 0); | ||
| 470 | - CHECK_GE(weight, 0); | ||
| 471 | - #endif | ||
| 468 | + DEBUG_HTTP2("Http2Session: submitting priority for stream %d", id); | ||
| 472 | 469 | ||
| 473 | 470 | Nghttp2Stream* stream; | |
| 474 | 471 | if (!(stream = session->FindStream(id))) { | |
| 475 | 472 | // invalid stream | |
| 476 | 473 | return args.GetReturnValue().Set(NGHTTP2_ERR_INVALID_STREAM_ID); | |
| 477 | 474 | } | |
| 478 | - nghttp2_priority_spec_init(&spec, parent, weight, exclusive ? 1 : 0); | ||
| 479 | 475 | ||
| 480 | - args.GetReturnValue().Set(stream->SubmitPriority(&spec, silent)); | ||
| 476 | + args.GetReturnValue().Set(stream->SubmitPriority(*priority, silent)); | ||
| 481 | 477 | } | |
| 482 | 478 | ||
| 483 | 479 | void Http2Session::SubmitSettings(const FunctionCallbackInfo<Value>& args) { | |
@@ -533,20 +529,14 @@ void Http2Session::SubmitRequest(const FunctionCallbackInfo<Value>& args) { | |||
| 533 | 529 | ||
| 534 | 530 | Local<Array> headers = args[0].As<Array>(); | |
| 535 | 531 | int options = args[1]->IntegerValue(context).ToChecked(); | |
| 536 | - int32_t parent = args[2]->Int32Value(context).ToChecked(); | ||
| 537 | - int32_t weight = args[3]->Int32Value(context).ToChecked(); | ||
| 538 | - bool exclusive = args[4]->BooleanValue(context).ToChecked(); | ||
| 539 | - | ||
| 540 | - DEBUG_HTTP2("Http2Session: submitting request: headers: %d, options: %d, " | ||
| 541 | - "parent: %d, weight: %d, exclusive: %d\n", headers->Length(), | ||
| 542 | - options, parent, weight, exclusive); | ||
| 532 | + Http2Priority priority(env, args[2], args[3], args[4]); | ||
| 543 | 533 | ||
| 544 | - nghttp2_priority_spec prispec; | ||
| 545 | - nghttp2_priority_spec_init(&prispec, parent, weight, exclusive ? 1 : 0); | ||
| 534 | + DEBUG_HTTP2("Http2Session: submitting request: headers: %d, options: %d\n", | ||
| 535 | + headers->Length(), options); | ||
| 546 | 536 | ||
| 547 | 537 | Headers list(isolate, context, headers); | |
| 548 | 538 | ||
| 549 | - int32_t ret = session->Nghttp2Session::SubmitRequest(&prispec, | ||
| 539 | + int32_t ret = session->Nghttp2Session::SubmitRequest(*priority, | ||
| 550 | 540 | *list, list.length(), | |
| 551 | 541 | nullptr, options); | |
| 552 | 542 | DEBUG_HTTP2("Http2Session: request submitted, response: %d\n", ret); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -368,6 +368,20 @@ class Http2Settings { | |||
| 368 | 368 | MaybeStackBuffer<nghttp2_settings_entry, IDX_SETTINGS_COUNT> entries_; | |
| 369 | 369 | }; | |
| 370 | 370 | ||
| 371 | + class Http2Priority { | ||
| 372 | + public: | ||
| 373 | + Http2Priority(Environment* env, | ||
| 374 | + Local<Value> parent, | ||
| 375 | + Local<Value> weight, | ||
| 376 | + Local<Value> exclusive); | ||
| 377 | + | ||
| 378 | + nghttp2_priority_spec* operator*() { | ||
| 379 | + return &spec; | ||
| 380 | + } | ||
| 381 | + private: | ||
| 382 | + nghttp2_priority_spec spec; | ||
| 383 | + }; | ||
| 384 | + | ||
| 371 | 385 | class Http2Session : public AsyncWrap, | |
| 372 | 386 | public StreamBase, | |
| 373 | 387 | public Nghttp2Session { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,18 +22,21 @@ inline int Nghttp2Session::OnNghttpError(nghttp2_session* session, | |||
| 22 | 22 | } | |
| 23 | 23 | #endif | |
| 24 | 24 | ||
| 25 | + inline int32_t GetFrameID(const nghttp2_frame* frame) { | ||
| 26 | + // If this is a push promise, we want to grab the id of the promised stream | ||
| 27 | + return (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 28 | + frame->push_promise.promised_stream_id : | ||
| 29 | + frame->hd.stream_id; | ||
| 30 | + } | ||
| 31 | + | ||
| 25 | 32 | // nghttp2 calls this at the beginning a new HEADERS or PUSH_PROMISE frame. | |
| 26 | 33 | // We use it to ensure that an Nghttp2Stream instance is allocated to store | |
| 27 | 34 | // the state. | |
| 28 | 35 | inline int Nghttp2Session::OnBeginHeadersCallback(nghttp2_session* session, | |
| 29 | 36 | const nghttp2_frame* frame, | |
| 30 | 37 | void* user_data) { | |
| 31 | 38 | Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data); | |
| 32 | - // If this is a push promise frame, we want to grab the handle of | ||
| 33 | - // the promised stream. | ||
| 34 | - int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 35 | - frame->push_promise.promised_stream_id : | ||
| 36 | - frame->hd.stream_id; | ||
| 39 | + int32_t id = GetFrameID(frame); | ||
| 37 | 40 | DEBUG_HTTP2("Nghttp2Session %s: beginning headers for stream %d\n", | |
| 38 | 41 | handle->TypeName(), id); | |
| 39 | 42 | ||
@@ -79,11 +82,7 @@ inline int Nghttp2Session::OnHeaderCallback(nghttp2_session* session, | |||
| 79 | 82 | uint8_t flags, | |
| 80 | 83 | void* user_data) { | |
| 81 | 84 | Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data); | |
| 82 | - // If this is a push promise frame, we want to grab the handle of | ||
| 83 | - // the promised stream. | ||
| 84 | - int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 85 | - frame->push_promise.promised_stream_id : | ||
| 86 | - frame->hd.stream_id; | ||
| 85 | + int32_t id = GetFrameID(frame); | ||
| 87 | 86 | Nghttp2Stream* stream = handle->FindStream(id); | |
| 88 | 87 | if (!stream->AddHeader(name, value, flags)) { | |
| 89 | 88 | // This will only happen if the connected peer sends us more | |
@@ -432,7 +431,7 @@ inline void Nghttp2Stream::FlushDataChunks() { | |||
| 432 | 431 | // see if the END_STREAM flag is set, and will flush the queued data chunks | |
| 433 | 432 | // to JS if the stream is flowing | |
| 434 | 433 | inline void Nghttp2Session::HandleDataFrame(const nghttp2_frame* frame) { | |
| 435 | - int32_t id = frame->hd.stream_id; | ||
| 434 | + int32_t id = GetFrameID(frame); | ||
| 436 | 435 | DEBUG_HTTP2("Nghttp2Session %s: handling data frame for stream %d\n", | |
| 437 | 436 | TypeName(), id); | |
| 438 | 437 | Nghttp2Stream* stream = this->FindStream(id); | |
@@ -450,8 +449,7 @@ inline void Nghttp2Session::HandleDataFrame(const nghttp2_frame* frame) { | |||
| 450 | 449 | // The headers are collected as the frame is being processed and sent out | |
| 451 | 450 | // to the JS side only when the frame is fully processed. | |
| 452 | 451 | inline void Nghttp2Session::HandleHeadersFrame(const nghttp2_frame* frame) { | |
| 453 | - int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | ||
| 454 | - frame->push_promise.promised_stream_id : frame->hd.stream_id; | ||
| 452 | + int32_t id = GetFrameID(frame); | ||
| 455 | 453 | DEBUG_HTTP2("Nghttp2Session %s: handling headers frame for stream %d\n", | |
| 456 | 454 | TypeName(), id); | |
| 457 | 455 | Nghttp2Stream* stream = FindStream(id); | |
@@ -469,7 +467,7 @@ inline void Nghttp2Session::HandleHeadersFrame(const nghttp2_frame* frame) { | |||
| 469 | 467 | // Notifies the JS layer that a PRIORITY frame has been received | |
| 470 | 468 | inline void Nghttp2Session::HandlePriorityFrame(const nghttp2_frame* frame) { | |
| 471 | 469 | nghttp2_priority priority_frame = frame->priority; | |
| 472 | - int32_t id = frame->hd.stream_id; | ||
| 470 | + int32_t id = GetFrameID(frame); | ||
| 473 | 471 | DEBUG_HTTP2("Nghttp2Session %s: handling priority frame for stream %d\n", | |
| 474 | 472 | TypeName(), id); | |
| 475 | 473 | ||
@@ -571,41 +569,24 @@ inline int Nghttp2Session::Init(const nghttp2_session_type type, | |||
| 571 | 569 | session_type_ = type; | |
| 572 | 570 | DEBUG_HTTP2("Nghttp2Session %s: initializing session\n", TypeName()); | |
| 573 | 571 | destroying_ = false; | |
| 574 | - int ret = 0; | ||
| 575 | 572 | ||
| 576 | 573 | max_header_pairs_ = maxHeaderPairs; | |
| 577 | 574 | ||
| 578 | 575 | nghttp2_session_callbacks* callbacks | |
| 579 | 576 | = callback_struct_saved[HasGetPaddingCallback() ? 1 : 0].callbacks; | |
| 580 | 577 | ||
| 581 | - nghttp2_option* opts; | ||
| 582 | - if (options != nullptr) { | ||
| 583 | - opts = options; | ||
| 584 | - } else { | ||
| 585 | - nghttp2_option_new(&opts); | ||
| 586 | - } | ||
| 578 | + CHECK_NE(options, nullptr); | ||
| 587 | 579 | ||
| 588 | - switch (type) { | ||
| 589 | - case NGHTTP2_SESSION_SERVER: | ||
| 590 | - ret = nghttp2_session_server_new3(&session_, | ||
| 591 | - callbacks, | ||
| 592 | - this, | ||
| 593 | - opts, | ||
| 594 | - mem); | ||
| 595 | - break; | ||
| 596 | - case NGHTTP2_SESSION_CLIENT: | ||
| 597 | - ret = nghttp2_session_client_new3(&session_, | ||
| 598 | - callbacks, | ||
| 599 | - this, | ||
| 600 | - opts, | ||
| 601 | - mem); | ||
| 602 | - break; | ||
| 603 | - } | ||
| 604 | - if (opts != options) { | ||
| 605 | - nghttp2_option_del(opts); | ||
| 606 | - } | ||
| 580 | + typedef int (*init_fn)(nghttp2_session** session, | ||
| 581 | + const nghttp2_session_callbacks* callbacks, | ||
| 582 | + void* user_data, | ||
| 583 | + const nghttp2_option* options, | ||
| 584 | + nghttp2_mem* mem); | ||
| 585 | + init_fn fn = type == NGHTTP2_SESSION_SERVER ? | ||
| 586 | + nghttp2_session_server_new3 : | ||
| 587 | + nghttp2_session_client_new3; | ||
| 607 | 588 | ||
| 608 | - return ret; | ||
| 589 | + return fn(&session_, callbacks, this, options, mem); | ||
| 609 | 590 | } | |
| 610 | 591 | ||
| 611 | 592 | inline void Nghttp2Session::MarkDestroying() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments