http-client.h revision 70505f4839520ac67895992621c97d2480c22e7f
45e9809aff7304721fddb95654901b32195c9c7avboxsync#ifndef HTTP_CLIENT_H
45e9809aff7304721fddb95654901b32195c9c7avboxsync#define HTTP_CLIENT_H
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include "http-response-parser.h"
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsyncstruct http_response;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsyncstruct http_client;
45e9809aff7304721fddb95654901b32195c9c7avboxsyncstruct http_client_request;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsyncenum http_client_request_error {
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_CLIENT_REQUEST_ERROR_ABORTED = 9000,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_CLIENT_REQUEST_ERROR_HOST_LOOKUP_FAILED,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_CLIENT_REQUEST_ERROR_CONNECTION_LOST,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_CLIENT_REQUEST_ERROR_BAD_RESPONSE,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_CLIENT_REQUEST_ERROR_TIMED_OUT,
45e9809aff7304721fddb95654901b32195c9c7avboxsync};
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsyncenum http_request_state {
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_NEW = 0,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_QUEUED,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_PAYLOAD_OUT,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_WAITING,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_GOT_RESPONSE,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_PAYLOAD_IN,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_FINISHED,
45e9809aff7304721fddb95654901b32195c9c7avboxsync HTTP_REQUEST_STATE_ABORTED
45e9809aff7304721fddb95654901b32195c9c7avboxsync};
45e9809aff7304721fddb95654901b32195c9c7avboxsyncextern const char *http_request_state_names[];
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsyncstruct http_client_settings {
45e9809aff7304721fddb95654901b32195c9c7avboxsync const char *dns_client_socket_path;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync const char *ssl_ca_dir, *ssl_ca_file, *ssl_ca;
45e9809aff7304721fddb95654901b32195c9c7avboxsync const char *ssl_crypto_device;
45e9809aff7304721fddb95654901b32195c9c7avboxsync bool ssl_allow_invalid_cert;
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* user cert */
45e9809aff7304721fddb95654901b32195c9c7avboxsync const char *ssl_cert, *ssl_key, *ssl_key_password;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync const char *rawlog_dir;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync unsigned int max_idle_time_msecs;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* maximum number of parallel connections per peer (default = 1) */
45e9809aff7304721fddb95654901b32195c9c7avboxsync unsigned int max_parallel_connections;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* maximum number of pipelined requests per connection (default = 1) */
45e9809aff7304721fddb95654901b32195c9c7avboxsync unsigned int max_pipelined_requests;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* maximum number of redirects for a request
45e9809aff7304721fddb95654901b32195c9c7avboxsync (default = 0; redirects refused)
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsync unsigned int max_redirects;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* maximum number of attempts for a request */
unsigned int max_attempts;
/* max time to wait for HTTP request to finish before retrying
(default = unlimited) */
unsigned int request_timeout_msecs;
/* max time to wait for connect() (and SSL handshake) to finish before
retrying (default = request_timeout_msecs) */
unsigned int connect_timeout_msecs;
/* time to wait for connect() (and SSL handshake) to finish for the first
connection before trying the next IP in parallel
(default = 0; wait until current connection attempt finishes) */
unsigned int soft_connect_timeout_msecs;
bool debug;
};
typedef void
http_client_request_callback_t(const struct http_response *response,
void *context);
struct http_client *http_client_init(const struct http_client_settings *set);
void http_client_deinit(struct http_client **_client);
struct http_client_request *
http_client_request(struct http_client *client,
const char *method, const char *host, const char *target,
http_client_request_callback_t *callback, void *context);
#define http_client_request(client, method, host, target, callback, context) \
http_client_request(client, method, host, target + \
CALLBACK_TYPECHECK(callback, void (*)( \
const struct http_response *response, typeof(context))), \
(http_client_request_callback_t *)callback, context)
void http_client_request_set_port(struct http_client_request *req,
unsigned int port);
void http_client_request_set_ssl(struct http_client_request *req,
bool ssl);
void http_client_request_set_urgent(struct http_client_request *req);
void http_client_request_add_header(struct http_client_request *req,
const char *key, const char *value);
void http_client_request_set_payload(struct http_client_request *req,
struct istream *input, bool sync);
enum http_request_state
http_client_request_get_state(struct http_client_request *req);
void http_client_request_submit(struct http_client_request *req);
bool http_client_request_try_retry(struct http_client_request *req);
void http_client_request_abort(struct http_client_request **req);
/* Call the specified callback when HTTP request is destroyed. */
void http_client_request_set_destroy_callback(struct http_client_request *req,
void (*callback)(void *),
void *context);
/* submits request and blocks until provided payload is sent. Multiple calls
are allowed; payload transmission is ended with
http_client_request_finish_payload(). */
int http_client_request_send_payload(struct http_client_request **req,
const unsigned char *data, size_t size);
int http_client_request_finish_payload(struct http_client_request **req);
void http_client_switch_ioloop(struct http_client *client);
/* blocks until all currently submitted requests are handled */
void http_client_wait(struct http_client *client);
#endif