38 typedef struct bio_st BIO;
56 HTTPChannel(HTTPClient *client);
59 virtual ~HTTPChannel();
73 SC_socks_invalid_version,
74 SC_socks_no_acceptable_login_method,
76 SC_socks_no_connection,
77 SC_ssl_internal_failure,
82 SC_http_error_watermark,
84 SC_ssl_invalid_server_certificate,
85 SC_ssl_self_signed_server_certificate,
86 SC_ssl_unexpected_server,
90 SC_download_open_error,
91 SC_download_write_error,
92 SC_download_invalid_range,
95 INLINE HTTPClient *get_client()
const;
97 INLINE
bool is_valid()
const;
98 INLINE
bool is_connection_ready()
const;
100 INLINE
const URLSpec &get_url()
const;
102 INLINE HTTPEnum::HTTPVersion get_http_version()
const;
103 INLINE
const std::string &get_http_version_string()
const;
104 INLINE
int get_status_code()
const;
105 std::string get_status_string()
const;
106 INLINE
const std::string &get_www_realm()
const;
107 INLINE
const std::string &get_proxy_realm()
const;
108 INLINE
const URLSpec &get_redirect()
const;
109 std::string get_header_value(
const std::string &key)
const;
111 INLINE
int get_num_redirect_steps()
const;
112 INLINE
const URLSpec &get_redirect_step(
int n)
const;
113 MAKE_SEQ(get_redirect_steps, get_num_redirect_steps, get_redirect_step);
115 INLINE
void set_persistent_connection(
bool persistent_connection);
116 INLINE
bool get_persistent_connection()
const;
117 bool will_close_connection()
const;
119 INLINE
void set_allow_proxy(
bool allow_proxy);
120 INLINE
bool get_allow_proxy()
const;
121 INLINE
void set_proxy_tunnel(
bool proxy_tunnel);
122 INLINE
bool get_proxy_tunnel()
const;
124 INLINE
void set_connect_timeout(
double timeout_seconds);
125 INLINE
double get_connect_timeout()
const;
126 INLINE
void set_blocking_connect(
bool blocking_connect);
127 INLINE
bool get_blocking_connect()
const;
129 INLINE
void set_http_timeout(
double timeout_seconds);
130 INLINE
double get_http_timeout()
const;
132 INLINE
void set_skip_body_size(
size_t skip_body_size);
133 INLINE
size_t get_skip_body_size()
const;
134 INLINE
void set_idle_timeout(
double idle_timeout);
135 INLINE
double get_idle_timeout()
const;
137 INLINE
void set_download_throttle(
bool download_throttle);
138 INLINE
bool get_download_throttle()
const;
140 INLINE
void set_max_bytes_per_second(
double max_bytes_per_second);
141 INLINE
double get_max_bytes_per_second()
const;
143 INLINE
void set_max_updates_per_second(
double max_updates_per_second);
144 INLINE
double get_max_updates_per_second()
const;
146 INLINE
void set_content_type(std::string content_type);
147 INLINE std::string get_content_type()
const;
149 INLINE
void set_expected_file_size(
size_t file_size);
150 std::streamsize get_file_size()
const;
151 INLINE
bool is_file_size_known()
const;
153 INLINE
size_t get_first_byte_requested()
const;
154 INLINE
size_t get_last_byte_requested()
const;
155 INLINE
size_t get_first_byte_delivered()
const;
156 INLINE
size_t get_last_byte_delivered()
const;
158 void write_headers(std::ostream &out)
const;
161 INLINE
void preserve_status();
163 INLINE
void clear_extra_headers();
164 INLINE
void send_extra_header(
const std::string &key,
const std::string &value);
166 BLOCKING INLINE
bool get_document(
const DocumentSpec &url);
167 BLOCKING INLINE
bool get_subdocument(
const DocumentSpec &url,
168 size_t first_byte,
size_t last_byte);
169 BLOCKING INLINE
bool get_header(
const DocumentSpec &url);
170 BLOCKING INLINE
bool post_form(
const DocumentSpec &url,
const std::string &body);
171 BLOCKING INLINE
bool put_document(
const DocumentSpec &url,
const std::string &body);
172 BLOCKING INLINE
bool delete_document(
const DocumentSpec &url);
173 BLOCKING INLINE
bool get_trace(
const DocumentSpec &url);
174 BLOCKING INLINE
bool connect_to(
const DocumentSpec &url);
175 BLOCKING INLINE
bool get_options(
const DocumentSpec &url);
177 INLINE
void begin_get_document(
const DocumentSpec &url);
178 INLINE
void begin_get_subdocument(
const DocumentSpec &url,
179 size_t first_byte,
size_t last_byte);
181 INLINE
void begin_post_form(
const DocumentSpec &url,
const std::string &body);
185 ISocketStream *open_read_body();
186 void close_read_body(std::istream *stream)
const;
188 BLOCKING
bool download_to_file(
const Filename &filename,
bool subdocument_resumes =
true);
189 BLOCKING
bool download_to_ram(
Ramfile *ramfile,
bool subdocument_resumes =
true);
190 BLOCKING
bool download_to_stream(std::ostream *strm,
bool subdocument_resumes =
true);
191 SocketStream *get_connection();
193 INLINE
size_t get_bytes_downloaded()
const;
194 INLINE
size_t get_bytes_requested()
const;
195 INLINE
bool is_download_complete()
const;
198 static std::string
downcase(
const std::string &s);
199 void body_stream_destructs(ISocketStream *stream);
202 bool reached_done_state();
203 bool run_try_next_proxy();
204 bool run_connecting();
205 bool run_connecting_wait();
206 bool run_http_proxy_ready();
207 bool run_http_proxy_request_sent();
208 bool run_http_proxy_reading_header();
209 bool run_socks_proxy_greet();
210 bool run_socks_proxy_greet_reply();
211 bool run_socks_proxy_connect();
212 bool run_socks_proxy_connect_reply();
213 bool run_setup_ssl();
214 bool run_ssl_handshake();
216 bool run_request_sent();
217 bool run_reading_header();
218 bool run_start_direct_file_read();
219 bool run_read_header();
220 bool run_begin_body();
221 bool run_reading_body();
222 bool run_read_body();
223 bool run_read_trailer();
225 bool run_download_to_file();
226 bool run_download_to_ram();
227 bool run_download_to_stream();
229 void begin_request(HTTPEnum::Method method,
const DocumentSpec &url,
230 const std::string &body,
bool nonblocking,
231 size_t first_byte,
size_t last_byte);
232 void reconsider_proxy();
233 void reset_for_new_request();
235 void finished_body(
bool has_trailer);
236 bool open_download_file();
238 bool server_getline(std::string &str);
239 bool server_getline_failsafe(std::string &str);
240 bool server_get(std::string &str,
size_t num_bytes);
241 bool server_get_failsafe(std::string &str,
size_t num_bytes);
242 bool server_send(
const std::string &str,
bool secret);
243 bool parse_http_response(
const std::string &line);
244 bool parse_http_header();
245 bool parse_content_range(
const std::string &content_range);
249 void check_preapproved_server_certificate(X509 *cert,
bool &cert_preapproved,
250 bool &cert_name_preapproved)
const;
251 bool validate_server_name(X509 *cert);
252 static bool match_cert_name(
const std::string &cert_name,
const std::string &hostname);
253 static std::string get_x509_name_component(X509_NAME *name,
int nid);
256 void make_proxy_request_text();
257 void make_request_text();
260 void store_header_field(
const std::string &field_name,
const std::string &field_value);
263 static void show_send(
const std::string &message);
266 void reset_download_to();
267 void close_download_stream();
269 void reset_body_stream();
270 void close_connection();
272 static bool more_useful_status_code(
int a,
int b);
282 S_http_proxy_request_sent,
283 S_http_proxy_reading_header,
285 S_socks_proxy_greet_reply,
286 S_socks_proxy_connect,
287 S_socks_proxy_connect_reply,
293 S_start_direct_file_read,
305 INLINE StatusEntry();
307 std::string _status_string;
314 size_t _proxy_next_index;
315 StatusList _status_list;
318 PT(BioStreamPtr) _source;
319 bool _persistent_connection;
322 double _connect_timeout;
323 double _http_timeout;
324 size_t _skip_body_size;
325 double _idle_timeout;
326 bool _blocking_connect;
327 bool _download_throttle;
328 double _max_bytes_per_second;
329 double _max_updates_per_second;
330 double _seconds_per_update;
331 int _bytes_per_update;
333 bool _wanted_nonblocking;
334 std::string _send_extra_headers;
338 HTTPEnum::Method _method;
339 std::string request_path;
342 std::string _content_type;
344 bool _proxy_serves_document;
345 bool _proxy_tunnel_now;
346 bool _server_response_has_no_body;
347 size_t _first_byte_requested;
348 size_t _last_byte_requested;
349 size_t _first_byte_delivered;
350 size_t _last_byte_delivered;
359 DownloadDest _download_dest;
360 bool _subdocument_resumes;
363 std::ostream *_download_to_stream;
367 HTTPEnum::HTTPVersion _http_version;
368 std::string _http_version_string;
369 StatusEntry _status_entry;
372 std::string _proxy_realm;
373 std::string _proxy_username;
374 PT(HTTPAuthorization) _proxy_auth;
376 std::string _www_realm;
377 std::string _www_username;
378 PT(HTTPAuthorization) _www_auth;
388 ResponseType _response_type;
394 size_t _expected_file_size;
396 size_t _transfer_file_size;
397 size_t _bytes_downloaded;
398 size_t _bytes_requested;
399 bool _got_expected_file_size;
401 bool _got_transfer_file_size;
410 double _started_connecting_time;
411 double _sent_request_time;
412 bool _started_download;
413 std::string _proxy_header;
414 std::string _proxy_request_text;
415 std::string _request_text;
416 std::string _working_get;
418 std::string _current_field_name;
419 std::string _current_field_value;
420 ISocketStream *_body_stream;
421 bool _owns_body_stream;
423 std::string _cipher_list;
425 int _last_status_code;
426 double _last_run_time;
430 double _extra_ssl_handshake_time;
434 return get_class_type();
436 virtual TypeHandle force_init_type() {init_type();
return get_class_type();}
440 static void init_type() {
441 TypedReferenceCount::init_type();
443 TypedReferenceCount::get_class_type());
448 friend class ChunkedStreamBuf;
449 friend class IdentityStreamBuf;
450 friend class HTTPClient;
453 std::ostream &operator << (std::ostream &out, HTTPChannel::State state);
457 #endif // HAVE_OPENSSL