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.

121 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2010, 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_ASYNCHTTPREQUEST_H_
  28. #define TALK_BASE_ASYNCHTTPREQUEST_H_
  29. #include <string>
  30. #include "talk/base/event.h"
  31. #include "talk/base/httpclient.h"
  32. #include "talk/base/signalthread.h"
  33. #include "talk/base/socketpool.h"
  34. #include "talk/base/sslsocketfactory.h"
  35. namespace talk_base {
  36. class FirewallManager;
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // AsyncHttpRequest
  39. // Performs an HTTP request on a background thread. Notifies on the foreground
  40. // thread once the request is done (successfully or unsuccessfully).
  41. ///////////////////////////////////////////////////////////////////////////////
  42. class AsyncHttpRequest : public SignalThread {
  43. public:
  44. explicit AsyncHttpRequest(const std::string &user_agent);
  45. ~AsyncHttpRequest();
  46. // If start_delay is less than or equal to zero, this starts immediately.
  47. // Start_delay defaults to zero.
  48. int start_delay() const { return start_delay_; }
  49. void set_start_delay(int delay) { start_delay_ = delay; }
  50. const ProxyInfo& proxy() const { return proxy_; }
  51. void set_proxy(const ProxyInfo& proxy) {
  52. proxy_ = proxy;
  53. }
  54. void set_firewall(FirewallManager * firewall) {
  55. firewall_ = firewall;
  56. }
  57. // The DNS name of the host to connect to.
  58. const std::string& host() { return host_; }
  59. void set_host(const std::string& host) { host_ = host; }
  60. // The port to connect to on the target host.
  61. int port() { return port_; }
  62. void set_port(int port) { port_ = port; }
  63. // Whether the request should use SSL.
  64. bool secure() { return secure_; }
  65. void set_secure(bool secure) { secure_ = secure; }
  66. // Time to wait on the download, in ms.
  67. int timeout() { return timeout_; }
  68. void set_timeout(int timeout) { timeout_ = timeout; }
  69. // Fail redirects to allow analysis of redirect urls, etc.
  70. bool fail_redirect() const { return fail_redirect_; }
  71. void set_fail_redirect(bool redirect) { fail_redirect_ = redirect; }
  72. // Returns the redirect when redirection occurs
  73. const std::string& response_redirect() { return response_redirect_; }
  74. HttpRequestData& request() { return client_.request(); }
  75. HttpResponseData& response() { return client_.response(); }
  76. HttpErrorType error() { return error_; }
  77. protected:
  78. void set_error(HttpErrorType error) { error_ = error; }
  79. virtual void OnWorkStart();
  80. virtual void OnWorkStop();
  81. void OnComplete(HttpClient* client, HttpErrorType error);
  82. virtual void OnMessage(Message* message);
  83. virtual void DoWork();
  84. private:
  85. void LaunchRequest();
  86. int start_delay_;
  87. ProxyInfo proxy_;
  88. FirewallManager* firewall_;
  89. std::string host_;
  90. int port_;
  91. bool secure_;
  92. int timeout_;
  93. bool fail_redirect_;
  94. SslSocketFactory factory_;
  95. ReuseSocketPool pool_;
  96. HttpClient client_;
  97. HttpErrorType error_;
  98. std::string response_redirect_;
  99. };
  100. } // namespace talk_base
  101. #endif // TALK_BASE_ASYNCHTTPREQUEST_H_