Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
stevenjb | b237e2ae | 2015-07-02 22:02:11 | [diff] [blame] | 5 | #include "components/proxy_config/proxy_config_dictionary.h" |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 6 | |
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 7 | #include <memory> |
vabr | 6f6e266 | 2017-03-31 08:08:33 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 10 | #include "base/check.h" |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 11 | #include "net/base/proxy_server.h" |
| 12 | #include "net/base/proxy_string_util.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 13 | #include "net/proxy_resolution/proxy_config.h" |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // Integer to specify the type of proxy settings. |
| 18 | // See ProxyPrefs for possible values and interactions with the other proxy |
| 19 | // preferences. |
| 20 | const char kProxyMode[] = "mode"; |
| 21 | // String specifying the proxy server. For a specification of the expected |
| 22 | // syntax see net::ProxyConfig::ProxyRules::ParseFromString(). |
| 23 | const char kProxyServer[] = "server"; |
| 24 | // URL to the proxy .pac file. |
| 25 | const char kProxyPacUrl[] = "pac_url"; |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 26 | // Optional boolean flag indicating whether a valid PAC script is mandatory. |
| 27 | // If true, network traffic does not fall back to direct connections in case the |
| 28 | // PAC script is not available. |
| 29 | const char kProxyPacMandatory[] = "pac_mandatory"; |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 30 | // String containing proxy bypass rules. For a specification of the |
| 31 | // expected syntax see net::ProxyBypassRules::ParseFromString(). |
| 32 | const char kProxyBypassList[] = "bypass_list"; |
| 33 | |
| 34 | } // namespace |
| 35 | |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 36 | ProxyConfigDictionary::ProxyConfigDictionary(base::Value::Dict dict) |
| 37 | : dict_(std::move(dict)) {} |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 38 | |
Andreea Costinas | dd0ee9e | 2021-09-13 15:17:45 | [diff] [blame] | 39 | ProxyConfigDictionary::ProxyConfigDictionary(ProxyConfigDictionary&& other) { |
| 40 | dict_ = std::move(other.dict_); |
| 41 | } |
| 42 | |
| 43 | ProxyConfigDictionary::~ProxyConfigDictionary() = default; |
[email protected] | b6ce91b | 2011-02-10 21:30:50 | [diff] [blame] | 44 | |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 45 | bool ProxyConfigDictionary::GetMode(ProxyPrefs::ProxyMode* out) const { |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 46 | const base::Value* mode_value = dict_.Find(kProxyMode); |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 47 | if (!mode_value || !mode_value->is_string()) |
| 48 | return false; |
| 49 | std::string mode_str = mode_value->GetString(); |
| 50 | return StringToProxyMode(mode_str, out); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool ProxyConfigDictionary::GetPacUrl(std::string* out) const { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 54 | return GetString(kProxyPacUrl, out); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 57 | bool ProxyConfigDictionary::GetPacMandatory(bool* out) const { |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 58 | const base::Value* value = dict_.Find(kProxyPacMandatory); |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 59 | if (!value || !value->is_bool()) { |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 60 | *out = false; |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 61 | return false; |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 62 | } |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 63 | *out = value->GetBool(); |
| 64 | return true; |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 67 | bool ProxyConfigDictionary::GetProxyServer(std::string* out) const { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 68 | return GetString(kProxyServer, out); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool ProxyConfigDictionary::GetBypassList(std::string* out) const { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 72 | return GetString(kProxyBypassList, out); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | ce50d07 | 2011-02-14 12:14:23 | [diff] [blame] | 75 | bool ProxyConfigDictionary::HasBypassList() const { |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 76 | return dict_.Find(kProxyBypassList); |
[email protected] | ce50d07 | 2011-02-14 12:14:23 | [diff] [blame] | 77 | } |
| 78 | |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 79 | const base::Value::Dict& ProxyConfigDictionary::GetDictionary() const { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 80 | return dict_; |
[email protected] | 9da3e45 | 2013-06-10 15:09:58 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 83 | // static |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 84 | base::Value::Dict ProxyConfigDictionary::CreateDirect() { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 85 | return CreateDictionary(ProxyPrefs::MODE_DIRECT, std::string(), false, |
| 86 | std::string(), std::string()); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // static |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 90 | base::Value::Dict ProxyConfigDictionary::CreateAutoDetect() { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 91 | return CreateDictionary(ProxyPrefs::MODE_AUTO_DETECT, std::string(), false, |
| 92 | std::string(), std::string()); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // static |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 96 | base::Value::Dict ProxyConfigDictionary::CreatePacScript( |
| 97 | const std::string& pac_url, |
| 98 | bool pac_mandatory) { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 99 | return CreateDictionary(ProxyPrefs::MODE_PAC_SCRIPT, pac_url, pac_mandatory, |
| 100 | std::string(), std::string()); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // static |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 104 | base::Value::Dict ProxyConfigDictionary::CreateFixedServers( |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 105 | const std::string& proxy_server, |
| 106 | const std::string& bypass_list) { |
[email protected] | b363361 | 2011-02-09 15:06:39 | [diff] [blame] | 107 | if (!proxy_server.empty()) { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 108 | return CreateDictionary(ProxyPrefs::MODE_FIXED_SERVERS, std::string(), |
| 109 | false, proxy_server, bypass_list); |
[email protected] | b363361 | 2011-02-09 15:06:39 | [diff] [blame] | 110 | } else { |
| 111 | return CreateDirect(); |
| 112 | } |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // static |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 116 | base::Value::Dict ProxyConfigDictionary::CreateSystem() { |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 117 | return CreateDictionary(ProxyPrefs::MODE_SYSTEM, std::string(), false, |
| 118 | std::string(), std::string()); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // static |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 122 | base::Value::Dict ProxyConfigDictionary::CreateDictionary( |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 123 | ProxyPrefs::ProxyMode mode, |
| 124 | const std::string& pac_url, |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 125 | bool pac_mandatory, |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 126 | const std::string& proxy_server, |
| 127 | const std::string& bypass_list) { |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 128 | base::Value::Dict dict; |
| 129 | dict.Set(kProxyMode, base::Value(ProxyModeToString(mode))); |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 130 | if (!pac_url.empty()) { |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 131 | dict.Set(kProxyPacUrl, base::Value(pac_url)); |
| 132 | dict.Set(kProxyPacMandatory, base::Value(pac_mandatory)); |
[email protected] | e0470bd2 | 2011-05-03 12:44:19 | [diff] [blame] | 133 | } |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 134 | if (!proxy_server.empty()) |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 135 | dict.Set(kProxyServer, base::Value(proxy_server)); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 136 | if (!bypass_list.empty()) |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 137 | dict.Set(kProxyBypassList, base::Value(bypass_list)); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 138 | return dict; |
| 139 | } |
stevenjb | 8336888 | 2015-07-10 20:51:45 | [diff] [blame] | 140 | |
| 141 | // static |
| 142 | void ProxyConfigDictionary::EncodeAndAppendProxyServer( |
| 143 | const std::string& url_scheme, |
| 144 | const net::ProxyServer& server, |
| 145 | std::string* spec) { |
| 146 | if (!server.is_valid()) |
| 147 | return; |
| 148 | |
| 149 | if (!spec->empty()) |
| 150 | *spec += ';'; |
| 151 | |
| 152 | if (!url_scheme.empty()) { |
| 153 | *spec += url_scheme; |
| 154 | *spec += "="; |
| 155 | } |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 156 | *spec += net::ProxyServerToProxyUri(server); |
stevenjb | 8336888 | 2015-07-10 20:51:45 | [diff] [blame] | 157 | } |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 158 | |
| 159 | bool ProxyConfigDictionary::GetString(const char* key, std::string* out) const { |
Andrey Davydov | 21c16e2 | 2022-09-29 16:19:45 | [diff] [blame] | 160 | const base::Value* value = dict_.Find(key); |
Steven Bennetts | 3e470ac | 2018-07-27 18:29:27 | [diff] [blame] | 161 | if (!value || !value->is_string()) { |
| 162 | *out = ""; |
| 163 | return false; |
| 164 | } |
| 165 | *out = value->GetString(); |
| 166 | return true; |
| 167 | } |