• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
38 namespace talk_base {
39 
40 class 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 
48 class AsyncHttpRequest : public SignalThread {
49  public:
50   explicit AsyncHttpRequest(const std::string &user_agent);
51   ~AsyncHttpRequest();
52 
set_proxy(const ProxyInfo & proxy)53   void set_proxy(const ProxyInfo& proxy) {
54     proxy_ = proxy;
55   }
set_firewall(FirewallManager * firewall)56   void set_firewall(FirewallManager * firewall) {
57     firewall_ = firewall;
58   }
59 
60   // The DNS name of the host to connect to.
host()61   const std::string& host() { return host_; }
set_host(const std::string & host)62   void set_host(const std::string& host) { host_ = host; }
63 
64   // The port to connect to on the target host.
port()65   int port() { return port_; }
set_port(int port)66   void set_port(int port) { port_ = port; }
67 
68   // Whether the request should use SSL.
secure()69   bool secure() { return secure_; }
set_secure(bool secure)70   void set_secure(bool secure) { secure_ = secure; }
71 
72   // Time to wait on the download, in ms.
timeout()73   int timeout() { return timeout_; }
set_timeout(int timeout)74   void set_timeout(int timeout) { timeout_ = timeout; }
75 
76   // Fail redirects to allow analysis of redirect urls, etc.
fail_redirect()77   bool fail_redirect() const { return fail_redirect_; }
set_fail_redirect(bool redirect)78   void set_fail_redirect(bool redirect) { fail_redirect_ = redirect; }
79 
80   // Returns the redirect when redirection occurs
response_redirect()81   const std::string& response_redirect() { return response_redirect_; }
82 
request()83   HttpRequestData& request() { return client_.request(); }
response()84   HttpResponseData& response() { return client_.response(); }
error()85   HttpErrorType error() { return error_; }
86 
87  protected:
set_error(HttpErrorType error)88   void set_error(HttpErrorType error) { error_ = error; }
89   virtual void OnWorkStart();
90   virtual void OnWorkStop();
91   void OnComplete(HttpClient* client, HttpErrorType error);
92   virtual void OnMessage(Message* message);
93   virtual void DoWork();
94 
95  private:
96   ProxyInfo proxy_;
97   FirewallManager* firewall_;
98   std::string host_;
99   int port_;
100   bool secure_;
101   int timeout_;
102   bool fail_redirect_;
103   SslSocketFactory factory_;
104   ReuseSocketPool pool_;
105   HttpClient client_;
106   HttpErrorType error_;
107   std::string response_redirect_;
108 };
109 
110 }  // namespace talk_base
111 
112 #endif  // TALK_BASE_ASYNCHTTPREQUEST_H_
113