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 // The class structure is as following: | |
17 // - NetworkRequest and the underlying NetworkRequestImpl provide fault | |
18 // tolerant client server http transactions. | |
19 // - HttpRequestInterface defines an interface for different mechanisms that | |
20 // can move bytes between the client and the server. These mechanisms are | |
21 // chained up so that the control passes from one mechanism to the next | |
22 // until one of them is able to fulfill the request or an error is | |
23 // generated. Currently, SimpleRequest and BitsRequest are provided. | |
24 // - HttpClient is the c++ wrapper over winhttp-wininet. | |
25 | |
26 #ifndef OMAHA_NET_NETWORK_REQUEST_IMPL_H__ | |
27 #define OMAHA_NET_NETWORK_REQUEST_IMPL_H__ | |
28 | |
29 #include <windows.h> | |
30 #include <atlstr.h> | |
31 #include <vector> | |
32 #include "base/basictypes.h" | |
33 #include "base/scoped_ptr.h" | |
34 #include "omaha/base/scoped_any.h" | |
35 #include "omaha/base/synchronized.h" | |
36 #include "omaha/net/network_config.h" | |
37 #include "omaha/net/network_request.h" | |
38 #include "omaha/net/http_request.h" | |
39 | |
40 namespace omaha { | |
41 | |
42 namespace detail { | |
43 | |
44 class NetworkRequestImpl { | |
45 public: | |
46 explicit NetworkRequestImpl(const NetworkConfig::Session& network_session); | |
47 ~NetworkRequestImpl(); | |
48 | |
49 HRESULT Close(); | |
50 | |
51 void AddHttpRequest(HttpRequestInterface* http_request); | |
52 | |
53 HRESULT Post(const CString& url, | |
54 const void* buffer, | |
55 size_t length, | |
56 std::vector<uint8>* response); | |
57 HRESULT Get(const CString& url, std::vector<uint8>* response); | |
58 HRESULT DownloadFile(const CString& url, const CString& filename); | |
59 | |
60 HRESULT Pause(); | |
61 HRESULT Resume(); | |
62 HRESULT Cancel(); | |
63 | |
64 void AddHeader(const TCHAR* name, const TCHAR* value); | |
65 | |
66 HRESULT QueryHeadersString(uint32 info_level, | |
67 const TCHAR* name, | |
68 CString* value); | |
69 | |
70 int http_status_code() const { return http_status_code_; } | |
71 | |
72 CString response_headers() const { return response_headers_; } | |
73 | |
74 void set_proxy_auth_config(const ProxyAuthConfig& proxy_auth_config) { | |
75 proxy_auth_config_ = proxy_auth_config; | |
76 } | |
77 | |
78 void set_num_retries(int num_retries) { num_retries_ = num_retries; } | |
79 | |
80 void set_time_between_retries(int time_between_retries_ms) { | |
81 time_between_retries_ms_ = time_between_retries_ms; | |
82 } | |
83 | |
84 void set_callback(NetworkRequestCallback* callback) { | |
85 callback_ = callback; | |
86 } | |
87 | |
88 void set_low_priority(bool low_priority) { low_priority_ = low_priority; } | |
89 | |
90 void set_proxy_configuration(const ProxyConfig* proxy_configuration) { | |
91 if (proxy_configuration) { | |
92 proxy_configuration_.reset(new ProxyConfig); | |
93 *proxy_configuration_ = *proxy_configuration; | |
94 } else { | |
95 proxy_configuration_.reset(); | |
96 } | |
97 } | |
98 | |
99 void set_preserve_protocol(bool preserve_protocol) { | |
100 preserve_protocol_ = preserve_protocol; | |
101 } | |
102 | |
103 CString trace() const { return trace_; } | |
104 | |
105 // Detects the available proxy configurations and returns the chain of | |
106 // configurations to be used. | |
107 void DetectProxyConfiguration( | |
108 std::vector<ProxyConfig>* proxy_configurations) const; | |
109 | |
110 private: | |
111 // Resets the state of the output data members. | |
112 void Reset(); | |
113 | |
114 // Sends the request with a retry policy. This is the only function that | |
115 // modifies the state of the output data members: the status code, the | |
116 // response headers, and the response. When errors are encountered, the | |
117 // output data members contain the values corresponding to first network | |
118 // configuration and first HttpRequest instance in the fallback chain. | |
119 HRESULT DoSendWithRetries(); | |
120 | |
121 // Sends a single request and receives the response. | |
122 HRESULT DoSend(int* http_status_code, | |
123 CString* response_headers, | |
124 std::vector<uint8>* response) const; | |
125 | |
126 // Sends the request using the current configuration. The request is tried for | |
127 // each HttpRequestInterface in the fallback chain until one of them succeeds | |
128 // or the end of the chain is reached. | |
129 HRESULT DoSendWithConfig(int* http_status_code, | |
130 CString* response_headers, | |
131 std::vector<uint8>* response) const; | |
132 | |
133 // Sends an http request using the current HttpRequest interface over the | |
134 // current network configuration. | |
135 HRESULT DoSendHttpRequest(int* http_status_code, | |
136 CString* response_headers, | |
137 std::vector<uint8>* response) const; | |
138 | |
139 // Builds headers for the current HttpRequest and network configuration. | |
140 CString BuildPerRequestHeaders() const; | |
141 | |
142 // Specifies the chain of HttpRequestInterface to handle the request. | |
143 std::vector<HttpRequestInterface*> http_request_chain_; | |
144 | |
145 // Specifies the detected proxy configurations. | |
146 std::vector<ProxyConfig> proxy_configurations_; | |
147 | |
148 // Specifies the proxy configuration override. When set, the proxy | |
149 // configurations are not auto detected. | |
150 scoped_ptr<ProxyConfig> proxy_configuration_; | |
151 | |
152 // Input data members. | |
153 // The request and response buffers are owner by the caller. | |
154 CString url_; | |
155 const void* request_buffer_; // Contains the request body for POST. | |
156 size_t request_buffer_length_; // Length of the request body. | |
157 CString filename_; // Contains the response for downloads. | |
158 CString additional_headers_; // Headers common to all requests. | |
159 // Each header is separated by \r\n. | |
160 ProxyAuthConfig proxy_auth_config_; | |
161 int num_retries_; | |
162 bool low_priority_; | |
163 int time_between_retries_ms_; | |
164 | |
165 // Output data members. | |
166 int http_status_code_; | |
167 CString response_headers_; // Each header is separated by \r\n. | |
168 std::vector<uint8>* response_; // Contains the response for Post and Get. | |
169 | |
170 const NetworkConfig::Session network_session_; | |
171 NetworkRequestCallback* callback_; | |
172 | |
173 // The http request and the network configuration currently in use. | |
174 mutable HttpRequestInterface* cur_http_request_; | |
175 mutable const ProxyConfig* cur_proxy_config_; | |
176 | |
177 // The HRESULT and HTTP status code updated by the prior | |
178 // DoSendHttpRequest() call. | |
179 mutable HRESULT last_hr_; | |
180 mutable int last_http_status_code_; | |
181 | |
182 // The current retry count defined by the outermost DoSendWithRetries() call. | |
183 int cur_retry_count_; | |
184 | |
185 volatile LONG is_canceled_; | |
186 scoped_event event_cancel_; | |
187 | |
188 LLock lock_; | |
189 | |
190 bool preserve_protocol_; | |
191 | |
192 // Contains the trace of the request as handled by the fallback chain. | |
193 mutable CString trace_; | |
194 | |
195 static const int kDefaultTimeBetweenRetriesMs = 5000; // 5 seconds. | |
196 static const int kTimeBetweenRetriesMultiplier = 2; | |
197 | |
198 DISALLOW_EVIL_CONSTRUCTORS(NetworkRequestImpl); | |
199 }; | |
200 | |
201 HRESULT PostRequest(NetworkRequest* network_request, | |
202 bool fallback_to_https, | |
203 const CString& url, | |
204 const CString& request_string, | |
205 std::vector<uint8>* response); | |
206 | |
207 HRESULT GetRequest(NetworkRequest* network_request, | |
208 const CString& url, | |
209 std::vector<uint8>* response); | |
210 | |
211 } // namespace detail | |
212 | |
213 } // namespace omaha | |
214 | |
215 #endif // OMAHA_NET_NETWORK_REQUEST_IMPL_H__ | |
216 | |
OLD | NEW |