| OLD | NEW |
| (Empty) |
| 1 // Copyright 2009-2010 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/common/web_services_client.h" | |
| 17 #include <atlstr.h> | |
| 18 #include "omaha/base/const_addresses.h" | |
| 19 #include "omaha/base/debug.h" | |
| 20 #include "omaha/base/error.h" | |
| 21 #include "omaha/base/logging.h" | |
| 22 #include "omaha/base/synchronized.h" | |
| 23 #include "omaha/base/utils.h" | |
| 24 #include "omaha/common/config_manager.h" | |
| 25 #include "omaha/common/update_request.h" | |
| 26 #include "omaha/common/update_response.h" | |
| 27 #include "omaha/net/cup_request.h" | |
| 28 #include "omaha/net/network_config.h" | |
| 29 #include "omaha/net/network_request.h" | |
| 30 #include "omaha/net/simple_request.h" | |
| 31 | |
| 32 namespace omaha { | |
| 33 | |
| 34 WebServicesClient::WebServicesClient(bool is_machine) | |
| 35 : lock_(NULL), | |
| 36 is_machine_(is_machine) { | |
| 37 } | |
| 38 | |
| 39 WebServicesClient::~WebServicesClient() { | |
| 40 CORE_LOG(L3, (_T("[WebServicesClient::~WebServicesClient]"))); | |
| 41 | |
| 42 delete &lock(); | |
| 43 omaha::interlocked_exchange_pointer(&lock_, static_cast<Lockable*>(NULL)); | |
| 44 } | |
| 45 | |
| 46 HRESULT WebServicesClient::Initialize(const CString& url, | |
| 47 const HeadersVector& headers, | |
| 48 bool use_cup) { | |
| 49 CORE_LOG(L3, (_T("[WebServicesClient::Initialize][%s][%d]"), url, use_cup)); | |
| 50 | |
| 51 omaha::interlocked_exchange_pointer(&lock_, | |
| 52 static_cast<Lockable*>(new LLock)); | |
| 53 __mutexScope(lock()); | |
| 54 | |
| 55 url_ = url; | |
| 56 | |
| 57 NetworkConfig* network_config = NULL; | |
| 58 NetworkConfigManager& network_manager = NetworkConfigManager::Instance(); | |
| 59 HRESULT hr = network_manager.GetUserNetworkConfig(&network_config); | |
| 60 if (FAILED(hr)) { | |
| 61 return hr; | |
| 62 } | |
| 63 | |
| 64 const NetworkConfig::Session& session(network_config->session()); | |
| 65 | |
| 66 network_request_.reset(new NetworkRequest(session)); | |
| 67 | |
| 68 for (size_t i = 0; i < headers.size(); ++i) { | |
| 69 network_request_->AddHeader(headers[i].first, headers[i].second); | |
| 70 } | |
| 71 | |
| 72 if (use_cup) { | |
| 73 network_request_->AddHttpRequest(new CupRequest(new SimpleRequest)); | |
| 74 } | |
| 75 network_request_->AddHttpRequest(new SimpleRequest); | |
| 76 network_request_->set_num_retries(1); | |
| 77 | |
| 78 return S_OK; | |
| 79 } | |
| 80 | |
| 81 const Lockable& WebServicesClient::lock() const { | |
| 82 return *omaha::interlocked_exchange_pointer(&lock_, lock_); | |
| 83 } | |
| 84 | |
| 85 CString WebServicesClient::url() const { | |
| 86 __mutexScope(lock()); | |
| 87 return url_; | |
| 88 } | |
| 89 | |
| 90 NetworkRequest* WebServicesClient::network_request() { | |
| 91 __mutexScope(lock()); | |
| 92 return network_request_.get(); | |
| 93 } | |
| 94 | |
| 95 HRESULT WebServicesClient::Send(const xml::UpdateRequest* update_request, | |
| 96 xml::UpdateResponse* update_response) { | |
| 97 CORE_LOG(L3, (_T("[WebServicesClient::Send]"))); | |
| 98 ASSERT1(update_request); | |
| 99 ASSERT1(update_response); | |
| 100 | |
| 101 if (!ConfigManager::Instance()->CanUseNetwork(is_machine_)) { | |
| 102 CORE_LOG(LE, (_T("[WebServicesClient::Send][network use prohibited]"))); | |
| 103 return GOOPDATE_E_CANNOT_USE_NETWORK; | |
| 104 } | |
| 105 | |
| 106 CString request_string; | |
| 107 HRESULT hr = update_request->Serialize(&request_string); | |
| 108 if (FAILED(hr)) { | |
| 109 CORE_LOG(LE, (_T("[Serialize failed][0x%08x]"), hr)); | |
| 110 return hr; | |
| 111 } | |
| 112 | |
| 113 ASSERT1(!request_string.IsEmpty()); | |
| 114 | |
| 115 // For security reasons, if there's tt_token in the request, we must | |
| 116 // set_preserve_protocol in network request to prevent it from replacing | |
| 117 // https with http scheme. | |
| 118 const bool need_preserve_https = update_request->has_tt_token(); | |
| 119 | |
| 120 return SendStringPreserveProtocol(need_preserve_https, &request_string, | |
| 121 update_response); | |
| 122 } | |
| 123 | |
| 124 HRESULT WebServicesClient::SendString(const CString* request_string, | |
| 125 xml::UpdateResponse* update_response) { | |
| 126 CORE_LOG(L3, (_T("[WebServicesClient::SendString]"))); | |
| 127 ASSERT1(request_string); | |
| 128 ASSERT1(update_response); | |
| 129 | |
| 130 return SendStringPreserveProtocol(false, request_string, update_response); | |
| 131 } | |
| 132 | |
| 133 HRESULT WebServicesClient::SendStringPreserveProtocol( | |
| 134 bool need_preserve_https, | |
| 135 const CString* request_string, | |
| 136 xml::UpdateResponse* update_response) { | |
| 137 CORE_LOG(L3, (_T("[WebServicesClient::SendStringPreserveProtocol]"))); | |
| 138 ASSERT1(request_string); | |
| 139 ASSERT1(update_response); | |
| 140 | |
| 141 CORE_LOG(L3, (_T("[sending web services request][%s]"), *request_string)); | |
| 142 | |
| 143 std::vector<uint8> response_buffer; | |
| 144 | |
| 145 CString request_url = url(); | |
| 146 ASSERT1(!need_preserve_https || | |
| 147 String_StartsWith(request_url, kHttpsProtoScheme, true)); | |
| 148 network_request_->set_preserve_protocol(need_preserve_https); | |
| 149 HRESULT hr = PostRequest(network_request_.get(), true, | |
| 150 request_url, *request_string, &response_buffer); | |
| 151 if (FAILED(hr)) { | |
| 152 CORE_LOG(LE, (_T("[PostString failed][0x%08x]"), hr)); | |
| 153 return hr; | |
| 154 } | |
| 155 | |
| 156 // The web services server is expected to reply with 200 OK if the | |
| 157 // transaction has been successful. | |
| 158 ASSERT1(is_http_success()); | |
| 159 if (!is_http_success()) { | |
| 160 CORE_LOG(LE, (_T("[PostString returned success on a failed transaction]"))); | |
| 161 return E_FAIL; | |
| 162 } | |
| 163 | |
| 164 CString response_string = Utf8BufferToWideChar(response_buffer); | |
| 165 CORE_LOG(L3, (_T("[received web services response][%s]"), response_string)); | |
| 166 | |
| 167 hr = update_response->Deserialize(response_buffer); | |
| 168 if (FAILED(hr)) { | |
| 169 CORE_LOG(LE, (_T("[UpdateResponse::Deserialize failed][0x%08x]"), hr)); | |
| 170 return hr; | |
| 171 } | |
| 172 | |
| 173 return S_OK; | |
| 174 } | |
| 175 | |
| 176 void WebServicesClient::Cancel() { | |
| 177 CORE_LOG(L3, (_T("[WebServicesClient::Cancel]"))); | |
| 178 NetworkRequest* network_request(network_request()); | |
| 179 if (network_request) { | |
| 180 network_request->Cancel(); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void WebServicesClient::set_proxy_auth_config(const ProxyAuthConfig& config) { | |
| 185 ASSERT1(network_request()); | |
| 186 network_request()->set_proxy_auth_config(config); | |
| 187 } | |
| 188 | |
| 189 bool WebServicesClient::is_http_success() const { | |
| 190 return network_request_->http_status_code() == HTTP_STATUS_OK; | |
| 191 } | |
| 192 | |
| 193 int WebServicesClient::http_status_code() const { | |
| 194 return network_request_->http_status_code(); | |
| 195 } | |
| 196 | |
| 197 CString WebServicesClient::http_trace() const { | |
| 198 return network_request_->trace(); | |
| 199 } | |
| 200 | |
| 201 } // namespace omaha | |
| OLD | NEW |