| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/sync/engine/net/syncapi_server_connection_manager.h" | |
| 6 | |
| 7 #include "chrome/browser/sync/engine/http_post_provider_factory.h" | |
| 8 #include "chrome/browser/sync/engine/http_post_provider_interface.h" | |
| 9 #include "chrome/browser/sync/util/oauth.h" | |
| 10 #include "chrome/common/net/http_return.h" | |
| 11 | |
| 12 using browser_sync::HttpResponse; | |
| 13 | |
| 14 namespace sync_api { | |
| 15 | |
| 16 SyncAPIBridgedPost::SyncAPIBridgedPost( | |
| 17 browser_sync::ServerConnectionManager* scm, | |
| 18 HttpPostProviderFactory* factory) | |
| 19 : Post(scm), factory_(factory) { | |
| 20 } | |
| 21 | |
| 22 SyncAPIBridgedPost::~SyncAPIBridgedPost() {} | |
| 23 | |
| 24 bool SyncAPIBridgedPost::Init(const char* path, | |
| 25 const std::string& auth_token, | |
| 26 const std::string& payload, | |
| 27 HttpResponse* response) { | |
| 28 std::string sync_server; | |
| 29 int sync_server_port = 0; | |
| 30 bool use_ssl = false; | |
| 31 GetServerParams(&sync_server, &sync_server_port, &use_ssl); | |
| 32 std::string connection_url = MakeConnectionURL(sync_server, path, use_ssl); | |
| 33 | |
| 34 HttpPostProviderInterface* http = factory_->Create(); | |
| 35 http->SetUserAgent(scm_->user_agent().c_str()); | |
| 36 http->SetURL(connection_url.c_str(), sync_server_port); | |
| 37 | |
| 38 if (!auth_token.empty()) { | |
| 39 std::string headers; | |
| 40 if (browser_sync::IsUsingOAuth()) { | |
| 41 headers = "Authorization: OAuth " + auth_token; | |
| 42 } else { | |
| 43 headers = "Authorization: GoogleLogin auth=" + auth_token; | |
| 44 } | |
| 45 http->SetExtraRequestHeaders(headers.c_str()); | |
| 46 } | |
| 47 | |
| 48 // Must be octet-stream, or the payload may be parsed for a cookie. | |
| 49 http->SetPostPayload("application/octet-stream", payload.length(), | |
| 50 payload.data()); | |
| 51 | |
| 52 // Issue the POST, blocking until it finishes. | |
| 53 int os_error_code = 0; | |
| 54 int response_code = 0; | |
| 55 if (!http->MakeSynchronousPost(&os_error_code, &response_code)) { | |
| 56 VLOG(1) << "Http POST failed, error returns: " << os_error_code; | |
| 57 response->server_status = HttpResponse::IO_ERROR; | |
| 58 factory_->Destroy(http); | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 // We got a server response, copy over response codes and content. | |
| 63 response->response_code = response_code; | |
| 64 response->content_length = | |
| 65 static_cast<int64>(http->GetResponseContentLength()); | |
| 66 response->payload_length = | |
| 67 static_cast<int64>(http->GetResponseContentLength()); | |
| 68 if (response->response_code < 400) | |
| 69 response->server_status = HttpResponse::SERVER_CONNECTION_OK; | |
| 70 else if (response->response_code == RC_UNAUTHORIZED) | |
| 71 response->server_status = HttpResponse::SYNC_AUTH_ERROR; | |
| 72 else | |
| 73 response->server_status = HttpResponse::SYNC_SERVER_ERROR; | |
| 74 | |
| 75 response->update_client_auth_header = | |
| 76 http->GetResponseHeaderValue("Update-Client-Auth"); | |
| 77 | |
| 78 // Write the content into our buffer. | |
| 79 buffer_.assign(http->GetResponseContent(), http->GetResponseContentLength()); | |
| 80 | |
| 81 // We're done with the HttpPostProvider. | |
| 82 factory_->Destroy(http); | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 SyncAPIServerConnectionManager::SyncAPIServerConnectionManager( | |
| 87 const std::string& server, | |
| 88 int port, | |
| 89 bool use_ssl, | |
| 90 const std::string& client_version, | |
| 91 HttpPostProviderFactory* factory) | |
| 92 : ServerConnectionManager(server, port, use_ssl, client_version), | |
| 93 post_provider_factory_(factory) { | |
| 94 DCHECK(post_provider_factory_.get()); | |
| 95 } | |
| 96 | |
| 97 SyncAPIServerConnectionManager::~SyncAPIServerConnectionManager() {} | |
| 98 | |
| 99 browser_sync::ServerConnectionManager::Post* | |
| 100 SyncAPIServerConnectionManager::MakePost() { | |
| 101 return new SyncAPIBridgedPost(this, post_provider_factory_.get()); | |
| 102 } | |
| 103 | |
| 104 } // namespace sync_api | |
| OLD | NEW |