Created
January 30, 2024 02:34
-
-
Save wd5gnr/4a0bcf234e57727f75e63e3bf0900ab7 to your computer and use it in GitHub Desktop.
Simple libcurl example (uses autogenerated skeleton)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* Sample code generated by the curl command line tool ********** | |
* All curl_easy_setopt() options are documented at: | |
* https://curl.se/libcurl/c/curl_easy_setopt.html | |
************************************************************************/ | |
// Pick up Hackaday home page | |
#include <curl/curl.h> | |
/* add some custom helper functions */ | |
#include <stdlib.h> // new header | |
#include <string.h> // new header | |
struct BUFFER | |
{ | |
size_t len; | |
char *s; | |
}; | |
struct BUFFER * init_buffer(struct BUFFER *b) | |
{ | |
b->len=0; | |
b->s=malloc(1); | |
if (!b->s) return NULL; | |
b->s[0]='\0'; | |
return b; | |
} | |
size_t writefn(void *ptr, size_t size, size_t nmemb, struct BUFFER *b) | |
{ | |
size_t nblen=size*nmemb; | |
size_t nlen=b->len+nblen; | |
b->s=realloc(b->s,nlen+1); | |
if (!b->s) return 0; | |
memcpy(b->s+b->len,ptr,nblen); | |
b->s[nlen]='\0'; | |
b->len=nlen; | |
return nblen; | |
} | |
/* back to boilerplate */ | |
int main(int argc, char *argv[]) | |
{ | |
CURLcode ret; | |
CURL *hnd; | |
struct BUFFER buff; // new code | |
if (!init_buffer(&buff)) // new code | |
{ | |
fprintf(stderr,"Memory allocation error\n"); | |
return 98; | |
} | |
hnd = curl_easy_init(); | |
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L); | |
curl_easy_setopt(hnd, CURLOPT_URL, "http://www.hackaday.com"); | |
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.81.0"); | |
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 1L); | |
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L); | |
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS); | |
curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L); | |
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L); | |
/* Here is a list of options the curl code used that cannot get generated | |
as source easily. You may choose to either not use them or implement | |
them yourself. | |
CURLOPT_WRITEDATA set to a objectpointer | |
CURLOPT_INTERLEAVEDATA set to a objectpointer | |
CURLOPT_WRITEFUNCTION set to a functionpointer | |
CURLOPT_READDATA set to a objectpointer | |
CURLOPT_READFUNCTION set to a functionpointer | |
CURLOPT_SEEKDATA set to a objectpointer | |
CURLOPT_SEEKFUNCTION set to a functionpointer | |
CURLOPT_ERRORBUFFER set to a objectpointer | |
CURLOPT_STDERR set to a objectpointer | |
CURLOPT_HEADERFUNCTION set to a functionpointer | |
CURLOPT_HEADERDATA set to a objectpointer | |
*/ | |
// custom code here | |
curl_easy_setopt(hnd,CURLOPT_WRITEFUNCTION, writefn); | |
curl_easy_setopt(hnd,CURLOPT_WRITEDATA,&buff); | |
// back to template code | |
ret = curl_easy_perform(hnd); | |
/* Custom code here */ | |
if (ret!=CURLE_OK) | |
{ | |
fprintf(stderr,"failure: %s\n",curl_easy_strerror(ret)); | |
free(buff.s); | |
return 99; | |
} | |
// ok here we have buff containing it all: | |
printf("%s\n",buff.s); | |
// presumably you really want to write code to parse it now, right... | |
/* End of custom code */ | |
curl_easy_cleanup(hnd); | |
hnd = NULL; | |
return (int)ret; | |
} | |
/**** End of sample code ****/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment