1 /* Test using curl to fetch pages, look at headers, cookies. */
8 static const char *cainfo = "/etc/pki/tls/certs/ca-bundle.crt";
9 static const char *url = "https://fedoraproject.org/wiki/MinGW";
11 static int bytes_received = 0;
14 write_fn (void *ptr, size_t size, size_t nmemb, void *stream)
16 int bytes = size * nmemb;
17 bytes_received += bytes;
22 header_fn (void *ptr, size_t size, size_t nmemb, void *stream)
24 int bytes = size * nmemb;
27 /* Note that we are called once for each header, but the
28 * header data is not NUL-terminated. However we expect each
29 * header is terminated by \r\n. Hence:
31 for (i = 0; i < bytes; ++i)
32 putchar (((char *)ptr)[i]);
37 /* Handle curl errors. */
38 #define CHECK_ERROR(fn, args) \
40 CURLcode r = fn args; \
41 if (r != CURLE_OK) { \
42 fprintf (stderr, "%s: %s\n", #fn, curl_easy_strerror (r)); \
52 char error[CURL_ERROR_SIZE];
54 CHECK_ERROR (curl_global_init, (CURL_GLOBAL_ALL));
56 curl = curl_easy_init ();
58 fprintf (stderr, "curl_easy_init failed\n");
62 CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, url));
63 CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_CAINFO, cainfo));
64 CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_WRITEFUNCTION, write_fn));
65 CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_HEADERFUNCTION, header_fn));
66 /* This enables error messages in curl_easy_perform: */
67 CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_ERRORBUFFER, error));
68 /* This enables cookie handling in libcurl: */
69 CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_COOKIEFILE, ""));
72 printf ("fetching %s ...\n", url);
73 CHECK_ERROR (curl_easy_perform, (curl));
74 printf ("... ok, bytes received in body was %d\n", bytes_received);
76 CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
77 printf ("HTTP response code: %d\n", (int) code);
79 curl_easy_cleanup (curl);
80 curl_global_cleanup ();