blob: 13edf618810d7ec6946a656ced20fb251f8b124c [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2010, 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#ifndef TALK_BASE_ASYNCHTTPREQUEST_H_
29#define TALK_BASE_ASYNCHTTPREQUEST_H_
30
31#include <string>
32#include "talk/base/event.h"
33#include "talk/base/httpclient.h"
34#include "talk/base/signalthread.h"
35#include "talk/base/socketpool.h"
36#include "talk/base/sslsocketfactory.h"
37
38namespace talk_base {
39
40class FirewallManager;
41
42///////////////////////////////////////////////////////////////////////////////
43// AsyncHttpRequest
44// Performs an HTTP request on a background thread. Notifies on the foreground
45// thread once the request is done (successfully or unsuccessfully).
46///////////////////////////////////////////////////////////////////////////////
47
48class AsyncHttpRequest : public SignalThread {
49 public:
50 explicit AsyncHttpRequest(const std::string &user_agent);
51 ~AsyncHttpRequest();
52
53 // If start_delay is less than or equal to zero, this starts immediately.
54 // Start_delay defaults to zero.
55 int start_delay() const { return start_delay_; }
56 void set_start_delay(int delay) { start_delay_ = delay; }
57
58 const ProxyInfo& proxy() const { return proxy_; }
59 void set_proxy(const ProxyInfo& proxy) {
60 proxy_ = proxy;
61 }
62 void set_firewall(FirewallManager * firewall) {
63 firewall_ = firewall;
64 }
65
66 // The DNS name of the host to connect to.
67 const std::string& host() { return host_; }
68 void set_host(const std::string& host) { host_ = host; }
69
70 // The port to connect to on the target host.
71 int port() { return port_; }
72 void set_port(int port) { port_ = port; }
73
74 // Whether the request should use SSL.
75 bool secure() { return secure_; }
76 void set_secure(bool secure) { secure_ = secure; }
77
78 // Time to wait on the download, in ms.
79 int timeout() { return timeout_; }
80 void set_timeout(int timeout) { timeout_ = timeout; }
81
82 // Fail redirects to allow analysis of redirect urls, etc.
83 bool fail_redirect() const { return fail_redirect_; }
84 void set_fail_redirect(bool redirect) { fail_redirect_ = redirect; }
85
86 // Returns the redirect when redirection occurs
87 const std::string& response_redirect() { return response_redirect_; }
88
89 HttpRequestData& request() { return client_.request(); }
90 HttpResponseData& response() { return client_.response(); }
91 HttpErrorType error() { return error_; }
92
93 protected:
94 void set_error(HttpErrorType error) { error_ = error; }
95 virtual void OnWorkStart();
96 virtual void OnWorkStop();
97 void OnComplete(HttpClient* client, HttpErrorType error);
98 virtual void OnMessage(Message* message);
99 virtual void DoWork();
100
101 private:
102 void LaunchRequest();
103
104 int start_delay_;
105 ProxyInfo proxy_;
106 FirewallManager* firewall_;
107 std::string host_;
108 int port_;
109 bool secure_;
110 int timeout_;
111 bool fail_redirect_;
112 SslSocketFactory factory_;
113 ReuseSocketPool pool_;
114 HttpClient client_;
115 HttpErrorType error_;
116 std::string response_redirect_;
117};
118
119} // namespace talk_base
120
121#endif // TALK_BASE_ASYNCHTTPREQUEST_H_