dalyk | 272d4b0 | 2019-08-14 00:37:51 | [diff] [blame^] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/net/dns_util.h" |
| 6 | |
| 7 | #include "net/third_party/uri_template/uri_template.h" |
| 8 | #include "url/gurl.h" |
| 9 | |
| 10 | bool IsValidDoHTemplate(const std::string& server_template, |
| 11 | std::string* server_method) { |
| 12 | std::string url_string; |
| 13 | std::string test_query = "this_is_a_test_query"; |
| 14 | std::unordered_map<std::string, std::string> template_params( |
| 15 | {{"dns", test_query}}); |
| 16 | std::set<std::string> vars_found; |
| 17 | bool valid_template = uri_template::Expand(server_template, template_params, |
| 18 | &url_string, &vars_found); |
| 19 | if (!valid_template) { |
| 20 | // The URI template is malformed. |
| 21 | return false; |
| 22 | } |
| 23 | GURL url(url_string); |
| 24 | if (!url.is_valid() || !url.SchemeIs("https")) { |
| 25 | // The expanded template must be a valid HTTPS URL. |
| 26 | return false; |
| 27 | } |
| 28 | if (url.host().find(test_query) != std::string::npos) { |
| 29 | // The dns variable may not be part of the hostname. |
| 30 | return false; |
| 31 | } |
| 32 | // If the template contains a dns variable, use GET, otherwise use POST. |
| 33 | DCHECK(server_method); |
| 34 | *server_method = |
| 35 | (vars_found.find("dns") == vars_found.end()) ? "POST" : "GET"; |
| 36 | return true; |
| 37 | } |