blob: 0c19d2efb4353dedea37a674c51fff0bcd6df24b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_HTTPCLIENT_H__
12#define WEBRTC_BASE_HTTPCLIENT_H__
13
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
15
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/common.h"
17#include "webrtc/base/httpbase.h"
18#include "webrtc/base/nethelpers.h"
19#include "webrtc/base/proxyinfo.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020#include "webrtc/base/sigslot.h"
21#include "webrtc/base/socketaddress.h"
22#include "webrtc/base/socketpool.h"
23
24namespace rtc {
25
26//////////////////////////////////////////////////////////////////////
27// Client-specific http utilities
28//////////////////////////////////////////////////////////////////////
29
30// Write cache-relevant response headers to output stream. If size is non-null,
31// it contains the length of the output in bytes. output may be null if only
32// the length is desired.
33bool HttpWriteCacheHeaders(const HttpResponseData* response,
34 StreamInterface* output, size_t* size);
35// Read cached headers from a stream, and them merge them into the response
36// object using the specified combine operation.
37bool HttpReadCacheHeaders(StreamInterface* input,
38 HttpResponseData* response,
39 HttpData::HeaderCombine combine);
40
41//////////////////////////////////////////////////////////////////////
42// HttpClient
43// Implements an HTTP 1.1 client.
44//////////////////////////////////////////////////////////////////////
45
46class DiskCache;
47class HttpClient;
48class IPNetPool;
49
50class SignalThread;
51// What to do: Define STRICT_HTTP_ERROR=1 in your makefile. Use HttpError in
52// your code (HttpErrorType should only be used for code that is shared
53// with groups which have not yet migrated).
54#if STRICT_HTTP_ERROR
55typedef HttpError HttpErrorType;
56#else // !STRICT_HTTP_ERROR
57typedef int HttpErrorType;
58#endif // !STRICT_HTTP_ERROR
59
60class HttpClient : private IHttpNotify, public sigslot::has_slots<> {
61public:
62 // If HttpRequestData and HttpResponseData objects are provided, they must
63 // be freed by the caller. Otherwise, an internal object is allocated.
64 HttpClient(const std::string& agent, StreamPool* pool,
65 HttpTransaction* transaction = NULL);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000066 ~HttpClient() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
68 void set_pool(StreamPool* pool) { pool_ = pool; }
69
70 void set_agent(const std::string& agent) { agent_ = agent; }
71 const std::string& agent() const { return agent_; }
Tommi00aac5a2015-05-25 11:25:59 +020072
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 void set_proxy(const ProxyInfo& proxy) { proxy_ = proxy; }
74 const ProxyInfo& proxy() const { return proxy_; }
75
76 // Request retries occur when the connection closes before the beginning of
77 // an http response is received. In these cases, the http server may have
78 // timed out the keepalive connection before it received our request. Note
79 // that if a request document cannot be rewound, no retry is made. The
80 // default is 1.
81 void set_request_retries(size_t retries) { retries_ = retries; }
82 size_t request_retries() const { return retries_; }
83
84 enum RedirectAction { REDIRECT_DEFAULT, REDIRECT_ALWAYS, REDIRECT_NEVER };
85 void set_redirect_action(RedirectAction action) { redirect_action_ = action; }
86 RedirectAction redirect_action() const { return redirect_action_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087
88 enum UriForm { URI_DEFAULT, URI_ABSOLUTE, URI_RELATIVE };
89 void set_uri_form(UriForm form) { uri_form_ = form; }
90 UriForm uri_form() const { return uri_form_; }
91
92 void set_cache(DiskCache* cache) { ASSERT(!IsCacheActive()); cache_ = cache; }
93 bool cache_enabled() const { return (NULL != cache_); }
94
95 // reset clears the server, request, and response structures. It will also
96 // abort an active request.
97 void reset();
Tommi00aac5a2015-05-25 11:25:59 +020098
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 void set_server(const SocketAddress& address);
100 const SocketAddress& server() const { return server_; }
101
102 // Note: in order for HttpClient to retry a POST in response to
103 // an authentication challenge, a redirect response, or socket disconnection,
104 // the request document must support 'replaying' by calling Rewind() on it.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 HttpTransaction* transaction() { return transaction_; }
106 const HttpTransaction* transaction() const { return transaction_; }
107 HttpRequestData& request() { return transaction_->request; }
108 const HttpRequestData& request() const { return transaction_->request; }
109 HttpResponseData& response() { return transaction_->response; }
110 const HttpResponseData& response() const { return transaction_->response; }
Tommi00aac5a2015-05-25 11:25:59 +0200111
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 // convenience methods
113 void prepare_get(const std::string& url);
114 void prepare_post(const std::string& url, const std::string& content_type,
115 StreamInterface* request_doc);
116
117 // Convert HttpClient to a pull-based I/O model.
118 StreamInterface* GetDocumentStream();
119
120 // After you finish setting up your request, call start.
121 void start();
Tommi00aac5a2015-05-25 11:25:59 +0200122
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 // Signalled when the header has finished downloading, before the document
124 // content is processed. You may change the response document in response
125 // to this signal. The second parameter indicates whether this is an
126 // intermediate (false) or final (true) header. An intermediate header is
127 // one that generates another request, such as a redirect or authentication
128 // challenge. The third parameter indicates the length of the response
129 // document, or else SIZE_UNKNOWN. Note: Do NOT abort the request in response
130 // to this signal.
131 sigslot::signal3<HttpClient*,bool,size_t> SignalHeaderAvailable;
132 // Signalled when the current request finishes. On success, err is 0.
133 sigslot::signal2<HttpClient*,HttpErrorType> SignalHttpClientComplete;
134
135protected:
136 void connect();
137 void release();
138
139 bool ShouldRedirect(std::string* location) const;
140
141 bool BeginCacheFile();
142 HttpError WriteCacheHeaders(const std::string& id);
143 void CompleteCacheFile();
144
145 bool CheckCache();
146 HttpError ReadCacheHeaders(const std::string& id, bool override);
147 HttpError ReadCacheBody(const std::string& id);
148
149 bool PrepareValidate();
150 HttpError CompleteValidate();
151
152 HttpError OnHeaderAvailable(bool ignore_data, bool chunked, size_t data_size);
153
154 void StartDNSLookup();
155 void OnResolveResult(AsyncResolverInterface* resolver);
156
157 // IHttpNotify Interface
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000158 HttpError onHttpHeaderComplete(bool chunked, size_t& data_size) override;
159 void onHttpComplete(HttpMode mode, HttpError err) override;
160 void onHttpClosed(HttpError err) override;
161
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162private:
163 enum CacheState { CS_READY, CS_WRITING, CS_READING, CS_VALIDATING };
164 bool IsCacheActive() const { return (cache_state_ > CS_READY); }
165
166 std::string agent_;
167 StreamPool* pool_;
168 HttpBase base_;
169 SocketAddress server_;
170 ProxyInfo proxy_;
171 HttpTransaction* transaction_;
172 bool free_transaction_;
173 size_t retries_, attempt_, redirects_;
174 RedirectAction redirect_action_;
175 UriForm uri_form_;
jbauch555604a2016-04-26 03:13:22 -0700176 std::unique_ptr<HttpAuthContext> context_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 DiskCache* cache_;
178 CacheState cache_state_;
179 AsyncResolverInterface* resolver_;
180};
181
182//////////////////////////////////////////////////////////////////////
183// HttpClientDefault - Default implementation of HttpClient
184//////////////////////////////////////////////////////////////////////
185
186class HttpClientDefault : public ReuseSocketPool, public HttpClient {
187public:
188 HttpClientDefault(SocketFactory* factory, const std::string& agent,
189 HttpTransaction* transaction = NULL);
190};
191
192//////////////////////////////////////////////////////////////////////
193
194} // namespace rtc
195
196#endif // WEBRTC_BASE_HTTPCLIENT_H__