| OLD | NEW | 
|---|
|  | (Empty) | 
| 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. |  | 
| 4 |  | 
| 5 #include "components/copresence/rpc/http_post.h" |  | 
| 6 |  | 
| 7 // TODO(ckehoe): Support third-party protobufs too. |  | 
| 8 #include <google/protobuf/message_lite.h> |  | 
| 9 |  | 
| 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" |  | 
| 18 #include "url/gurl.h" |  | 
| 19 |  | 
| 20 namespace copresence { |  | 
| 21 |  | 
| 22 const char HttpPost::kApiKeyField[] = "key"; |  | 
| 23 const char HttpPost::kTracingField[] = "trace"; |  | 
| 24 |  | 
| 25 HttpPost::HttpPost(net::URLRequestContextGetter* url_context_getter, |  | 
| 26                    const std::string& server_host, |  | 
| 27                    const std::string& rpc_name, |  | 
| 28                    std::string api_key, |  | 
| 29                    const std::string& auth_token, |  | 
| 30                    const std::string& tracing_token, |  | 
| 31                    const google::protobuf::MessageLite& request_proto) { |  | 
| 32   // Create the base URL to call. |  | 
| 33   GURL url(server_host + "/" + rpc_name); |  | 
| 34 |  | 
| 35   // Add the tracing token, if specified. |  | 
| 36   if (!tracing_token.empty()) { |  | 
| 37     url = net::AppendQueryParameter( |  | 
| 38         url, kTracingField, "token:" + tracing_token); |  | 
| 39   } |  | 
| 40 |  | 
| 41   // If we have no auth token, authenticate using the API key. |  | 
| 42   // If no API key is specified, use the Chrome API key. |  | 
| 43   if (auth_token.empty()) { |  | 
| 44     if (api_key.empty()) { |  | 
| 45 #ifdef GOOGLE_CHROME_BUILD |  | 
| 46       DCHECK(google_apis::HasKeysConfigured()); |  | 
| 47       api_key = google_apis::GetAPIKey(); |  | 
| 48 #else |  | 
| 49       LOG(ERROR) << "No Copresence API key provided"; |  | 
| 50 #endif |  | 
| 51     } |  | 
| 52     url = net::AppendQueryParameter(url, kApiKeyField, api_key); |  | 
| 53   } |  | 
| 54 |  | 
| 55   // Serialize the proto for transmission. |  | 
| 56   std::string request_data; |  | 
| 57   bool serialize_success = request_proto.SerializeToString(&request_data); |  | 
| 58   DCHECK(serialize_success); |  | 
| 59 |  | 
| 60   // Configure and send the request. |  | 
| 61   url_fetcher_ = |  | 
| 62       net::URLFetcher::Create(kUrlFetcherId, url, net::URLFetcher::POST, this); |  | 
| 63   url_fetcher_->SetRequestContext(url_context_getter); |  | 
| 64   url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | |  | 
| 65                              net::LOAD_DISABLE_CACHE | |  | 
| 66                              net::LOAD_DO_NOT_SAVE_COOKIES | |  | 
| 67                              net::LOAD_DO_NOT_SEND_COOKIES | |  | 
| 68                              net::LOAD_DO_NOT_SEND_AUTH_DATA); |  | 
| 69   url_fetcher_->SetUploadData("application/x-protobuf", request_data); |  | 
| 70   if (!auth_token.empty()) { |  | 
| 71     url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + auth_token); |  | 
| 72   } |  | 
| 73 } |  | 
| 74 |  | 
| 75 HttpPost::~HttpPost() {} |  | 
| 76 |  | 
| 77 void HttpPost::Start(const ResponseCallback& response_callback) { |  | 
| 78   response_callback_ = response_callback; |  | 
| 79   DVLOG(3) << "Sending Copresence request to " |  | 
| 80            << url_fetcher_->GetOriginalURL().spec(); |  | 
| 81   url_fetcher_->Start(); |  | 
| 82 } |  | 
| 83 |  | 
| 84 void HttpPost::OnURLFetchComplete(const net::URLFetcher* source) { |  | 
| 85   DCHECK_EQ(url_fetcher_.get(), source); |  | 
| 86 |  | 
| 87   // Gather response info. |  | 
| 88   std::string response; |  | 
| 89   source->GetResponseAsString(&response); |  | 
| 90   int response_code = source->GetResponseCode(); |  | 
| 91 |  | 
| 92   // Log any errors. |  | 
| 93   if (response_code < 0) { |  | 
| 94     net::URLRequestStatus status = source->GetStatus(); |  | 
| 95     LOG(WARNING) << "Couldn't contact the Copresence server at " |  | 
| 96                  << source->GetURL() << ". Status code " << status.status(); |  | 
| 97     LOG_IF(WARNING, status.error()) |  | 
| 98         << "Network error: " << net::ErrorToString(status.error()); |  | 
| 99     LOG_IF(WARNING, !response.empty()) << "HTTP response: " << response; |  | 
| 100   } else if (response_code != net::HTTP_OK) { |  | 
| 101     LOG(WARNING) << "Copresence request got HTTP response code " |  | 
| 102                  << response_code << ". Response:\n" << response; |  | 
| 103   } |  | 
| 104 |  | 
| 105   // Return the response. |  | 
| 106   response_callback_.Run(response_code, response); |  | 
| 107   delete this; |  | 
| 108 } |  | 
| 109 |  | 
| 110 }  // namespace copresence |  | 
| OLD | NEW | 
|---|