Include a bugfix for GCC 4.4
[fedora-mingw.git] / curl / test1.c
1 /* Test using curl to fetch pages, look at headers, cookies. */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <curl/curl.h>
7
8 static const char *cainfo = "/etc/pki/tls/certs/ca-bundle.crt";
9 static const char *url = "https://fedoraproject.org/wiki/MinGW";
10
11 static int bytes_received = 0;
12
13 static size_t
14 write_fn (void *ptr, size_t size, size_t nmemb, void *stream)
15 {
16   int bytes = size * nmemb;
17   bytes_received += bytes;
18   return bytes;
19 }
20
21 static size_t
22 header_fn (void *ptr, size_t size, size_t nmemb, void *stream)
23 {
24   int bytes = size * nmemb;
25   int i;
26
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:
30    */
31   for (i = 0; i < bytes; ++i)
32     putchar (((char *)ptr)[i]);
33
34   return bytes;
35 }
36
37 /* Handle curl errors. */
38 #define CHECK_ERROR(fn, args)                                           \
39   do {                                                                  \
40     CURLcode r = fn args;                                               \
41     if (r != CURLE_OK) {                                                \
42       fprintf (stderr, "%s: %s\n", #fn, curl_easy_strerror (r));        \
43       exit (1);                                                         \
44     }                                                                   \
45   } while (0)
46
47 int
48 main ()
49 {
50   CURL *curl;
51   long code;
52   char error[CURL_ERROR_SIZE];
53
54   CHECK_ERROR (curl_global_init, (CURL_GLOBAL_ALL));
55
56   curl = curl_easy_init ();
57   if (curl == NULL) {
58     fprintf (stderr, "curl_easy_init failed\n");
59     exit (1);
60   }
61
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, ""));
70
71   /* Fetch the page. */
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);
75
76   CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
77   printf ("HTTP response code: %d\n", (int) code);
78
79   curl_easy_cleanup (curl);
80   curl_global_cleanup ();
81   exit (0);
82 }