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 | |
10 #include <glib.h> | 12 #include <glib.h> |
11 #include "base/basictypes.h" | 13 #include <base/basictypes.h> |
petkov
2010/11/19 05:37:15
sort
adlr
2010/11/20 02:52:29
Done.
| |
14 #include <base/logging.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 explicit HttpFetcher(ProxyResolver* proxy_resolver) |
petkov
2010/11/19 05:37:15
a brief docstring might be useful -- can proxy_res
adlr
2010/11/20 02:52:29
decided to allow NULL and documented it as such.
| |
27 : post_data_set_(false), | 32 : post_data_set_(false), |
28 http_response_code_(0), | 33 http_response_code_(0), |
29 delegate_(NULL) {} | 34 delegate_(NULL), |
35 proxies_(1, kNoProxy), | |
36 proxy_resolver_(proxy_resolver) {} | |
30 virtual ~HttpFetcher() {} | 37 virtual ~HttpFetcher() {} |
31 | 38 |
32 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } | 39 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } |
33 HttpFetcherDelegate* delegate() const { return delegate_; } | 40 HttpFetcherDelegate* delegate() const { return delegate_; } |
34 int http_response_code() const { return http_response_code_; } | 41 int http_response_code() const { return http_response_code_; } |
35 | 42 |
36 // Optional: Post data to the server. The HttpFetcher should make a copy | 43 // 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. | 44 // of this data and upload it via HTTP POST during the transfer. |
38 void SetPostData(const void* data, size_t size) { | 45 void SetPostData(const void* data, size_t size) { |
39 post_data_set_ = true; | 46 post_data_set_ = true; |
40 post_data_.clear(); | 47 post_data_.clear(); |
41 const char *char_data = reinterpret_cast<const char*>(data); | 48 const char *char_data = reinterpret_cast<const char*>(data); |
42 post_data_.insert(post_data_.end(), char_data, char_data + size); | 49 post_data_.insert(post_data_.end(), char_data, char_data + size); |
43 } | 50 } |
44 | 51 |
52 // Proxy methods to set the proxies, then to pop them off. | |
53 void ResolveProxiesForUrl(const std::string& url) { | |
54 if (!proxy_resolver_) { | |
petkov
2010/11/19 05:37:15
Lots of code for a header file. I'd suggest move a
adlr
2010/11/20 02:52:29
Done.
| |
55 LOG(ERROR) << "Missing proxy resolver!"; | |
petkov
2010/11/19 05:37:15
Why an ERROR? Maybe just default to kNoProxy. If n
adlr
2010/11/20 02:52:29
fixed. NULL now legal
| |
56 } | |
57 std::vector<std::string> proxies; | |
58 if (!proxy_resolver_->GetProxiesForUrl(url, &proxies)) { | |
59 // just do direct | |
60 proxies.push_back(kNoProxy); | |
61 } | |
62 SetProxies(proxies); | |
63 } | |
64 void SetProxies(const std::vector<std::string>& proxies) { | |
65 proxies_.clear(); | |
66 proxies_.insert(proxies_.begin(), proxies.begin(), proxies.end()); | |
petkov
2010/11/19 05:37:15
you could probably just proxies_.assign(begin, end
adlr
2010/11/20 02:52:29
Done.
| |
67 } | |
68 const std::string& CurrentProxy() const { | |
69 return proxies_.front(); | |
70 } | |
71 bool HasProxy() const { return !proxies_.empty(); } | |
72 void PopProxy() { proxies_.pop_front(); } | |
73 | |
45 // Downloading should resume from this offset | 74 // Downloading should resume from this offset |
46 virtual void SetOffset(off_t offset) = 0; | 75 virtual void SetOffset(off_t offset) = 0; |
47 | 76 |
48 // Begins the transfer to the specified URL. This fetcher instance should not | 77 // Begins the transfer to the specified URL. This fetcher instance should not |
49 // be destroyed until either TransferComplete, or TransferTerminated is | 78 // be destroyed until either TransferComplete, or TransferTerminated is |
50 // called. | 79 // called. |
51 virtual void BeginTransfer(const std::string& url) = 0; | 80 virtual void BeginTransfer(const std::string& url) = 0; |
52 | 81 |
53 // Aborts the transfer. The transfer may not abort right away -- delegate's | 82 // Aborts the transfer. The transfer may not abort right away -- delegate's |
54 // TransferTerminated() will be called when the transfer is actually done. | 83 // TransferTerminated() will be called when the transfer is actually done. |
(...skipping 22 matching lines...) Expand all Loading... | |
77 bool post_data_set_; | 106 bool post_data_set_; |
78 std::vector<char> post_data_; | 107 std::vector<char> post_data_; |
79 | 108 |
80 // The server's HTTP response code from the last transfer. This | 109 // 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 | 110 // field should be set to 0 when a new transfer is initiated, and |
82 // set to the response code when the transfer is complete. | 111 // set to the response code when the transfer is complete. |
83 int http_response_code_; | 112 int http_response_code_; |
84 | 113 |
85 // The delegate; may be NULL. | 114 // The delegate; may be NULL. |
86 HttpFetcherDelegate* delegate_; | 115 HttpFetcherDelegate* delegate_; |
116 | |
117 // Proxy servers | |
118 std::deque<std::string> proxies_; | |
119 | |
120 ProxyResolver* const proxy_resolver_; | |
121 | |
87 private: | 122 private: |
88 DISALLOW_COPY_AND_ASSIGN(HttpFetcher); | 123 DISALLOW_COPY_AND_ASSIGN(HttpFetcher); |
89 }; | 124 }; |
90 | 125 |
91 // Interface for delegates | 126 // Interface for delegates |
92 class HttpFetcherDelegate { | 127 class HttpFetcherDelegate { |
93 public: | 128 public: |
94 // Called every time bytes are received. | 129 // Called every time bytes are received. |
95 virtual void ReceivedBytes(HttpFetcher* fetcher, | 130 virtual void ReceivedBytes(HttpFetcher* fetcher, |
96 const char* bytes, | 131 const char* bytes, |
97 int length) = 0; | 132 int length) = 0; |
98 | 133 |
99 // Called if the fetcher seeks to a particular offset. | 134 // Called if the fetcher seeks to a particular offset. |
100 virtual void SeekToOffset(off_t offset) {} | 135 virtual void SeekToOffset(off_t offset) {} |
101 | 136 |
102 // Called when the transfer has completed successfully or been aborted through | 137 // Called when the transfer has completed successfully or been aborted through |
103 // means other than TerminateTransfer. It's OK to destroy the |fetcher| object | 138 // means other than TerminateTransfer. It's OK to destroy the |fetcher| object |
104 // in this callback. | 139 // in this callback. |
105 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; | 140 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; |
106 | 141 |
107 // Called when the transfer has been aborted through TerminateTransfer. It's | 142 // Called when the transfer has been aborted through TerminateTransfer. It's |
108 // OK to destroy the |fetcher| object in this callback. | 143 // OK to destroy the |fetcher| object in this callback. |
109 virtual void TransferTerminated(HttpFetcher* fetcher) {} | 144 virtual void TransferTerminated(HttpFetcher* fetcher) {} |
110 }; | 145 }; |
111 | 146 |
112 } // namespace chromeos_update_engine | 147 } // namespace chromeos_update_engine |
113 | 148 |
114 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ | 149 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
OLD | NEW |