blob: 3199c8f94f5b403c1774f489242c1db059db50d7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
22using namespace rtc;
23
24///////////////////////////////////////////////////////////////////////////////
25// HttpMonitor
26///////////////////////////////////////////////////////////////////////////////
27
28HttpMonitor::HttpMonitor(SocketServer *ss) {
29 ASSERT(Thread::Current() != NULL);
30 ss_ = ss;
31 reset();
32}
33
34void HttpMonitor::Connect(HttpClient *http) {
35 http->SignalHttpClientComplete.connect(this,
36 &HttpMonitor::OnHttpClientComplete);
37}
38
39void HttpMonitor::OnHttpClientComplete(HttpClient * http, HttpErrorType error) {
40 complete_ = true;
41 error_ = error;
42 ss_->WakeUp();
43}
44
45///////////////////////////////////////////////////////////////////////////////
46// HttpRequest
47///////////////////////////////////////////////////////////////////////////////
48
49const int kDefaultHTTPTimeout = 30 * 1000; // 30 sec
50
51HttpRequest::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
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000057HttpRequest::~HttpRequest() = default;
58
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059void HttpRequest::Send() {
60 // TODO: Rewrite this to use the thread's native socket server, and a more
61 // natural flow?
62
63 PhysicalSocketServer physical;
64 SocketServer * ss = &physical;
65 if (firewall_) {
66 ss = new FirewallSocketServer(ss, firewall_);
67 }
68
69 SslSocketFactory factory(ss, client_.agent());
70 factory.SetProxy(proxy_);
71 if (secure_)
72 factory.UseSSL(host_.c_str());
73
74 //factory.SetLogging("HttpRequest");
75
76 ReuseSocketPool pool(&factory);
77 client_.set_pool(&pool);
78
79 bool transparent_proxy = (port_ == 80) && ((proxy_.type == PROXY_HTTPS) ||
80 (proxy_.type == PROXY_UNKNOWN));
81
82 if (transparent_proxy) {
83 client_.set_proxy(proxy_);
84 }
85 client_.set_fail_redirect(fail_redirect_);
86
87 SocketAddress server(host_, port_);
88 client_.set_server(server);
89
90 LOG(LS_INFO) << "HttpRequest start: " << host_ + client_.request().path;
91
92 HttpMonitor monitor(ss);
93 monitor.Connect(&client_);
94 client_.start();
95 ss->Wait(timeout_, true);
96 if (!monitor.done()) {
97 LOG(LS_INFO) << "HttpRequest request timed out";
98 client_.reset();
99 return;
100 }
101
102 set_error(monitor.error());
103 if (error_) {
104 LOG(LS_INFO) << "HttpRequest request error: " << error_;
105 return;
106 }
107
108 std::string value;
109 if (client_.response().hasHeader(HH_LOCATION, &value)) {
110 response_redirect_ = value.c_str();
111 }
112}