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.

133 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. #include "talk/base/asynchttprequest.h"
  28. namespace talk_base {
  29. enum {
  30. MSG_TIMEOUT = SignalThread::ST_MSG_FIRST_AVAILABLE,
  31. MSG_LAUNCH_REQUEST
  32. };
  33. static const int kDefaultHTTPTimeout = 30 * 1000; // 30 sec
  34. ///////////////////////////////////////////////////////////////////////////////
  35. // AsyncHttpRequest
  36. ///////////////////////////////////////////////////////////////////////////////
  37. AsyncHttpRequest::AsyncHttpRequest(const std::string &user_agent)
  38. : start_delay_(0),
  39. firewall_(NULL),
  40. port_(80),
  41. secure_(false),
  42. timeout_(kDefaultHTTPTimeout),
  43. fail_redirect_(false),
  44. factory_(Thread::Current()->socketserver(), user_agent),
  45. pool_(&factory_),
  46. client_(user_agent.c_str(), &pool_),
  47. error_(HE_NONE) {
  48. client_.SignalHttpClientComplete.connect(this,
  49. &AsyncHttpRequest::OnComplete);
  50. }
  51. AsyncHttpRequest::~AsyncHttpRequest() {
  52. }
  53. void AsyncHttpRequest::OnWorkStart() {
  54. if (start_delay_ <= 0) {
  55. LaunchRequest();
  56. } else {
  57. Thread::Current()->PostDelayed(start_delay_, this, MSG_LAUNCH_REQUEST);
  58. }
  59. }
  60. void AsyncHttpRequest::OnWorkStop() {
  61. // worker is already quitting, no need to explicitly quit
  62. LOG(LS_INFO) << "HttpRequest cancelled";
  63. }
  64. void AsyncHttpRequest::OnComplete(HttpClient* client, HttpErrorType error) {
  65. Thread::Current()->Clear(this, MSG_TIMEOUT);
  66. set_error(error);
  67. if (!error) {
  68. LOG(LS_INFO) << "HttpRequest completed successfully";
  69. std::string value;
  70. if (client_.response().hasHeader(HH_LOCATION, &value)) {
  71. response_redirect_ = value.c_str();
  72. }
  73. } else {
  74. LOG(LS_INFO) << "HttpRequest completed with error: " << error;
  75. }
  76. worker()->Quit();
  77. }
  78. void AsyncHttpRequest::OnMessage(Message* message) {
  79. switch (message->message_id) {
  80. case MSG_TIMEOUT:
  81. LOG(LS_INFO) << "HttpRequest timed out";
  82. client_.reset();
  83. worker()->Quit();
  84. break;
  85. case MSG_LAUNCH_REQUEST:
  86. LaunchRequest();
  87. break;
  88. default:
  89. SignalThread::OnMessage(message);
  90. break;
  91. }
  92. }
  93. void AsyncHttpRequest::DoWork() {
  94. // Do nothing while we wait for the request to finish. We only do this so
  95. // that we can be a SignalThread; in the future this class should not be
  96. // a SignalThread, since it does not need to spawn a new thread.
  97. Thread::Current()->ProcessMessages(kForever);
  98. }
  99. void AsyncHttpRequest::LaunchRequest() {
  100. factory_.SetProxy(proxy_);
  101. if (secure_)
  102. factory_.UseSSL(host_.c_str());
  103. bool transparent_proxy = (port_ == 80) &&
  104. ((proxy_.type == PROXY_HTTPS) || (proxy_.type == PROXY_UNKNOWN));
  105. if (transparent_proxy) {
  106. client_.set_proxy(proxy_);
  107. }
  108. client_.set_fail_redirect(fail_redirect_);
  109. client_.set_server(SocketAddress(host_, port_));
  110. LOG(LS_INFO) << "HttpRequest start: " << host_ + client_.request().path;
  111. Thread::Current()->PostDelayed(timeout_, this, MSG_TIMEOUT);
  112. client_.start();
  113. }
  114. } // namespace talk_base