blob: bea43ffbca4fcc27e1dd17356168c28b010d8789 [file] [log] [blame]
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/proxy_config/proxy_prefs_utils.h"
#include "net/base/proxy_string_util.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
namespace proxy_config {
url::SchemeHostPort ProxyOverrideRuleHostFromString(
std::string_view raw_value) {
GURL url(raw_value);
std::string scheme = url.GetScheme();
std::string host = url.GetHost();
int port = url.IntPort();
// If the value used to initialize `url` is missing parts (for example because
// it's only a hostname), then try to specify a replacement scheme and/or
// port. This gives flexibility to the policy field to only specify certain
// values.
if (host.empty()) {
// This path is reached when only the host is provided (ex. "foo.com").
// In this case, `url` will be invalid and see "foo.com" as a scheme, so if
// that happens the string is instead parsed as a host:port pair, with HTTP
// as the default scheme.
scheme = url::kHttpScheme;
if (!net::ParseHostAndPort(raw_value, &host, &port)) {
return url::SchemeHostPort();
}
}
if (scheme.empty()) {
scheme = url::kHttpScheme;
}
if (port == url::PORT_UNSPECIFIED) {
port = url::DefaultPortForScheme(scheme);
}
return url::SchemeHostPort(scheme, host, port);
}
net::ProxyChain ProxyOverrideRuleProxyFromString(std::string_view raw_value) {
GURL url(raw_value);
if (url.is_valid()) {
net::ProxyServer::Scheme scheme = net::GetSchemeFromUriScheme(url.scheme());
if (scheme == net::ProxyServer::SCHEME_INVALID) {
return net::ProxyChain();
}
return net::ProxyChain::FromSchemeHostAndPort(scheme, url.host(),
url.port());
}
return net::PacResultElementToProxyChain(raw_value);
}
} // namespace proxy_config