Skip to content

Commit

Permalink
src: rename http2 class and suppress compile warnings
Browse files Browse the repository at this point in the history
Suppress compile warnings on Windows, rename class for
consistent styling.

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #32551
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
jasnell authored and targos committed Apr 11, 2020
1 parent afc6a25 commit 8f766e8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 51 deletions.
77 changes: 43 additions & 34 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Http2Options::Http2Options(Environment* env, nghttp2_session_type type) {
// Important: The maxSessionMemory option in javascript is expressed in
// terms of MB increments (i.e. the value 1 == 1 MB)
if (flags & (1 << IDX_OPTIONS_MAX_SESSION_MEMORY))
SetMaxSessionMemory(buffer[IDX_OPTIONS_MAX_SESSION_MEMORY] * 1e6);
SetMaxSessionMemory(buffer[IDX_OPTIONS_MAX_SESSION_MEMORY] * 1000000);
}

void Http2Session::Http2Settings::Init() {
Expand Down Expand Up @@ -349,8 +349,8 @@ Http2Priority::Http2Priority(Environment* env,
int32_t weight_ = weight->Int32Value(context).ToChecked();
bool exclusive_ = exclusive->BooleanValue(env->isolate());
Debug(env, DebugCategory::HTTP2STREAM,
"Http2Priority: parent: %d, weight: %d, exclusive: %d\n",
parent_, weight_, exclusive_);
"Http2Priority: parent: %d, weight: %d, exclusive: %s\n",
parent_, weight_, exclusive_ ? "yes" : "no");
nghttp2_priority_spec_init(&spec, parent_, weight_, exclusive_ ? 1 : 0);
}

