OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
7 | 7 |
8 #include <deque> | |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
11 | |
12 #include <base/basictypes.h> | |
13 #include <base/logging.h> | |
10 #include <glib.h> | 14 #include <glib.h> |
11 #include "base/basictypes.h" | 15 |
16 #include "update_engine/proxy_resolver.h" | |
12 | 17 |
13 // This class is a simple wrapper around an HTTP library (libcurl). We can | 18 // This class is a simple wrapper around an HTTP library (libcurl). We can |
14 // easily mock out this interface for testing. | 19 // easily mock out this interface for testing. |
15 | 20 |
16 // Implementations of this class should use asynchronous i/o. They can access | 21 // Implementations of this class should use asynchronous i/o. They can access |
17 // the glib main loop to request callbacks when timers or file descriptors | 22 // the glib main loop to request callbacks when timers or file descriptors |
18 // change. | 23 // change. |
19 | 24 |
20 namespace chromeos_update_engine { | 25 namespace chromeos_update_engine { |
21 | 26 |
22 class HttpFetcherDelegate; | 27 class HttpFetcherDelegate; |
23 | 28 |
24 class HttpFetcher { | 29 class HttpFetcher { |
25 public: | 30 public: |
26 HttpFetcher() | 31 // |proxy_resolver| is the resolver that will be consulted for proxy |
32 // settings. It may be null, in which case direct connections will | |
33 // be used. Does not take ownership of the resolver. | |
34 explicit HttpFetcher(ProxyResolver* proxy_resolver) | |
27 : post_data_set_(false), | 35 : post_data_set_(false), |
28 http_response_code_(0), | 36 http_response_code_(0), |
29 delegate_(NULL) {} | 37 delegate_(NULL), |
38 proxies_(1, kNoProxy), | |
39 proxy_resolver_(proxy_resolver) {} | |
30 virtual ~HttpFetcher() {} | 40 virtual ~HttpFetcher() {} |
31 | 41 |
32 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } | 42 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } |
33 HttpFetcherDelegate* delegate() const { return delegate_; } | 43 HttpFetcherDelegate* delegate() const { return delegate_; } |
34 int http_response_code() const { return http_response_code_; } | 44 int http_response_code() const { return http_response_code_; } |
35 | 45 |
36 // Optional: Post data to the server. The HttpFetcher should make a copy | 46 // Optional: Post data to the server. The HttpFetcher should make a copy |
37 // of this data and upload it via HTTP POST during the transfer. | 47 // of this data and upload it via HTTP POST during the transfer. |
38 void SetPostData(const void* data, size_t size) { | 48 void SetPostData(const void* data, size_t size); |
39 post_data_set_ = true; | 49 |
40 post_data_.clear(); | 50 // Proxy methods to set the proxies, then to pop them off. |
41 const char *char_data = reinterpret_cast<const char*>(data); | 51 void ResolveProxiesForUrl(const std::string& url); |
42 post_data_.insert(post_data_.end(), char_data, char_data + size); | 52 |
53 void SetProxies(const std::vector<std::string>& proxies) { | |
54 proxies_.assign(proxies.begin(), proxies.end()); | |
43 } | 55 } |
56 const std::string& CurrentProxy() const { | |
petkov
2010/11/22 17:49:00
This should be GetCurrentProxy.
adlr
2010/11/22 18:54:46
Done.
| |
57 return proxies_.front(); | |
58 } | |
59 bool HasProxy() const { return !proxies_.empty(); } | |
60 void PopProxy() { proxies_.pop_front(); } | |
44 | 61 |
45 // Downloading should resume from this offset | 62 // Downloading should resume from this offset |
46 virtual void SetOffset(off_t offset) = 0; | 63 virtual void SetOffset(off_t offset) = 0; |
47 | 64 |
48 // Begins the transfer to the specified URL. This fetcher instance should not | 65 // Begins the transfer to the specified URL. This fetcher instance should not |
49 // be destroyed until either TransferComplete, or TransferTerminated is | 66 // be destroyed until either TransferComplete, or TransferTerminated is |
50 // called. | 67 // called. |
51 virtual void BeginTransfer(const std::string& url) = 0; | 68 virtual void BeginTransfer(const std::string& url) = 0; |
52 | 69 |
53 // Aborts the transfer. The transfer may not abort right away -- delegate's | 70 // Aborts the transfer. The transfer may not abort right away -- delegate's |
(...skipping 23 matching lines...) Expand all Loading... | |
77 bool post_data_set_; | 94 bool post_data_set_; |
78 std::vector<char> post_data_; | 95 std::vector<char> post_data_; |
79 | 96 |
80 // The server's HTTP response code from the last transfer. This | 97 // The server's HTTP response code from the last transfer. This |
81 // field should be set to 0 when a new transfer is initiated, and | 98 // field should be set to 0 when a new transfer is initiated, and |
82 // set to the response code when the transfer is complete. | 99 // set to the response code when the transfer is complete. |
83 int http_response_code_; | 100 int http_response_code_; |
84 | 101 |
85 // The delegate; may be NULL. | 102 // The delegate; may be NULL. |
86 HttpFetcherDelegate* delegate_; | 103 HttpFetcherDelegate* delegate_; |
104 | |
105 // Proxy servers | |
106 std::deque<std::string> proxies_; | |
107 | |
108 ProxyResolver* const proxy_resolver_; | |
109 | |
87 private: | 110 private: |
88 DISALLOW_COPY_AND_ASSIGN(HttpFetcher); | 111 DISALLOW_COPY_AND_ASSIGN(HttpFetcher); |
89 }; | 112 }; |
90 | 113 |
91 // Interface for delegates | 114 // Interface for delegates |
92 class HttpFetcherDelegate { | 115 class HttpFetcherDelegate { |
93 public: | 116 public: |
94 // Called every time bytes are received. | 117 // Called every time bytes are received. |
95 virtual void ReceivedBytes(HttpFetcher* fetcher, | 118 virtual void ReceivedBytes(HttpFetcher* fetcher, |
96 const char* bytes, | 119 const char* bytes, |
97 int length) = 0; | 120 int length) = 0; |
98 | 121 |
99 // Called if the fetcher seeks to a particular offset. | 122 // Called if the fetcher seeks to a particular offset. |
100 virtual void SeekToOffset(off_t offset) {} | 123 virtual void SeekToOffset(off_t offset) {} |
101 | 124 |
102 // Called when the transfer has completed successfully or been aborted through | 125 // Called when the transfer has completed successfully or been aborted through |
103 // means other than TerminateTransfer. It's OK to destroy the |fetcher| object | 126 // means other than TerminateTransfer. It's OK to destroy the |fetcher| object |
104 // in this callback. | 127 // in this callback. |
105 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; | 128 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; |
106 | 129 |
107 // Called when the transfer has been aborted through TerminateTransfer. It's | 130 // Called when the transfer has been aborted through TerminateTransfer. It's |
108 // OK to destroy the |fetcher| object in this callback. | 131 // OK to destroy the |fetcher| object in this callback. |
109 virtual void TransferTerminated(HttpFetcher* fetcher) {} | 132 virtual void TransferTerminated(HttpFetcher* fetcher) {} |
110 }; | 133 }; |
111 | 134 |
112 } // namespace chromeos_update_engine | 135 } // namespace chromeos_update_engine |
113 | 136 |
114 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ | 137 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
OLD | NEW |