Skip to content
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

feat: add url host type attribute #480

Merged
merged 2 commits into from
Aug 25, 2023
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
24 changes: 24 additions & 0 deletions include/ada/url_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@

namespace ada {

/**
* Type of URL host as an enum.
*/
enum url_host_type : uint8_t {
/**
* Represents common URLs such as "https://www.google.com"
*/
DEFAULT = 0,
/**
* Represents ipv4 addresses such as "http://127.0.0.1"
*/
IPV4 = 1,
/**
* Represents ipv6 addresses such as
* "http://[2001:db8:3333:4444:5555:6666:7777:8888]"
*/
IPV6 = 2,
};

/**
* @brief Base class of URL implementations
*
Expand All @@ -35,6 +54,11 @@ struct url_base {
*/
bool has_opaque_path{false};

/**
* URL hosts type
*/
url_host_type host_type = url_host_type::DEFAULT;

/**
* @private
*/
Expand Down
1 change: 1 addition & 0 deletions include/ada_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ ada_string ada_get_hostname(ada_url result);
ada_string ada_get_pathname(ada_url result);
ada_string ada_get_search(ada_url result);
ada_string ada_get_protocol(ada_url result);
uint8_t ada_get_url_host_type(ada_url result);

// url_aggregator setters
// if ada_is_valid(result)) is false, the setters have no effect
Expand Down
8 changes: 8 additions & 0 deletions src/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ ada_string ada_get_protocol(ada_url result) noexcept {
return ada_string_create(out.data(), out.length());
}

uint8_t ada_get_url_host_type(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (!r) {
return 0;
}
return r->host_type;
}

bool ada_set_href(ada_url result, const char* input, size_t length) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (!r) {
Expand Down
2 changes: 2 additions & 0 deletions src/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ bool url::parse_ipv4(std::string_view input) {
} else {
host = ada::serializers::ipv4(ipv4); // We have to reserialize the address.
}
host_type = IPV4;
return true;
}

Expand Down Expand Up @@ -322,6 +323,7 @@ bool url::parse_ipv6(std::string_view input) {
}
host = ada::serializers::ipv6(address);
ada_log("parse_ipv6 ", *host);
host_type = IPV6;
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ bool url_aggregator::parse_ipv4(std::string_view input) {
update_base_hostname(
ada::serializers::ipv4(ipv4)); // We have to reserialize the address.
}
host_type = IPV4;
ADA_ASSERT_TRUE(validate());
return true;
}
Expand Down Expand Up @@ -1170,6 +1171,7 @@ bool url_aggregator::parse_ipv6(std::string_view input) {
update_base_hostname(ada::serializers::ipv6(address));
ada_log("parse_ipv6 ", get_hostname());
ADA_ASSERT_TRUE(validate());
host_type = IPV6;
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ TEST(ada_c, setters) {
ada_set_protocol(url, "wss", 3);
ASSERT_EQ(convert_string(ada_get_protocol(url)), "wss:");

ASSERT_EQ(ada_get_url_host_type(url), 0);

ada_free(url);

SUCCEED();
Expand Down
14 changes: 13 additions & 1 deletion tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,16 @@ TYPED_TEST(basic_tests, node_issue_48254) {
auto url = ada::parse<TypeParam>("", &*base_url);
ASSERT_FALSE(url);
SUCCEED();
}
}

TYPED_TEST(basic_tests, url_host_type) {
ASSERT_EQ(ada::parse<TypeParam>("http://localhost:3000")->host_type,
ada::url_host_type::DEFAULT);
ASSERT_EQ(ada::parse<TypeParam>("http://0.0.0.0")->host_type,
ada::url_host_type::IPV4);
ASSERT_EQ(
ada::parse<TypeParam>("http://[2001:db8:3333:4444:5555:6666:7777:8888]")
->host_type,
ada::url_host_type::IPV6);
SUCCEED();
}
Loading