| OLD | NEW |
| (Empty) |
| 1 // Copyright 2007-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 // SimpleRequest provides for http transactions using WinHttp/WinInet. | |
| 17 // | |
| 18 // TODO(omaha): the class interface is not stable yet, as a few more | |
| 19 // getters and setters are still needed. | |
| 20 // | |
| 21 // TODO(omaha): receiving a response into a file is not implemented yet. | |
| 22 | |
| 23 #ifndef OMAHA_NET_SIMPLE_REQUEST_H_ | |
| 24 #define OMAHA_NET_SIMPLE_REQUEST_H_ | |
| 25 | |
| 26 #include <atlstr.h> | |
| 27 #include <vector> | |
| 28 #include "base/basictypes.h" | |
| 29 #include "base/scoped_ptr.h" | |
| 30 #include "omaha/base/debug.h" | |
| 31 #include "omaha/base/scoped_any.h" | |
| 32 #include "omaha/base/synchronized.h" | |
| 33 #include "omaha/net/http_request.h" | |
| 34 #include "omaha/net/network_config.h" | |
| 35 | |
| 36 namespace omaha { | |
| 37 | |
| 38 class WinHttpAdapter; | |
| 39 | |
| 40 class SimpleRequest : public HttpRequestInterface { | |
| 41 public: | |
| 42 SimpleRequest(); | |
| 43 virtual ~SimpleRequest(); | |
| 44 | |
| 45 virtual HRESULT Close(); | |
| 46 | |
| 47 virtual HRESULT Send(); | |
| 48 | |
| 49 virtual HRESULT Cancel(); | |
| 50 | |
| 51 virtual HRESULT Pause(); | |
| 52 | |
| 53 virtual HRESULT Resume(); | |
| 54 | |
| 55 virtual std::vector<uint8> GetResponse() const; | |
| 56 | |
| 57 virtual int GetHttpStatusCode() const { | |
| 58 return request_state_.get() ? request_state_->http_status_code : 0; | |
| 59 } | |
| 60 | |
| 61 virtual HRESULT QueryHeadersString(uint32 info_level, | |
| 62 const TCHAR* name, | |
| 63 CString* value) const; | |
| 64 | |
| 65 virtual CString GetResponseHeaders() const; | |
| 66 | |
| 67 virtual CString ToString() const { return _T("winhttp"); } | |
| 68 | |
| 69 virtual void set_session_handle(HINTERNET session_handle) { | |
| 70 session_handle_ = session_handle; | |
| 71 } | |
| 72 | |
| 73 virtual void set_url(const CString& url); | |
| 74 | |
| 75 virtual void set_request_buffer(const void* buffer, size_t buffer_length) { | |
| 76 request_buffer_ = buffer; | |
| 77 request_buffer_length_ = buffer_length; | |
| 78 } | |
| 79 | |
| 80 virtual void set_proxy_configuration(const ProxyConfig& proxy_config) { | |
| 81 proxy_config_ = proxy_config; | |
| 82 } | |
| 83 | |
| 84 // Sets the filename to receive the response instead of the memory buffer. | |
| 85 virtual void set_filename(const CString& filename); | |
| 86 | |
| 87 virtual void set_low_priority(bool low_priority) { | |
| 88 low_priority_ = low_priority; | |
| 89 } | |
| 90 | |
| 91 virtual void set_callback(NetworkRequestCallback* callback) { | |
| 92 callback_ = callback; | |
| 93 } | |
| 94 | |
| 95 virtual void set_additional_headers(const CString& additional_headers) { | |
| 96 additional_headers_ = additional_headers; | |
| 97 } | |
| 98 | |
| 99 // This request always uses the specified protocol so it is fine to ignore | |
| 100 // this attribute. | |
| 101 virtual void set_preserve_protocol(bool preserve_protocol) { | |
| 102 UNREFERENCED_PARAMETER(preserve_protocol); | |
| 103 } | |
| 104 | |
| 105 virtual CString user_agent() const { return user_agent_; } | |
| 106 | |
| 107 virtual void set_user_agent(const CString& user_agent) { | |
| 108 user_agent_ = user_agent; | |
| 109 } | |
| 110 | |
| 111 virtual void set_proxy_auth_config(const ProxyAuthConfig& proxy_auth_config) { | |
| 112 proxy_auth_config_ = proxy_auth_config; | |
| 113 } | |
| 114 | |
| 115 private: | |
| 116 HRESULT OpenDestinationFile(HANDLE* file_handle); | |
| 117 HRESULT PrepareRequest(HANDLE* file_handle); | |
| 118 HRESULT Connect(); | |
| 119 HRESULT SendRequest(); | |
| 120 HRESULT ReceiveData(HANDLE file_handle); | |
| 121 HRESULT RequestData(HANDLE file_handle); | |
| 122 bool IsResumeNeeded() const; | |
| 123 bool IsPauseSupported() const; | |
| 124 | |
| 125 void LogResponseHeaders(); | |
| 126 | |
| 127 // Sets proxy information for the request. | |
| 128 void SetProxyInformation(); | |
| 129 | |
| 130 struct TransientRequestState; | |
| 131 void CloseHandles(); | |
| 132 | |
| 133 static uint32 ChooseProxyAuthScheme(uint32 supported_schemes); | |
| 134 | |
| 135 // Returns true if the request is a POST request, in other words, if there | |
| 136 // is a request buffer to be sent to the server. | |
| 137 bool IsPostRequest() const { return request_buffer_ != NULL; } | |
| 138 | |
| 139 // When in pause state, caller will be blocked until Resume() is called. | |
| 140 // Returns immediately otherwise. | |
| 141 void WaitForResumeEvent(); | |
| 142 | |
| 143 // Holds the transient state corresponding to a single http request. We | |
| 144 // prefer to isolate the state of a request to avoid dirty state. | |
| 145 struct TransientRequestState { | |
| 146 TransientRequestState() | |
| 147 : port(0), | |
| 148 http_status_code(0), | |
| 149 proxy_authentication_scheme(0), | |
| 150 is_https(false), | |
| 151 content_length(0), | |
| 152 current_bytes(0) {} | |
| 153 | |
| 154 CString scheme; | |
| 155 CString server; | |
| 156 int port; | |
| 157 CString url_path; | |
| 158 bool is_https; | |
| 159 | |
| 160 std::vector<uint8> response; | |
| 161 int http_status_code; | |
| 162 uint32 proxy_authentication_scheme; | |
| 163 CString proxy; | |
| 164 CString proxy_bypass; | |
| 165 int content_length; | |
| 166 int current_bytes; | |
| 167 }; | |
| 168 | |
| 169 LLock lock_; | |
| 170 volatile bool is_canceled_; | |
| 171 volatile bool is_closed_; | |
| 172 volatile bool pause_happened_; | |
| 173 LLock ready_to_pause_lock_; | |
| 174 HINTERNET session_handle_; // Not owned by this class. | |
| 175 CString url_; | |
| 176 CString filename_; | |
| 177 const void* request_buffer_; // Contains the request body for POST. | |
| 178 size_t request_buffer_length_; // Length of the request body. | |
| 179 CString additional_headers_; | |
| 180 CString user_agent_; | |
| 181 ProxyAuthConfig proxy_auth_config_; | |
| 182 ProxyConfig proxy_config_; | |
| 183 bool low_priority_; | |
| 184 NetworkRequestCallback* callback_; | |
| 185 scoped_ptr<WinHttpAdapter> winhttp_adapter_; | |
| 186 scoped_ptr<TransientRequestState> request_state_; | |
| 187 scoped_event event_resume_; | |
| 188 bool download_completed_; | |
| 189 | |
| 190 DISALLOW_COPY_AND_ASSIGN(SimpleRequest); | |
| 191 }; | |
| 192 | |
| 193 } // namespace omaha | |
| 194 | |
| 195 #endif // OMAHA_NET_SIMPLE_REQUEST_H_ | |
| 196 | |
| OLD | NEW |