| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 #ifndef OMAHA_NET_WINHTTP_ADAPTER_H_ | |
| 17 #define OMAHA_NET_WINHTTP_ADAPTER_H_ | |
| 18 | |
| 19 #include <windows.h> | |
| 20 #include "base/basictypes.h" | |
| 21 #include "base/scoped_ptr.h" | |
| 22 #include "omaha/base/scoped_any.h" | |
| 23 #include "omaha/base/synchronized.h" | |
| 24 #include "omaha/net/winhttp.h" | |
| 25 | |
| 26 namespace omaha { | |
| 27 | |
| 28 // Provides a sync-async adapter between the caller and the asynchronous | |
| 29 // WinHttp client. Solves the issue of reliably canceling of WinHttp calls by | |
| 30 // closing the handles and avoding the race condition between handle closing | |
| 31 // and the incoming WinHttp call. | |
| 32 // The class manages the connection and the request handles. It registers a | |
| 33 // callback for all WinHttp status notifications. Once an asynchrous WinHttp | |
| 34 // call is made, the code blocks waiting for the corresponding notification | |
| 35 // to arrive, handle the completion result, and then return to the caller. | |
| 36 // WinHttp is guaranteed to send a notification callback for all asynchronous | |
| 37 // request calls that have succeeded. | |
| 38 // TODO(omaha): consider eliminating this class and implementing the same | |
| 39 // functionality in the WinHttp class. Most likely, another class is needed | |
| 40 // to manage the WinHttp session handle. | |
| 41 class WinHttpAdapter { | |
| 42 public: | |
| 43 WinHttpAdapter(); | |
| 44 | |
| 45 HRESULT Initialize(); | |
| 46 | |
| 47 HRESULT Connect(HINTERNET session_handle, const TCHAR* server, int port); | |
| 48 | |
| 49 HRESULT OpenRequest(const TCHAR* verb, | |
| 50 const TCHAR* uri, | |
| 51 const TCHAR* version, | |
| 52 const TCHAR* referrer, | |
| 53 const TCHAR** accept_types, | |
| 54 uint32 flags); | |
| 55 | |
| 56 HRESULT AddRequestHeaders(const TCHAR* headers, | |
| 57 int length, | |
| 58 uint32 modifiers); | |
| 59 | |
| 60 HRESULT SendRequest(const TCHAR* headers, | |
| 61 DWORD headers_length, | |
| 62 const void* optional_data, | |
| 63 DWORD optional_data_length, | |
| 64 DWORD content_length); | |
| 65 | |
| 66 HRESULT SetCredentials(uint32 auth_targets, | |
| 67 uint32 auth_scheme, | |
| 68 const TCHAR* user_name, | |
| 69 const TCHAR* password); | |
| 70 | |
| 71 HRESULT ReceiveResponse(); | |
| 72 | |
| 73 HRESULT QueryAuthSchemes(uint32* supported_schemes, | |
| 74 uint32* first_scheme, | |
| 75 uint32* auth_target); | |
| 76 | |
| 77 HRESULT QueryRequestHeadersInt(uint32 info_level, | |
| 78 const TCHAR* name, | |
| 79 int* value, | |
| 80 DWORD* index); | |
| 81 | |
| 82 HRESULT QueryRequestHeadersString(uint32 info_level, | |
| 83 const TCHAR* name, | |
| 84 CString* value, | |
| 85 DWORD* index); | |
| 86 | |
| 87 HRESULT QueryDataAvailable(DWORD* num_bytes); | |
| 88 | |
| 89 HRESULT ReadData(void* buffer, DWORD buffer_length, DWORD* bytes_read); | |
| 90 | |
| 91 HRESULT SetRequestOptionInt(uint32 option, int value); | |
| 92 | |
| 93 HRESULT SetRequestOption(uint32 option, | |
| 94 const void* buffer, | |
| 95 DWORD buffer_length); | |
| 96 | |
| 97 void CloseHandles(); | |
| 98 | |
| 99 HRESULT CrackUrl(const TCHAR* url, | |
| 100 uint32 flags, | |
| 101 CString* scheme, | |
| 102 CString* server, | |
| 103 int* port, | |
| 104 CString* url_path, | |
| 105 CString* extra_info) { | |
| 106 return http_client_->CrackUrl( | |
| 107 url, flags, scheme, server, port, url_path, extra_info); | |
| 108 } | |
| 109 | |
| 110 CString server_name() const { return server_name_; } | |
| 111 CString server_ip() const { return server_ip_; } | |
| 112 | |
| 113 private: | |
| 114 | |
| 115 HRESULT AsyncCallBegin(DWORD async_call_type); | |
| 116 HRESULT AsyncCallEnd(DWORD async_call_type); | |
| 117 | |
| 118 void StatusCallback(HINTERNET handle, | |
| 119 uint32 status, | |
| 120 void* info, | |
| 121 uint32 info_len); | |
| 122 | |
| 123 static void __stdcall WinHttpStatusCallback(HINTERNET handle, | |
| 124 uint32 context, | |
| 125 uint32 status, | |
| 126 void* info, | |
| 127 uint32 info_len); | |
| 128 | |
| 129 scoped_ptr<HttpClient> http_client_; | |
| 130 | |
| 131 HINTERNET connection_handle_; | |
| 132 HINTERNET request_handle_; | |
| 133 | |
| 134 CString server_name_; | |
| 135 CString server_ip_; | |
| 136 | |
| 137 DWORD async_call_type_; | |
| 138 bool async_call_is_error_; | |
| 139 WINHTTP_ASYNC_RESULT async_call_result_; | |
| 140 DWORD async_bytes_available_; | |
| 141 DWORD async_bytes_read_; | |
| 142 scoped_event async_completion_event_; | |
| 143 | |
| 144 LLock lock_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(WinHttpAdapter); | |
| 147 }; | |
| 148 | |
| 149 } // namespace omaha | |
| 150 | |
| 151 #endif // OMAHA_NET_WINHTTP_ADAPTER_H_ | |
| 152 | |
| OLD | NEW |