blob: 896b40e6a94edd2ac5883eb627bb31c80707ca1d [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_HTTPCOMMON_H__
12#define WEBRTC_BASE_HTTPCOMMON_H__
13
14#include <map>
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include <string>
17#include <vector>
18#include "webrtc/base/basictypes.h"
nissea9dd4a12017-01-13 07:08:34 -080019#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020#include "webrtc/base/common.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#include "webrtc/base/stringutils.h"
22#include "webrtc/base/stream.h"
23
24namespace rtc {
25
26class CryptString;
27class SocketAddress;
28
29//////////////////////////////////////////////////////////////////////
30// Constants
31//////////////////////////////////////////////////////////////////////
32
33enum HttpCode {
34 HC_OK = 200,
35 HC_NON_AUTHORITATIVE = 203,
36 HC_NO_CONTENT = 204,
37 HC_PARTIAL_CONTENT = 206,
38
39 HC_MULTIPLE_CHOICES = 300,
40 HC_MOVED_PERMANENTLY = 301,
41 HC_FOUND = 302,
42 HC_SEE_OTHER = 303,
43 HC_NOT_MODIFIED = 304,
44 HC_MOVED_TEMPORARILY = 307,
45
46 HC_BAD_REQUEST = 400,
47 HC_UNAUTHORIZED = 401,
48 HC_FORBIDDEN = 403,
49 HC_NOT_FOUND = 404,
50 HC_PROXY_AUTHENTICATION_REQUIRED = 407,
51 HC_GONE = 410,
52
53 HC_INTERNAL_SERVER_ERROR = 500,
54 HC_NOT_IMPLEMENTED = 501,
55 HC_SERVICE_UNAVAILABLE = 503,
56};
57
58enum HttpVersion {
59 HVER_1_0, HVER_1_1, HVER_UNKNOWN,
60 HVER_LAST = HVER_UNKNOWN
61};
62
63enum HttpVerb {
64 HV_GET, HV_POST, HV_PUT, HV_DELETE, HV_CONNECT, HV_HEAD,
65 HV_LAST = HV_HEAD
66};
67
68enum HttpError {
69 HE_NONE,
70 HE_PROTOCOL, // Received non-valid HTTP data
71 HE_DISCONNECTED, // Connection closed unexpectedly
72 HE_OVERFLOW, // Received too much data for internal buffers
73 HE_CONNECT_FAILED, // The socket failed to connect.
74 HE_SOCKET_ERROR, // An error occurred on a connected socket
75 HE_SHUTDOWN, // Http object is being destroyed
76 HE_OPERATION_CANCELLED, // Connection aborted locally
77 HE_AUTH, // Proxy Authentication Required
78 HE_CERTIFICATE_EXPIRED, // During SSL negotiation
79 HE_STREAM, // Problem reading or writing to the document
80 HE_CACHE, // Problem reading from cache
81 HE_DEFAULT
82};
83
84enum HttpHeader {
85 HH_AGE,
86 HH_CACHE_CONTROL,
87 HH_CONNECTION,
88 HH_CONTENT_DISPOSITION,
89 HH_CONTENT_LENGTH,
90 HH_CONTENT_RANGE,
91 HH_CONTENT_TYPE,
92 HH_COOKIE,
93 HH_DATE,
94 HH_ETAG,
95 HH_EXPIRES,
96 HH_HOST,
97 HH_IF_MODIFIED_SINCE,
98 HH_IF_NONE_MATCH,
99 HH_KEEP_ALIVE,
100 HH_LAST_MODIFIED,
101 HH_LOCATION,
102 HH_PROXY_AUTHENTICATE,
103 HH_PROXY_AUTHORIZATION,
104 HH_PROXY_CONNECTION,
105 HH_RANGE,
106 HH_SET_COOKIE,
107 HH_TE,
108 HH_TRAILERS,
109 HH_TRANSFER_ENCODING,
110 HH_UPGRADE,
111 HH_USER_AGENT,
112 HH_WWW_AUTHENTICATE,
113 HH_LAST = HH_WWW_AUTHENTICATE
114};
115
Peter Boström0c4e06b2015-10-07 12:23:21 +0200116const uint16_t HTTP_DEFAULT_PORT = 80;
117const uint16_t HTTP_SECURE_PORT = 443;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118
119//////////////////////////////////////////////////////////////////////
120// Utility Functions
121//////////////////////////////////////////////////////////////////////
122
123inline HttpError mkerr(HttpError err, HttpError def_err = HE_DEFAULT) {
124 return (err != HE_NONE) ? err : def_err;
125}
126
127const char* ToString(HttpVersion version);
128bool FromString(HttpVersion& version, const std::string& str);
129
130const char* ToString(HttpVerb verb);
131bool FromString(HttpVerb& verb, const std::string& str);
132
133const char* ToString(HttpHeader header);
134bool FromString(HttpHeader& header, const std::string& str);
135
Peter Boström0c4e06b2015-10-07 12:23:21 +0200136inline bool HttpCodeIsInformational(uint32_t code) {
137 return ((code / 100) == 1);
138}
139inline bool HttpCodeIsSuccessful(uint32_t code) {
140 return ((code / 100) == 2);
141}
142inline bool HttpCodeIsRedirection(uint32_t code) {
143 return ((code / 100) == 3);
144}
145inline bool HttpCodeIsClientError(uint32_t code) {
146 return ((code / 100) == 4);
147}
148inline bool HttpCodeIsServerError(uint32_t code) {
149 return ((code / 100) == 5);
150}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151
Peter Boström0c4e06b2015-10-07 12:23:21 +0200152bool HttpCodeHasBody(uint32_t code);
153bool HttpCodeIsCacheable(uint32_t code);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154bool HttpHeaderIsEndToEnd(HttpHeader header);
155bool HttpHeaderIsCollapsible(HttpHeader header);
156
157struct HttpData;
158bool HttpShouldKeepAlive(const HttpData& data);
159
160typedef std::pair<std::string, std::string> HttpAttribute;
161typedef std::vector<HttpAttribute> HttpAttributeList;
162void HttpComposeAttributes(const HttpAttributeList& attributes, char separator,
163 std::string* composed);
164void HttpParseAttributes(const char * data, size_t len,
165 HttpAttributeList& attributes);
166bool HttpHasAttribute(const HttpAttributeList& attributes,
167 const std::string& name,
168 std::string* value);
169bool HttpHasNthAttribute(HttpAttributeList& attributes,
170 size_t index,
171 std::string* name,
172 std::string* value);
173
174// Convert RFC1123 date (DoW, DD Mon YYYY HH:MM:SS TZ) to unix timestamp
175bool HttpDateToSeconds(const std::string& date, time_t* seconds);
176
Peter Boström0c4e06b2015-10-07 12:23:21 +0200177inline uint16_t HttpDefaultPort(bool secure) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178 return secure ? HTTP_SECURE_PORT : HTTP_DEFAULT_PORT;
179}
180
181// Returns the http server notation for a given address
182std::string HttpAddress(const SocketAddress& address, bool secure);
183
184// functional for insensitive std::string compare
185struct iless {
186 bool operator()(const std::string& lhs, const std::string& rhs) const {
187 return (::_stricmp(lhs.c_str(), rhs.c_str()) < 0);
188 }
189};
190
191// put quotes around a string and escape any quotes inside it
192std::string quote(const std::string& str);
193
194//////////////////////////////////////////////////////////////////////
195// Url
196//////////////////////////////////////////////////////////////////////
197
198template<class CTYPE>
199class Url {
200public:
201 typedef typename Traits<CTYPE>::string string;
202
203 // TODO: Implement Encode/Decode
204 static int Encode(const CTYPE* source, CTYPE* destination, size_t len);
205 static int Encode(const string& source, string& destination);
206 static int Decode(const CTYPE* source, CTYPE* destination, size_t len);
207 static int Decode(const string& source, string& destination);
208
209 Url(const string& url) { do_set_url(url.c_str(), url.size()); }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200210 Url(const string& path, const string& host, uint16_t port = HTTP_DEFAULT_PORT)
211 : host_(host), port_(port), secure_(HTTP_SECURE_PORT == port) {
212 set_full_path(path);
213 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214
215 bool valid() const { return !host_.empty(); }
216 void clear() {
217 host_.clear();
218 port_ = HTTP_DEFAULT_PORT;
219 secure_ = false;
220 path_.assign(1, static_cast<CTYPE>('/'));
221 query_.clear();
222 }
223
224 void set_url(const string& val) {
225 do_set_url(val.c_str(), val.size());
226 }
227 string url() const {
228 string val; do_get_url(&val); return val;
229 }
230
231 void set_address(const string& val) {
232 do_set_address(val.c_str(), val.size());
233 }
234 string address() const {
235 string val; do_get_address(&val); return val;
236 }
237
238 void set_full_path(const string& val) {
239 do_set_full_path(val.c_str(), val.size());
240 }
241 string full_path() const {
242 string val; do_get_full_path(&val); return val;
243 }
244
245 void set_host(const string& val) { host_ = val; }
246 const string& host() const { return host_; }
247
Peter Boström0c4e06b2015-10-07 12:23:21 +0200248 void set_port(uint16_t val) { port_ = val; }
249 uint16_t port() const { return port_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000250
251 void set_secure(bool val) { secure_ = val; }
252 bool secure() const { return secure_; }
253
254 void set_path(const string& val) {
255 if (val.empty()) {
256 path_.assign(1, static_cast<CTYPE>('/'));
257 } else {
nissea9dd4a12017-01-13 07:08:34 -0800258 RTC_DCHECK(val[0] == static_cast<CTYPE>('/'));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 path_ = val;
260 }
261 }
262 const string& path() const { return path_; }
263
264 void set_query(const string& val) {
nissea9dd4a12017-01-13 07:08:34 -0800265 RTC_DCHECK(val.empty() || (val[0] == static_cast<CTYPE>('?')));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000266 query_ = val;
267 }
268 const string& query() const { return query_; }
269
270 bool get_attribute(const string& name, string* value) const;
271
272private:
273 void do_set_url(const CTYPE* val, size_t len);
274 void do_set_address(const CTYPE* val, size_t len);
275 void do_set_full_path(const CTYPE* val, size_t len);
276
277 void do_get_url(string* val) const;
278 void do_get_address(string* val) const;
279 void do_get_full_path(string* val) const;
280
281 string host_, path_, query_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200282 uint16_t port_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 bool secure_;
284};
285
286//////////////////////////////////////////////////////////////////////
287// HttpData
288//////////////////////////////////////////////////////////////////////
289
290struct HttpData {
291 typedef std::multimap<std::string, std::string, iless> HeaderMap;
292 typedef HeaderMap::const_iterator const_iterator;
293 typedef HeaderMap::iterator iterator;
294
295 HttpVersion version;
jbauch555604a2016-04-26 03:13:22 -0700296 std::unique_ptr<StreamInterface> document;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000298 HttpData();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000299
300 enum HeaderCombine { HC_YES, HC_NO, HC_AUTO, HC_REPLACE, HC_NEW };
301 void changeHeader(const std::string& name, const std::string& value,
302 HeaderCombine combine);
303 inline void addHeader(const std::string& name, const std::string& value,
304 bool append = true) {
305 changeHeader(name, value, append ? HC_AUTO : HC_NO);
306 }
307 inline void setHeader(const std::string& name, const std::string& value,
308 bool overwrite = true) {
309 changeHeader(name, value, overwrite ? HC_REPLACE : HC_NEW);
310 }
311 // Returns count of erased headers
312 size_t clearHeader(const std::string& name);
313 // Returns iterator to next header
314 iterator clearHeader(iterator header);
315
316 // keep in mind, this may not do what you want in the face of multiple headers
317 bool hasHeader(const std::string& name, std::string* value) const;
318
319 inline const_iterator begin() const {
320 return headers_.begin();
321 }
322 inline const_iterator end() const {
323 return headers_.end();
324 }
325 inline iterator begin() {
326 return headers_.begin();
327 }
328 inline iterator end() {
329 return headers_.end();
330 }
331 inline const_iterator begin(const std::string& name) const {
332 return headers_.lower_bound(name);
333 }
334 inline const_iterator end(const std::string& name) const {
335 return headers_.upper_bound(name);
336 }
337 inline iterator begin(const std::string& name) {
338 return headers_.lower_bound(name);
339 }
340 inline iterator end(const std::string& name) {
341 return headers_.upper_bound(name);
342 }
343
344 // Convenience methods using HttpHeader
345 inline void changeHeader(HttpHeader header, const std::string& value,
346 HeaderCombine combine) {
347 changeHeader(ToString(header), value, combine);
348 }
349 inline void addHeader(HttpHeader header, const std::string& value,
350 bool append = true) {
351 addHeader(ToString(header), value, append);
352 }
353 inline void setHeader(HttpHeader header, const std::string& value,
354 bool overwrite = true) {
355 setHeader(ToString(header), value, overwrite);
356 }
357 inline void clearHeader(HttpHeader header) {
358 clearHeader(ToString(header));
359 }
360 inline bool hasHeader(HttpHeader header, std::string* value) const {
361 return hasHeader(ToString(header), value);
362 }
363 inline const_iterator begin(HttpHeader header) const {
364 return headers_.lower_bound(ToString(header));
365 }
366 inline const_iterator end(HttpHeader header) const {
367 return headers_.upper_bound(ToString(header));
368 }
369 inline iterator begin(HttpHeader header) {
370 return headers_.lower_bound(ToString(header));
371 }
372 inline iterator end(HttpHeader header) {
373 return headers_.upper_bound(ToString(header));
374 }
375
376 void setContent(const std::string& content_type, StreamInterface* document);
377 void setDocumentAndLength(StreamInterface* document);
378
379 virtual size_t formatLeader(char* buffer, size_t size) const = 0;
380 virtual HttpError parseLeader(const char* line, size_t len) = 0;
381
382protected:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000383 virtual ~HttpData();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000384 void clear(bool release_document);
385 void copy(const HttpData& src);
386
387private:
388 HeaderMap headers_;
389};
390
391struct HttpRequestData : public HttpData {
392 HttpVerb verb;
393 std::string path;
394
395 HttpRequestData() : verb(HV_GET) { }
396
397 void clear(bool release_document);
398 void copy(const HttpRequestData& src);
399
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000400 size_t formatLeader(char* buffer, size_t size) const override;
401 HttpError parseLeader(const char* line, size_t len) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402
403 bool getAbsoluteUri(std::string* uri) const;
404 bool getRelativeUri(std::string* host, std::string* path) const;
405};
406
407struct HttpResponseData : public HttpData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200408 uint32_t scode;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000409 std::string message;
410
411 HttpResponseData() : scode(HC_INTERNAL_SERVER_ERROR) { }
412 void clear(bool release_document);
413 void copy(const HttpResponseData& src);
414
415 // Convenience methods
Peter Boström0c4e06b2015-10-07 12:23:21 +0200416 void set_success(uint32_t scode = HC_OK);
417 void set_success(const std::string& content_type,
418 StreamInterface* document,
419 uint32_t scode = HC_OK);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000420 void set_redirect(const std::string& location,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200421 uint32_t scode = HC_MOVED_TEMPORARILY);
422 void set_error(uint32_t scode);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000423
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000424 size_t formatLeader(char* buffer, size_t size) const override;
425 HttpError parseLeader(const char* line, size_t len) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000426};
427
428struct HttpTransaction {
429 HttpRequestData request;
430 HttpResponseData response;
431};
432
433//////////////////////////////////////////////////////////////////////
434// Http Authentication
435//////////////////////////////////////////////////////////////////////
436
437struct HttpAuthContext {
438 std::string auth_method;
439 HttpAuthContext(const std::string& auth) : auth_method(auth) { }
440 virtual ~HttpAuthContext() { }
441};
442
443enum HttpAuthResult { HAR_RESPONSE, HAR_IGNORE, HAR_CREDENTIALS, HAR_ERROR };
444
445// 'context' is used by this function to record information between calls.
446// Start by passing a null pointer, then pass the same pointer each additional
447// call. When the authentication attempt is finished, delete the context.
448HttpAuthResult HttpAuthenticate(
449 const char * challenge, size_t len,
450 const SocketAddress& server,
451 const std::string& method, const std::string& uri,
452 const std::string& username, const CryptString& password,
453 HttpAuthContext *& context, std::string& response, std::string& auth_method);
454
455//////////////////////////////////////////////////////////////////////
456
457} // namespace rtc
458
459#endif // WEBRTC_BASE_HTTPCOMMON_H__