Panda3D
httpClient.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file httpClient.h
10  * @author drose
11  * @date 2002-09-24
12  */
13 
14 #ifndef HTTPCLIENT_H
15 #define HTTPCLIENT_H
16 
17 #include "pandabase.h"
18 
19 // This module requires OpenSSL to compile, even if you do not intend to use
20 // this to establish https connections; this is because it uses the OpenSSL
21 // library to portably handle all of the socket communications.
22 
23 #ifdef HAVE_OPENSSL
24 
25 #include "urlSpec.h"
26 #include "httpAuthorization.h"
27 #include "httpEnum.h"
28 #include "httpCookie.h"
29 #include "globPattern.h"
30 #include "pointerTo.h"
31 #include "pvector.h"
32 #include "pmap.h"
33 #include "pset.h"
34 #include "referenceCount.h"
35 
36 typedef struct ssl_ctx_st SSL_CTX;
37 typedef struct x509_st X509;
38 typedef struct X509_name_st X509_NAME;
39 typedef struct evp_pkey_st EVP_PKEY;
40 
41 class Filename;
42 class HTTPChannel;
43 
44 /**
45  * Handles contacting an HTTP server and retrieving a document. Each
46  * HTTPClient object represents a separate context, and stores its own list of
47  * cookies, passwords, and certificates; however, a given HTTPClient is
48  * capable of making multiple simultaneous requests to the same or different
49  * servers.
50  *
51  * It is up to the programmer whether one HTTPClient should be used to
52  * retrieve all documents, or a separate one should be created each time.
53  * There is a default, global HTTPClient available in
54  * HTTPClient::get_global_ptr().
55  */
56 class EXPCL_PANDA_DOWNLOADER HTTPClient : public ReferenceCount {
57 PUBLISHED:
58  HTTPClient();
59  HTTPClient(const HTTPClient &copy);
60  void operator = (const HTTPClient &copy);
61  ~HTTPClient();
62 
63  static void init_random_seed();
64 
65  void set_proxy_spec(const std::string &proxy_spec);
66  std::string get_proxy_spec() const;
67 
68  void set_direct_host_spec(const std::string &direct_host_spec);
69  std::string get_direct_host_spec() const;
70 
71  INLINE void set_try_all_direct(bool try_all_direct);
72  INLINE bool get_try_all_direct() const;
73 
74  void clear_proxy();
75  void add_proxy(const std::string &scheme, const URLSpec &proxy);
76  void clear_direct_host();
77  void add_direct_host(const std::string &hostname);
78 
79  std::string get_proxies_for_url(const URLSpec &url) const;
80 
81  void set_username(const std::string &server, const std::string &realm, const std::string &username);
82  std::string get_username(const std::string &server, const std::string &realm) const;
83 
84  void set_cookie(const HTTPCookie &cookie);
85  bool clear_cookie(const HTTPCookie &cookie);
86  void clear_all_cookies();
87  bool has_cookie(const HTTPCookie &cookie) const;
88  HTTPCookie get_cookie(const HTTPCookie &cookie) const;
89  void copy_cookies_from(const HTTPClient &other);
90 
91  void write_cookies(std::ostream &out) const;
92  void send_cookies(std::ostream &out, const URLSpec &url);
93 
94  INLINE void set_client_certificate_filename(const Filename &filename);
95  INLINE void set_client_certificate_pem(const std::string &pem);
96  INLINE void set_client_certificate_passphrase(const std::string &passphrase);
97  bool load_client_certificate();
98 
99  bool add_preapproved_server_certificate_filename(const URLSpec &url, const Filename &filename);
100  bool add_preapproved_server_certificate_pem(const URLSpec &url, const std::string &pem);
101  bool add_preapproved_server_certificate_name(const URLSpec &url, const std::string &name);
102  void clear_preapproved_server_certificates(const URLSpec &url);
103  void clear_all_preapproved_server_certificates();
104 
105  INLINE void set_http_version(HTTPEnum::HTTPVersion version);
106  INLINE HTTPEnum::HTTPVersion get_http_version() const;
107  std::string get_http_version_string() const;
108  static HTTPEnum::HTTPVersion parse_http_version_string(const std::string &version);
109 
110  bool load_certificates(const Filename &filename);
111 
112  enum VerifySSL {
113  VS_no_verify, // Don't care who we talk to
114  VS_no_date_check, // Must identify certs, but old, expired certs are OK
115  VS_normal // Identify certs and also check expiration dates.
116  };
117 
118  INLINE void set_verify_ssl(VerifySSL verify_ssl);
119  INLINE VerifySSL get_verify_ssl() const;
120 
121  INLINE void set_cipher_list(const std::string &cipher_list);
122  INLINE const std::string &get_cipher_list() const;
123 
124  PT(HTTPChannel) make_channel(bool persistent_connection);
125  BLOCKING PT(HTTPChannel) post_form(const URLSpec &url, const std::string &body);
126  BLOCKING PT(HTTPChannel) get_document(const URLSpec &url);
127  BLOCKING PT(HTTPChannel) get_header(const URLSpec &url);
128 
129  INLINE static std::string base64_encode(const std::string &s);
130  INLINE static std::string base64_decode(const std::string &s);
131 
132  static HTTPClient *get_global_ptr();
133 
134 public:
135  void get_proxies_for_url(const URLSpec &url, pvector<URLSpec> &proxies) const;
136 
137  SSL_CTX *get_ssl_ctx();
138 
139 private:
140  void check_preapproved_server_certificate(const URLSpec &url, X509 *cert,
141  bool &cert_preapproved, bool &cert_name_preapproved) const;
142 
143  bool get_proxies_for_scheme(const std::string &scheme,
144  pvector<URLSpec> &proxies) const;
145 
146  void add_http_username(const std::string &http_username);
147  std::string select_username(const URLSpec &url, bool is_proxy,
148  const std::string &realm) const;
149 
150  HTTPAuthorization *select_auth(const URLSpec &url, bool is_proxy,
151  const std::string &last_realm);
152  PT(HTTPAuthorization) generate_auth(const URLSpec &url, bool is_proxy,
153  const std::string &challenge);
154 
155  void unload_client_certificate();
156 
157  static X509_NAME *parse_x509_name(const std::string &source);
158  static bool x509_name_subset(X509_NAME *name_a, X509_NAME *name_b);
159 
160  static void split_whitespace(std::string &a, std::string &b, const std::string &c);
161 
162  typedef pvector<URLSpec> Proxies;
163  typedef pmap<std::string, Proxies> ProxiesByScheme;
164  ProxiesByScheme _proxies_by_scheme;
165  typedef pvector<GlobPattern> DirectHosts;
166  DirectHosts _direct_hosts;
167  bool _try_all_direct;
168 
169  HTTPEnum::HTTPVersion _http_version;
170  VerifySSL _verify_ssl;
171  std::string _cipher_list;
172 
173  typedef pmap<std::string, std::string> Usernames;
174  Usernames _usernames;
175 
176  typedef pmap<std::string, PT(HTTPAuthorization)> Realms;
177  class Domain {
178  public:
179  Realms _realms;
180  };
181  typedef pmap<std::string, Domain> Domains;
182  Domains _proxy_domains, _www_domains;
183 
184  // Not a phash_set, since we want this to be maintained in order.
185  typedef pset<HTTPCookie> Cookies;
186  Cookies _cookies;
187 
188  Filename _client_certificate_filename;
189  std::string _client_certificate_pem;
190  std::string _client_certificate_passphrase;
191 
192  SSL_CTX *_ssl_ctx;
193  bool _client_certificate_loaded;
194  X509 *_client_certificate_pub;
195  EVP_PKEY *_client_certificate_priv;
196 
197  typedef pvector<X509 *> ServerCerts;
198  typedef pvector<X509_NAME *> ServerCertNames;
199  class PreapprovedServerCert {
200  public:
201  ~PreapprovedServerCert();
202 
203  ServerCerts _certs;
204  ServerCertNames _cert_names;
205  };
206 
207  typedef pmap<std::string, PreapprovedServerCert> PreapprovedServerCerts;
208  PreapprovedServerCerts _preapproved_server_certs;
209 
210  static PT(HTTPClient) _global_ptr;
211 
212  friend class HTTPChannel;
213 };
214 
215 #include "httpClient.I"
216 
217 #endif // HAVE_OPENSSL
218 
219 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
A container for a URL, e.g.
Definition: urlSpec.h:28
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A base class for all things that want to be reference-counted.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL set.
Definition: pset.h:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.