diff --git a/src/node_http_common-inl.h b/src/node_http_common-inl.h index 2c76dc3a1fd372..54f2faf39c49bc 100644 --- a/src/node_http_common-inl.h +++ b/src/node_http_common-inl.h @@ -76,6 +76,14 @@ size_t GetServerMaxHeaderPairs(size_t max_header_pairs) { return std::max(max_header_pairs, min_header_pairs); } +template +std::string NgHeaderBase::ToString() const { + std::string ret = name(); + ret += " = "; + ret += value(); + return ret; +} + template bool NgHeader::IsZeroLength( NgHeader::rcbuf_t* name, @@ -132,6 +140,12 @@ NgHeader::NgHeader(NgHeader&& other) noexcept other.env_ = nullptr; } +template +void NgHeader::MemoryInfo(MemoryTracker* tracker) const { + tracker->TrackField("name", name_); + tracker->TrackField("value", value_); +} + template v8::MaybeLocal NgHeader::GetName( NgHeader::allocator_t* allocator) const { diff --git a/src/node_http_common.h b/src/node_http_common.h index d2bdddd93f4649..c7e4d34af24196 100644 --- a/src/node_http_common.h +++ b/src/node_http_common.h @@ -453,6 +453,16 @@ class NgRcBufPointer : public MemoryRetainer { bool internalizable_ = false; }; +template +struct NgHeaderBase : public MemoryRetainer { + virtual v8::MaybeLocal GetName(allocator_t* allocator) const = 0; + virtual v8::MaybeLocal GetValue(allocator_t* allocator) const = 0; + virtual std::string name() const = 0; + virtual std::string value() const = 0; + virtual size_t length() const = 0; + virtual std::string ToString() const; +}; + // The ng libraries use nearly identical structs to represent // received http headers. The NgHeader class wraps those in a // consistent way and allows converting the name and value to @@ -461,7 +471,7 @@ class NgRcBufPointer : public MemoryRetainer { // memory tracking, and implementation of static utility functions. // See Http2HeaderTraits in node_http2.h for an example. template -class NgHeader : public MemoryRetainer { +class NgHeader final : public NgHeaderBase { public: typedef typename T::rcbufferpointer_t rcbufferpointer_t; typedef typename T::rcbufferpointer_t::rcbuf_t rcbuf_t; @@ -487,28 +497,20 @@ class NgHeader : public MemoryRetainer { // object to the v8 string. Once the v8 string is garbage collected, // the reference counter will be decremented. - inline v8::MaybeLocal GetName(allocator_t* allocator) const; - inline v8::MaybeLocal GetValue(allocator_t* allocator) const; + inline v8::MaybeLocal GetName( + allocator_t* allocator) const override; + inline v8::MaybeLocal GetValue( + allocator_t* allocator) const override; - inline std::string name() const; - inline std::string value() const; - inline size_t length() const; + inline std::string name() const override; + inline std::string value() const override; + inline size_t length() const override; - void MemoryInfo(MemoryTracker* tracker) const override { - tracker->TrackField("name", name_); - tracker->TrackField("value", value_); - } + void MemoryInfo(MemoryTracker* tracker) const override; SET_MEMORY_INFO_NAME(NgHeader) SET_SELF_SIZE(NgHeader) - std::string ToString() const { - std::string ret = name(); - ret += " = "; - ret += value(); - return ret; - } - private: Environment* env_; rcbufferpointer_t name_;