Skip to content

Commit c998e47

Browse files
committed
feat: add url host type attribute
1 parent b2be6e4 commit c998e47

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed

include/ada/url_base.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
namespace ada {
1515

16+
/**
17+
* Type of URL host as an enum.
18+
*/
19+
enum url_host_type : uint8_t {
20+
DEFAULT = 0,
21+
IPV4 = 1,
22+
IPV6 = 2,
23+
};
24+
1625
/**
1726
* @brief Base class of URL implementations
1827
*
@@ -35,6 +44,11 @@ struct url_base {
3544
*/
3645
bool has_opaque_path{false};
3746

47+
/**
48+
* URL hosts type
49+
*/
50+
url_host_type host_type = url_host_type::DEFAULT;
51+
3852
/**
3953
* @private
4054
*/

include/ada_c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ ada_string ada_get_hostname(ada_url result);
6767
ada_string ada_get_pathname(ada_url result);
6868
ada_string ada_get_search(ada_url result);
6969
ada_string ada_get_protocol(ada_url result);
70+
uint8_t ada_get_url_host_type(ada_url result);
7071

7172
// url_aggregator setters
7273
// if ada_is_valid(result)) is false, the setters have no effect

src/ada_c.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ ada_string ada_get_protocol(ada_url result) noexcept {
205205
return ada_string_create(out.data(), out.length());
206206
}
207207

208+
uint8_t ada_get_url_host_type(ada_url result) noexcept {
209+
ada::result<ada::url_aggregator>& r = get_instance(result);
210+
if (!r) {
211+
return 0;
212+
}
213+
return r->host_type;
214+
}
215+
208216
bool ada_set_href(ada_url result, const char* input, size_t length) noexcept {
209217
ada::result<ada::url_aggregator>& r = get_instance(result);
210218
if (!r) {

src/url.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ bool url::parse_ipv4(std::string_view input) {
9393
} else {
9494
host = ada::serializers::ipv4(ipv4); // We have to reserialize the address.
9595
}
96+
host_type = IPV4;
9697
return true;
9798
}
9899

@@ -322,6 +323,7 @@ bool url::parse_ipv6(std::string_view input) {
322323
}
323324
host = ada::serializers::ipv6(address);
324325
ada_log("parse_ipv6 ", *host);
326+
host_type = IPV6;
325327
return true;
326328
}
327329

src/url_aggregator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ bool url_aggregator::parse_ipv4(std::string_view input) {
935935
update_base_hostname(
936936
ada::serializers::ipv4(ipv4)); // We have to reserialize the address.
937937
}
938+
host_type = IPV4;
938939
ADA_ASSERT_TRUE(validate());
939940
return true;
940941
}
@@ -1170,6 +1171,7 @@ bool url_aggregator::parse_ipv6(std::string_view input) {
11701171
update_base_hostname(ada::serializers::ipv6(address));
11711172
ada_log("parse_ipv6 ", get_hostname());
11721173
ADA_ASSERT_TRUE(validate());
1174+
host_type = IPV6;
11731175
return true;
11741176
}
11751177

tests/ada_c.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ TEST(ada_c, setters) {
106106
ada_set_protocol(url, "wss", 3);
107107
ASSERT_EQ(convert_string(ada_get_protocol(url)), "wss:");
108108

109+
ASSERT_EQ(ada_get_url_host_type(url), 0);
110+
109111
ada_free(url);
110112

111113
SUCCEED();

tests/basic_tests.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,16 @@ TYPED_TEST(basic_tests, node_issue_48254) {
365365
auto url = ada::parse<TypeParam>("", &*base_url);
366366
ASSERT_FALSE(url);
367367
SUCCEED();
368-
}
368+
}
369+
370+
TYPED_TEST(basic_tests, url_host_type) {
371+
ASSERT_EQ(ada::parse<TypeParam>("http://localhost:3000")->host_type,
372+
ada::url_host_type::DEFAULT);
373+
ASSERT_EQ(ada::parse<TypeParam>("http://0.0.0.0")->host_type,
374+
ada::url_host_type::IPV4);
375+
ASSERT_EQ(
376+
ada::parse<TypeParam>("http://[2001:db8:3333:4444:5555:6666:7777:8888]")
377+
->host_type,
378+
ada::url_host_type::IPV6);
379+
SUCCEED();
380+
}

0 commit comments

Comments
 (0)