1 // Copyright 2022 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_PUBLIC_DNS_OVER_HTTPS_CONFIG_H_ 6 #define NET_DNS_PUBLIC_DNS_OVER_HTTPS_CONFIG_H_ 7 8 #include <optional> 9 #include <string> 10 #include <string_view> 11 #include <vector> 12 13 #include "base/values.h" 14 #include "net/base/net_export.h" 15 #include "net/dns/public/dns_over_https_server_config.h" 16 17 namespace net { 18 19 // Represents a collection of DnsOverHttpsServerConfig. The string 20 // representation is either a JSON object or a whitespace-separated 21 // list of DoH URI templates. 22 // The Value representation is a list of dictionaries. 23 class NET_EXPORT DnsOverHttpsConfig { 24 public: 25 DnsOverHttpsConfig(); 26 ~DnsOverHttpsConfig(); 27 DnsOverHttpsConfig(const DnsOverHttpsConfig& other); 28 DnsOverHttpsConfig& operator=(const DnsOverHttpsConfig& other); 29 DnsOverHttpsConfig(DnsOverHttpsConfig&& other); 30 DnsOverHttpsConfig& operator=(DnsOverHttpsConfig&& other); 31 32 explicit DnsOverHttpsConfig(std::vector<DnsOverHttpsServerConfig> servers); 33 34 // Constructs a Config from URI templates of zero or more servers. 35 // Returns `nullopt` if any string is invalid. 36 static std::optional<DnsOverHttpsConfig> FromTemplatesForTesting( 37 std::vector<std::string> servers); 38 39 // Constructs a Config from its text form if valid. Returns `nullopt` if the 40 // input is empty or invalid (even partly invalid). 41 static std::optional<DnsOverHttpsConfig> FromString( 42 std::string_view doh_config); 43 44 // Constructs a DnsOverHttpsConfig from its text form, skipping any invalid 45 // templates in the whitespace-separated form. The result may be empty. 46 static DnsOverHttpsConfig FromStringLax(std::string_view doh_config); 47 48 bool operator==(const DnsOverHttpsConfig& other) const; 49 50 // The servers that comprise this config. May be empty. servers()51 const std::vector<DnsOverHttpsServerConfig>& servers() const { 52 return servers_; 53 } 54 55 // Inverse of FromString(). Uses the JSON representation if necessary. 56 std::string ToString() const; 57 58 // Encodes the config as a Value. Used to produce the JSON representation. 59 base::Value::Dict ToValue() const; 60 61 private: 62 // Constructs a Config from URI templates of zero or more servers. 63 // Returns `nullopt` if any string is invalid. 64 static std::optional<DnsOverHttpsConfig> FromTemplates( 65 std::vector<std::string> servers); 66 67 std::vector<DnsOverHttpsServerConfig> servers_; 68 }; 69 70 } // namespace net 71 72 #endif // NET_DNS_PUBLIC_DNS_OVER_HTTPS_CONFIG_H_ 73