| 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 // | |
| 17 // TODO(omaha): implement a way to get the last network error. | |
| 18 // | |
| 19 // TODO(omaha): might have to do some working regarding the cookie handling. | |
| 20 // WinHttp keeps a per session cookie cache. We are currently tearing down | |
| 21 // the session after each request, so cookies are lost. Normally we don't | |
| 22 // need cookies at Omaha except for CUP. | |
| 23 | |
| 24 #ifndef OMAHA_NET_NETWORK_REQUEST_H__ | |
| 25 #define OMAHA_NET_NETWORK_REQUEST_H__ | |
| 26 | |
| 27 #include <windows.h> | |
| 28 #include <winhttp.h> | |
| 29 #include <atlstr.h> | |
| 30 #include <vector> | |
| 31 #include "base/basictypes.h" | |
| 32 #include "base/scoped_ptr.h" | |
| 33 #include "omaha/base/string.h" | |
| 34 #include "omaha/base/time.h" | |
| 35 #include "omaha/net/network_config.h" | |
| 36 | |
| 37 namespace omaha { | |
| 38 | |
| 39 namespace detail { | |
| 40 | |
| 41 class NetworkRequestImpl; | |
| 42 | |
| 43 } // namespace detail | |
| 44 | |
| 45 class NetworkRequestCallback { | |
| 46 public: | |
| 47 virtual ~NetworkRequestCallback() {} | |
| 48 | |
| 49 // Notifies download begins. This gives the callee a chance to initialize its | |
| 50 // download state, such as resetting the download progress. | |
| 51 virtual void OnRequestBegin() = 0; | |
| 52 | |
| 53 // Indicates the progress of the NetworkRequest. | |
| 54 // | |
| 55 // bytes - Current number of bytes transferred, relative to the expected | |
| 56 // maximum indicated by the bytes_total. | |
| 57 // bytes_total - The expected total number of bytes to transfer. This is | |
| 58 // usually the content length value or 0 if the content length | |
| 59 // is not available. | |
| 60 // status - WinHttp status codes regarding the progress of the request. | |
| 61 // status_text - Additional information, when available. | |
| 62 virtual void OnProgress(int bytes, int bytes_total, | |
| 63 int status, const TCHAR* status_text) = 0; | |
| 64 | |
| 65 virtual void OnRequestRetryScheduled(time64 next_retry_time) = 0; | |
| 66 }; | |
| 67 | |
| 68 class HttpRequestInterface; | |
| 69 | |
| 70 // NetworkRequest is the main interface to the net module. The semantics of | |
| 71 // the interface is defined as transferring bytes from a url, with an optional | |
| 72 // request body, to a destination specified as a memory buffer or a file. | |
| 73 // NetworkRequest encapsulates the retry logic for a request, the fall back to | |
| 74 // different network configurations, and ultimately the fallback to different | |
| 75 // network mechanisms. The calls are blocking calls. One instance of the class | |
| 76 // is responsible for one request only. | |
| 77 // The client of NetworkRequest is responsible for configuring the network | |
| 78 // fallbacks. This gives the caller a lot of flexibility over the fallbacks but | |
| 79 // it requires more work to properly set the fallback chain. | |
| 80 class NetworkRequest { | |
| 81 public: | |
| 82 explicit NetworkRequest(const NetworkConfig::Session& network_session); | |
| 83 ~NetworkRequest(); | |
| 84 | |
| 85 // Cancels the request. Cancel can be called from a different thread. | |
| 86 // Calling Cancel makes the control return to the caller of Post or Get | |
| 87 // methods. The object can't be reused once it is canceled. | |
| 88 HRESULT Cancel(); | |
| 89 | |
| 90 // Closes the request and releases the request resources, such as handles or | |
| 91 // interface pointers. Unlike Cancel, the request object can be reused | |
| 92 // after closing. | |
| 93 HRESULT Close(); | |
| 94 | |
| 95 // Adds an instance of HttpRequestInterface at the end of the fallback | |
| 96 // chain of http requests. This class takes ownership of the http_request | |
| 97 // object. | |
| 98 void AddHttpRequest(HttpRequestInterface* http_request); | |
| 99 | |
| 100 // Post, Get, and DownloadFile return S_OK if the request completed | |
| 101 // successfully and the response is available to the caller. For errors, | |
| 102 // the status code can be checked. | |
| 103 // | |
| 104 // Posts a buffer to a url. | |
| 105 HRESULT Post(const CString& url, | |
| 106 const void* buffer, | |
| 107 size_t length, | |
| 108 std::vector<uint8>* response); | |
| 109 | |
| 110 // Posts an UTF-8 string to a url. | |
| 111 HRESULT PostUtf8String(const CString& url, | |
| 112 const CStringA& request, | |
| 113 std::vector<uint8>* response) { | |
| 114 const uint8* buffer = reinterpret_cast<const uint8*>(request.GetString()); | |
| 115 size_t length = request.GetLength(); | |
| 116 return Post(url, buffer, length, response); | |
| 117 } | |
| 118 | |
| 119 // Posts a Unicode string to a url. | |
| 120 HRESULT PostString(const CString& url, | |
| 121 const CString& request, | |
| 122 std::vector<uint8>* response) { | |
| 123 return PostUtf8String(url, WideToUtf8(request), response); | |
| 124 } | |
| 125 | |
| 126 // Gets a url. | |
| 127 HRESULT Get(const CString& url, std::vector<uint8>* response); | |
| 128 | |
| 129 // Downloads a url to a file. | |
| 130 HRESULT DownloadFile(const CString& url, const CString& filename); | |
| 131 | |
| 132 // Enables a separate thread to temporarily stops the network downloading. | |
| 133 // The downloading thread will be blocked inside NetworkRequest::Post/Get | |
| 134 // infinitely by an event until Resume/Cancel/Close is called. | |
| 135 HRESULT Pause(); | |
| 136 | |
| 137 // Enables a separate thread to resume current network request if it is in | |
| 138 // pause state. | |
| 139 HRESULT Resume(); | |
| 140 | |
| 141 // Adds a request header. The header with the same name is only added once. | |
| 142 // The method is only good enough for what Omaha needs and it is not good | |
| 143 // for general purpose header manipulation, which is quite sophisticated. | |
| 144 void AddHeader(const TCHAR* name, const TCHAR* value); | |
| 145 | |
| 146 // Queries a response header. This is the companion for the AddHeader | |
| 147 // method above. | |
| 148 HRESULT QueryHeadersString(uint32 info_level, | |
| 149 const TCHAR* name, | |
| 150 CString* value); | |
| 151 | |
| 152 // Returns the http status code if available. | |
| 153 int http_status_code() const; | |
| 154 | |
| 155 // Returns the response headers, separated by \r\n. | |
| 156 CString response_headers() const; | |
| 157 | |
| 158 // Returns a trace of the request for logging purposes. | |
| 159 CString trace() const; | |
| 160 | |
| 161 void set_proxy_auth_config(const ProxyAuthConfig& proxy_auth_config); | |
| 162 | |
| 163 // Sets the number of retries for the request. The retry mechanism uses | |
| 164 // exponential backoff to decrease the rate of the retries. | |
| 165 void set_num_retries(int num_retries); | |
| 166 | |
| 167 void set_time_between_retries(int time_between_retries_ms); | |
| 168 | |
| 169 // Sets an external observer for the request. The ownership of the callback | |
| 170 // remains with the caller. The current implementation provides callback | |
| 171 // notification for DownloadFile only. | |
| 172 void set_callback(NetworkRequestCallback* callback); | |
| 173 | |
| 174 // Sets the priority of the request. Currently, only BITS requests support | |
| 175 // prioritization of requests. | |
| 176 void set_low_priority(bool low_priority); | |
| 177 | |
| 178 // Overrides detecting the network configuration and uses the configuration | |
| 179 // specified. If parameter is NULL, it defaults to detecting the configuration | |
| 180 // automatically. | |
| 181 void set_proxy_configuration(const ProxyConfig* proxy_configuration); | |
| 182 | |
| 183 void set_preserve_protocol(bool preserve_protocol); | |
| 184 | |
| 185 private: | |
| 186 // Uses pimpl idiom to minimize dependencies on implementation details. | |
| 187 scoped_ptr<detail::NetworkRequestImpl> impl_; | |
| 188 DISALLOW_EVIL_CONSTRUCTORS(NetworkRequest); | |
| 189 }; | |
| 190 | |
| 191 | |
| 192 // Posts a request, falling back from http to https if the http request failed. | |
| 193 // Returns S_OK if the request is successfully sent. Returns the error | |
| 194 // corresponding to the http request in case of errors. | |
| 195 HRESULT PostRequest(NetworkRequest* network_request, | |
| 196 bool fallback_to_https, | |
| 197 const CString& url, | |
| 198 const CString& request_string, | |
| 199 std::vector<uint8>* response); | |
| 200 | |
| 201 // Gets a request. | |
| 202 HRESULT GetRequest(NetworkRequest* network_request, | |
| 203 const CString& url, | |
| 204 std::vector<uint8>* response); | |
| 205 | |
| 206 } // namespace omaha | |
| 207 | |
| 208 #endif // OMAHA_NET_NETWORK_REQUEST_H__ | |
| 209 | |
| OLD | NEW |