You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_HTTPCOMMON_H__
  28. #define TALK_BASE_HTTPCOMMON_H__
  29. #include <map>
  30. #include <string>
  31. #include <vector>
  32. #include "talk/base/basictypes.h"
  33. #include "talk/base/common.h"
  34. #include "talk/base/scoped_ptr.h"
  35. #include "talk/base/stringutils.h"
  36. #include "talk/base/stream.h"
  37. namespace talk_base {
  38. class CryptString;
  39. class SocketAddress;
  40. //////////////////////////////////////////////////////////////////////
  41. // Constants
  42. //////////////////////////////////////////////////////////////////////
  43. enum HttpCode {
  44. HC_OK = 200,
  45. HC_NON_AUTHORITATIVE = 203,
  46. HC_NO_CONTENT = 204,
  47. HC_PARTIAL_CONTENT = 206,
  48. HC_MULTIPLE_CHOICES = 300,
  49. HC_MOVED_PERMANENTLY = 301,
  50. HC_FOUND = 302,
  51. HC_SEE_OTHER = 303,
  52. HC_NOT_MODIFIED = 304,
  53. HC_MOVED_TEMPORARILY = 307,
  54. HC_BAD_REQUEST = 400,
  55. HC_UNAUTHORIZED = 401,
  56. HC_FORBIDDEN = 403,
  57. HC_NOT_FOUND = 404,
  58. HC_PROXY_AUTHENTICATION_REQUIRED = 407,
  59. HC_GONE = 410,
  60. HC_INTERNAL_SERVER_ERROR = 500,
  61. HC_NOT_IMPLEMENTED = 501,
  62. HC_SERVICE_UNAVAILABLE = 503,
  63. };
  64. enum HttpVersion {
  65. HVER_1_0, HVER_1_1, HVER_UNKNOWN,
  66. HVER_LAST = HVER_UNKNOWN
  67. };
  68. enum HttpVerb {
  69. HV_GET, HV_POST, HV_PUT, HV_DELETE, HV_CONNECT, HV_HEAD,
  70. HV_LAST = HV_HEAD
  71. };
  72. enum HttpError {
  73. HE_NONE,
  74. HE_PROTOCOL, // Received non-valid HTTP data
  75. HE_DISCONNECTED, // Connection closed unexpectedly
  76. HE_OVERFLOW, // Received too much data for internal buffers
  77. HE_CONNECT_FAILED, // The socket failed to connect.
  78. HE_SOCKET_ERROR, // An error occurred on a connected socket
  79. HE_SHUTDOWN, // Http object is being destroyed
  80. HE_OPERATION_CANCELLED, // Connection aborted locally
  81. HE_AUTH, // Proxy Authentication Required
  82. HE_CERTIFICATE_EXPIRED, // During SSL negotiation
  83. HE_STREAM, // Problem reading or writing to the document
  84. HE_CACHE, // Problem reading from cache
  85. HE_DEFAULT
  86. };
  87. enum HttpHeader {
  88. HH_AGE,
  89. HH_CACHE_CONTROL,
  90. HH_CONNECTION,
  91. HH_CONTENT_DISPOSITION,
  92. HH_CONTENT_LENGTH,
  93. HH_CONTENT_RANGE,
  94. HH_CONTENT_TYPE,
  95. HH_COOKIE,
  96. HH_DATE,
  97. HH_ETAG,
  98. HH_EXPIRES,
  99. HH_HOST,
  100. HH_IF_MODIFIED_SINCE,
  101. HH_IF_NONE_MATCH,
  102. HH_KEEP_ALIVE,
  103. HH_LAST_MODIFIED,
  104. HH_LOCATION,
  105. HH_PROXY_AUTHENTICATE,
  106. HH_PROXY_AUTHORIZATION,
  107. HH_PROXY_CONNECTION,
  108. HH_RANGE,
  109. HH_SET_COOKIE,
  110. HH_TE,
  111. HH_TRAILERS,
  112. HH_TRANSFER_ENCODING,
  113. HH_UPGRADE,
  114. HH_USER_AGENT,
  115. HH_WWW_AUTHENTICATE,
  116. HH_LAST = HH_WWW_AUTHENTICATE
  117. };
  118. const uint16 HTTP_DEFAULT_PORT = 80;
  119. const uint16 HTTP_SECURE_PORT = 443;
  120. //////////////////////////////////////////////////////////////////////
  121. // Utility Functions
  122. //////////////////////////////////////////////////////////////////////
  123. inline HttpError mkerr(HttpError err, HttpError def_err = HE_DEFAULT) {
  124. return (err != HE_NONE) ? err : def_err;
  125. }
  126. const char* ToString(HttpVersion version);
  127. bool FromString(HttpVersion& version, const std::string& str);
  128. const char* ToString(HttpVerb verb);
  129. bool FromString(HttpVerb& verb, const std::string& str);
  130. const char* ToString(HttpHeader header);
  131. bool FromString(HttpHeader& header, const std::string& str);
  132. inline bool HttpCodeIsInformational(uint32 code) { return ((code / 100) == 1); }
  133. inline bool HttpCodeIsSuccessful(uint32 code) { return ((code / 100) == 2); }
  134. inline bool HttpCodeIsRedirection(uint32 code) { return ((code / 100) == 3); }
  135. inline bool HttpCodeIsClientError(uint32 code) { return ((code / 100) == 4); }
  136. inline bool HttpCodeIsServerError(uint32 code) { return ((code / 100) == 5); }
  137. bool HttpCodeHasBody(uint32 code);
  138. bool HttpCodeIsCacheable(uint32 code);
  139. bool HttpHeaderIsEndToEnd(HttpHeader header);
  140. bool HttpHeaderIsCollapsible(HttpHeader header);
  141. struct HttpData;
  142. bool HttpShouldKeepAlive(const HttpData& data);
  143. typedef std::pair<std::string, std::string> HttpAttribute;
  144. typedef std::vector<HttpAttribute> HttpAttributeList;
  145. void HttpComposeAttributes(const HttpAttributeList& attributes, char separator,
  146. std::string* composed);
  147. void HttpParseAttributes(const char * data, size_t len,
  148. HttpAttributeList& attributes);
  149. bool HttpHasAttribute(const HttpAttributeList& attributes,
  150. const std::string& name,
  151. std::string* value);
  152. bool HttpHasNthAttribute(HttpAttributeList& attributes,
  153. size_t index,
  154. std::string* name,
  155. std::string* value);
  156. // Convert RFC1123 date (DoW, DD Mon YYYY HH:MM:SS TZ) to unix timestamp
  157. bool HttpDateToSeconds(const std::string& date, time_t* seconds);
  158. inline uint16 HttpDefaultPort(bool secure) {
  159. return secure ? HTTP_SECURE_PORT : HTTP_DEFAULT_PORT;
  160. }
  161. // Returns the http server notation for a given address
  162. std::string HttpAddress(const SocketAddress& address, bool secure);
  163. // functional for insensitive std::string compare
  164. struct iless {
  165. bool operator()(const std::string& lhs, const std::string& rhs) const {
  166. return (::_stricmp(lhs.c_str(), rhs.c_str()) < 0);
  167. }
  168. };
  169. // put quotes around a string and escape any quotes inside it
  170. std::string quote(const std::string& str);
  171. //////////////////////////////////////////////////////////////////////
  172. // Url
  173. //////////////////////////////////////////////////////////////////////
  174. template<class CTYPE>
  175. class Url {
  176. public:
  177. typedef typename Traits<CTYPE>::string string;
  178. // TODO: Implement Encode/Decode
  179. static int Encode(const CTYPE* source, CTYPE* destination, size_t len);
  180. static int Encode(const string& source, string& destination);
  181. static int Decode(const CTYPE* source, CTYPE* destination, size_t len);
  182. static int Decode(const string& source, string& destination);
  183. Url(const string& url) { do_set_url(url.c_str(), url.size()); }
  184. Url(const string& path, const string& host, uint16 port = HTTP_DEFAULT_PORT)
  185. : host_(host), port_(port), secure_(HTTP_SECURE_PORT == port)
  186. { set_full_path(path); }
  187. bool valid() const { return !host_.empty(); }
  188. void clear() {
  189. host_.clear();
  190. port_ = HTTP_DEFAULT_PORT;
  191. secure_ = false;
  192. path_.assign(1, static_cast<CTYPE>('/'));
  193. query_.clear();
  194. }
  195. void set_url(const string& val) {
  196. do_set_url(val.c_str(), val.size());
  197. }
  198. string url() const {
  199. string val; do_get_url(&val); return val;
  200. }
  201. void set_address(const string& val) {
  202. do_set_address(val.c_str(), val.size());
  203. }
  204. string address() const {
  205. string val; do_get_address(&val); return val;
  206. }
  207. void set_full_path(const string& val) {
  208. do_set_full_path(val.c_str(), val.size());
  209. }
  210. string full_path() const {
  211. string val; do_get_full_path(&val); return val;
  212. }
  213. void set_host(const string& val) { host_ = val; }
  214. const string& host() const { return host_; }
  215. void set_port(uint16 val) { port_ = val; }
  216. uint16 port() const { return port_; }
  217. void set_secure(bool val) { secure_ = val; }
  218. bool secure() const { return secure_; }
  219. void set_path(const string& val) {
  220. if (val.empty()) {
  221. path_.assign(1, static_cast<CTYPE>('/'));
  222. } else {
  223. ASSERT(val[0] == static_cast<CTYPE>('/'));
  224. path_ = val;
  225. }
  226. }
  227. const string& path() const { return path_; }
  228. void set_query(const string& val) {
  229. ASSERT(val.empty() || (val[0] == static_cast<CTYPE>('?')));
  230. query_ = val;
  231. }
  232. const string& query() const { return query_; }
  233. bool get_attribute(const string& name, string* value) const;
  234. private:
  235. void do_set_url(const CTYPE* val, size_t len);
  236. void do_set_address(const CTYPE* val, size_t len);
  237. void do_set_full_path(const CTYPE* val, size_t len);
  238. void do_get_url(string* val) const;
  239. void do_get_address(string* val) const;
  240. void do_get_full_path(string* val) const;
  241. string host_, path_, query_;
  242. uint16 port_;
  243. bool secure_;
  244. };
  245. //////////////////////////////////////////////////////////////////////
  246. // HttpData
  247. //////////////////////////////////////////////////////////////////////
  248. struct HttpData {
  249. typedef std::multimap<std::string, std::string, iless> HeaderMap;
  250. typedef HeaderMap::const_iterator const_iterator;
  251. typedef HeaderMap::iterator iterator;
  252. HttpVersion version;
  253. scoped_ptr<StreamInterface> document;
  254. HttpData() : version(HVER_1_1) { }
  255. enum HeaderCombine { HC_YES, HC_NO, HC_AUTO, HC_REPLACE, HC_NEW };
  256. void changeHeader(const std::string& name, const std::string& value,
  257. HeaderCombine combine);
  258. inline void addHeader(const std::string& name, const std::string& value,
  259. bool append = true) {
  260. changeHeader(name, value, append ? HC_AUTO : HC_NO);
  261. }
  262. inline void setHeader(const std::string& name, const std::string& value,
  263. bool overwrite = true) {
  264. changeHeader(name, value, overwrite ? HC_REPLACE : HC_NEW);
  265. }
  266. // Returns count of erased headers
  267. size_t clearHeader(const std::string& name);
  268. // Returns iterator to next header
  269. iterator clearHeader(iterator header);
  270. // keep in mind, this may not do what you want in the face of multiple headers
  271. bool hasHeader(const std::string& name, std::string* value) const;
  272. inline const_iterator begin() const {
  273. return headers_.begin();
  274. }
  275. inline const_iterator end() const {
  276. return headers_.end();
  277. }
  278. inline iterator begin() {
  279. return headers_.begin();
  280. }
  281. inline iterator end() {
  282. return headers_.end();
  283. }
  284. inline const_iterator begin(const std::string& name) const {
  285. return headers_.lower_bound(name);
  286. }
  287. inline const_iterator end(const std::string& name) const {
  288. return headers_.upper_bound(name);
  289. }
  290. inline iterator begin(const std::string& name) {
  291. return headers_.lower_bound(name);
  292. }
  293. inline iterator end(const std::string& name) {
  294. return headers_.upper_bound(name);
  295. }
  296. // Convenience methods using HttpHeader
  297. inline void changeHeader(HttpHeader header, const std::string& value,
  298. HeaderCombine combine) {
  299. changeHeader(ToString(header), value, combine);
  300. }
  301. inline void addHeader(HttpHeader header, const std::string& value,
  302. bool append = true) {
  303. addHeader(ToString(header), value, append);
  304. }
  305. inline void setHeader(HttpHeader header, const std::string& value,
  306. bool overwrite = true) {
  307. setHeader(ToString(header), value, overwrite);
  308. }
  309. inline void clearHeader(HttpHeader header) {
  310. clearHeader(ToString(header));
  311. }
  312. inline bool hasHeader(HttpHeader header, std::string* value) const {
  313. return hasHeader(ToString(header), value);
  314. }
  315. inline const_iterator begin(HttpHeader header) const {
  316. return headers_.lower_bound(ToString(header));
  317. }
  318. inline const_iterator end(HttpHeader header) const {
  319. return headers_.upper_bound(ToString(header));
  320. }
  321. inline iterator begin(HttpHeader header) {
  322. return headers_.lower_bound(ToString(header));
  323. }
  324. inline iterator end(HttpHeader header) {
  325. return headers_.upper_bound(ToString(header));
  326. }
  327. void setContent(const std::string& content_type, StreamInterface* document);
  328. void setDocumentAndLength(StreamInterface* document);
  329. virtual size_t formatLeader(char* buffer, size_t size) const = 0;
  330. virtual HttpError parseLeader(const char* line, size_t len) = 0;
  331. protected:
  332. virtual ~HttpData() { }
  333. void clear(bool release_document);
  334. void copy(const HttpData& src);
  335. private:
  336. HeaderMap headers_;
  337. };
  338. struct HttpRequestData : public HttpData {
  339. HttpVerb verb;
  340. std::string path;
  341. HttpRequestData() : verb(HV_GET) { }
  342. void clear(bool release_document);
  343. void copy(const HttpRequestData& src);
  344. virtual size_t formatLeader(char* buffer, size_t size) const;
  345. virtual HttpError parseLeader(const char* line, size_t len);
  346. bool getAbsoluteUri(std::string* uri) const;
  347. bool getRelativeUri(std::string* host, std::string* path) const;
  348. };
  349. struct HttpResponseData : public HttpData {
  350. uint32 scode;
  351. std::string message;
  352. HttpResponseData() : scode(HC_INTERNAL_SERVER_ERROR) { }
  353. void clear(bool release_document);
  354. void copy(const HttpResponseData& src);
  355. // Convenience methods
  356. void set_success(uint32 scode = HC_OK);
  357. void set_success(const std::string& content_type, StreamInterface* document,
  358. uint32 scode = HC_OK);
  359. void set_redirect(const std::string& location,
  360. uint32 scode = HC_MOVED_TEMPORARILY);
  361. void set_error(uint32 scode);
  362. virtual size_t formatLeader(char* buffer, size_t size) const;
  363. virtual HttpError parseLeader(const char* line, size_t len);
  364. };
  365. struct HttpTransaction {
  366. HttpRequestData request;
  367. HttpResponseData response;
  368. };
  369. //////////////////////////////////////////////////////////////////////
  370. // Http Authentication
  371. //////////////////////////////////////////////////////////////////////
  372. struct HttpAuthContext {
  373. std::string auth_method;
  374. HttpAuthContext(const std::string& auth) : auth_method(auth) { }
  375. virtual ~HttpAuthContext() { }
  376. };
  377. enum HttpAuthResult { HAR_RESPONSE, HAR_IGNORE, HAR_CREDENTIALS, HAR_ERROR };
  378. // 'context' is used by this function to record information between calls.
  379. // Start by passing a null pointer, then pass the same pointer each additional
  380. // call. When the authentication attempt is finished, delete the context.
  381. HttpAuthResult HttpAuthenticate(
  382. const char * challenge, size_t len,
  383. const SocketAddress& server,
  384. const std::string& method, const std::string& uri,
  385. const std::string& username, const CryptString& password,
  386. HttpAuthContext *& context, std::string& response, std::string& auth_method);
  387. //////////////////////////////////////////////////////////////////////
  388. } // namespace talk_base
  389. #endif // TALK_BASE_HTTPCOMMON_H__