Expand Down Expand Up @@ -578,8 +578,10 @@ void Http2Stream::EmitStatistics() {
} else {
buffer[IDX_STREAM_STATS_TIMETOFIRSTBYTESENT] = 0;
}
buffer[IDX_STREAM_STATS_SENTBYTES] = entry->sent_bytes();
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = entry->received_bytes();
buffer[IDX_STREAM_STATS_SENTBYTES] =
static_cast<double>(entry->sent_bytes());
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] =
static_cast<double>(entry->received_bytes());
Local<Object> obj;
if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj);
});
Expand All @@ -602,10 +604,12 @@ void Http2Session::EmitStatistics() {
buffer[IDX_SESSION_STATS_STREAMCOUNT] = entry->stream_count();
buffer[IDX_SESSION_STATS_STREAMAVERAGEDURATION] =
entry->stream_average_duration();
buffer[IDX_SESSION_STATS_DATA_SENT] = entry->data_sent();
buffer[IDX_SESSION_STATS_DATA_RECEIVED] = entry->data_received();
buffer[IDX_SESSION_STATS_DATA_SENT] =
static_cast<double>(entry->data_sent());
buffer[IDX_SESSION_STATS_DATA_RECEIVED] =
static_cast<double>(entry->data_received());
buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] =
entry->max_concurrent_streams();
static_cast<double>(entry->max_concurrent_streams());
Local<Object> obj;
if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj);
});
Expand Down Expand Up @@ -1513,9 +1517,9 @@ void Http2Session::ClearOutgoing(int status) {
outgoing_storage_.clear();
outgoing_length_ = 0;

std::vector<nghttp2_stream_write> current_outgoing_buffers_;
std::vector<NgHttp2StreamWrite> current_outgoing_buffers_;
current_outgoing_buffers_.swap(outgoing_buffers_);
for (const nghttp2_stream_write& wr : current_outgoing_buffers_) {
for (const NgHttp2StreamWrite& wr : current_outgoing_buffers_) {
WriteWrap* wrap = wr.req_wrap;
if (wrap != nullptr) {
// TODO(addaleax): Pass `status` instead of 0, so that we actually error
Expand All @@ -1542,7 +1546,7 @@ void Http2Session::ClearOutgoing(int status) {
}
}

void Http2Session::PushOutgoingBuffer(nghttp2_stream_write&& write) {
void Http2Session::PushOutgoingBuffer(NgHttp2StreamWrite&& write) {
outgoing_length_ += write.buf.len;
outgoing_buffers_.emplace_back(std::move(write));
}
Expand All @@ -1559,7 +1563,7 @@ void Http2Session::CopyDataIntoOutgoing(const uint8_t* src, size_t src_length) {
// of the outgoing_buffers_ vector may invalidate the pointer.
// The correct base pointers will be set later, before writing to the
// underlying socket.
PushOutgoingBuffer(nghttp2_stream_write {
PushOutgoingBuffer(NgHttp2StreamWrite {
uv_buf_init(nullptr, src_length)
});
}
Expand Down Expand Up @@ -1622,7 +1626,7 @@ uint8_t Http2Session::SendPendingData() {
// (Those are marked by having .base == nullptr.)
size_t offset = 0;
size_t i = 0;
for (const nghttp2_stream_write& write : outgoing_buffers_) {
for (const NgHttp2StreamWrite& write : outgoing_buffers_) {
statistics_.data_sent += write.buf.len;
if (write.buf.base == nullptr) {
bufs[i++] = uv_buf_init(
Expand Down Expand Up @@ -1678,7 +1682,7 @@ int Http2Session::OnSendData(
// we told it so, which means that we *should* have data available.
CHECK(!stream->queue_.empty());

nghttp2_stream_write& write = stream->queue_.front();
NgHttp2StreamWrite& write = stream->queue_.front();
if (write.buf.len <= length) {
// This write does not suffice by itself, so we can consume it completely.
length -= write.buf.len;
Expand All @@ -1688,7 +1692,7 @@ int Http2Session::OnSendData(
}

// Slice off `length` bytes of the first write in the queue.
session->PushOutgoingBuffer(nghttp2_stream_write {
session->PushOutgoingBuffer(NgHttp2StreamWrite {
uv_buf_init(write.buf.base, length)
});
write.buf.base += length;
Expand All @@ -1698,7 +1702,7 @@ int Http2Session::OnSendData(

if (frame->data.padlen > 0) {
// Send padding if that was requested.
session->PushOutgoingBuffer(nghttp2_stream_write {
session->PushOutgoingBuffer(NgHttp2StreamWrite {
uv_buf_init(const_cast<char*>(zero_bytes_256), frame->data.padlen - 1)
});
}
Expand Down Expand Up @@ -1779,7 +1783,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {

// Remember the current buffer, so that OnDataChunkReceived knows the
// offset of a DATA frame's data into the socket read buffer.
stream_buf_ = uv_buf_init(buf.data(), nread);
stream_buf_ = uv_buf_init(buf.data(), static_cast<unsigned int>(nread));

Isolate* isolate = env()->isolate();

Expand All @@ -1792,7 +1796,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {

if (UNLIKELY(ret < 0)) {
Debug(this, "fatal error receiving data: %d", ret);
Local<Value> arg = Integer::New(isolate, ret);
Local<Value> arg = Integer::New(isolate, static_cast<int32_t>(ret));
MakeCallback(env()->http2session_on_error_function(), 1, &arg);
return;
}
Expand All @@ -1801,7 +1805,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
}

bool Http2Session::HasWritesOnSocketForStream(Http2Stream* stream) {
for (const nghttp2_stream_write& wr : outgoing_buffers_) {
for (const NgHttp2StreamWrite& wr : outgoing_buffers_) {
if (wr.req_wrap != nullptr && wr.req_wrap->stream() == stream)
return true;
}
Expand Down Expand Up @@ -1948,7 +1952,7 @@ void Http2Stream::Destroy() {
// here because it's possible for destroy to have been called while
// we still have queued outbound writes.
while (!queue_.empty()) {
nghttp2_stream_write& head = queue_.front();
NgHttp2StreamWrite& head = queue_.front();
if (head.req_wrap != nullptr)
head.req_wrap->Done(UV_ECANCELED);
queue_.pop();
Expand Down Expand Up @@ -2166,7 +2170,7 @@ int Http2Stream::DoWrite(WriteWrap* req_wrap,
for (size_t i = 0; i < nbufs; ++i) {
// Store the req_wrap on the last write info in the queue, so that it is
// only marked as finished once all buffers associated with it are finished.
queue_.emplace(nghttp2_stream_write {
queue_.emplace(NgHttp2StreamWrite {
i == nbufs - 1 ? req_wrap : nullptr,
bufs[i]
});
Expand Down Expand Up @@ -2402,19 +2406,19 @@ void Http2Session::RefreshState(const FunctionCallbackInfo<Value>& args) {
buffer[IDX_SESSION_STATE_REMOTE_WINDOW_SIZE] =
nghttp2_session_get_remote_window_size(s);
buffer[IDX_SESSION_STATE_OUTBOUND_QUEUE_SIZE] =
nghttp2_session_get_outbound_queue_size(s);
static_cast<double>(nghttp2_session_get_outbound_queue_size(s));
buffer[IDX_SESSION_STATE_HD_DEFLATE_DYNAMIC_TABLE_SIZE] =
nghttp2_session_get_hd_deflate_dynamic_table_size(s);
static_cast<double>(nghttp2_session_get_hd_deflate_dynamic_table_size(s));
buffer[IDX_SESSION_STATE_HD_INFLATE_DYNAMIC_TABLE_SIZE] =
nghttp2_session_get_hd_inflate_dynamic_table_size(s);
static_cast<double>(nghttp2_session_get_hd_inflate_dynamic_table_size(s));
}


// Constructor for new Http2Session instances.
void Http2Session::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args.IsConstructCall());
int val = args[0]->IntegerValue(env->context()).ToChecked();
int32_t val = args[0]->Int32Value(env->context()).ToChecked();
nghttp2_session_type type = static_cast<nghttp2_session_type>(val);
Http2Session* session = new Http2Session(env, args.This(), type);
session->get_async_id(); // avoid compiler warning
Expand Down Expand Up @@ -2452,7 +2456,7 @@ void Http2Session::Request(const FunctionCallbackInfo<Value>& args) {
Environment* env = session->env();

Local<Array> headers = args[0].As<Array>();
int options = args[1]->IntegerValue(env->context()).ToChecked();
int32_t options = args[1]->Int32Value(env->context()).ToChecked();
Http2Priority priority(env, args[2], args[3], args[4]);

Debug(session, "request submitted");
Expand All @@ -2463,7 +2467,7 @@ void Http2Session::Request(const FunctionCallbackInfo<Value>& args) {
*priority,
Http2Headers(env, headers),
&ret,
options);
static_cast<int>(options));

if (ret <= 0 || stream == nullptr) {
Debug(session, "could not submit request: %s", nghttp2_strerror(ret));
Expand Down Expand Up @@ -2552,10 +2556,12 @@ void Http2Stream::Respond(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&stream, args.Holder());

Local<Array> headers = args[0].As<Array>();
int options = args[1]->IntegerValue(env->context()).ToChecked();
int32_t options = args[1]->Int32Value(env->context()).ToChecked();

args.GetReturnValue().Set(
stream->SubmitResponse(Http2Headers(env, headers), options));
stream->SubmitResponse(
Http2Headers(env, headers),
static_cast<int>(options)));
Debug(stream, "response submitted");
}

Expand Down Expand Up @@ -2605,13 +2611,16 @@ void Http2Stream::PushPromise(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&parent, args.Holder());

Local<Array> headers = args[0].As<Array>();
int options = args[1]->IntegerValue(env->context()).ToChecked();
int32_t options = args[1]->Int32Value(env->context()).ToChecked();

Debug(parent, "creating push promise");

int32_t ret = 0;
Http2Stream* stream =
parent->SubmitPushPromise(Http2Headers(env, headers), &ret, options);
parent->SubmitPushPromise(
Http2Headers(env, headers),
&ret,
static_cast<int>(options));

if (ret <= 0 || stream == nullptr) {
Debug(parent, "failed to create push stream: %d", ret);
Expand Down Expand Up @@ -2725,13 +2734,13 @@ void Http2Session::Origin(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());

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


Origins origins(env->isolate(),
env->context(),
origin_string,
count);
static_cast<int>(count));

session->Origin(*origins, origins.length());
}
Expand Down Expand Up @@ -2885,7 +2894,7 @@ void Http2Session::Http2Ping::DetachFromSession() {
session_ = nullptr;
}

void nghttp2_stream_write::MemoryInfo(MemoryTracker* tracker) const {
void NgHttp2StreamWrite::MemoryInfo(MemoryTracker* tracker) const {
if (req_wrap != nullptr)
tracker->TrackField("req_wrap", req_wrap->GetAsyncWrap());
tracker->TrackField("buf", buf);
Expand Down
34 changes: 17 additions & 17 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ namespace http2 {
// may send in order to prevent abuse. The current default cap is 10. The
// user may set a different limit using a per Http2Session configuration
// option.
#define DEFAULT_MAX_PINGS 10
constexpr size_t kDefaultMaxPings = 10;

// Also strictly limit the number of outstanding SETTINGS frames a user sends
#define DEFAULT_MAX_SETTINGS 10
constexpr size_t kDefaultMaxSettings = 10;

// Default maximum total memory cap for Http2Session.
#define DEFAULT_MAX_SESSION_MEMORY 1e7
constexpr uint64_t kDefaultMaxSessionMemory = 10000000;

// These are the standard HTTP/2 defaults as specified by the RFC
#define DEFAULT_SETTINGS_HEADER_TABLE_SIZE 4096
Expand Down Expand Up @@ -123,17 +123,17 @@ enum nghttp2_stream_options {
STREAM_OPTION_GET_TRAILERS = 0x2,
};

struct nghttp2_stream_write : public MemoryRetainer {
struct NgHttp2StreamWrite : public MemoryRetainer {
WriteWrap* req_wrap = nullptr;
uv_buf_t buf;

inline explicit nghttp2_stream_write(uv_buf_t buf_) : buf(buf_) {}
inline nghttp2_stream_write(WriteWrap* req, uv_buf_t buf_) :
inline explicit NgHttp2StreamWrite(uv_buf_t buf_) : buf(buf_) {}
inline NgHttp2StreamWrite(WriteWrap* req, uv_buf_t buf_) :
req_wrap(req), buf(buf_) {}

void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(nghttp2_stream_write)
SET_SELF_SIZE(nghttp2_stream_write)
SET_MEMORY_INFO_NAME(NgHttp2StreamWrite)
SET_SELF_SIZE(NgHttp2StreamWrite)
};

// The Padding Strategy determines the method by which extra padding is
Expand Down Expand Up @@ -239,11 +239,11 @@ class Http2Options {

private:
Nghttp2OptionPointer options_;
uint64_t max_session_memory_ = DEFAULT_MAX_SESSION_MEMORY;
uint64_t max_session_memory_ = kDefaultMaxSessionMemory;
uint32_t max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS;
padding_strategy_type padding_strategy_ = PADDING_STRATEGY_NONE;
size_t max_outstanding_pings_ = DEFAULT_MAX_PINGS;
size_t max_outstanding_settings_ = DEFAULT_MAX_SETTINGS;
size_t max_outstanding_pings_ = kDefaultMaxPings;
size_t max_outstanding_settings_ = kDefaultMaxSettings;
};

class Http2Priority {
Expand Down Expand Up @@ -473,7 +473,7 @@ class Http2Stream : public AsyncWrap,

// Outbound Data... This is the data written by the JS layer that is
// waiting to be written out to the socket.
std::queue<nghttp2_stream_write> queue_;
std::queue<NgHttp2StreamWrite> queue_;
size_t available_outbound_length_ = 0;

Http2StreamListener stream_listener_;
Expand Down Expand Up @@ -835,7 +835,7 @@ class Http2Session : public AsyncWrap,
uint32_t max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS;

// The maximum amount of memory allocated for this session
uint64_t max_session_memory_ = DEFAULT_MAX_SESSION_MEMORY;
uint64_t max_session_memory_ = kDefaultMaxSessionMemory;
uint64_t current_session_memory_ = 0;
// The amount of memory allocated by nghttp2 internals
uint64_t current_nghttp2_memory_ = 0;
Expand All @@ -858,13 +858,13 @@ class Http2Session : public AsyncWrap,
AllocatedBuffer stream_buf_allocation_;
size_t stream_buf_offset_ = 0;

size_t max_outstanding_pings_ = DEFAULT_MAX_PINGS;
size_t max_outstanding_pings_ = kDefaultMaxPings;
std::queue<BaseObjectPtr<Http2Ping>> outstanding_pings_;

size_t max_outstanding_settings_ = DEFAULT_MAX_SETTINGS;
size_t max_outstanding_settings_ = kDefaultMaxSettings;
std::queue<BaseObjectPtr<Http2Settings>> outstanding_settings_;

std::vector<nghttp2_stream_write> outgoing_buffers_;
std::vector<NgHttp2StreamWrite> outgoing_buffers_;
std::vector<uint8_t> outgoing_storage_;
size_t outgoing_length_ = 0;
std::vector<int32_t> pending_rst_streams_;
Expand All @@ -876,7 +876,7 @@ class Http2Session : public AsyncWrap,
// Also use the invalid frame count as a measure for rejecting input frames.
uint32_t invalid_frame_count_ = 0;

void PushOutgoingBuffer(nghttp2_stream_write&& write);
void PushOutgoingBuffer(NgHttp2StreamWrite&& write);
void CopyDataIntoOutgoing(const uint8_t* src, size_t src_length);
void ClearOutgoing(int status);

Expand Down

0 comments on commit 8f766e8

Please sign in to comment.