1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/* file : tests/basic/driver.c
* license : cURL License; see accompanying COPYING file
*/
#include <stdio.h>
#include <assert.h>
#include <string.h> /* strcmp() */
#include <curl/curl.h>
/* Usage: argv[0] <URL>
*
* Request the specified URL following location for 3xx responses and print
* the 200 response body to stdout.
*
* --verbose
* Print additional information (API calls, HTTP headers, etc) to stderr.
*/
int
main (int argc, char* argv[])
{
int v = 0;
int i = 1;
for (; i < argc; ++i)
{
const char* o = argv[i];
if (strcmp (o, "--verbose") == 0)
v = 1;
else
break;
}
assert (i + 1 == argc);
const char* url = argv[i];
int r = 1;
if (v != 0)
fprintf (stderr, "calling curl_global_init()\n");
curl_global_init (CURL_GLOBAL_DEFAULT);
if (v != 0)
fprintf (stderr, "calling curl_easy_init()\n");
CURL* curl = curl_easy_init ();
if (curl != NULL)
{
if (v != 0)
fprintf (stderr, "calling curl_easy_setopt()\n");
curl_easy_setopt (curl, CURLOPT_URL, url);
curl_easy_setopt (curl, CURLOPT_TIMEOUT, 600L); // 10 mins.
curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
char agent[] = "libcurl-test/" LIBCURL_VERSION;
curl_easy_setopt (curl, CURLOPT_USERAGENT, agent);
if (v != 0)
curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
if (v != 0)
fprintf (stderr, "calling curl_easy_perform()\n");
CURLcode cr = curl_easy_perform (curl);
if (cr == CURLE_OK)
{
if (v != 0)
fprintf (stderr, "calling curl_easy_getinfo()\n");
long status;
cr = curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &status);
if (cr == CURLE_OK)
{
if (status == 200)
{
if (v != 0)
fprintf (stderr, "calling fflush()\n");
/* Flush the response body.
*/
if (fflush (stdout) != 0)
fprintf (stderr, "failed to flush stdout\n");
r = 0;
}
else
fprintf (stderr, "HTTP error: status code %ld\n", status);
}
else
fprintf (stderr,
"failed to get HTTP status code: %s\n",
curl_easy_strerror (cr));
}
else
fprintf (stderr,
"failed to request '%s': %s\n",
url,
curl_easy_strerror (cr));
curl_easy_cleanup (curl);
}
else
fprintf (stderr, "curl_easy_init() failed\n");
if (v != 0)
fprintf (stderr, "calling curl_global_cleanup()\n");
curl_global_cleanup ();
/* Make sure the verbose output and diagnostics is printed properly.
*/
if (fflush (stderr) != 0)
r = 1;
return r;
}
|