| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008-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/net/urlmon_request.h" | |
| 17 #include <winhttp.h> | |
| 18 #include <atlbase.h> | |
| 19 #include <atlcom.h> | |
| 20 #include <atlcomcli.h> | |
| 21 #include <vector> | |
| 22 #include "omaha/base/error.h" | |
| 23 #include "omaha/base/file.h" | |
| 24 #include "omaha/base/logging.h" | |
| 25 #include "omaha/base/string.h" | |
| 26 #include "omaha/base/utils.h" | |
| 27 #include "omaha/net/http_client.h" | |
| 28 #include "omaha/net/network_request.h" | |
| 29 #include "omaha/net/network_config.h" | |
| 30 | |
| 31 namespace omaha { | |
| 32 | |
| 33 const DWORD response_headers_needed[] = { | |
| 34 WINHTTP_QUERY_CONTENT_TYPE, | |
| 35 WINHTTP_QUERY_ETAG, | |
| 36 WINHTTP_QUERY_SET_COOKIE, | |
| 37 WINHTTP_QUERY_FLAG_REQUEST_HEADERS | WINHTTP_QUERY_USER_AGENT, | |
| 38 WINHTTP_QUERY_RAW_HEADERS_CRLF, | |
| 39 }; | |
| 40 | |
| 41 UrlmonRequest::UrlmonRequest() | |
| 42 : request_buffer_(NULL), | |
| 43 request_buffer_length_(0), | |
| 44 proxy_auth_config_(NULL, CString()), | |
| 45 http_status_code_(0), | |
| 46 is_cancelled_(false) { | |
| 47 NET_LOG(L3, (_T("[UrlmonRequest::UrlmonRequest]"))); | |
| 48 user_agent_.Format(_T("%s;urlmon"), NetworkConfig::GetUserAgent()); | |
| 49 } | |
| 50 | |
| 51 UrlmonRequest::~UrlmonRequest() { | |
| 52 Close(); | |
| 53 } | |
| 54 | |
| 55 HRESULT UrlmonRequest::Close() { | |
| 56 http_status_code_ = 0; | |
| 57 response_body_.clear(); | |
| 58 raw_response_headers_.Empty(); | |
| 59 response_headers_map_.clear(); | |
| 60 return S_OK; | |
| 61 } | |
| 62 | |
| 63 HRESULT UrlmonRequest::ProcessResponseHeaders( | |
| 64 const CComVariant& headers, | |
| 65 const CComSafeArray<DWORD>& headers_needed) { | |
| 66 if (headers.vt != (VT_ARRAY | VT_BSTR)) { | |
| 67 return E_FAIL; | |
| 68 } | |
| 69 | |
| 70 CComSafeArray<BSTR> response_headers(headers.parray); | |
| 71 DWORD count = response_headers.GetCount(); | |
| 72 if (count != headers_needed.GetCount()) { | |
| 73 return E_FAIL; | |
| 74 } | |
| 75 | |
| 76 response_headers_map_.clear(); | |
| 77 LONG lower_bound = response_headers.GetLowerBound(); | |
| 78 LONG upper_bound = response_headers.GetUpperBound(); | |
| 79 for (int i = lower_bound; i <= upper_bound; ++i) { | |
| 80 response_headers_map_.insert(std::make_pair(headers_needed[i], | |
| 81 response_headers[i])); | |
| 82 NET_LOG(L3, (_T("[ProcessResponseHeaders][%d][%s]"), | |
| 83 i, response_headers[i])); | |
| 84 } | |
| 85 raw_response_headers_ = response_headers_map_[WINHTTP_QUERY_RAW_HEADERS_CRLF]; | |
| 86 return S_OK; | |
| 87 } | |
| 88 | |
| 89 HRESULT UrlmonRequest::ProcessResponseFile(const CComBSTR& cache_filename) { | |
| 90 if (!cache_filename.Length()) { | |
| 91 // Response with no body. | |
| 92 return S_OK; | |
| 93 } | |
| 94 | |
| 95 if (!filename_.IsEmpty()) { | |
| 96 // The caller expects the response to be stored in the target file. | |
| 97 return File::Copy(cache_filename, filename_, true); | |
| 98 } | |
| 99 | |
| 100 response_body_.clear(); | |
| 101 HRESULT hr = ReadEntireFileShareMode(cache_filename, | |
| 102 0, | |
| 103 FILE_SHARE_READ, | |
| 104 &response_body_); | |
| 105 if (FAILED(hr) || response_body_.empty()) { | |
| 106 return hr; | |
| 107 } | |
| 108 return S_OK; | |
| 109 } | |
| 110 | |
| 111 HRESULT UrlmonRequest::SendRequest(BSTR url, | |
| 112 BSTR post_data, | |
| 113 BSTR request_headers, | |
| 114 VARIANT response_headers_needed, | |
| 115 CComVariant* response_headers, | |
| 116 DWORD* response_code, | |
| 117 BSTR* cache_filename) { | |
| 118 HRESULT hr = bsc_.Send(url, | |
| 119 post_data, | |
| 120 request_headers, | |
| 121 response_headers_needed, | |
| 122 response_headers, | |
| 123 response_code, | |
| 124 cache_filename); | |
| 125 | |
| 126 NET_LOG(L3, (_T("[UrlmonRequest::SendRequest][0x%x][%d][%s]"), | |
| 127 hr, *response_code, *cache_filename)); | |
| 128 if (!*response_code) { | |
| 129 return FAILED(hr) ? hr : E_UNEXPECTED; | |
| 130 } | |
| 131 | |
| 132 return S_OK; | |
| 133 } | |
| 134 | |
| 135 HRESULT UrlmonRequest::Send() { | |
| 136 NET_LOG(L3, (_T("[UrlmonRequest::Send]"))); | |
| 137 if (is_cancelled_) { | |
| 138 return GOOPDATE_E_CANCELLED; | |
| 139 } | |
| 140 | |
| 141 ASSERT1(url_.Length() > 0); | |
| 142 CComBSTR headers_to_send(additional_headers_); | |
| 143 CComBSTR post_data; | |
| 144 if (request_buffer_) { | |
| 145 post_data.AppendBytes(static_cast<const char*>(request_buffer_), | |
| 146 request_buffer_length_); | |
| 147 } | |
| 148 CComSafeArray<DWORD> headers_needed(arraysize(response_headers_needed)); | |
| 149 for (size_t i = 0; i < arraysize(response_headers_needed); ++i) { | |
| 150 headers_needed.SetAt(i, response_headers_needed[i]); | |
| 151 } | |
| 152 CComVariant response_headers; | |
| 153 CComBSTR cache_filename; | |
| 154 | |
| 155 HRESULT hr = SendRequest(url_, | |
| 156 post_data, | |
| 157 headers_to_send, | |
| 158 CComVariant(headers_needed), | |
| 159 &response_headers, | |
| 160 &http_status_code_, | |
| 161 &cache_filename); | |
| 162 if (FAILED(hr)) { | |
| 163 NET_LOG(LW, (_T("[UrlmonRequest::Send][0x%08x]"), hr)); | |
| 164 return hr; | |
| 165 } | |
| 166 | |
| 167 VERIFY1(SUCCEEDED(ProcessResponseHeaders(response_headers, headers_needed))); | |
| 168 VERIFY1(SUCCEEDED(ProcessResponseFile(cache_filename))); | |
| 169 return S_OK; | |
| 170 } | |
| 171 | |
| 172 HRESULT UrlmonRequest::Cancel() { | |
| 173 NET_LOG(L2, (_T("[UrlmonRequest::Cancel]"))); | |
| 174 ::InterlockedExchange(&is_cancelled_, true); | |
| 175 return bsc_.Cancel(); | |
| 176 } | |
| 177 | |
| 178 HRESULT UrlmonRequest::Pause() { | |
| 179 return E_NOTIMPL; | |
| 180 } | |
| 181 | |
| 182 HRESULT UrlmonRequest::Resume() { | |
| 183 return E_NOTIMPL; | |
| 184 } | |
| 185 | |
| 186 } // namespace omaha | |
| 187 | |
| OLD | NEW |