OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/copresence/rpc/http_post.h" | 5 #include "components/copresence/rpc/http_post.h" |
6 | 6 |
7 // TODO(ckehoe): Support third-party protobufs too. | 7 // TODO(ckehoe): Support third-party protobufs too. |
8 #include <google/protobuf/message_lite.h> | 8 #include <google/protobuf/message_lite.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "google_apis/google_api_keys.h" | 11 #include "google_apis/google_api_keys.h" |
12 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/base/url_util.h" | 14 #include "net/base/url_util.h" |
15 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
16 #include "net/url_request/url_fetcher.h" | 16 #include "net/url_request/url_fetcher.h" |
17 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace copresence { | 20 namespace copresence { |
21 | 21 |
22 const char HttpPost::kApiKeyField[] = "key"; | 22 const char HttpPost::kApiKeyField[] = "key"; |
23 const char HttpPost::kTracingField[] = "trace"; | 23 const char HttpPost::kTracingField[] = "trace"; |
24 | 24 |
25 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, | 25 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |
26 const std::string& server_host, | 26 const std::string& server_host, |
27 const std::string& rpc_name, | 27 const std::string& rpc_name, |
| 28 std::string api_key, |
| 29 const std::string& auth_token, |
28 const std::string& tracing_token, | 30 const std::string& tracing_token, |
29 std::string api_key, | |
30 const google::protobuf::MessageLite& request_proto) { | 31 const google::protobuf::MessageLite& request_proto) { |
31 // Create the base URL to call. | 32 // Create the base URL to call. |
32 GURL url(server_host + "/" + rpc_name); | 33 GURL url(server_host + "/" + rpc_name); |
33 | 34 |
34 // Add the tracing token, if specified. | 35 // Add the tracing token, if specified. |
35 if (!tracing_token.empty()) { | 36 if (!tracing_token.empty()) { |
36 url = net::AppendQueryParameter( | 37 url = net::AppendQueryParameter( |
37 url, kTracingField, "token:" + tracing_token); | 38 url, kTracingField, "token:" + tracing_token); |
38 } | 39 } |
39 | 40 |
(...skipping 16 matching lines...) Expand all Loading... |
56 // Configure and send the request. | 57 // Configure and send the request. |
57 url_fetcher_.reset(net::URLFetcher::Create( | 58 url_fetcher_.reset(net::URLFetcher::Create( |
58 kUrlFetcherId, url, net::URLFetcher::POST, this)); | 59 kUrlFetcherId, url, net::URLFetcher::POST, this)); |
59 url_fetcher_->SetRequestContext(url_context_getter); | 60 url_fetcher_->SetRequestContext(url_context_getter); |
60 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 61 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
61 net::LOAD_DISABLE_CACHE | | 62 net::LOAD_DISABLE_CACHE | |
62 net::LOAD_DO_NOT_SAVE_COOKIES | | 63 net::LOAD_DO_NOT_SAVE_COOKIES | |
63 net::LOAD_DO_NOT_SEND_COOKIES | | 64 net::LOAD_DO_NOT_SEND_COOKIES | |
64 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 65 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
65 url_fetcher_->SetUploadData("application/x-protobuf", request_data); | 66 url_fetcher_->SetUploadData("application/x-protobuf", request_data); |
| 67 if (!auth_token.empty()) { |
| 68 url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + auth_token); |
| 69 } |
66 } | 70 } |
67 | 71 |
68 HttpPost::~HttpPost() {} | 72 HttpPost::~HttpPost() {} |
69 | 73 |
70 void HttpPost::Start(const ResponseCallback& response_callback) { | 74 void HttpPost::Start(const ResponseCallback& response_callback) { |
71 response_callback_ = response_callback; | 75 response_callback_ = response_callback; |
72 DVLOG(3) << "Sending Copresence request to " | 76 DVLOG(3) << "Sending Copresence request to " |
73 << url_fetcher_->GetOriginalURL().spec(); | 77 << url_fetcher_->GetOriginalURL().spec(); |
74 url_fetcher_->Start(); | 78 url_fetcher_->Start(); |
75 } | 79 } |
(...skipping 17 matching lines...) Expand all Loading... |
93 } else if (response_code != net::HTTP_OK) { | 97 } else if (response_code != net::HTTP_OK) { |
94 LOG(WARNING) << "Copresence request got HTTP response code " | 98 LOG(WARNING) << "Copresence request got HTTP response code " |
95 << response_code << ". Response:\n" << response; | 99 << response_code << ". Response:\n" << response; |
96 } | 100 } |
97 | 101 |
98 // Return the response. | 102 // Return the response. |
99 response_callback_.Run(response_code, response); | 103 response_callback_.Run(response_code, response); |
100 } | 104 } |
101 | 105 |
102 } // namespace copresence | 106 } // namespace copresence |
OLD | NEW |