From 103bbd493d090320f926099b09f8a84530175574 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 5 Dec 2019 11:17:34 -0800 Subject: [PATCH 1/3] quic: simplify and condense --- src/node_quic_crypto.cc | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/node_quic_crypto.cc b/src/node_quic_crypto.cc index 56632a3c39..b3cd484037 100644 --- a/src/node_quic_crypto.cc +++ b/src/node_quic_crypto.cc @@ -95,29 +95,20 @@ bool DeriveTokenKey( secret.size())); } -bool MessageDigest( - std::array* dest, - const std::array& rand) { +void GenerateRandData(uint8_t* buf, size_t len) { + std::array rand; + std::array md; + const EVP_MD* meth = EVP_sha256(); + unsigned int mdlen = EVP_MD_size(meth); DeleteFnPtr ctx; ctx.reset(EVP_MD_CTX_new()); CHECK(ctx); - if (EVP_DigestInit_ex(ctx.get(), meth, nullptr) != 1 || - EVP_DigestUpdate(ctx.get(), rand.data(), rand.size()) != 1) { - return false; - } - - unsigned int mdlen = EVP_MD_size(meth); - - return EVP_DigestFinal_ex(ctx.get(), dest->data(), &mdlen) == 1; -} - -void GenerateRandData(uint8_t* buf, size_t len) { - std::array rand; - std::array md; EntropySource(rand.data(), rand.size()); - CHECK(MessageDigest(&md, rand)); + CHECK_EQ(EVP_DigestInit_ex(ctx.get(), meth, nullptr), 1); + CHECK_EQ(EVP_DigestUpdate(ctx.get(), rand.data(), rand.size()), 1); + CHECK_EQ(EVP_DigestFinal_ex(ctx.get(), rand.data(), &mdlen), 1); CHECK_LE(len, md.size()); std::copy_n(std::begin(md), len, buf); } From ee6440515f77e782e311dc070a3003468eecfbf6 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 5 Dec 2019 12:08:18 -0800 Subject: [PATCH 2/3] quic: minor cleanups --- src/node_quic_default_application.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/node_quic_default_application.cc b/src/node_quic_default_application.cc index 0269cb95b4..59145fba36 100644 --- a/src/node_quic_default_application.cc +++ b/src/node_quic_default_application.cc @@ -42,8 +42,9 @@ bool DefaultApplication::ReceiveStreamData( // empty stream frames to commit resources. Check that // here. Essentially, we only want to create a new stream // if the datalen is greater than 0, otherwise, we ignore - // the packet. - if (datalen == 0) + // the packet. ngtcp2 should be handling this for us, + // but we handle it just to be safe. + if (UNLIKELY(datalen == 0)) return true; stream = Session()->CreateStream(stream_id); @@ -62,7 +63,7 @@ void DefaultApplication::AcknowledgeStreamData( Debug(Session(), "Default QUIC Application acknowledging stream data"); // It's possible that the stream has already been destroyed and // removed. If so, just silently ignore the ack - if (stream) + if (stream != nullptr) stream->AckedDataOffset(offset, datalen); } @@ -126,7 +127,7 @@ bool DefaultApplication::SendStreamData(QuicStream* stream) { size_t remaining = stream->DrainInto(&vec); Debug(stream, "Sending %d bytes of stream data. Still writable? %s", remaining, - stream->IsWritable()?"yes":"no"); + stream->IsWritable() ? "yes" : "no"); // c and v are used to track the current serialization position // for each iteration of the for(;;) loop below. From 6b6605407c48cd551511293525b2507671419a59 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 5 Dec 2019 13:22:32 -0800 Subject: [PATCH 3/3] quic: get name and value as std::string option --- src/node_quic_http3_application.cc | 30 ++++++++++++++++++++++++++++++ src/node_quic_http3_application.h | 11 +++++------ src/node_quic_stream.h | 3 +++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/node_quic_http3_application.cc b/src/node_quic_http3_application.cc index 01aa699389..176c1cac65 100644 --- a/src/node_quic_http3_application.cc +++ b/src/node_quic_http3_application.cc @@ -10,6 +10,7 @@ #include #include +#include namespace node { @@ -59,6 +60,13 @@ Http3Header::Http3Header( value_.reset(value); } +Http3Header::Http3Header(Http3Header&& other) noexcept : + token_(other.token_), + name_(std::move(other.name_)), + value_(std::move(other.value_)) { + other.token_ = -1; +} + MaybeLocal Http3Header::GetName(QuicApplication* app) const { const char* header_name = to_http_header_name(token_); Environment* env = app->env(); @@ -97,6 +105,28 @@ MaybeLocal Http3Header::GetValue(QuicApplication* app) const { value_); } +std::string Http3Header::GetName() const { + const char* header_name = to_http_header_name(token_); + if (header_name != nullptr) + return std::string(header_name); + + if (UNLIKELY(!name_)) + return std::string(); // Empty String + + return std::string( + reinterpret_cast(name_.data()), + name_.len()); +} + +std::string Http3Header::GetValue() const { + if (UNLIKELY(!value_)) + return std::string(); // Empty String + + return std::string( + reinterpret_cast(value_.data()), + value_.len()); +} + namespace { template inline void SetConfig(Environment* env, int idx, t* val) { diff --git a/src/node_quic_http3_application.h b/src/node_quic_http3_application.h index 5c234cd8b4..cd54d7b755 100644 --- a/src/node_quic_http3_application.h +++ b/src/node_quic_http3_application.h @@ -13,6 +13,7 @@ #include #include +#include namespace node { namespace quic { @@ -52,16 +53,14 @@ using Http3ConnectionPointer = DeleteFnPtr; class Http3Header : public QuicHeader { public: Http3Header(int32_t token, nghttp3_rcbuf* name, nghttp3_rcbuf* value); - Http3Header(Http3Header&& other) noexcept : - token_(other.token_), - name_(std::move(other.name_)), - value_(std::move(other.value_)) { - other.token_ = -1; - } + Http3Header(Http3Header&& other) noexcept; v8::MaybeLocal GetName(QuicApplication* app) const override; v8::MaybeLocal GetValue(QuicApplication* app) const override; + std::string GetName() const override; + std::string GetValue() const override; + private: int32_t token_ = -1; Http3RcBufferPointer name_; diff --git a/src/node_quic_stream.h b/src/node_quic_stream.h index 23205c2948..bd522e2643 100644 --- a/src/node_quic_stream.h +++ b/src/node_quic_stream.h @@ -11,6 +11,7 @@ #include "stream_base-inl.h" #include "v8.h" +#include #include namespace node { @@ -60,6 +61,8 @@ class QuicHeader { virtual ~QuicHeader() {} virtual v8::MaybeLocal GetName(QuicApplication* app) const = 0; virtual v8::MaybeLocal GetValue(QuicApplication* app) const = 0; + virtual std::string GetName() const = 0; + virtual std::string GetValue() const = 0; }; // QuicStream's are simple data flows that, fortunately, do not