There is now a variant of this function that return an utf-8 string, simplifying how this mehtod is used. Chromium changes: https://chromium.googlesource.com/chromium/src/+/7020f45881b59d21fdfa505fae7896d6e7ff8f5c https://chromium.googlesource.com/chromium/src/+/f3aa498c5cf8995d340c96861c7e3ecd67a08aa4 https://chromium.googlesource.com/chromium/src/+/cc9e49b56e502928873a876eca6538231c23f49c commit 7020f45881b59d21fdfa505fae7896d6e7ff8f5c Author: Kent Tamura <tkent@chromium.org> Date: Mon Feb 23 20:18:38 2026 -0800 url: Rename DecodeURLMode and DecodeURLEscapeSequences This CL renames DecodeURLMode to DecodeUrlMode, and DecodeURLEscapeSequences() to DecodeUrlEscapeSequences() to follow the Google C++ style guide. The style guide recommends treating acronyms as words in names (e.g., `StartRpc` instead of `StartRPC`). This change applies this convention to the "URL" acronym in these names for consistency across the codebase. Change-Id: I29ac4456e9745f66a2cd9e1d733cacecfe683a88 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7593238 Reviewed-by: Hayato Ito <hayato@chromium.org> Auto-Submit: Kent Tamura <tkent@chromium.org> Commit-Queue: Hayato Ito <hayato@chromium.org> Owners-Override: Hayato Ito <hayato@chromium.org> Cr-Commit-Position: refs/heads/main@{#1589145} commit f3aa498c5cf8995d340c96861c7e3ecd67a08aa4 Author: Kent Tamura <tkent@chromium.org> Date: Tue Feb 24 17:20:59 2026 -0800 url: Add an overload of DecodeUrlEscapeSequences() This CL introduces an overload of DecodeUrlEscapeSequences(), which returns a decoded UTF-8 string. Change-Id: I3349cf8376801831675391228c9c4bd3e5d1a5ef Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7603356 Commit-Queue: Hayato Ito <hayato@chromium.org> Reviewed-by: Hayato Ito <hayato@chromium.org> Auto-Submit: Kent Tamura <tkent@chromium.org> Owners-Override: Hayato Ito <hayato@chromium.org> Cr-Commit-Position: refs/heads/main@{#1589812} commit cc9e49b56e502928873a876eca6538231c23f49c Author: Kent Tamura <tkent@chromium.org> Date: Wed Feb 25 22:50:18 2026 -0800 url: Introduce UrlEscapeDecoder to simplify URL decoding This change introduces url::UrlEscapeDecoder, a helper class that simplifies the process of decoding URL escape sequences. Previously, decoding required manually setting up a RawCanonOutputW buffer and then calling DecodeUrlEscapeSequences(). This pattern was verbose. The new UrlEscapeDecoder encapsulates this logic. It performs the decoding in its constructor. This makes the calling code more concise and readable: url::UrlEscapeDecoder decoder(input, mode); ... use decoder.view() ... This CL refactors several existing call sites to use the new, simpler pattern. Change-Id: I7b3c1538136b537ac33d6f101ca0e011308cc974 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7610180 Commit-Queue: Kent Tamura <tkent@chromium.org> Owners-Override: Hayato Ito <hayato@chromium.org> Auto-Submit: Kent Tamura <tkent@chromium.org> Reviewed-by: Hayato Ito <hayato@chromium.org> Cr-Commit-Position: refs/heads/main@{#1590661}
104 lines
3.6 KiB
C++
104 lines
3.6 KiB
C++
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "brave/components/web_discovery/browser/util.h"
|
|
|
|
#include <utility>
|
|
|
|
#include "base/command_line.h"
|
|
#include "brave/brave_domains/service_domains.h"
|
|
#include "brave/components/web_discovery/common/features.h"
|
|
#include "third_party/abseil-cpp/absl/strings/str_format.h"
|
|
#include "url/url_util.h"
|
|
|
|
namespace web_discovery {
|
|
|
|
namespace {
|
|
constexpr char kCollectorHostPrefix[] = "collector.wdp";
|
|
constexpr char kQuorumHostPrefix[] = "quorum.wdp";
|
|
constexpr char kPatternsHostPrefix[] = "patterns.wdp";
|
|
constexpr char kPatternsPath[] = "patterns.gz";
|
|
} // namespace
|
|
|
|
std::string GetDirectHPNHost() {
|
|
// TODO(djandries): Replace with non-proxied endpoint once available
|
|
return GetAnonymousHPNHost();
|
|
}
|
|
|
|
std::string GetAnonymousHPNHost() {
|
|
auto* cmd_line = base::CommandLine::ForCurrentProcess();
|
|
if (cmd_line->HasSwitch(kCollectorHostSwitch)) {
|
|
return cmd_line->GetSwitchValueASCII(kCollectorHostSwitch);
|
|
}
|
|
return base::StrCat({url::kHttpsScheme, url::kStandardSchemeSeparator,
|
|
brave_domains::GetServicesDomain(kCollectorHostPrefix)});
|
|
}
|
|
|
|
std::string GetQuorumHost() {
|
|
return base::StrCat({url::kHttpsScheme, url::kStandardSchemeSeparator,
|
|
brave_domains::GetServicesDomain(kQuorumHostPrefix)});
|
|
}
|
|
|
|
GURL GetPatternsEndpoint() {
|
|
auto* cmd_line = base::CommandLine::ForCurrentProcess();
|
|
std::string patterns_url_str;
|
|
if (cmd_line->HasSwitch(kPatternsURLSwitch)) {
|
|
patterns_url_str = cmd_line->GetSwitchValueASCII(kPatternsURLSwitch);
|
|
} else {
|
|
auto patterns_path = features::kPatternsPath.Get();
|
|
if (patterns_path.empty()) {
|
|
patterns_path = kPatternsPath;
|
|
}
|
|
patterns_url_str =
|
|
base::StrCat({url::kHttpsScheme, url::kStandardSchemeSeparator,
|
|
brave_domains::GetServicesDomain(kPatternsHostPrefix),
|
|
"/", patterns_path});
|
|
}
|
|
|
|
return GURL(patterns_url_str);
|
|
}
|
|
|
|
std::unique_ptr<network::ResourceRequest> CreateResourceRequest(GURL url) {
|
|
auto resource_request = std::make_unique<network::ResourceRequest>();
|
|
resource_request->url = std::move(url);
|
|
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
|
|
return resource_request;
|
|
}
|
|
|
|
std::string FormatServerDate(const base::Time& date) {
|
|
base::Time::Exploded exploded;
|
|
date.UTCExplode(&exploded);
|
|
return absl::StrFormat("%04d%02d%02d", exploded.year, exploded.month,
|
|
exploded.day_of_month);
|
|
}
|
|
|
|
std::string DecodeURLComponent(const std::string_view value) {
|
|
return url::DecodeUrlEscapeSequences(value,
|
|
url::DecodeUrlMode::kUtf8OrIsomorphic);
|
|
}
|
|
|
|
std::optional<std::string> ExtractValueFromQueryString(
|
|
const std::string_view query_string,
|
|
const std::string_view key) {
|
|
url::Component query_slice(0, query_string.length());
|
|
url::Component key_slice;
|
|
url::Component value_slice;
|
|
while (url::ExtractQueryKeyValue(query_string, &query_slice, &key_slice,
|
|
&value_slice)) {
|
|
if (query_string.substr(key_slice.begin, key_slice.len) != key) {
|
|
continue;
|
|
}
|
|
return DecodeURLComponent(
|
|
query_string.substr(value_slice.begin, value_slice.len));
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
void TransformToAlphanumeric(std::string& str) {
|
|
std::erase_if(str, [](char c) { return !std::isalnum(c); });
|
|
}
|
|
|
|
} // namespace web_discovery
|