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

Fix IPv6 handling when converting file URL to UNC path #64

Merged
merged 1 commit into from
Aug 22, 2024
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
12 changes: 10 additions & 2 deletions include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <cassert>
#include <cstddef>
#include <cstdint> // uint8_t
#include <iterator> // std::next
#include <iterator>
#include <string>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -3264,7 +3264,15 @@ inline std::string path_from_file_url(const url& file_url, file_path_format form
throw url_error(validation_errc::file_url_unsupported_host, "UNC path cannot have \".\" hostname");
// UNC path
path.append("\\\\");
path.append(hostname);
if (file_url.host_type() == HostType::IPv6) {
// Form an IPV6 address host-name by substituting hyphens for the colons and appending ".ipv6-literal.net"
// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/62e862f4-2a51-452e-8eeb-dc4ff5ee33cc
std::replace_copy(std::next(hostname.begin()), std::prev(hostname.end()),
std::back_inserter(path), ':', '-');
path.append(".ipv6-literal.net");
} else {
path.append(hostname);
}
}

// percent decode pathname and normalize slashes
Expand Down
3 changes: 3 additions & 0 deletions test/test-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ TEST_CASE("path_from_file_url") {
CHECK(path_from_file_url("file://host/path", upa::file_path_format::windows) == "\\\\host\\path");
CHECK(path_from_file_url("file:////host/path", upa::file_path_format::windows) == "\\\\host\\path");
CHECK(path_from_file_url("file://///host/path", upa::file_path_format::windows) == "\\\\host\\path");
// UNC: IPv4 and IPv6 hostnames
CHECK(path_from_file_url("file://127.0.0.1/path", upa::file_path_format::windows) == "\\\\127.0.0.1\\path");
CHECK(path_from_file_url("file://[::1]/path", upa::file_path_format::windows) == "\\\\--1.ipv6-literal.net\\path");
// Invalid UNC
CHECK_THROWS_AS(path_from_file_url("file://host", upa::file_path_format::windows), upa::url_error);
CHECK_THROWS_AS(path_from_file_url("file://host/", upa::file_path_format::windows), upa::url_error);
Expand Down
Loading