Skip to content

Commit

Permalink
docs + fix bug with zero expansion on boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Jan 24, 2025
1 parent 86c67e1 commit e42d572
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion source/host_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static struct aws_byte_cursor s_percent_uri_enc = AWS_BYTE_CUR_INIT_FROM_STRING_
* ipv6 can be embedded in url, in which case % must be uri encoded as %25.
* Implementation is fairly trivial and just iterates through the string
* keeping track of the spec above.
* Note: there is no single rfc for IPv6 address - base format defined in RFC 5952,
* zoneId and uri extensions defined in RFC 6874 and RFC 3986
*/
bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
if (host.len == 0) {
Expand Down Expand Up @@ -99,13 +101,15 @@ bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
return false;
}
has_double_colon = true;
--group_count; /* avoid double counting groups */
}
} else {
++digit_count;
}

if (digit_count > 4 || /* too many digits in group */
group_count > 8) { /* too many groups */
AWS_LOGF_DEBUG(0, "here %d", group_count);
return false;
}
}
Expand All @@ -121,5 +125,5 @@ bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
}
}

return has_double_colon ? group_count <= 7 : group_count == 8;
return has_double_colon ? group_count <= 8 : group_count == 8;
}
3 changes: 2 additions & 1 deletion tests/host_util_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ static int s_test_is_ipv6(struct aws_allocator *allocator, void *ctx) {
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001::"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("1:2:3:4:5:6:7::"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::1:2:3:4:5:6:7"), false));

ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:DB8:85A3::8A2E:370:7334"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), false));
Expand All @@ -71,7 +73,6 @@ static int s_test_is_ipv6(struct aws_allocator *allocator, void *ctx) {
ASSERT_FALSE(
aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("z001:0db8:0000:0000:0000:8a2e:0370:7334:8745"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("z001::8a2e::8745"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::2001:0db8:0000:0000:8a2e:0370:7334"), false));

ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%24en0"), true));
Expand Down

0 comments on commit e42d572

Please sign in to comment.