commit b64ae5d111c187bd01c79d1d1050168596988c22
parent 22ebfae2404e7bd91013ce74a09c8794f2711c38
Author: Joris Vink <joris@coders.se>
Date: Mon, 21 Oct 2019 13:26:44 +0200
Allow kore_curl calls to be synchronous.
Changes kore_curl_init() to take a flag parameter, much like pgsql api
in which you specify KORE_CURL_ASYNC or KORE_CURL_SYNC.
If KORE_CURL_ASYNC is specified, Kore will behave as before.
If Kore_CURL_SYNC is specified, Kore will execute the libcurl immediately
and return once it has been completed.
Diffstat:
2 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/include/kore/curl.h b/include/kore/curl.h
@@ -31,6 +31,9 @@ extern "C" {
#define KORE_CURL_FLAG_HTTP_PARSED_HEADERS 0x0001
#define KORE_CURL_FLAG_BOUND 0x0002
+#define KORE_CURL_SYNC 0x1000
+#define KORE_CURL_ASYNC 0x2000
+
#define KORE_CURL_TYPE_CUSTOM 1
#define KORE_CURL_TYPE_HTTP_CLIENT 2
@@ -71,8 +74,9 @@ void kore_curl_do_timeout(void);
void kore_curl_run(struct kore_curl *);
void kore_curl_cleanup(struct kore_curl *);
int kore_curl_success(struct kore_curl *);
+void kore_curl_run_sync(struct kore_curl *);
void kore_curl_logerror(struct kore_curl *);
-int kore_curl_init(struct kore_curl *, const char *);
+int kore_curl_init(struct kore_curl *, const char *, int);
size_t kore_curl_tobuf(char *, size_t, size_t, void *);
size_t kore_curl_frombuf(char *, size_t, size_t, void *);
diff --git a/src/curl.c b/src/curl.c
@@ -117,16 +117,25 @@ kore_curl_sysinit(void)
}
int
-kore_curl_init(struct kore_curl *client, const char *url)
+kore_curl_init(struct kore_curl *client, const char *url, int flags)
{
CURL *handle;
+ if ((flags & KORE_CURL_ASYNC) && (flags & KORE_CURL_SYNC)) {
+ (void)kore_strlcpy(client->errbuf, "invalid flags",
+ sizeof(client->errbuf));
+ return (KORE_RESULT_ERROR);
+ }
+
memset(client, 0, sizeof(*client));
TAILQ_INIT(&client->http.resp_hdrs);
- if ((handle = curl_easy_init()) == NULL)
+ if ((handle = curl_easy_init()) == NULL) {
+ (void)kore_strlcpy(client->errbuf, "failed to setup curl",
+ sizeof(client->errbuf));
return (KORE_RESULT_ERROR);
+ }
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &client->response);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, kore_curl_tobuf);
@@ -138,6 +147,7 @@ kore_curl_init(struct kore_curl *client, const char *url)
curl_easy_setopt(handle, CURLOPT_TIMEOUT, kore_curl_timeout);
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, client->errbuf);
+ client->flags = flags;
client->handle = handle;
client->url = kore_strdup(url);
client->type = KORE_CURL_TYPE_CUSTOM;
@@ -276,7 +286,18 @@ kore_curl_bind_callback(struct kore_curl *client,
void
kore_curl_run(struct kore_curl *client)
{
- curl_multi_add_handle(multi, client->handle);
+ if (client->flags & KORE_CURL_ASYNC) {
+ curl_multi_add_handle(multi, client->handle);
+ return;
+ }
+
+ client->result = curl_easy_perform(client->handle);
+
+ curl_easy_getinfo(client->handle,
+ CURLINFO_RESPONSE_CODE, &client->http.status);
+
+ curl_easy_cleanup(client->handle);
+ client->handle = NULL;
}
int