srctree

Robin Linden parent f9db1cf1 1e9921a0
uri: Normalize the casing of the scheme and host

inlinesplit
uri/BUILD added: 23, removed: 3, total 20
@@ -5,6 +5,7 @@ cc_library(
srcs = ["uri.cpp"],
hdrs = ["uri.h"],
visibility = ["//visibility:public"],
deps = ["//util:string"],
)
 
cc_test(
 
uri/uri.cpp added: 23, removed: 3, total 20
@@ -5,6 +5,8 @@
 
#include "uri/uri.h"
 
#include "util/string.h"
 
#include <regex>
#include <utility>
 
@@ -13,6 +15,11 @@ namespace {
 
// https://en.wikipedia.org/wiki/URI_normalization#Normalization_process
void normalize(Uri &uri) {
// The scheme and host components of the URI are case-insensitive and
// therefore should be normalized to lowercase.
uri.scheme = util::to_lower(std::move(uri.scheme));
uri.authority.host = util::to_lower(std::move(uri.authority.host));
 
// 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/uri_test.cpp added: 23, removed: 3, total 20
@@ -146,6 +146,18 @@ int main() {
.path = "/hello/there.html"});
});
 
etest::test("normalization, lowercasing scheme+host", [] {
auto actual = *Uri::parse("HTTPS://EXAMPLE.COM/");
Uri expected{
.uri = "HTTPS://EXAMPLE.COM/",
.scheme = "https",
.authority = {.host = "example.com"},
.path = "/",
};
 
expect_eq(actual, expected);
});
 
// TODO(Zer0-One): Test for parsing failure.
// etest::test("parse failure", [] {
// auto tel_uri = Uri::parse("");