1 /* Test using curl to fetch pages, look at headers, cookies. */
8 static const char *url = "https://fedoraproject.org/wiki/MinGW";
10 static int bytes_received = 0;
13 write_fn (void *ptr, size_t size, size_t nmemb, void *stream)
15 int bytes = size * nmemb;
16 bytes_received += bytes;
21 header_fn (void *ptr, size_t size, size_t nmemb, void *stream)
23 int bytes = size * nmemb;
26 /* Note that we are called once for each header, but the
27 * header data is not NUL-terminated. However we expect each
28 * header is terminated by \r\n. Hence:
30 for (i = 0; i < bytes; ++i)
31 putchar (((char *)ptr)[i]);
43 r = curl_global_init (CURL_GLOBAL_ALL);
45 fprintf (stderr, "curl_global_init failed with code %d\n", r);
49 curl = curl_easy_init ();
51 fprintf (stderr, "curl_easy_init failed\n");
55 r = curl_easy_setopt (curl, CURLOPT_URL, url);
57 fprintf (stderr, "curl_easy_setopt CURLOPT_URL failed with code %d\n", r);
61 r = curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_fn);
63 fprintf (stderr, "curl_easy_setopt CURLOPT_WRITEFUNCTION failed with code %d\n", r);
67 r = curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, header_fn);
69 fprintf (stderr, "curl_easy_setopt CURLOPT_HEADERFUNCTION failed with code %d\n", r);
73 /* This enables cookie handling in libcurl: */
74 r = curl_easy_setopt (curl, CURLOPT_COOKIEFILE, "");
76 fprintf (stderr, "curl_easy_setopt CURLOPT_COOKIEFILE failed with code %d\n", r);
81 printf ("fetching %s ...\n", url);
82 r = curl_easy_perform (curl);
84 fprintf (stderr, "curl_easy_perform failed with code %d\n", r);
87 printf ("... ok, bytes received in body was %d\n", bytes_received);
89 r = curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &code);
91 fprintf (stderr, "curl_easy_getinfo CURLINFO_RESPONSE_CODE failed with code %d\n", r);
94 printf ("HTTP response code: %d\n", (int) code);
96 curl_easy_cleanup (curl);
97 curl_global_cleanup ();