1
+ #include < charconv>
1
2
#include < cctype>
2
3
#include < iostream>
3
4
#include < optional>
5
+ #include < string>
6
+ #include < string_view>
4
7
5
8
// TODO: remove these once the warnings are fixed
6
9
#pragma clang diagnostic push
@@ -23,7 +26,7 @@ bool isNotAlphaNumericDotOrDash(char character) {
23
26
return !std::isalnum (character) && !isDash (character) && !isDot (character);
24
27
}
25
28
26
- bool isValidHost (std::string host) {
29
+ bool isValidHost (std::string_view host) {
27
30
// ValidHostRegex =
28
31
// "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])(:[0-9]+)$";
29
32
auto firstCharacter = host.front ();
@@ -33,7 +36,7 @@ bool isValidHost(std::string host) {
33
36
}
34
37
// split the hostname from the port
35
38
int pos = host.find_first_of (' :' );
36
- std::string hostname = host.substr (0 , pos);
39
+ std::string_view hostname = host.substr (0 , pos);
37
40
38
41
auto lastCharacter = hostname.back ();
39
42
// check last character is in the regex [a-zA-Z0-9]
@@ -67,11 +70,15 @@ bool isValidHost(std::string host) {
67
70
}
68
71
// if there is a port - confirm it is all digits and is between 0 and 65536
69
72
if (pos != std::string::npos) {
70
- std::string port = host.substr (pos + 1 );
73
+ std::string_view port = host.substr (pos + 1 );
71
74
if (!std::all_of (port.begin (), port.end (), ::isdigit)) {
72
75
return false ;
73
76
}
74
- auto value = std::stoi (port);
77
+ int value;
78
+ const std::from_chars_result result = std::from_chars (port.data (), port.data () + port.size (), value);
79
+ if (result.ec == std::errc::invalid_argument || result.ec == std::errc::result_out_of_range) {
80
+ return false ;
81
+ }
75
82
if (value == 0 || value >= 65536 ) {
76
83
return false ;
77
84
}
@@ -302,7 +309,7 @@ bool Backend::set_target(JSContext *cx, JSObject *backend, JS::HandleValue targe
302
309
return false ;
303
310
}
304
311
305
- std::string targetString ((char *)targetStringSlice.data , targetStringSlice.len );
312
+ std::string_view targetString ((char *)targetStringSlice.data , targetStringSlice.len );
306
313
auto length = targetString.length ();
307
314
if (length == 0 ) {
308
315
JS_ReportErrorNumberASCII (cx, GetErrorMessage, nullptr , JSMSG_BACKEND_TARGET_EMPTY);
@@ -315,7 +322,7 @@ bool Backend::set_target(JSContext *cx, JSObject *backend, JS::HandleValue targe
315
322
return false ;
316
323
}
317
324
318
- auto targetStr = JS_NewStringCopyN (cx, targetString.c_str (), targetString.length ());
325
+ auto targetStr = JS_NewStringCopyN (cx, targetString.data (), targetString.length ());
319
326
if (!targetStr) {
320
327
return false ;
321
328
}
@@ -358,7 +365,7 @@ JSObject *Backend::create(JSContext *cx, JS::HandleObject request) {
358
365
return nullptr ;
359
366
}
360
367
const jsurl::SpecString origin_specstring = jsurl::origin (url);
361
- std::string origin ((char *)origin_specstring.data , origin_specstring.len );
368
+ std::string_view origin ((char *)origin_specstring.data , origin_specstring.len );
362
369
363
370
auto use_ssl = origin.rfind (" https://" , 0 ) == 0 ;
364
371
JS::SetReservedSlot (backend, Backend::Slots::UseSsl, JS::BooleanValue (use_ssl));
0 commit comments