| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "sync/internal_api/syncapi_server_connection_manager.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include "net/base/net_errors.h" | |
| 10 #include "net/http/http_status_code.h" | |
| 11 #include "sync/internal_api/public/http_post_provider_factory.h" | |
| 12 #include "sync/internal_api/public/http_post_provider_interface.h" | |
| 13 | |
| 14 namespace syncer { | |
| 15 | |
| 16 SyncAPIBridgedConnection::SyncAPIBridgedConnection( | |
| 17 ServerConnectionManager* scm, | |
| 18 HttpPostProviderFactory* factory) | |
| 19 : Connection(scm), factory_(factory) { | |
| 20 post_provider_ = factory_->Create(); | |
| 21 } | |
| 22 | |
| 23 SyncAPIBridgedConnection::~SyncAPIBridgedConnection() { | |
| 24 DCHECK(post_provider_); | |
| 25 factory_->Destroy(post_provider_); | |
| 26 post_provider_ = NULL; | |
| 27 } | |
| 28 | |
| 29 bool SyncAPIBridgedConnection::Init(const char* path, | |
| 30 const std::string& auth_token, | |
| 31 const std::string& payload, | |
| 32 HttpResponse* response) { | |
| 33 std::string sync_server; | |
| 34 int sync_server_port = 0; | |
| 35 bool use_ssl = false; | |
| 36 GetServerParams(&sync_server, &sync_server_port, &use_ssl); | |
| 37 std::string connection_url = MakeConnectionURL(sync_server, path, use_ssl); | |
| 38 | |
| 39 HttpPostProviderInterface* http = post_provider_; | |
| 40 http->SetURL(connection_url.c_str(), sync_server_port); | |
| 41 | |
| 42 if (!auth_token.empty()) { | |
| 43 std::string headers; | |
| 44 headers = "Authorization: Bearer " + auth_token; | |
| 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 error_code = 0; | |
| 54 int response_code = 0; | |
| 55 if (!http->MakeSynchronousPost(&error_code, &response_code)) { | |
| 56 DCHECK_NE(error_code, net::OK); | |
| 57 DVLOG(1) << "Http POST failed, error returns: " << error_code; | |
| 58 response->server_status = HttpResponse::CONNECTION_UNAVAILABLE; | |
| 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_t>(http->GetResponseContentLength()); | |
| 66 response->payload_length = | |
| 67 static_cast<int64_t>(http->GetResponseContentLength()); | |
| 68 if (response->response_code < 400) | |
| 69 response->server_status = HttpResponse::SERVER_CONNECTION_OK; | |
| 70 else if (response->response_code == net::HTTP_UNAUTHORIZED) | |
| 71 response->server_status = HttpResponse::SYNC_AUTH_ERROR; | |
| 72 else | |
| 73 response->server_status = HttpResponse::SYNC_SERVER_ERROR; | |
| 74 | |
| 75 // Write the content into our buffer. | |
| 76 buffer_.assign(http->GetResponseContent(), http->GetResponseContentLength()); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 void SyncAPIBridgedConnection::Abort() { | |
| 81 DCHECK(post_provider_); | |
| 82 post_provider_->Abort(); | |
| 83 } | |
| 84 | |
| 85 SyncAPIServerConnectionManager::SyncAPIServerConnectionManager( | |
| 86 const std::string& server, | |
| 87 int port, | |
| 88 bool use_ssl, | |
| 89 HttpPostProviderFactory* factory, | |
| 90 CancelationSignal* cancelation_signal) | |
| 91 : ServerConnectionManager(server, | |
| 92 port, | |
| 93 use_ssl, | |
| 94 cancelation_signal), | |
| 95 post_provider_factory_(factory) { | |
| 96 DCHECK(post_provider_factory_.get()); | |
| 97 } | |
| 98 | |
| 99 SyncAPIServerConnectionManager::~SyncAPIServerConnectionManager() {} | |
| 100 | |
| 101 ServerConnectionManager::Connection* | |
| 102 SyncAPIServerConnectionManager::MakeConnection() { | |
| 103 return new SyncAPIBridgedConnection(this, post_provider_factory_.get()); | |
| 104 } | |
| 105 | |
| 106 } // namespace syncer | |
| OLD | NEW |