From: Michael Tremer Date: Sat, 12 Oct 2024 18:05:30 +0000 (+0000) Subject: ctx: Move the cURL share handle here X-Git-Tag: 0.9.30~1058 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=187ff958b552283e7dd7c76d342d6324aa268971;p=pakfire.git ctx: Move the cURL share handle here Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/ctx.c b/src/libpakfire/ctx.c index 417fe66bc..554c28e9a 100644 --- a/src/libpakfire/ctx.c +++ b/src/libpakfire/ctx.c @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -81,6 +82,9 @@ struct pakfire_ctx { void* data; } pick_solution; + // cURL share handle + CURLSH* share; + // Magic Context magic_t magic; }; @@ -166,9 +170,14 @@ static int pakfire_ctx_load_config(struct pakfire_ctx* ctx, const char* path) { } static void pakfire_ctx_free(struct pakfire_ctx* ctx) { + // Release cURL Share Handle + if (ctx->share) + curl_share_cleanup(ctx->share); + // Release Magic Context if (ctx->magic) magic_close(ctx->magic); + if (ctx->config) pakfire_config_unref(ctx->config); free(ctx); @@ -379,6 +388,38 @@ int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire, return ctx->pick_solution.callback(ctx, pakfire, ctx->pick_solution.data, transaction); } +// cURL + +CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* ctx) { + int r; + + // Setup a new handle + if (!ctx->share) { + ctx->share = curl_share_init(); + if (!ctx->share) { + CTX_ERROR(ctx, "Could not setup cURL share handle\n"); + goto ERROR; + } + + // Share connections between handles + r = curl_share_setopt(ctx->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); + if (r) { + CTX_ERROR(ctx, "Could not configure the share handle: %s\n", curl_share_strerror(r)); + goto ERROR; + } + } + + return ctx->share; + +ERROR: + if (ctx->share) { + curl_share_cleanup(ctx->share); + ctx->share = NULL; + } + + return NULL; +} + // Magic magic_t pakfire_ctx_magic(struct pakfire_ctx* ctx) { diff --git a/src/libpakfire/httpclient.c b/src/libpakfire/httpclient.c index b7de1d145..3a4a2dfc8 100644 --- a/src/libpakfire/httpclient.c +++ b/src/libpakfire/httpclient.c @@ -54,9 +54,6 @@ struct pakfire_httpclient { // How many transfers in parallel? long max_parallel; - // cURL share handle - CURLSH* share; - // cURL multi handle CURLM* curl; @@ -310,13 +307,6 @@ static int pakfire_httpclient_setup_curl(struct pakfire_httpclient* client) { return r; } - // Create a new share handle - client->share = curl_share_init(); - if (!client->share) { - CTX_ERROR(client->ctx, "Could not setup cURL share handle\n"); - return 1; - } - // Create a new multi handle client->curl = curl_multi_init(); if (!client->curl) { @@ -324,14 +314,6 @@ static int pakfire_httpclient_setup_curl(struct pakfire_httpclient* client) { return 1; } - // Share connections between handles - r = curl_share_setopt(client->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); - if (r) { - CTX_ERROR(client->ctx, "Could not configure the share handle: %s\n", - curl_share_strerror(r)); - return r; - } - // Register with the event loop r = curl_multi_setopt(client->curl, CURLMOPT_TIMERFUNCTION, pakfire_httpclient_timer); if (r) { @@ -397,8 +379,6 @@ static int pakfire_httpclient_setup_progress(struct pakfire_httpclient* client) static void pakfire_httpclient_free(struct pakfire_httpclient* client) { if (client->progress) pakfire_progress_unref(client->progress); - if (client->share) - curl_share_cleanup(client->share); if (client->curl) curl_multi_cleanup(client->curl); if (client->timer) @@ -479,10 +459,6 @@ sd_event* pakfire_httpclient_loop(struct pakfire_httpclient* client) { return sd_event_ref(client->loop); } -CURLSH* pakfire_httpclient_share(struct pakfire_httpclient* client) { - return client->share; -} - const char* pakfire_httpclient_get_baseurl(struct pakfire_httpclient* client) { if (*client->baseurl) return client->baseurl; diff --git a/src/libpakfire/include/pakfire/ctx.h b/src/libpakfire/include/pakfire/ctx.h index efcbed363..0806e2825 100644 --- a/src/libpakfire/include/pakfire/ctx.h +++ b/src/libpakfire/include/pakfire/ctx.h @@ -110,6 +110,12 @@ int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire_progress* int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire, struct pakfire_transaction* transaction); +// cURL + +#include + +CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* ctx); + // Magic #include @@ -117,5 +123,4 @@ int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire, magic_t pakfire_ctx_magic(struct pakfire_ctx* ctx); #endif /* PAKFIRE_PRIVATE */ - #endif /* PAKFIRE_CTX_H */ diff --git a/src/libpakfire/xfer.c b/src/libpakfire/xfer.c index f68fd1882..7703d354d 100644 --- a/src/libpakfire/xfer.c +++ b/src/libpakfire/xfer.c @@ -254,7 +254,7 @@ static int pakfire_xfer_setup(struct pakfire_xfer* xfer) { const char* proxy = NULL; int r; - CURLSH* share = pakfire_httpclient_share(xfer->client); + CURLSH* share = pakfire_ctx_curl_share(xfer->ctx); // Configure the share handle r = curl_easy_setopt(xfer->handle, CURLOPT_SHARE, share);