Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: cleanup and refactoring of http2 internals #32884

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,34 +348,32 @@ const char* Http2Session::TypeName() const {
}
}

Origins::Origins(Isolate* isolate,
Local<Context> context,
Local<String> origin_string,
size_t origin_count) : count_(origin_count) {
Origins::Origins(
Environment* env,
Local<String> origin_string,
size_t origin_count)
: count_(origin_count) {
int origin_string_len = origin_string->Length();
if (count_ == 0) {
CHECK_EQ(origin_string_len, 0);
return;
}

// Allocate a single buffer with count_ nghttp2_nv structs, followed
// by the raw header data as passed from JS. This looks like:
// | possible padding | nghttp2_nv | nghttp2_nv | ... | header contents |
buf_.AllocateSufficientStorage((alignof(nghttp2_origin_entry) - 1) +
count_ * sizeof(nghttp2_origin_entry) +
origin_string_len);
buf_ = env->AllocateManaged((alignof(nghttp2_origin_entry) - 1) +
count_ * sizeof(nghttp2_origin_entry) +
origin_string_len);

// Make sure the start address is aligned appropriately for an nghttp2_nv*.
char* start = reinterpret_cast<char*>(
RoundUp(reinterpret_cast<uintptr_t>(*buf_),
RoundUp(reinterpret_cast<uintptr_t>(buf_.data()),
alignof(nghttp2_origin_entry)));
char* origin_contents = start + (count_ * sizeof(nghttp2_origin_entry));
nghttp2_origin_entry* const nva =
reinterpret_cast<nghttp2_origin_entry*>(start);

CHECK_LE(origin_contents + origin_string_len, *buf_ + buf_.length());
CHECK_LE(origin_contents + origin_string_len, buf_.data() + buf_.size());
CHECK_EQ(origin_string->WriteOneByte(
isolate,
env->isolate(),
reinterpret_cast<uint8_t*>(origin_contents),
0,
origin_string_len,
Expand Down Expand Up @@ -2672,12 +2670,13 @@ void Http2Session::AltSvc(int32_t id,
origin, origin_len, value, value_len), 0);
}

void Http2Session::Origin(nghttp2_origin_entry* ov, size_t count) {
void Http2Session::Origin(const Origins& origins) {
Http2Scope h2scope(this);
CHECK_EQ(nghttp2_submit_origin(
session_.get(),
NGHTTP2_FLAG_NONE,
ov, count), 0);
*origins,
origins.length()), 0);
}

// Submits an AltSvc frame to be sent to the connected peer.
Expand Down Expand Up @@ -2720,14 +2719,9 @@ void Http2Session::Origin(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());

Local<String> origin_string = args[0].As<String>();
int32_t count = args[1]->Int32Value(context).FromMaybe(0);

Origins origins(env->isolate(),
env->context(),
origin_string,
static_cast<int>(count));
size_t count = args[1]->Int32Value(context).FromMaybe(0);
jasnell marked this conversation as resolved.
Show resolved Hide resolved

session->Origin(*origins, origins.length());
session->Origin(Origins(env, origin_string, count));
}

// Submits a PING frame to be sent to the connected peer.
Expand Down
16 changes: 10 additions & 6 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class Http2Ping;
class Http2Session;
class Http2Settings;
class Http2Stream;
class Origins;

// This scope should be present when any call into nghttp2 that may schedule
// data to be written to the underlying transport is made, and schedules
Expand Down Expand Up @@ -581,15 +582,19 @@ class Http2Session : public AsyncWrap,

void Close(uint32_t code = NGHTTP2_NO_ERROR,
bool socket_closed = false);

void Consume(v8::Local<v8::Object> stream);

void Goaway(uint32_t code, int32_t lastStreamID,
const uint8_t* data, size_t len);

void AltSvc(int32_t id,
uint8_t* origin,
size_t origin_len,
uint8_t* value,
size_t value_len);
void Origin(nghttp2_origin_entry* ov, size_t count);

void Origin(const Origins& origins);

uint8_t SendPendingData();

Expand Down Expand Up @@ -1092,14 +1097,13 @@ class Http2Settings : public AsyncWrap {

class Origins {
public:
Origins(v8::Isolate* isolate,
v8::Local<v8::Context> context,
Origins(Environment* env,
v8::Local<v8::String> origin_string,
size_t origin_count);
~Origins() = default;

nghttp2_origin_entry* operator*() {
return reinterpret_cast<nghttp2_origin_entry*>(*buf_);
const nghttp2_origin_entry* operator*() const {
return reinterpret_cast<const nghttp2_origin_entry*>(buf_.data());
}

size_t length() const {
Expand All @@ -1108,7 +1112,7 @@ class Origins {

private:
size_t count_;
MaybeStackBuffer<char, 512> buf_;
AllocatedBuffer buf_;
};

#define HTTP2_HIDDEN_CONSTANTS(V) \
Expand Down