srctree

Robin Linden parent f7ea2269 92f16d2c
uri: Fix path sometimes being empty when it shouldn't be

inlinesplit
uri/uri.cpp added: 24, removed: 3, total 21
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2021 David Zero <zero-one@zer0-one.net>
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -8,6 +9,18 @@
#include <utility>
 
namespace uri {
namespace {
 
// https://en.wikipedia.org/wiki/URI_normalization#Normalization_process
void normalize(Uri &uri) {
// In presence of an authority component, an empty path component should be
// normalized to a path component of "/".
if (!uri.authority.empty() && uri.path.empty()) {
uri.path = "/";
}
}
 
} // namespace
 
std::optional<Uri> Uri::parse(std::string uristr) {
std::smatch match;
@@ -53,6 +66,9 @@ std::optional<Uri> Uri::parse(std::string uristr) {
.fragment{match.str(9)},
};
uri.uri = std::move(uristr);
 
normalize(uri);
 
return uri;
}
 
 
uri/uri.h added: 24, removed: 3, total 21
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2021 David Zero <zero-one@zer0-one.net>
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -16,6 +17,8 @@ struct Authority {
std::string host;
std::string port;
 
[[nodiscard]] bool empty() const { return user.empty() && passwd.empty() && host.empty() && port.empty(); }
 
[[nodiscard]] bool operator==(Authority const &) const = default;
};
 
 
uri/uri_test.cpp added: 24, removed: 3, total 21
@@ -18,6 +18,7 @@ int main() {
.uri = "https://example.com",
.scheme = "https",
.authority = {.host = "example.com"},
.path = "/",
};
 
expect(uri == expected);
@@ -29,6 +30,7 @@ int main() {
.uri = "https://gr.ht",
.scheme = "https",
.authority = {.host = "gr.ht"},
.path = "/",
};
 
expect(uri == expected);