Skip to content

Commit 1976c7c

Browse files
addaleaxMylesBorins
authored andcommitted
src: move url internals into anonymous namespace
This helps because `static` doesn’t work for C++ classes, but refactoring `url_host` into a proper C++ class seems the most reasonable soluation for the memory leak fixed by the next commit. Backport-PR-URL: #18324 PR-URL: #17470 Reviewed-By: Timothy Gu <timothygu99@gmail.com>
1 parent d66f469 commit 1976c7c

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

src/node_url.cc

+58-59
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ using v8::Value;
4646

4747
namespace url {
4848

49+
namespace {
50+
4951
// https://url.spec.whatwg.org/#eof-code-point
50-
static const char kEOL = -1;
52+
const char kEOL = -1;
5153

5254
// Used in ToUSVString().
53-
static const char16_t kUnicodeReplacementCharacter = 0xFFFD;
55+
const char16_t kUnicodeReplacementCharacter = 0xFFFD;
5456

5557
// https://url.spec.whatwg.org/#concept-host
5658
union url_host_value {
@@ -103,21 +105,21 @@ enum url_error_cb_args {
103105

104106
#define CHAR_TEST(bits, name, expr) \
105107
template <typename T> \
106-
static inline bool name(const T ch) { \
108+
inline bool name(const T ch) { \
107109
static_assert(sizeof(ch) >= (bits) / 8, \
108110
"Character must be wider than " #bits " bits"); \
109111
return (expr); \
110112
}
111113

112114
#define TWO_CHAR_STRING_TEST(bits, name, expr) \
113115
template <typename T> \
114-
static inline bool name(const T ch1, const T ch2) { \
116+
inline bool name(const T ch1, const T ch2) { \
115117
static_assert(sizeof(ch1) >= (bits) / 8, \
116118
"Character must be wider than " #bits " bits"); \
117119
return (expr); \
118120
} \
119121
template <typename T> \
120-
static inline bool name(const std::basic_string<T>& str) { \
122+
inline bool name(const std::basic_string<T>& str) { \
121123
static_assert(sizeof(str[0]) >= (bits) / 8, \
122124
"Character must be wider than " #bits " bits"); \
123125
return str.length() >= 2 && name(str[0], str[1]); \
@@ -146,7 +148,7 @@ CHAR_TEST(8, IsASCIIAlphanumeric, (IsASCIIDigit(ch) || IsASCIIAlpha(ch)))
146148

147149
// https://infra.spec.whatwg.org/#ascii-lowercase
148150
template <typename T>
149-
static inline T ASCIILowercase(T ch) {
151+
inline T ASCIILowercase(T ch) {
150152
return IsASCIIAlpha(ch) ? (ch | 0x20) : ch;
151153
}
152154

@@ -177,7 +179,7 @@ CHAR_TEST(16, IsUnicodeSurrogateTrail, (ch & 0x400) != 0)
177179
#undef CHAR_TEST
178180
#undef TWO_CHAR_STRING_TEST
179181

180-
static const char* hex[256] = {
182+
const char* hex[256] = {
181183
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
182184
"%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
183185
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
@@ -212,7 +214,7 @@ static const char* hex[256] = {
212214
"%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF"
213215
};
214216

215-
static const uint8_t C0_CONTROL_ENCODE_SET[32] = {
217+
const uint8_t C0_CONTROL_ENCODE_SET[32] = {
216218
// 00 01 02 03 04 05 06 07
217219
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
218220
// 08 09 0A 0B 0C 0D 0E 0F
@@ -279,7 +281,7 @@ static const uint8_t C0_CONTROL_ENCODE_SET[32] = {
279281
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
280282
};
281283

282-
static const uint8_t PATH_ENCODE_SET[32] = {
284+
const uint8_t PATH_ENCODE_SET[32] = {
283285
// 00 01 02 03 04 05 06 07
284286
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
285287
// 08 09 0A 0B 0C 0D 0E 0F
@@ -346,7 +348,7 @@ static const uint8_t PATH_ENCODE_SET[32] = {
346348
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
347349
};
348350

349-
static const uint8_t USERINFO_ENCODE_SET[32] = {
351+
const uint8_t USERINFO_ENCODE_SET[32] = {
350352
// 00 01 02 03 04 05 06 07
351353
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
352354
// 08 09 0A 0B 0C 0D 0E 0F
@@ -413,7 +415,7 @@ static const uint8_t USERINFO_ENCODE_SET[32] = {
413415
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
414416
};
415417

416-
static const uint8_t QUERY_ENCODE_SET[32] = {
418+
const uint8_t QUERY_ENCODE_SET[32] = {
417419
// 00 01 02 03 04 05 06 07
418420
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
419421
// 08 09 0A 0B 0C 0D 0E 0F
@@ -480,23 +482,23 @@ static const uint8_t QUERY_ENCODE_SET[32] = {
480482
0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
481483
};
482484

483-
static inline bool BitAt(const uint8_t a[], const uint8_t i) {
485+
inline bool BitAt(const uint8_t a[], const uint8_t i) {
484486
return !!(a[i >> 3] & (1 << (i & 7)));
485487
}
486488

487489
// Appends ch to str. If ch position in encode_set is set, the ch will
488490
// be percent-encoded then appended.
489-
static inline void AppendOrEscape(std::string* str,
490-
const unsigned char ch,
491-
const uint8_t encode_set[]) {
491+
inline void AppendOrEscape(std::string* str,
492+
const unsigned char ch,
493+
const uint8_t encode_set[]) {
492494
if (BitAt(encode_set, ch))
493495
*str += hex[ch];
494496
else
495497
*str += ch;
496498
}
497499

498500
template <typename T>
499-
static inline unsigned hex2bin(const T ch) {
501+
inline unsigned hex2bin(const T ch) {
500502
if (ch >= '0' && ch <= '9')
501503
return ch - '0';
502504
if (ch >= 'A' && ch <= 'F')
@@ -544,16 +546,15 @@ inline std::string PercentDecode(const char* input, size_t len) {
544546
XX("ws:", 80) \
545547
XX("wss:", 443)
546548

547-
static inline bool IsSpecial(std::string scheme) {
549+
inline bool IsSpecial(std::string scheme) {
548550
#define XX(name, _) if (scheme == name) return true;
549551
SPECIALS(XX);
550552
#undef XX
551553
return false;
552554
}
553555

554556
// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter
555-
static inline bool StartsWithWindowsDriveLetter(const char* p,
556-
const char* end) {
557+
inline bool StartsWithWindowsDriveLetter(const char* p, const char* end) {
557558
const size_t length = end - p;
558559
return length >= 2 &&
559560
IsWindowsDriveLetter(p[0], p[1]) &&
@@ -564,23 +565,23 @@ static inline bool StartsWithWindowsDriveLetter(const char* p,
564565
p[2] == '#');
565566
}
566567

567-
static inline int NormalizePort(std::string scheme, int p) {
568+
inline int NormalizePort(std::string scheme, int p) {
568569
#define XX(name, port) if (scheme == name && p == port) return -1;
569570
SPECIALS(XX);
570571
#undef XX
571572
return p;
572573
}
573574

574575
#if defined(NODE_HAVE_I18N_SUPPORT)
575-
static inline bool ToUnicode(const std::string& input, std::string* output) {
576+
inline bool ToUnicode(const std::string& input, std::string* output) {
576577
MaybeStackBuffer<char> buf;
577578
if (i18n::ToUnicode(&buf, input.c_str(), input.length()) < 0)
578579
return false;
579580
output->assign(*buf, buf.length());
580581
return true;
581582
}
582583

583-
static inline bool ToASCII(const std::string& input, std::string* output) {
584+
inline bool ToASCII(const std::string& input, std::string* output) {
584585
MaybeStackBuffer<char> buf;
585586
if (i18n::ToASCII(&buf, input.c_str(), input.length()) < 0)
586587
return false;
@@ -589,20 +590,18 @@ static inline bool ToASCII(const std::string& input, std::string* output) {
589590
}
590591
#else
591592
// Intentional non-ops if ICU is not present.
592-
static inline bool ToUnicode(const std::string& input, std::string* output) {
593+
inline bool ToUnicode(const std::string& input, std::string* output) {
593594
*output = input;
594595
return true;
595596
}
596597

597-
static inline bool ToASCII(const std::string& input, std::string* output) {
598+
inline bool ToASCII(const std::string& input, std::string* output) {
598599
*output = input;
599600
return true;
600601
}
601602
#endif
602603

603-
static url_host_type ParseIPv6Host(url_host* host,
604-
const char* input,
605-
size_t length) {
604+
url_host_type ParseIPv6Host(url_host* host, const char* input, size_t length) {
606605
url_host_type type = HOST_TYPE_FAILED;
607606
for (unsigned n = 0; n < 8; n++)
608607
host->value.ipv6[n] = 0;
@@ -720,7 +719,7 @@ static url_host_type ParseIPv6Host(url_host* host,
720719
return type;
721720
}
722721

723-
static inline int64_t ParseNumber(const char* start, const char* end) {
722+
inline int64_t ParseNumber(const char* start, const char* end) {
724723
unsigned R = 10;
725724
if (end - start >= 2 && start[0] == '0' && (start[1] | 0x20) == 'x') {
726725
start += 2;
@@ -755,9 +754,7 @@ static inline int64_t ParseNumber(const char* start, const char* end) {
755754
return strtoll(start, NULL, R);
756755
}
757756

758-
static url_host_type ParseIPv4Host(url_host* host,
759-
const char* input,
760-
size_t length) {
757+
url_host_type ParseIPv4Host(url_host* host, const char* input, size_t length) {
761758
url_host_type type = HOST_TYPE_DOMAIN;
762759
const char* pointer = input;
763760
const char* mark = input;
@@ -816,9 +813,9 @@ static url_host_type ParseIPv4Host(url_host* host,
816813
return type;
817814
}
818815

819-
static url_host_type ParseOpaqueHost(url_host* host,
820-
const char* input,
821-
size_t length) {
816+
url_host_type ParseOpaqueHost(url_host* host,
817+
const char* input,
818+
size_t length) {
822819
url_host_type type = HOST_TYPE_OPAQUE;
823820
std::string output;
824821
output.reserve(length * 3);
@@ -838,11 +835,11 @@ static url_host_type ParseOpaqueHost(url_host* host,
838835
return type;
839836
}
840837

841-
static url_host_type ParseHost(url_host* host,
842-
const char* input,
843-
size_t length,
844-
bool is_special,
845-
bool unicode = false) {
838+
url_host_type ParseHost(url_host* host,
839+
const char* input,
840+
size_t length,
841+
bool is_special,
842+
bool unicode = false) {
846843
url_host_type type = HOST_TYPE_FAILED;
847844
const char* pointer = input;
848845
std::string decoded;
@@ -895,7 +892,7 @@ static url_host_type ParseHost(url_host* host,
895892
// Locates the longest sequence of 0 segments in an IPv6 address
896893
// in order to use the :: compression when serializing
897894
template<typename T>
898-
static inline T* FindLongestZeroSequence(T* values, size_t len) {
895+
inline T* FindLongestZeroSequence(T* values, size_t len) {
899896
T* start = values;
900897
T* end = start + len;
901898
T* result = nullptr;
@@ -923,7 +920,7 @@ static inline T* FindLongestZeroSequence(T* values, size_t len) {
923920
return result;
924921
}
925922

926-
static url_host_type WriteHost(const url_host* host, std::string* dest) {
923+
url_host_type WriteHost(const url_host* host, std::string* dest) {
927924
dest->clear();
928925
switch (host->type) {
929926
case HOST_TYPE_DOMAIN:
@@ -978,10 +975,10 @@ static url_host_type WriteHost(const url_host* host, std::string* dest) {
978975
return host->type;
979976
}
980977

981-
static bool ParseHost(const std::string& input,
982-
std::string* output,
983-
bool is_special,
984-
bool unicode = false) {
978+
bool ParseHost(const std::string& input,
979+
std::string* output,
980+
bool is_special,
981+
bool unicode = false) {
985982
if (input.length() == 0) {
986983
output->clear();
987984
return true;
@@ -994,9 +991,9 @@ static bool ParseHost(const std::string& input,
994991
return true;
995992
}
996993

997-
static inline void Copy(Environment* env,
998-
Local<Array> ary,
999-
std::vector<std::string>* vec) {
994+
inline void Copy(Environment* env,
995+
Local<Array> ary,
996+
std::vector<std::string>* vec) {
1000997
const int32_t len = ary->Length();
1001998
if (len == 0)
1002999
return; // nothing to copy
@@ -1010,18 +1007,18 @@ static inline void Copy(Environment* env,
10101007
}
10111008
}
10121009

1013-
static inline Local<Array> Copy(Environment* env,
1014-
const std::vector<std::string>& vec) {
1010+
inline Local<Array> Copy(Environment* env,
1011+
const std::vector<std::string>& vec) {
10151012
Isolate* isolate = env->isolate();
10161013
Local<Array> ary = Array::New(isolate, vec.size());
10171014
for (size_t n = 0; n < vec.size(); n++)
10181015
ary->Set(env->context(), n, UTF8STRING(isolate, vec[n])).FromJust();
10191016
return ary;
10201017
}
10211018

1022-
static inline void HarvestBase(Environment* env,
1023-
struct url_data* base,
1024-
Local<Object> base_obj) {
1019+
inline void HarvestBase(Environment* env,
1020+
struct url_data* base,
1021+
Local<Object> base_obj) {
10251022
Local<Context> context = env->context();
10261023
Local<Value> flags = GET(env, base_obj, "flags");
10271024
if (flags->IsInt32())
@@ -1045,9 +1042,9 @@ static inline void HarvestBase(Environment* env,
10451042
}
10461043
}
10471044

1048-
static inline void HarvestContext(Environment* env,
1049-
struct url_data* context,
1050-
Local<Object> context_obj) {
1045+
inline void HarvestContext(Environment* env,
1046+
struct url_data* context,
1047+
Local<Object> context_obj) {
10511048
Local<Value> flags = GET(env, context_obj, "flags");
10521049
if (flags->IsInt32()) {
10531050
int32_t _flags = flags->Int32Value(env->context()).FromJust();
@@ -1090,7 +1087,7 @@ static inline void HarvestContext(Environment* env,
10901087
}
10911088

10921089
// Single dot segment can be ".", "%2e", or "%2E"
1093-
static inline bool IsSingleDotSegment(const std::string& str) {
1090+
inline bool IsSingleDotSegment(const std::string& str) {
10941091
switch (str.size()) {
10951092
case 1:
10961093
return str == ".";
@@ -1106,7 +1103,7 @@ static inline bool IsSingleDotSegment(const std::string& str) {
11061103
// Double dot segment can be:
11071104
// "..", ".%2e", ".%2E", "%2e.", "%2E.",
11081105
// "%2e%2e", "%2E%2E", "%2e%2E", or "%2E%2e"
1109-
static inline bool IsDoubleDotSegment(const std::string& str) {
1106+
inline bool IsDoubleDotSegment(const std::string& str) {
11101107
switch (str.size()) {
11111108
case 2:
11121109
return str == "..";
@@ -1133,13 +1130,15 @@ static inline bool IsDoubleDotSegment(const std::string& str) {
11331130
}
11341131
}
11351132

1136-
static inline void ShortenUrlPath(struct url_data* url) {
1133+
inline void ShortenUrlPath(struct url_data* url) {
11371134
if (url->path.empty()) return;
11381135
if (url->path.size() == 1 && url->scheme == "file:" &&
11391136
IsNormalizedWindowsDriveLetter(url->path[0])) return;
11401137
url->path.pop_back();
11411138
}
11421139

1140+
} // anonymous namespace
1141+
11431142
void URL::Parse(const char* input,
11441143
size_t len,
11451144
enum url_parse_state state_override,

0 commit comments

Comments
 (0)