Skip to content

Refactored part of the parser implementation #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# http://www.boost.org/LICENSE_1_0.txt)


cmake_minimum_required(VERSION 3.17)
cmake_minimum_required(VERSION 3.16)

project(
skyr-url
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int main() {

std::cout << "Domain? " << std::boolalpha << url.is_domain() << std::endl;
std::cout << "Domain: " << url.hostname() << std::endl;
std::cout << "Domain: " << url.u8domain() << std::endl;
std::cout << "Domain: " << url.u8domain().value() << std::endl;

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

Expand All @@ -161,6 +161,8 @@ Here is the ``CMake`` script to build the example:
```cmake
# CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(my_project)

find_package(tl-expected CONFIG REQUIRED)
Expand Down
35 changes: 19 additions & 16 deletions include/skyr/v2/core/url_parser_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,35 +520,30 @@ class url_parser_context {

auto parse_file(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
set_file_scheme();
set_empty_host();

if ((byte == '/') || (byte == '\\')) {
if (byte == '\\') {
*validation_error |= true;
}
state = url_parse_state::file_slash;
} else if (base && (base->scheme == "file")) {
if (is_eof()) {
set_host_from_base();
set_path_from_base();
set_query_from_base();
} else if (byte == '?') {
set_host_from_base();
set_path_from_base();
set_host_from_base();
set_path_from_base();
set_query_from_base();
if (byte == '?') {
set_empty_query();
state = url_parse_state::query;
} else if (byte == '#') {
set_host_from_base();
set_path_from_base();
set_query_from_base();
set_empty_fragment();
state = url_parse_state::fragment;
} else {
clear_query();
if (!details::is_windows_drive_letter(still_to_process())) {
set_host_from_base();
set_path_from_base();
details::shorten_path(url.scheme, url.path);
} else {
*validation_error |= true;
clear_path();
}
state = url_parse_state::path;
if (input_it == std::begin(input)) {
Expand All @@ -574,11 +569,11 @@ class url_parser_context {
}
state = url_parse_state::file_host;
} else {
if (base && ((base->scheme == "file") && !details::is_windows_drive_letter(still_to_process()))) {
if (!base->path.empty() && details::is_windows_drive_letter(base->path[0])) {
if (base && (base->scheme == "file")) {
set_host_from_base();
if (!details::is_windows_drive_letter(still_to_process()) &&
(!base->path.empty() && details::is_windows_drive_letter(base->path[0]))) {
set_path_from_base0();
} else {
set_host_from_base();
}
}

Expand Down Expand Up @@ -849,6 +844,10 @@ class url_parser_context {
url.cannot_be_a_base_url = true;
}

void clear_path() {
url.path.clear();
}

void add_empty_path_element() {
url.path.emplace_back();
}
Expand Down Expand Up @@ -877,6 +876,10 @@ class url_parser_context {
url.path[0] += pct_encoded.to_string();
}

void clear_query() {
url.query->clear();
}

void set_empty_query() {
url.query = std::string();
}
Expand Down
28 changes: 12 additions & 16 deletions src/v1/core/url_parser_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,35 +465,30 @@ auto url_parser_context::parse_port(char byte) -> tl::expected<url_parse_action,

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

if ((byte == '/') || (byte == '\\')) {
if (byte == '\\') {
*validation_error |= true;
}
state = url_parse_state::file_slash;
} else if (base && (base->scheme == "file")) {
if (is_eof()) {
url.host = base->host;
url.path = base->path;
url.query = base->query;
} else if (byte == '?') {
url.host = base->host;
url.path = base->path;
url.host = base->host;
url.path = base->path;
url.query = base->query;
if (byte == '?') {
url.query = std::string();
state = url_parse_state::query;
} else if (byte == '#') {
url.host = base->host;
url.path = base->path;
url.query = base->query;
url.fragment = std::string();
state = url_parse_state::fragment;
} else {
url.query = std::nullopt;
if (!is_windows_drive_letter(input.substr(std::distance(begin(input), it)))) {
url.host = base->host;
url.path = base->path;
shorten_path(url.scheme, url.path);
} else {
*validation_error |= true;
url.path.clear();
}
state = url_parse_state::path;
if (it == begin(input)) {
Expand All @@ -519,10 +514,11 @@ auto url_parser_context::parse_file_slash(char byte) -> tl::expected<url_parse_a
}
state = url_parse_state::file_host;
} else {
auto substr = input.substr(std::distance(begin(input), it));
if (base && ((base->scheme == "file") && !is_windows_drive_letter(substr))) {
if (!base->path.empty() && is_windows_drive_letter(base->path[0])) {
url.path.push_back(base->path[0]);
if (base && (base->scheme == "file")) {
auto substr = input.substr(std::distance(begin(input), it));
if (!is_windows_drive_letter(substr) &&
(!base->path.empty() && is_windows_drive_letter(base->path[0]))) {
url.path.push_back(base->path[0]);
} else {
url.host = base->host;
}
Expand Down