]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Move the cURL share handle here
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Oct 2024 18:05:30 +0000 (18:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Oct 2024 18:05:30 +0000 (18:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/ctx.c
src/libpakfire/httpclient.c
src/libpakfire/include/pakfire/ctx.h
src/libpakfire/xfer.c

index 417fe66bc70f2250d0846ddd53747b160503db38..554c28e9aa899e4ef7d33fd8b741a140408e73f6 100644 (file)
@@ -27,6 +27,7 @@
 #include <syslog.h>
 #include <wordexp.h>
 
+#include <curl/curl.h>
 #include <magic.h>
 
 #include <pakfire/config.h>
@@ -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) {
index b7de1d1457b16d4cb9c76a461ac9ced0b75a7b12..3a4a2dfc8b0358cf8c5e85073bf6128ce9895195 100644 (file)
@@ -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;
index efcbed363012aba1d453697dc8f9ebf522d5df77..0806e2825d42e1aeebe0a87eec2c76ecaf923459 100644 (file)
@@ -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 <curl/curl.h>
+
+CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* ctx);
+
 // Magic
 
 #include <magic.h>
@@ -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 */
index f68fd1882e227c103f4e3f9a5b2c39904dd42db7..7703d354d22dcbdaf9ee5b823ece27b82d6c467c 100644 (file)
@@ -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);