| OLD | NEW |
| (Empty) |
| 1 // Copyright 2007-2009 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 // | |
| 17 // The code below is not thread safe. | |
| 18 | |
| 19 #include "omaha/net/http_client.h" | |
| 20 | |
| 21 #include "omaha/base/debug.h" | |
| 22 #include "omaha/base/safe_format.h" | |
| 23 | |
| 24 namespace omaha { | |
| 25 | |
| 26 HttpClient::Factory* HttpClient::factory_ = NULL; | |
| 27 | |
| 28 HttpClient::Factory& HttpClient::GetFactory() { | |
| 29 if (!factory_) { | |
| 30 factory_ = new Factory(); | |
| 31 } | |
| 32 return *factory_; | |
| 33 } | |
| 34 | |
| 35 void HttpClient::DeleteFactory() { | |
| 36 delete factory_; | |
| 37 factory_ = NULL; | |
| 38 } | |
| 39 | |
| 40 HttpClient* CreateHttpClient() { | |
| 41 HttpClient* http_client = | |
| 42 HttpClient::GetFactory().CreateObject(HttpClient::WINHTTP); | |
| 43 if (!http_client) { | |
| 44 http_client = HttpClient::GetFactory().CreateObject(HttpClient::WININET); | |
| 45 } | |
| 46 return http_client; | |
| 47 } | |
| 48 | |
| 49 CString HttpClient::BuildRequestHeader(const TCHAR* name, const TCHAR* value) { | |
| 50 ASSERT1(name && *name); | |
| 51 ASSERT1(value && *value); | |
| 52 CString header; | |
| 53 SafeCStringFormat(&header, _T("%s: %s\r\n"), name, value); | |
| 54 return header; | |
| 55 } | |
| 56 | |
| 57 HttpClient::StatusCodeClass HttpClient::GetStatusCodeClass(int status_code) { | |
| 58 ASSERT1(!status_code || | |
| 59 (HTTP_STATUS_FIRST <= status_code && status_code <= HTTP_STATUS_LAST)); | |
| 60 return static_cast<StatusCodeClass>(status_code / 100 * 100); | |
| 61 } | |
| 62 | |
| 63 HRESULT HttpClient::QueryHeadersString(HINTERNET request_handle, | |
| 64 uint32 info_level, | |
| 65 const TCHAR* name, | |
| 66 CString* value, | |
| 67 DWORD* index) { | |
| 68 ASSERT1(value); | |
| 69 | |
| 70 DWORD num_bytes = 0; | |
| 71 HRESULT hr = QueryHeaders(request_handle, | |
| 72 info_level, | |
| 73 name, | |
| 74 NULL, | |
| 75 &num_bytes, | |
| 76 index); | |
| 77 if (hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)) { | |
| 78 return hr; | |
| 79 } | |
| 80 CString val; | |
| 81 hr = QueryHeaders(request_handle, | |
| 82 info_level, | |
| 83 name, | |
| 84 val.GetBuffer(num_bytes/sizeof(TCHAR)), | |
| 85 &num_bytes, | |
| 86 index); | |
| 87 if (FAILED(hr)) { | |
| 88 return hr; | |
| 89 } | |
| 90 ASSERT1(num_bytes); | |
| 91 val.ReleaseBufferSetLength(num_bytes/sizeof(TCHAR)); | |
| 92 *value = val; | |
| 93 return S_OK; | |
| 94 } | |
| 95 | |
| 96 HRESULT HttpClient::QueryHeadersInt(HINTERNET request_handle, | |
| 97 uint32 info_level, | |
| 98 const TCHAR* name, | |
| 99 int* value, | |
| 100 DWORD* index) { | |
| 101 ASSERT1(value); | |
| 102 info_level |= WINHTTP_QUERY_FLAG_NUMBER; | |
| 103 DWORD value_size = sizeof(*value); | |
| 104 HRESULT hr = QueryHeaders(request_handle, | |
| 105 info_level, | |
| 106 name, | |
| 107 value, | |
| 108 &value_size, | |
| 109 index); | |
| 110 if (FAILED(hr)) { | |
| 111 return hr; | |
| 112 } | |
| 113 ASSERT1(value_size == sizeof(*value)); | |
| 114 return S_OK; | |
| 115 } | |
| 116 | |
| 117 HRESULT HttpClient::QueryOptionString(HINTERNET handle, | |
| 118 uint32 option, | |
| 119 CString* value) { | |
| 120 ASSERT1(value); | |
| 121 DWORD num_bytes = 0; | |
| 122 HRESULT hr = QueryOption(handle, option, NULL, &num_bytes); | |
| 123 DWORD last_error = ::GetLastError(); | |
| 124 if (hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)) { | |
| 125 return hr; | |
| 126 } | |
| 127 ASSERT1(num_bytes); | |
| 128 CString val; | |
| 129 hr = QueryOption(handle, option, val.GetBuffer(num_bytes), &num_bytes); | |
| 130 if (FAILED(hr)) { | |
| 131 return hr; | |
| 132 } | |
| 133 ASSERT1(num_bytes); | |
| 134 val.ReleaseBufferSetLength(num_bytes/sizeof(TCHAR)); | |
| 135 *value = val; | |
| 136 return S_OK; | |
| 137 } | |
| 138 | |
| 139 HRESULT HttpClient::QueryOptionInt(HINTERNET handle, | |
| 140 uint32 option, | |
| 141 int* value) { | |
| 142 ASSERT1(value); | |
| 143 DWORD val = 0; | |
| 144 DWORD num_bytes = sizeof(val); | |
| 145 HRESULT hr = QueryOption(handle, option, &val, &num_bytes); | |
| 146 if (FAILED(hr)) { | |
| 147 return hr; | |
| 148 } | |
| 149 ASSERT1(num_bytes == sizeof(val)); | |
| 150 *value = val; | |
| 151 return S_OK; | |
| 152 } | |
| 153 | |
| 154 HRESULT HttpClient::SetOptionString(HINTERNET handle, | |
| 155 uint32 option, | |
| 156 const TCHAR* value) { | |
| 157 ASSERT1(value); | |
| 158 const void* buffer = value; | |
| 159 DWORD buffer_length = _tcslen(value) * sizeof(TCHAR); | |
| 160 return SetOption(handle, option, buffer, buffer_length); | |
| 161 } | |
| 162 | |
| 163 HRESULT HttpClient::SetOptionInt(HINTERNET handle, uint32 option, int value) { | |
| 164 DWORD val = value; | |
| 165 return SetOption(handle, option, &val, sizeof(val)); | |
| 166 } | |
| 167 | |
| 168 } // namespace omaha | |
| 169 | |
| OLD | NEW |