Skip to content

Commit 1cc7d12

Browse files
committed
Add curl_kput(), which slurps an URL into a kstring
Reads from the URL, appending its contents to the kstring.
1 parent f581de8 commit 1cc7d12

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

hfile_libcurl.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,41 @@ int PLUGIN_GLOBAL(hfile_plugin_init,_libcurl)(struct hFILE_plugin *self)
598598
* Rewrite S3 URLs *
599599
*******************/
600600

601+
static size_t kput_callback(char *ptr, size_t size, size_t nmemb, void *strv)
602+
{
603+
kstring_t *str = (kstring_t *) strv;
604+
size_t len = size * nmemb;
605+
return (kputsn(ptr, len, str) >= 0)? len : 0;
606+
}
607+
608+
static int curl_kput(const char *url, kstring_t *str)
609+
{
610+
CURLcode err;
611+
CURL *easy = curl_easy_init();
612+
if (easy == NULL) { errno = ENOMEM; return -1; }
613+
614+
err = curl_easy_setopt(easy, CURLOPT_URL, url);
615+
err |= curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, kput_callback);
616+
err |= curl_easy_setopt(easy, CURLOPT_WRITEDATA, str);
617+
err |= curl_easy_setopt(easy, CURLOPT_USERAGENT, curl.useragent.s);
618+
err |= curl_easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1L);
619+
err |= curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
620+
if (hts_verbose >= 8) err |= curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
621+
if (err != 0) { curl_easy_cleanup(easy); errno = ENOSYS; return -1; }
622+
623+
err = curl_easy_perform(easy);
624+
if (err != CURLE_OK) {
625+
int save_errno = easy_errno(easy, err);
626+
curl_easy_cleanup(easy);
627+
errno = save_errno;
628+
return -1;
629+
}
630+
631+
curl_easy_cleanup(easy);
632+
return 0;
633+
}
634+
635+
601636
#if defined HAVE_COMMONCRYPTO
602637

603638
#include <CommonCrypto/CommonHMAC.h>

0 commit comments

Comments
 (0)