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
3 // found in the LICENSE file.
5 #include "components/copresence/rpc/http_post.h"
7 // TODO(ckehoe): Support third-party protobufs too.
8 #include <google/protobuf/message_lite.h>
10 #include "base/bind.h"
11 #include "google_apis/google_api_keys.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/url_util.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_request_context_getter.h"
20 namespace copresence
{
22 const char HttpPost::kApiKeyField
[] = "key";
23 const char HttpPost::kTracingField
[] = "trace";
25 HttpPost::HttpPost(net::URLRequestContextGetter
* url_context_getter
,
26 const std::string
& server_host
,
27 const std::string
& rpc_name
,
28 const std::string
& tracing_token
,
30 const google::protobuf::MessageLite
& request_proto
) {
31 // Create the base URL to call.
32 GURL
url(server_host
+ "/" + rpc_name
);
34 // Add the tracing token, if specified.
35 if (!tracing_token
.empty()) {
36 url
= net::AppendQueryParameter(
37 url
, kTracingField
, "token:" + tracing_token
);
40 // If no API key is specified, use the Chrome API key.
41 if (api_key
.empty()) {
42 #ifdef GOOGLE_CHROME_BUILD
43 DCHECK(google_apis::HasKeysConfigured());
44 api_key
= google_apis::GetAPIKey();
46 LOG(ERROR
) << "No Copresence API key provided";
49 url
= net::AppendQueryParameter(url
, kApiKeyField
, api_key
);
51 // Serialize the proto for transmission.
52 std::string request_data
;
53 bool serialize_success
= request_proto
.SerializeToString(&request_data
);
54 DCHECK(serialize_success
);
56 // Configure and send the request.
57 url_fetcher_
.reset(net::URLFetcher::Create(
58 kUrlFetcherId
, url
, net::URLFetcher::POST
, this));
59 url_fetcher_
->SetRequestContext(url_context_getter
);
60 url_fetcher_
->SetLoadFlags(net::LOAD_BYPASS_CACHE
|
61 net::LOAD_DISABLE_CACHE
|
62 net::LOAD_DO_NOT_SAVE_COOKIES
|
63 net::LOAD_DO_NOT_SEND_COOKIES
|
64 net::LOAD_DO_NOT_SEND_AUTH_DATA
);
65 url_fetcher_
->SetUploadData("application/x-protobuf", request_data
);
68 HttpPost::~HttpPost() {}
70 void HttpPost::Start(const ResponseCallback
& response_callback
) {
71 response_callback_
= response_callback
;
72 DVLOG(3) << "Sending Copresence request to "
73 << url_fetcher_
->GetOriginalURL().spec();
74 url_fetcher_
->Start();
77 void HttpPost::OnURLFetchComplete(const net::URLFetcher
* source
) {
78 DCHECK_EQ(url_fetcher_
.get(), source
);
80 // Gather response info.
82 source
->GetResponseAsString(&response
);
83 int response_code
= source
->GetResponseCode();
86 if (response_code
< 0) {
87 net::URLRequestStatus status
= source
->GetStatus();
88 LOG(WARNING
) << "Couldn't contact the Copresence server at "
89 << source
->GetURL() << ". Status code " << status
.status();
90 LOG_IF(WARNING
, status
.error())
91 << "Network error: " << net::ErrorToString(status
.error());
92 LOG_IF(WARNING
, !response
.empty()) << "HTTP response: " << response
;
93 } else if (response_code
!= net::HTTP_OK
) {
94 LOG(WARNING
) << "Copresence request got HTTP response code "
95 << response_code
<< ". Response:\n" << response
;
98 // Return the response.
99 response_callback_
.Run(response_code
, response
);
102 } // namespace copresence