summaryrefslogtreecommitdiffstats
path: root/third_party/libjingle/files/talk/base/asynchttprequest.h
blob: 543210d44e456463e9cc5beec659ad195f75a677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef _ASYNCHTTPREQUEST_H_
#define _ASYNCHTTPREQUEST_H_

#include "talk/base/httpclient.h"
#include "talk/base/logging.h"
#include "talk/base/proxyinfo.h"
#include "talk/base/socketserver.h"
#include "talk/base/thread.h"
#include "talk/base/signalthread.h"

namespace talk_base {

///////////////////////////////////////////////////////////////////////////////
// AsyncHttpRequest
// Performs an HTTP request on a background thread.  Notifies on the foreground
// thread once the request is done (successfully or unsuccessfully).
///////////////////////////////////////////////////////////////////////////////

class FirewallManager;
class MemoryStream;

class AsyncHttpRequest:
  public SignalThread {
public:
  AsyncHttpRequest(const std::string &user_agent);

  void set_proxy(const talk_base::ProxyInfo& proxy) {
    proxy_ = proxy;
  }
  void set_firewall(talk_base::FirewallManager * firewall) {
    firewall_ = firewall;
  }

  // The DNS name of the host to connect to.
  const std::string& host() { return host_; }
  void set_host(const std::string& host) { host_ = host; }

  // The port to connect to on the target host.
  int port() { return port_; }
  void set_port(int port) { port_ = port; }
       
   // Whether the request should use SSL.
  bool secure() { return secure_; }
  void set_secure(bool secure) { secure_ = secure; }

  // Returns the redirect when redirection occurs
  const std::string& response_redirect() { return response_redirect_; }

  // Time to wait on the download, in ms.  Default is 5000 (5s)
  int timeout() { return timeout_; }
  void set_timeout(int timeout) { timeout_ = timeout; }

  // Fail redirects to allow analysis of redirect urls, etc.
  bool fail_redirect() const { return fail_redirect_; }
  void set_fail_redirect(bool fail_redirect) { fail_redirect_ = fail_redirect; }

  HttpRequestData& request() { return client_.request(); }
  HttpResponseData& response() { return client_.response(); }
   
private:
  // SignalThread Interface
  virtual void DoWork();
  
  talk_base::ProxyInfo proxy_;
  talk_base::FirewallManager * firewall_;
  std::string host_;
  int port_;
  bool secure_;
  int timeout_;
  bool fail_redirect_;
  HttpClient client_;
  std::string response_redirect_;
};

///////////////////////////////////////////////////////////////////////////////
// HttpMonitor
///////////////////////////////////////////////////////////////////////////////

class HttpMonitor : public sigslot::has_slots<> {
public:
  HttpMonitor(SocketServer *ss);

  void reset() { complete_ = false; }

  bool done() const { return complete_; }
  int error() const { return err_; }

  void Connect(talk_base::HttpClient* http);  
  void OnHttpClientComplete(talk_base::HttpClient * http, int err);

private:
  bool complete_;
  int err_;
  SocketServer *ss_;
};

///////////////////////////////////////////////////////////////////////////////
// SslSocketFactory
///////////////////////////////////////////////////////////////////////////////

class SslSocketFactory : public talk_base::SocketFactory {
 public:
  SslSocketFactory(talk_base::SocketFactory * factory, const std::string &user_agent)
    : factory_(factory), logging_level_(talk_base::LS_VERBOSE), 
      binary_mode_(false), agent_(user_agent),
      ignore_bad_cert_(false), use_restartable_ssl_sockets_(false) { }

  void UseSSL(const char * hostname) { hostname_ = hostname; }
  void DisableSSL() { hostname_.clear(); }

  void SetProxy(const talk_base::ProxyInfo& proxy) { proxy_ = proxy; }
  const talk_base::ProxyInfo& proxy() const { return proxy_; }
  bool ignore_bad_cert() {return ignore_bad_cert_;}
  void SetIgnoreBadCert(bool ignore) { ignore_bad_cert_ = ignore; }
  bool use_restartable_ssl_sockets() const {
    return use_restartable_ssl_sockets_;
  }
  void SetUseRestartableSSLSockets(bool use_restartable_ssl_sockets) {
    use_restartable_ssl_sockets_ = use_restartable_ssl_sockets;
  }

  void SetLogging(talk_base::LoggingSeverity level, const std::string& label, 
      bool binary_mode = false) {
    logging_level_ = level;
    logging_label_ = label;
    binary_mode_ = binary_mode;
  }

  virtual talk_base::Socket * CreateSocket(int type);
  virtual talk_base::AsyncSocket * CreateAsyncSocket(int type);

private:
  talk_base::SocketFactory * factory_;
  talk_base::ProxyInfo proxy_;
  std::string hostname_, logging_label_;
  talk_base::LoggingSeverity logging_level_;
  bool binary_mode_;
  std::string agent_;
  bool ignore_bad_cert_;
  bool use_restartable_ssl_sockets_;
};

///////////////////////////////////////////////////////////////////////////////

}  // namespace talk_base_

#endif  // _ASYNCHTTPREQUEST_H_