1 /* 2 * Copyright 2006 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/base/httprequest.h" 12 13 #include "webrtc/base/common.h" 14 #include "webrtc/base/firewallsocketserver.h" 15 #include "webrtc/base/httpclient.h" 16 #include "webrtc/base/logging.h" 17 #include "webrtc/base/physicalsocketserver.h" 18 #include "webrtc/base/socketadapters.h" 19 #include "webrtc/base/socketpool.h" 20 #include "webrtc/base/ssladapter.h" 21 22 using namespace rtc; 23 24 /////////////////////////////////////////////////////////////////////////////// 25 // HttpMonitor 26 /////////////////////////////////////////////////////////////////////////////// 27 28 HttpMonitor::HttpMonitor(SocketServer *ss) { 29 ASSERT(Thread::Current() != NULL); 30 ss_ = ss; 31 reset(); 32 } 33 34 void HttpMonitor::Connect(HttpClient *http) { 35 http->SignalHttpClientComplete.connect(this, 36 &HttpMonitor::OnHttpClientComplete); 37 } 38 39 void HttpMonitor::OnHttpClientComplete(HttpClient * http, HttpErrorType error) { 40 complete_ = true; 41 error_ = error; 42 ss_->WakeUp(); 43 } 44 45 /////////////////////////////////////////////////////////////////////////////// 46 // HttpRequest 47 /////////////////////////////////////////////////////////////////////////////// 48 49 const int kDefaultHTTPTimeout = 30 * 1000; // 30 sec 50 51 HttpRequest::HttpRequest(const std::string &user_agent) 52 : firewall_(0), port_(80), secure_(false), 53 timeout_(kDefaultHTTPTimeout), fail_redirect_(false), 54 client_(user_agent.c_str(), NULL), error_(HE_NONE) { 55 } 56 57 void HttpRequest::Send() { 58 // TODO: Rewrite this to use the thread's native socket server, and a more 59 // natural flow? 60 61 PhysicalSocketServer physical; 62 SocketServer * ss = &physical; 63 if (firewall_) { 64 ss = new FirewallSocketServer(ss, firewall_); 65 } 66 67 SslSocketFactory factory(ss, client_.agent()); 68 factory.SetProxy(proxy_); 69 if (secure_) 70 factory.UseSSL(host_.c_str()); 71 72 //factory.SetLogging("HttpRequest"); 73 74 ReuseSocketPool pool(&factory); 75 client_.set_pool(&pool); 76 77 bool transparent_proxy = (port_ == 80) && ((proxy_.type == PROXY_HTTPS) || 78 (proxy_.type == PROXY_UNKNOWN)); 79 80 if (transparent_proxy) { 81 client_.set_proxy(proxy_); 82 } 83 client_.set_fail_redirect(fail_redirect_); 84 85 SocketAddress server(host_, port_); 86 client_.set_server(server); 87 88 LOG(LS_INFO) << "HttpRequest start: " << host_ + client_.request().path; 89 90 HttpMonitor monitor(ss); 91 monitor.Connect(&client_); 92 client_.start(); 93 ss->Wait(timeout_, true); 94 if (!monitor.done()) { 95 LOG(LS_INFO) << "HttpRequest request timed out"; 96 client_.reset(); 97 return; 98 } 99 100 set_error(monitor.error()); 101 if (error_) { 102 LOG(LS_INFO) << "HttpRequest request error: " << error_; 103 return; 104 } 105 106 std::string value; 107 if (client_.response().hasHeader(HH_LOCATION, &value)) { 108 response_redirect_ = value.c_str(); 109 } 110 } 111