3
* Copyright 2006, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#include "talk/base/httprequest.h"
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"
39
using namespace talk_base;
41
///////////////////////////////////////////////////////////////////////////////
43
///////////////////////////////////////////////////////////////////////////////
45
HttpMonitor::HttpMonitor(SocketServer *ss) {
46
ASSERT(Thread::Current() != NULL);
51
void HttpMonitor::Connect(HttpClient *http) {
52
http->SignalHttpClientComplete.connect(this,
53
&HttpMonitor::OnHttpClientComplete);
56
void HttpMonitor::OnHttpClientComplete(HttpClient * http, HttpErrorType error) {
62
///////////////////////////////////////////////////////////////////////////////
64
///////////////////////////////////////////////////////////////////////////////
66
const int kDefaultHTTPTimeout = 30 * 1000; // 30 sec
68
HttpRequest::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) {
74
void HttpRequest::Send() {
75
// TODO: Rewrite this to use the thread's native socket server, and a more
78
PhysicalSocketServer physical;
79
SocketServer * ss = &physical;
81
ss = new FirewallSocketServer(ss, firewall_);
84
SslSocketFactory factory(ss, client_.agent());
85
factory.SetProxy(proxy_);
87
factory.UseSSL(host_.c_str());
89
//factory.SetLogging("HttpRequest");
91
ReuseSocketPool pool(&factory);
92
client_.set_pool(&pool);
94
bool transparent_proxy = (port_ == 80) && ((proxy_.type == PROXY_HTTPS) ||
95
(proxy_.type == PROXY_UNKNOWN));
97
if (transparent_proxy) {
98
client_.set_proxy(proxy_);
100
client_.set_fail_redirect(fail_redirect_);
102
SocketAddress server(host_, port_);
103
client_.set_server(server);
105
LOG(LS_INFO) << "HttpRequest start: " << host_ + client_.request().path;
107
HttpMonitor monitor(ss);
108
monitor.Connect(&client_);
110
ss->Wait(timeout_, true);
111
if (!monitor.done()) {
112
LOG(LS_INFO) << "HttpRequest request timed out";
117
set_error(monitor.error());
119
LOG(LS_INFO) << "HttpRequest request error: " << error_;
124
if (client_.response().hasHeader(HH_LOCATION, &value)) {
125
response_redirect_ = value.c_str();