Skip to content

Commit 354ff4f

Browse files
jasnellcodebytere
authored andcommitted
src: small modification to NgHeader
This is separated out of the QUIC PR. It is not specific to QUIC but provides a new base class that is used there as an abstraction of the actual implementation. This is a purely internal implementation detail that has no outward functional changes (so no need for tests) Signed-off-by: James M Snell <[email protected]> PR-URL: #33289 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 6d2aaaf commit 354ff4f

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/node_http_common-inl.h

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ size_t GetServerMaxHeaderPairs(size_t max_header_pairs) {
7676
return std::max(max_header_pairs, min_header_pairs);
7777
}
7878

79+
template <typename allocator_t>
80+
std::string NgHeaderBase<allocator_t>::ToString() const {
81+
std::string ret = name();
82+
ret += " = ";
83+
ret += value();
84+
return ret;
85+
}
86+
7987
template <typename T>
8088
bool NgHeader<T>::IsZeroLength(
8189
NgHeader<T>::rcbuf_t* name,
@@ -132,6 +140,12 @@ NgHeader<T>::NgHeader(NgHeader<T>&& other) noexcept
132140
other.env_ = nullptr;
133141
}
134142

143+
template <typename T>
144+
void NgHeader<T>::MemoryInfo(MemoryTracker* tracker) const {
145+
tracker->TrackField("name", name_);
146+
tracker->TrackField("value", value_);
147+
}
148+
135149
template <typename T>
136150
v8::MaybeLocal<v8::String> NgHeader<T>::GetName(
137151
NgHeader<T>::allocator_t* allocator) const {

src/node_http_common.h

+19-17
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@ class NgRcBufPointer : public MemoryRetainer {
453453
bool internalizable_ = false;
454454
};
455455

456+
template <typename allocator_t>
457+
struct NgHeaderBase : public MemoryRetainer {
458+
virtual v8::MaybeLocal<v8::String> GetName(allocator_t* allocator) const = 0;
459+
virtual v8::MaybeLocal<v8::String> GetValue(allocator_t* allocator) const = 0;
460+
virtual std::string name() const = 0;
461+
virtual std::string value() const = 0;
462+
virtual size_t length() const = 0;
463+
virtual std::string ToString() const;
464+
};
465+
456466
// The ng libraries use nearly identical structs to represent
457467
// received http headers. The NgHeader class wraps those in a
458468
// consistent way and allows converting the name and value to
@@ -461,7 +471,7 @@ class NgRcBufPointer : public MemoryRetainer {
461471
// memory tracking, and implementation of static utility functions.
462472
// See Http2HeaderTraits in node_http2.h for an example.
463473
template <typename T>
464-
class NgHeader : public MemoryRetainer {
474+
class NgHeader final : public NgHeaderBase<typename T::allocator_t> {
465475
public:
466476
typedef typename T::rcbufferpointer_t rcbufferpointer_t;
467477
typedef typename T::rcbufferpointer_t::rcbuf_t rcbuf_t;
@@ -487,28 +497,20 @@ class NgHeader : public MemoryRetainer {
487497
// object to the v8 string. Once the v8 string is garbage collected,
488498
// the reference counter will be decremented.
489499

490-
inline v8::MaybeLocal<v8::String> GetName(allocator_t* allocator) const;
491-
inline v8::MaybeLocal<v8::String> GetValue(allocator_t* allocator) const;
500+
inline v8::MaybeLocal<v8::String> GetName(
501+
allocator_t* allocator) const override;
502+
inline v8::MaybeLocal<v8::String> GetValue(
503+
allocator_t* allocator) const override;
492504

493-
inline std::string name() const;
494-
inline std::string value() const;
495-
inline size_t length() const;
505+
inline std::string name() const override;
506+
inline std::string value() const override;
507+
inline size_t length() const override;
496508

497-
void MemoryInfo(MemoryTracker* tracker) const override {
498-
tracker->TrackField("name", name_);
499-
tracker->TrackField("value", value_);
500-
}
509+
void MemoryInfo(MemoryTracker* tracker) const override;
501510

502511
SET_MEMORY_INFO_NAME(NgHeader)
503512
SET_SELF_SIZE(NgHeader)
504513

505-
std::string ToString() const {
506-
std::string ret = name();
507-
ret += " = ";
508-
ret += value();
509-
return ret;
510-
}
511-
512514
private:
513515
Environment* env_;
514516
rcbufferpointer_t name_;

0 commit comments

Comments
 (0)