From 8a308f2e12ab920bf0a35d903291a6be38c8ce7c Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Mon, 27 Feb 2023 02:01:41 +0000 Subject: [PATCH] Feature: Use ping requests for URL events --- src/event_url.c | 38 +++----------------------------------- src/ping.c | 13 ++++++++++++- src/ping.h | 2 +- 3 files changed, 16 insertions(+), 37 deletions(-) diff --git a/src/event_url.c b/src/event_url.c index 08388868..6fb9c775 100644 --- a/src/event_url.c +++ b/src/event_url.c @@ -19,7 +19,7 @@ #include "global.h" /* for igloo_instance */ #include "string_renderer.h" -#include "curl.h" +#include "ping.h" #include "event.h" #include "cfgfile.h" #include "util.h" @@ -33,22 +33,8 @@ typedef struct event_url { char *action; char *username; char *password; - CURL *handle; - char errormsg[CURL_ERROR_SIZE]; } event_url_t; -static size_t handle_returned (void *ptr, size_t size, size_t nmemb, void *stream) { - (void)ptr, (void)stream; - return size * nmemb; -} - -static inline char *__escape(const char *src, const char *default_value) { - if (src) - return util_url_escape(src); - - return strdup(default_value); -} - static int event_url_emit(void *state, event_t *event) { event_url_t *self = state; ice_config_t *config; @@ -93,19 +79,7 @@ static int event_url_emit(void *state, event_t *event) { string_renderer_end_list(renderer); - - if (strchr(self->url, '@') == NULL && self->username && self->password) { - curl_easy_setopt(self->handle, CURLOPT_USERNAME, self->username); - curl_easy_setopt(self->handle, CURLOPT_PASSWORD, self->password); - } else { - curl_easy_setopt(self->handle, CURLOPT_USERPWD, ""); - } - - curl_easy_setopt(self->handle, CURLOPT_URL, self->url); - curl_easy_setopt(self->handle, CURLOPT_POSTFIELDS, string_renderer_to_string_zero_copy(renderer)); - - if (curl_easy_perform(self->handle)) - ICECAST_LOG_WARN("auth to server %s failed with %s", self->url, self->errormsg); + ping_simple(self->url, self->username, self->password, string_renderer_to_string_zero_copy(renderer)); igloo_ro_unref(&renderer); @@ -114,7 +88,6 @@ static int event_url_emit(void *state, event_t *event) { static void event_url_free(void *state) { event_url_t *self = state; - icecast_curl_free(self->handle); free(self->url); free(self->action); free(self->username); @@ -158,17 +131,12 @@ int event_get_url(event_registration_t *er, config_options_t *options) { } while ((options = options->next)); } - self->handle = icecast_curl_new(NULL, NULL); - /* check if we are in sane state */ - if (!self->url || !self->handle) { + if (!self->url) { event_url_free(self); return -1; } - curl_easy_setopt(self->handle, CURLOPT_HEADERFUNCTION, handle_returned); - curl_easy_setopt(self->handle, CURLOPT_ERRORBUFFER, self->errormsg); - er->state = self; er->emit = event_url_emit; er->free = event_url_free; diff --git a/src/ping.c b/src/ping.c index ad462b6d..2a46a5b9 100644 --- a/src/ping.c +++ b/src/ping.c @@ -13,6 +13,7 @@ #include #include +#include #include "thread/thread.h" @@ -103,7 +104,7 @@ static void ping_add_to_queue(ping_queue_t *entry) thread_mutex_unlock(&ping_mutex); } -void ping_simple(const char *url) +void ping_simple(const char *url, const char *username, const char *password, const char *data) { ping_queue_t *entry = calloc(1, sizeof(*entry)); @@ -116,6 +117,16 @@ void ping_simple(const char *url) return; } + if (strchr(url, '@') == NULL) { + if (username) + curl_easy_setopt(entry->curl, CURLOPT_USERNAME, username); + if (password) + curl_easy_setopt(entry->curl, CURLOPT_PASSWORD, password); + } + + if (data) + curl_easy_setopt(entry->curl, CURLOPT_COPYPOSTFIELDS, data); + ping_add_to_queue(entry); } diff --git a/src/ping.h b/src/ping.h index 913aabf5..78bc9fff 100644 --- a/src/ping.h +++ b/src/ping.h @@ -12,6 +12,6 @@ void ping_initialize(void); void ping_shutdown(void); -void ping_simple(const char *url); +void ping_simple(const char *url, const char *username, const char *password, const char *data); #endif /* __PING_H__ */