Skip to content

Instantly share code, notes, and snippets.

@nomissbowling
Last active January 4, 2021 07:21
Show Gist options
  • Save nomissbowling/dd75fc593f5d523592c611a36e06a37e to your computer and use it in GitHub Desktop.
Save nomissbowling/dd75fc593f5d523592c611a36e06a37e to your computer and use it in GitHub Desktop.
test_libcurl.c
/*
test_libcurl.c
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe" -source-charset:utf-8 -execution-charset:utf-8 -EHsc -Fe.\test_libcurl.exe .\test_libcurl.c -I.\curl-7.74.0_2-win64-mingw\include -link /LIBPATH:.\ /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64" libcurl-x64.lib
del test_libcurl.obj
test_libcurl
------------------------------------------------------------------------
extract
from curl-7.74.0/win64/curl-7.74.0_2-win64-mingw.zip 3,565,580
to curl-7.74.0_2-win64-mingw/
copy
from curl-7.74.0_2-win64-mingw/bin/
to ./
curl-ca-bundle.crt 2020-12-08 04:12 221,418
libcurl-x64.def 2020-12-09 06:40 2,126
libcurl-x64.dll 2020-12-09 06:40 1,288,312
create def (skip)
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\dumpbin.exe" /exports libcurl-x64.dll > libcurl-x64_tmp.def -> edit -> libcurl-x64.def
create lib
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\lib.exe" /def:libcurl-x64.def /machine:x64 /out:libcurl-x64.lib
ready to use
libcurl-x64.lib 2021-01-04 12:48 19,400
libcurl-x64.exp 2021-01-04 12:48 11,280
------------------------------------------------------------------------
dependency
libssl-1_1-x64.dll 2020-12-08 13:21 549,496
libcrypto-1_1-x64.dll 2020-12-08 13:21 2,815,096
libssh2-x64.dll 2019-06-20 06:19 318,072
libzstd.dll 2020-12-19 00:39 901,752
libbrotlicommon.dll 2020-08-27 14:12 144,504
libbrotlidec.dll 2020-08-27 14:12 59,512
libbrotlienc.dll 2020-08-27 14:12 771,192
------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define COOKIEJAR_FILE "test_libcurl_cookiejar.txt"
#define CRT_FILE "curl-ca-bundle.crt"
#define OUT_FILE "test_libcurl_out.txt"
#define TEST_URL "https://raw.githubusercontent.com/curl/curl/master/docs/examples/getinmemory.c"
typedef struct _Mngr {
size_t len;
char dat[2048];
FILE *fp;
} Mngr;
size_t cb_write(char *buf, size_t sz, size_t n, void *p)
{
Mngr *m = (Mngr *)p;
size_t wb = sz * n;
if(wb >= sizeof(m->dat) - 1) return 0;
memcpy(m->dat, buf, wb);
m->dat[wb] = '\0';
fprintf(stdout, "\n----\nRead %zu bytes.\n[%s]\n----\n", wb, m->dat);
fwrite(m->dat, wb, 1, m->fp); // includes HTTP headers and CRLF
return wb;
}
int main(int ac, char **av)
{
fprintf(stdout, "sizeof(size_t): %zu\n", sizeof(size_t));
curl_global_init(CURL_GLOBAL_ALL); // CURL_GLOBAL_WIN32 | CURL_GLOBAL_SSL
CURL *hc = curl_easy_init();
Mngr m = {0, {'\0'}};
m.fp = fopen(OUT_FILE, "wb");
m.dat[sizeof(m.dat) - 1] = '\0';
curl_easy_setopt(hc, CURLOPT_WRITEDATA, &m);
curl_easy_setopt(hc, CURLOPT_WRITEFUNCTION, cb_write);
// curl_easy_setopt(hc, CURLOPT_DEBUGFUNCTION, dbg);
curl_easy_setopt(hc, CURLOPT_VERBOSE, 1);
curl_easy_setopt(hc, CURLOPT_HEADER, 1);
curl_easy_setopt(hc, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(hc, CURLOPT_TCP_KEEPALIVE, 1);
curl_easy_setopt(hc, CURLOPT_MAXREDIRS, 50);
curl_easy_setopt(hc, CURLOPT_USERAGENT, "curl/7.74.0");
curl_easy_setopt(hc, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
#if 0
curl_easy_setopt(hc, CURLOPT_SSL_VERIFYPEER, 0); // *** CAUTION ***
#else
curl_easy_setopt(hc, CURLOPT_CAINFO, CRT_FILE);
#endif
curl_easy_setopt(hc, CURLOPT_COOKIEJAR, COOKIEJAR_FILE);
curl_easy_setopt(hc, CURLOPT_URL, TEST_URL);
CURLcode r = curl_easy_perform(hc); // calls curl_global_init()
fclose(m.fp);
curl_easy_cleanup(hc);
curl_global_cleanup();
return 0; // return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment