]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-http: Add http_client_get_global_context()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 15 Dec 2017 12:08:20 +0000 (14:08 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 15 Dec 2017 12:31:24 +0000 (14:31 +0200)
src/lib-http/http-client.c
src/lib-http/http-client.h

index 08c9cceb569bead1ba819be4346ee8e1ab5c1b94..35fd347359867bd66108e8c39cf8eb885975140d 100644 (file)
@@ -87,6 +87,8 @@
 
  */
 
+static struct http_client_context *http_client_global_context = NULL;
+
 /*
  * Client
  */
@@ -533,3 +535,21 @@ void http_client_context_switch_ioloop(struct http_client_context *cctx)
                hshared = hshared->next)
                http_client_host_shared_switch_ioloop(hshared);
 }
+
+static void http_client_global_context_free(void)
+{
+       http_client_context_unref(&http_client_global_context);
+}
+
+struct http_client_context *http_client_get_global_context(void)
+{
+       if (http_client_global_context != NULL)
+               return http_client_global_context;
+
+       struct http_client_settings set;
+       i_zero(&set);
+       http_client_global_context = http_client_context_create(&set);
+       /* keep this a bit higher than lib-ssl-iostream */
+       lib_atexit_priority(http_client_global_context_free, LIB_ATEXIT_PRIORITY_LOW-1);
+       return http_client_global_context;
+}
index f339a544f129ceca15e813ed8cec3d7cf55deddc..234cd577c77ad2ab678a3e1044e8cdecaa5175fb 100644 (file)
@@ -448,4 +448,9 @@ http_client_context_create(const struct http_client_settings *set);
 void http_client_context_ref(struct http_client_context *cctx);
 void http_client_context_unref(struct http_client_context **_cctx);
 
+/* Return the default global shared client context, creating it if necessary.
+   The context is freed automatically at exit. Don't unreference the
+   returned context. */
+struct http_client_context *http_client_get_global_context(void);
+
 #endif