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.

127 lines
4.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2006, 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/httprequest.h"
  28. #include "talk/base/common.h"
  29. #include "talk/base/firewallsocketserver.h"
  30. #include "talk/base/httpclient.h"
  31. #include "talk/base/logging.h"
  32. #include "talk/base/physicalsocketserver.h"
  33. #include "talk/base/socketadapters.h"
  34. #include "talk/base/socketpool.h"
  35. #include "talk/base/ssladapter.h"
  36. using namespace talk_base;
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // HttpMonitor
  39. ///////////////////////////////////////////////////////////////////////////////
  40. HttpMonitor::HttpMonitor(SocketServer *ss) {
  41. ASSERT(Thread::Current() != NULL);
  42. ss_ = ss;
  43. reset();
  44. }
  45. void HttpMonitor::Connect(HttpClient *http) {
  46. http->SignalHttpClientComplete.connect(this,
  47. &HttpMonitor::OnHttpClientComplete);
  48. }
  49. void HttpMonitor::OnHttpClientComplete(HttpClient * http, HttpErrorType error) {
  50. complete_ = true;
  51. error_ = error;
  52. ss_->WakeUp();
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // HttpRequest
  56. ///////////////////////////////////////////////////////////////////////////////
  57. const int kDefaultHTTPTimeout = 30 * 1000; // 30 sec
  58. HttpRequest::HttpRequest(const std::string &user_agent)
  59. : firewall_(0), port_(80), secure_(false),
  60. timeout_(kDefaultHTTPTimeout), fail_redirect_(false),
  61. client_(user_agent.c_str(), NULL), error_(HE_NONE) {
  62. }
  63. void HttpRequest::Send() {
  64. // TODO: Rewrite this to use the thread's native socket server, and a more
  65. // natural flow?
  66. PhysicalSocketServer physical;
  67. SocketServer * ss = &physical;
  68. if (firewall_) {
  69. ss = new FirewallSocketServer(ss, firewall_);
  70. }
  71. SslSocketFactory factory(ss, client_.agent());
  72. factory.SetProxy(proxy_);
  73. if (secure_)
  74. factory.UseSSL(host_.c_str());
  75. //factory.SetLogging("HttpRequest");
  76. ReuseSocketPool pool(&factory);
  77. client_.set_pool(&pool);
  78. bool transparent_proxy = (port_ == 80) && ((proxy_.type == PROXY_HTTPS) ||
  79. (proxy_.type == PROXY_UNKNOWN));
  80. if (transparent_proxy) {
  81. client_.set_proxy(proxy_);
  82. }
  83. client_.set_fail_redirect(fail_redirect_);
  84. SocketAddress server(host_, port_);
  85. client_.set_server(server);
  86. LOG(LS_INFO) << "HttpRequest start: " << host_ + client_.request().path;
  87. HttpMonitor monitor(ss);
  88. monitor.Connect(&client_);
  89. client_.start();
  90. ss->Wait(timeout_, true);
  91. if (!monitor.done()) {
  92. LOG(LS_INFO) << "HttpRequest request timed out";
  93. client_.reset();
  94. return;
  95. }
  96. set_error(monitor.error());
  97. if (error_) {
  98. LOG(LS_INFO) << "HttpRequest request error: " << error_;
  99. return;
  100. }
  101. std::string value;
  102. if (client_.response().hasHeader(HH_LOCATION, &value)) {
  103. response_redirect_ = value.c_str();
  104. }
  105. }