diff options
Diffstat (limited to 'src/libstrongswan/plugins/curl/curl_fetcher.c')
-rw-r--r-- | src/libstrongswan/plugins/curl/curl_fetcher.c | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/src/libstrongswan/plugins/curl/curl_fetcher.c b/src/libstrongswan/plugins/curl/curl_fetcher.c index 7ee9fa1bd..08315e932 100644 --- a/src/libstrongswan/plugins/curl/curl_fetcher.c +++ b/src/libstrongswan/plugins/curl/curl_fetcher.c @@ -33,14 +33,14 @@ struct private_curl_fetcher_t { * Public data */ curl_fetcher_t public; - + /** * CURL handle */ CURL* curl; - + /** - * Optional HTTP headers + * Optional HTTP headers */ struct curl_slist *headers; }; @@ -51,7 +51,7 @@ struct private_curl_fetcher_t { static size_t append(void *ptr, size_t size, size_t nmemb, chunk_t *data) { size_t realsize = size * nmemb; - + data->ptr = (u_char*)realloc(data->ptr, data->len + realsize); if (data->ptr) { @@ -61,16 +61,14 @@ static size_t append(void *ptr, size_t size, size_t nmemb, chunk_t *data) return realsize; } -/** - * Implements fetcher_t.fetch. - */ -static status_t fetch(private_curl_fetcher_t *this, char *uri, chunk_t *result) +METHOD(fetcher_t, fetch, status_t, + private_curl_fetcher_t *this, char *uri, chunk_t *result) { char error[CURL_ERROR_SIZE]; status_t status; - + *result = chunk_empty; - + if (curl_easy_setopt(this->curl, CURLOPT_URL, uri) != CURLE_OK) { /* URL type not supported by curl */ return NOT_SUPPORTED; @@ -85,7 +83,7 @@ static status_t fetch(private_curl_fetcher_t *this, char *uri, chunk_t *result) { curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers); } - + DBG2(" sending http request to '%s'...", uri); switch (curl_easy_perform(this->curl)) { @@ -103,13 +101,11 @@ static status_t fetch(private_curl_fetcher_t *this, char *uri, chunk_t *result) return status; } -/** - * Implementation of fetcher_t.set_option. - */ -static bool set_option(private_curl_fetcher_t *this, fetcher_option_t option, ...) +METHOD(fetcher_t, set_option, bool, + private_curl_fetcher_t *this, fetcher_option_t option, ...) { va_list args; - + va_start(args, option); switch (option) { @@ -154,10 +150,8 @@ static bool set_option(private_curl_fetcher_t *this, fetcher_option_t option, .. } } -/** - * Implements fetcher_t.destroy - */ -static void destroy(private_curl_fetcher_t *this) +METHOD(fetcher_t, destroy, void, + private_curl_fetcher_t *this) { curl_slist_free_all(this->headers); curl_easy_cleanup(this->curl); @@ -169,20 +163,22 @@ static void destroy(private_curl_fetcher_t *this) */ curl_fetcher_t *curl_fetcher_create() { - private_curl_fetcher_t *this = malloc_thing(private_curl_fetcher_t); - - this->curl = curl_easy_init(); - if (this->curl == NULL) + private_curl_fetcher_t *this; + + INIT(this, + .public.interface = { + .fetch = _fetch, + .set_option = _set_option, + .destroy = _destroy, + }, + .curl = curl_easy_init(), + ); + + if (!this->curl) { free(this); return NULL; } - this->headers = NULL; - - this->public.interface.fetch = (status_t(*)(fetcher_t*,char*,chunk_t*))fetch; - this->public.interface.set_option = (bool(*)(fetcher_t*, fetcher_option_t option, ...))set_option; - this->public.interface.destroy = (void (*)(fetcher_t*))destroy; - return &this->public; } |