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.

219 lines
8.8 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_HTTPCLIENT_H__
  28. #define TALK_BASE_HTTPCLIENT_H__
  29. #include "talk/base/common.h"
  30. #include "talk/base/httpbase.h"
  31. #include "talk/base/nethelpers.h"
  32. #include "talk/base/proxyinfo.h"
  33. #include "talk/base/scoped_ptr.h"
  34. #include "talk/base/sigslot.h"
  35. #include "talk/base/socketaddress.h"
  36. #include "talk/base/socketpool.h"
  37. namespace talk_base {
  38. //////////////////////////////////////////////////////////////////////
  39. // Client-specific http utilities
  40. //////////////////////////////////////////////////////////////////////
  41. // Write cache-relevant response headers to output stream. If size is non-null,
  42. // it contains the length of the output in bytes. output may be null if only
  43. // the length is desired.
  44. bool HttpWriteCacheHeaders(const HttpResponseData* response,
  45. StreamInterface* output, size_t* size);
  46. // Read cached headers from a stream, and them merge them into the response
  47. // object using the specified combine operation.
  48. bool HttpReadCacheHeaders(StreamInterface* input,
  49. HttpResponseData* response,
  50. HttpData::HeaderCombine combine);
  51. //////////////////////////////////////////////////////////////////////
  52. // HttpClient
  53. // Implements an HTTP 1.1 client.
  54. //////////////////////////////////////////////////////////////////////
  55. class DiskCache;
  56. class HttpClient;
  57. class IPNetPool;
  58. class SignalThread;
  59. // What to do: Define STRICT_HTTP_ERROR=1 in your makefile. Use HttpError in
  60. // your code (HttpErrorType should only be used for code that is shared
  61. // with groups which have not yet migrated).
  62. #if STRICT_HTTP_ERROR
  63. typedef HttpError HttpErrorType;
  64. #else // !STRICT_HTTP_ERROR
  65. typedef int HttpErrorType;
  66. #endif // !STRICT_HTTP_ERROR
  67. class HttpClient : private IHttpNotify, public sigslot::has_slots<> {
  68. public:
  69. // If HttpRequestData and HttpResponseData objects are provided, they must
  70. // be freed by the caller. Otherwise, an internal object is allocated.
  71. HttpClient(const std::string& agent, StreamPool* pool,
  72. HttpTransaction* transaction = NULL);
  73. virtual ~HttpClient();
  74. void set_pool(StreamPool* pool) { pool_ = pool; }
  75. void set_agent(const std::string& agent) { agent_ = agent; }
  76. const std::string& agent() const { return agent_; }
  77. void set_proxy(const ProxyInfo& proxy) { proxy_ = proxy; }
  78. const ProxyInfo& proxy() const { return proxy_; }
  79. // Request retries occur when the connection closes before the beginning of
  80. // an http response is received. In these cases, the http server may have
  81. // timed out the keepalive connection before it received our request. Note
  82. // that if a request document cannot be rewound, no retry is made. The
  83. // default is 1.
  84. void set_request_retries(size_t retries) { retries_ = retries; }
  85. size_t request_retries() const { return retries_; }
  86. enum RedirectAction { REDIRECT_DEFAULT, REDIRECT_ALWAYS, REDIRECT_NEVER };
  87. void set_redirect_action(RedirectAction action) { redirect_action_ = action; }
  88. RedirectAction redirect_action() const { return redirect_action_; }
  89. // Deprecated
  90. void set_fail_redirect(bool fail_redirect) {
  91. redirect_action_ = REDIRECT_NEVER;
  92. }
  93. bool fail_redirect() const { return (REDIRECT_NEVER == redirect_action_); }
  94. enum UriForm { URI_DEFAULT, URI_ABSOLUTE, URI_RELATIVE };
  95. void set_uri_form(UriForm form) { uri_form_ = form; }
  96. UriForm uri_form() const { return uri_form_; }
  97. void set_cache(DiskCache* cache) { ASSERT(!IsCacheActive()); cache_ = cache; }
  98. bool cache_enabled() const { return (NULL != cache_); }
  99. // reset clears the server, request, and response structures. It will also
  100. // abort an active request.
  101. void reset();
  102. void set_server(const SocketAddress& address);
  103. const SocketAddress& server() const { return server_; }
  104. // Note: in order for HttpClient to retry a POST in response to
  105. // an authentication challenge, a redirect response, or socket disconnection,
  106. // the request document must support 'replaying' by calling Rewind() on it.
  107. // In the case where just a subset of a stream should be used as the request
  108. // document, the stream may be wrapped with the StreamSegment adapter.
  109. HttpTransaction* transaction() { return transaction_; }
  110. const HttpTransaction* transaction() const { return transaction_; }
  111. HttpRequestData& request() { return transaction_->request; }
  112. const HttpRequestData& request() const { return transaction_->request; }
  113. HttpResponseData& response() { return transaction_->response; }
  114. const HttpResponseData& response() const { return transaction_->response; }
  115. // convenience methods
  116. void prepare_get(const std::string& url);
  117. void prepare_post(const std::string& url, const std::string& content_type,
  118. StreamInterface* request_doc);
  119. // Convert HttpClient to a pull-based I/O model.
  120. StreamInterface* GetDocumentStream();
  121. // After you finish setting up your request, call start.
  122. void start();
  123. // 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. protected:
  135. void connect();
  136. void release();
  137. bool ShouldRedirect(std::string* location) const;
  138. bool BeginCacheFile();
  139. HttpError WriteCacheHeaders(const std::string& id);
  140. void CompleteCacheFile();
  141. bool CheckCache();
  142. HttpError ReadCacheHeaders(const std::string& id, bool override);
  143. HttpError ReadCacheBody(const std::string& id);
  144. bool PrepareValidate();
  145. HttpError CompleteValidate();
  146. HttpError OnHeaderAvailable(bool ignore_data, bool chunked, size_t data_size);
  147. void StartDNSLookup();
  148. void OnResolveResult(SignalThread* thread);
  149. // IHttpNotify Interface
  150. virtual HttpError onHttpHeaderComplete(bool chunked, size_t& data_size);
  151. virtual void onHttpComplete(HttpMode mode, HttpError err);
  152. virtual void onHttpClosed(HttpError err);
  153. private:
  154. enum CacheState { CS_READY, CS_WRITING, CS_READING, CS_VALIDATING };
  155. bool IsCacheActive() const { return (cache_state_ > CS_READY); }
  156. std::string agent_;
  157. StreamPool* pool_;
  158. HttpBase base_;
  159. SocketAddress server_;
  160. ProxyInfo proxy_;
  161. HttpTransaction* transaction_;
  162. bool free_transaction_;
  163. size_t retries_, attempt_, redirects_;
  164. RedirectAction redirect_action_;
  165. UriForm uri_form_;
  166. scoped_ptr<HttpAuthContext> context_;
  167. DiskCache* cache_;
  168. CacheState cache_state_;
  169. AsyncResolver* resolver_;
  170. };
  171. //////////////////////////////////////////////////////////////////////
  172. // HttpClientDefault - Default implementation of HttpClient
  173. //////////////////////////////////////////////////////////////////////
  174. class HttpClientDefault : public ReuseSocketPool, public HttpClient {
  175. public:
  176. HttpClientDefault(SocketFactory* factory, const std::string& agent,
  177. HttpTransaction* transaction = NULL);
  178. };
  179. //////////////////////////////////////////////////////////////////////
  180. } // namespace talk_base
  181. #endif // TALK_BASE_HTTPCLIENT_H__