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