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