| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "net/base/escape.h" | 11 #include "net/base/escape.h" |
| 12 #include "net/http/http_request_headers.h" |
| 12 | 13 |
| 13 using base::Time; | 14 using base::Time; |
| 14 using base::TimeDelta; | 15 using base::TimeDelta; |
| 15 | 16 |
| 16 namespace safe_browsing { | 17 namespace safe_browsing { |
| 17 | 18 |
| 18 // The Safe Browsing V4 server URL prefix. | 19 // The Safe Browsing V4 server URL prefix. |
| 19 const char kSbV4UrlPrefix[] = "https://safebrowsing.googleapis.com/v4"; | 20 const char kSbV4UrlPrefix[] = "https://safebrowsing.googleapis.com/v4"; |
| 20 | 21 |
| 21 bool UpdateListIdentifier::operator==(const UpdateListIdentifier& other) const { | 22 bool UpdateListIdentifier::operator==(const UpdateListIdentifier& other) const { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // static | 71 // static |
| 71 void V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( | 72 void V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
| 72 const char* metric_name, | 73 const char* metric_name, |
| 73 const net::URLRequestStatus& status, | 74 const net::URLRequestStatus& status, |
| 74 int response_code) { | 75 int response_code) { |
| 75 UMA_HISTOGRAM_SPARSE_SLOWLY( | 76 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 76 metric_name, status.is_success() ? response_code : status.error()); | 77 metric_name, status.is_success() ? response_code : status.error()); |
| 77 } | 78 } |
| 78 | 79 |
| 79 // static | 80 // static |
| 80 // The API hash call uses the pver4 Safe Browsing server. | 81 void V4ProtocolManagerUtil::GetRequestUrlAndHeaders( |
| 81 GURL V4ProtocolManagerUtil::GetRequestUrl(const std::string& request_base64, | 82 const std::string& request_base64, |
| 82 const std::string& method_name, | 83 const std::string& method_name, |
| 83 const V4ProtocolConfig& config) { | 84 const V4ProtocolConfig& config, |
| 84 std::string url = | 85 GURL* gurl, |
| 85 ComposeUrl(kSbV4UrlPrefix, method_name, request_base64, | 86 net::HttpRequestHeaders* headers) { |
| 86 config.client_name, config.version, config.key_param); | 87 *gurl = GURL(ComposeUrl(kSbV4UrlPrefix, method_name, request_base64, |
| 87 return GURL(url); | 88 config.key_param)); |
| 89 UpdateHeaders(headers); |
| 88 } | 90 } |
| 89 | 91 |
| 90 // static | 92 // static |
| 91 std::string V4ProtocolManagerUtil::ComposeUrl(const std::string& prefix, | 93 std::string V4ProtocolManagerUtil::ComposeUrl(const std::string& prefix, |
| 92 const std::string& method, | 94 const std::string& method, |
| 93 const std::string& request_base64, | 95 const std::string& request_base64, |
| 94 const std::string& client_id, | |
| 95 const std::string& version, | |
| 96 const std::string& key_param) { | 96 const std::string& key_param) { |
| 97 DCHECK(!prefix.empty() && !method.empty() && !client_id.empty() && | 97 DCHECK(!prefix.empty() && !method.empty()); |
| 98 !version.empty()); | 98 std::string url = base::StringPrintf( |
| 99 std::string url = | 99 "%s/%s?$req=%s&$ct=application/x-protobuf", prefix.c_str(), |
| 100 base::StringPrintf("%s/%s/%s?alt=proto&client_id=%s&client_version=%s", | 100 method.c_str(), request_base64.c_str()); |
| 101 prefix.c_str(), method.c_str(), request_base64.c_str(), | |
| 102 client_id.c_str(), version.c_str()); | |
| 103 if (!key_param.empty()) { | 101 if (!key_param.empty()) { |
| 104 base::StringAppendF(&url, "&key=%s", | 102 base::StringAppendF(&url, "&key=%s", |
| 105 net::EscapeQueryParamValue(key_param, true).c_str()); | 103 net::EscapeQueryParamValue(key_param, true).c_str()); |
| 106 } | 104 } |
| 107 return url; | 105 return url; |
| 108 } | 106 } |
| 109 | 107 |
| 108 // static |
| 109 void V4ProtocolManagerUtil::UpdateHeaders(net::HttpRequestHeaders* headers) { |
| 110 // NOTE(vakh): The following header informs the envelope server (which sits in |
| 111 // front of Google's stubby server) that the received GET request should be |
| 112 // interpreted as a POST. |
| 113 headers->SetHeaderIfMissing("X-HTTP-Method-Override", "POST"); |
| 114 } |
| 115 |
| 110 } // namespace safe_browsing | 116 } // namespace safe_browsing |
| OLD | NEW |