Skip to content

Commit 6805f55

Browse files
authored
Refactored prt of the parser implementation (#159)
1 parent 281501b commit 6805f55

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# http://www.boost.org/LICENSE_1_0.txt)
55

66

7-
cmake_minimum_required(VERSION 3.17)
7+
cmake_minimum_required(VERSION 3.16)
88

99
project(
1010
skyr-url

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ int main() {
139139

140140
std::cout << "Domain? " << std::boolalpha << url.is_domain() << std::endl;
141141
std::cout << "Domain: " << url.hostname() << std::endl;
142-
std::cout << "Domain: " << url.u8domain() << std::endl;
142+
std::cout << "Domain: " << url.u8domain().value() << std::endl;
143143

144144
std::cout << "Port: " << url.port<std::uint16_t>().value() << std::endl;
145145

@@ -161,6 +161,8 @@ Here is the ``CMake`` script to build the example:
161161
```cmake
162162
# CMakeLists.txt
163163
164+
cmake_minimum_required(VERSION 3.16)
165+
164166
project(my_project)
165167
166168
find_package(tl-expected CONFIG REQUIRED)

include/skyr/v2/core/url_parser_context.hpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -520,35 +520,30 @@ class url_parser_context {
520520

521521
auto parse_file(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
522522
set_file_scheme();
523+
set_empty_host();
523524

524525
if ((byte == '/') || (byte == '\\')) {
525526
if (byte == '\\') {
526527
*validation_error |= true;
527528
}
528529
state = url_parse_state::file_slash;
529530
} else if (base && (base->scheme == "file")) {
530-
if (is_eof()) {
531-
set_host_from_base();
532-
set_path_from_base();
533-
set_query_from_base();
534-
} else if (byte == '?') {
535-
set_host_from_base();
536-
set_path_from_base();
531+
set_host_from_base();
532+
set_path_from_base();
533+
set_query_from_base();
534+
if (byte == '?') {
537535
set_empty_query();
538536
state = url_parse_state::query;
539537
} else if (byte == '#') {
540-
set_host_from_base();
541-
set_path_from_base();
542-
set_query_from_base();
543538
set_empty_fragment();
544539
state = url_parse_state::fragment;
545540
} else {
541+
clear_query();
546542
if (!details::is_windows_drive_letter(still_to_process())) {
547-
set_host_from_base();
548-
set_path_from_base();
549543
details::shorten_path(url.scheme, url.path);
550544
} else {
551545
*validation_error |= true;
546+
clear_path();
552547
}
553548
state = url_parse_state::path;
554549
if (input_it == std::begin(input)) {
@@ -574,11 +569,11 @@ class url_parser_context {
574569
}
575570
state = url_parse_state::file_host;
576571
} else {
577-
if (base && ((base->scheme == "file") && !details::is_windows_drive_letter(still_to_process()))) {
578-
if (!base->path.empty() && details::is_windows_drive_letter(base->path[0])) {
572+
if (base && (base->scheme == "file")) {
573+
set_host_from_base();
574+
if (!details::is_windows_drive_letter(still_to_process()) &&
575+
(!base->path.empty() && details::is_windows_drive_letter(base->path[0]))) {
579576
set_path_from_base0();
580-
} else {
581-
set_host_from_base();
582577
}
583578
}
584579

@@ -849,6 +844,10 @@ class url_parser_context {
849844
url.cannot_be_a_base_url = true;
850845
}
851846

847+
void clear_path() {
848+
url.path.clear();
849+
}
850+
852851
void add_empty_path_element() {
853852
url.path.emplace_back();
854853
}
@@ -877,6 +876,10 @@ class url_parser_context {
877876
url.path[0] += pct_encoded.to_string();
878877
}
879878

879+
void clear_query() {
880+
url.query->clear();
881+
}
882+
880883
void set_empty_query() {
881884
url.query = std::string();
882885
}

src/v1/core/url_parser_context.cpp

+12-16
Original file line numberDiff line numberDiff line change
@@ -465,35 +465,30 @@ auto url_parser_context::parse_port(char byte) -> tl::expected<url_parse_action,
465465

466466
auto url_parser_context::parse_file(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
467467
url.scheme = "file";
468+
url.host = host{empty_host{}};
468469

469470
if ((byte == '/') || (byte == '\\')) {
470471
if (byte == '\\') {
471472
*validation_error |= true;
472473
}
473474
state = url_parse_state::file_slash;
474475
} else if (base && (base->scheme == "file")) {
475-
if (is_eof()) {
476-
url.host = base->host;
477-
url.path = base->path;
478-
url.query = base->query;
479-
} else if (byte == '?') {
480-
url.host = base->host;
481-
url.path = base->path;
476+
url.host = base->host;
477+
url.path = base->path;
478+
url.query = base->query;
479+
if (byte == '?') {
482480
url.query = std::string();
483481
state = url_parse_state::query;
484482
} else if (byte == '#') {
485-
url.host = base->host;
486-
url.path = base->path;
487-
url.query = base->query;
488483
url.fragment = std::string();
489484
state = url_parse_state::fragment;
490485
} else {
486+
url.query = std::nullopt;
491487
if (!is_windows_drive_letter(input.substr(std::distance(begin(input), it)))) {
492-
url.host = base->host;
493-
url.path = base->path;
494488
shorten_path(url.scheme, url.path);
495489
} else {
496490
*validation_error |= true;
491+
url.path.clear();
497492
}
498493
state = url_parse_state::path;
499494
if (it == begin(input)) {
@@ -519,10 +514,11 @@ auto url_parser_context::parse_file_slash(char byte) -> tl::expected<url_parse_a
519514
}
520515
state = url_parse_state::file_host;
521516
} else {
522-
auto substr = input.substr(std::distance(begin(input), it));
523-
if (base && ((base->scheme == "file") && !is_windows_drive_letter(substr))) {
524-
if (!base->path.empty() && is_windows_drive_letter(base->path[0])) {
525-
url.path.push_back(base->path[0]);
517+
if (base && (base->scheme == "file")) {
518+
auto substr = input.substr(std::distance(begin(input), it));
519+
if (!is_windows_drive_letter(substr) &&
520+
(!base->path.empty() && is_windows_drive_letter(base->path[0]))) {
521+
url.path.push_back(base->path[0]);
526522
} else {
527523
url.host = base->host;
528524
}

0 commit comments

Comments
 (0)