Skip to content
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
42 changes: 38 additions & 4 deletions src/core/uri/filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

#include "escaping.h"

#include <algorithm> // std::ranges::replace
#include <filesystem> // std::filesystem
#include <iterator> // std::advance, std::next
#include <string> // std::string
#include <algorithm> // std::ranges::equal, std::ranges::replace
#include <cctype> // std::tolower
#include <filesystem> // std::filesystem
#include <iterator> // std::advance, std::next
#include <string> // std::string
#include <string_view> // std::string_view

namespace {

auto is_localhost_host(const std::string_view host) -> bool {
constexpr std::string_view localhost{"localhost"};
return std::ranges::equal(
host, localhost, [](const char left, const char right) {
return std::tolower(static_cast<unsigned char>(left)) == right;
});
}

} // namespace

namespace sourcemeta::core {

Expand All @@ -17,6 +31,26 @@ auto URI::to_path() const -> std::filesystem::path {
return path;
}

// RFC 8089: a non-empty, non-localhost host on a file URI denotes a UNC
// server. The "localhost" host is equivalent to no host
const auto host_value = this->host();
const auto is_unc = host_value.has_value() && !host_value->empty() &&
!is_localhost_host(host_value.value());
if (is_unc) {
if (!path.empty() && path.front() == '/') {
path.erase(0, 1);
}
std::ranges::replace(path, '/', '\\');
uri_unescape_all_inplace(path);
std::string unc{"\\\\"};
unc.append(host_value.value());
if (!path.empty()) {
unc.push_back('\\');
unc.append(path);
}
return unc;
}

// Check for Windows absolute path (e.g., /C:/)
const auto is_windows_absolute =
path.size() >= 3 && path[0] == '/' && path[2] == ':';
Expand Down
42 changes: 42 additions & 0 deletions test/uri/uri_to_path_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,45 @@ TEST(URI_to_path, unicode_windows) {
const std::filesystem::path expected{u8R"(C:\data\résumé.doc)"};
EXPECT_EQ(uri.to_path(), expected);
}

TEST(URI_to_path, windows_unc_simple) {
const sourcemeta::core::URI uri{"file://server/share/file.txt"};
const std::filesystem::path expected{R"(\\server\share\file.txt)"};
EXPECT_EQ(uri.to_path(), expected);
}

TEST(URI_to_path, windows_unc_with_space) {
const sourcemeta::core::URI uri{"file://srv/My%20Docs/a%20b.txt"};
const std::filesystem::path expected{R"(\\srv\My Docs\a b.txt)"};
EXPECT_EQ(uri.to_path(), expected);
}

TEST(URI_to_path, windows_unc_unicode) {
const sourcemeta::core::URI uri{"file://server/data/%C3%A9clair.txt"};
const std::filesystem::path expected{u8R"(\\server\data\éclair.txt)"};
EXPECT_EQ(uri.to_path(), expected);
}

TEST(URI_to_path, localhost_treated_as_no_host) {
const sourcemeta::core::URI uri{"file://localhost/foo/bar"};
const std::filesystem::path expected{"/foo/bar"};
EXPECT_EQ(uri.to_path(), expected);
}

TEST(URI_to_path, localhost_with_windows_drive) {
const sourcemeta::core::URI uri{"file://localhost/C:/foo"};
const std::filesystem::path expected{R"(C:\foo)"};
EXPECT_EQ(uri.to_path(), expected);
}

TEST(URI_to_path, localhost_uppercase) {
const sourcemeta::core::URI uri{"file://LOCALHOST/foo/bar"};
const std::filesystem::path expected{"/foo/bar"};
EXPECT_EQ(uri.to_path(), expected);
}

TEST(URI_to_path, localhost_mixed_case) {
const sourcemeta::core::URI uri{"file://LocalHost/foo/bar"};
const std::filesystem::path expected{"/foo/bar"};
EXPECT_EQ(uri.to_path(), expected);
}
Loading