From 0763cd71ad79090db8e107ba42ba9bd14ac7812a Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sat, 31 Dec 2016 21:40:31 +0100 Subject: [PATCH] Client contexts now use callbacks --- include/libhttp.h | 2 +- src/httplib_create_client_context.c | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/libhttp.h b/include/libhttp.h index 2168ec90..a4df8953 100644 --- a/include/libhttp.h +++ b/include/libhttp.h @@ -907,7 +907,7 @@ LIBHTTP_API int httplib_closedir( DIR *dir ); LIBHTTP_API struct httplib_connection * httplib_connect_client( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size ); LIBHTTP_API struct httplib_connection * httplib_connect_client_secure( struct httplib_context *ctx, const struct httplib_client_options *client_options, char *error_buffer, size_t error_buffer_size ); LIBHTTP_API struct httplib_connection * httplib_connect_websocket_client( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size, const char *path, const char *origin, httplib_websocket_data_handler data_func, httplib_websocket_close_handler close_func, void *user_data ); -LIBHTTP_API struct httplib_context * httplib_create_client_context( const struct httplib_option_t *options ); +LIBHTTP_API struct httplib_context * httplib_create_client_context( const struct httplib_callbacks *callbacks, const struct httplib_option_t *options ); LIBHTTP_API void httplib_cry( enum debug_level_t debug_level, const struct httplib_context *ctx, const struct httplib_connection *conn, PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(4, 5); LIBHTTP_API void httplib_destroy_client_context( struct httplib_context *ctx ); LIBHTTP_API struct httplib_connection * httplib_download( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size, PRINTF_FORMAT_STRING(const char *request_fmt), ...) PRINTF_ARGS(7, 8); diff --git a/src/httplib_create_client_context.c b/src/httplib_create_client_context.c index 79c955f4..8501dbf1 100644 --- a/src/httplib_create_client_context.c +++ b/src/httplib_create_client_context.c @@ -23,7 +23,7 @@ #include "httplib_main.h" /* - * struct httplib_context *httplib_create_client_context( const struct httplib_option_t *options ); + * struct httplib_context *httplib_create_client_context( const struct httplib_callbacks *callbacks, const struct httplib_option_t *options ); * * The function httplib_create_client_context() creates a context to be used * for one simultaneous client connection. It is not possible to use one client @@ -31,16 +31,30 @@ * contains SSL context information which is specific for one connection. */ -struct httplib_context *httplib_create_client_context( const struct httplib_option_t *options ) { +struct httplib_context *httplib_create_client_context( const struct httplib_callbacks *callbacks, const struct httplib_option_t *options ) { struct httplib_context *ctx; + void (*exit_callback)(const struct httplib_context *ctx); - ctx = httplib_calloc( 1, sizeof(struct httplib_context) ); + exit_callback = NULL; + ctx = httplib_calloc( 1, sizeof(struct httplib_context) ); if ( ctx == NULL ) return NULL; + if ( callbacks != NULL ) { + + ctx->callbacks = *callbacks; + exit_callback = callbacks->exit_context; + ctx->callbacks.exit_context = NULL; + } + if ( XX_httplib_init_options( ctx ) ) return NULL; if ( XX_httplib_process_options( ctx, options ) ) return NULL; + if ( ctx->callbacks.init_context != NULL ) ctx->callbacks.init_context( ctx ); + + ctx->callbacks.exit_context = exit_callback; + ctx->ctx_type = CTX_TYPE_CLIENT; + return ctx; } /* httplib_create_client_context */