Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmcurl/curltest.c
3150 views
1
#include "curl/curl.h"
2
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
7
int test_curl(char const* url)
8
{
9
CURL* curl;
10
CURLcode r;
11
char proxy[1024];
12
int proxy_type = 0;
13
char const* env_HTTP_PROXY = getenv("HTTP_PROXY");
14
15
if (env_HTTP_PROXY) {
16
char const* env_HTTP_PROXY_PORT = getenv("HTTP_PROXY_PORT");
17
char const* env_HTTP_PROXY_TYPE = getenv("HTTP_PROXY_TYPE");
18
proxy_type = 1;
19
if (env_HTTP_PROXY_PORT) {
20
sprintf(proxy, "%s:%s", env_HTTP_PROXY, env_HTTP_PROXY_PORT);
21
} else {
22
sprintf(proxy, "%s", env_HTTP_PROXY);
23
}
24
if (env_HTTP_PROXY_TYPE) {
25
/* HTTP/SOCKS4/SOCKS5 */
26
if (strcmp(env_HTTP_PROXY_TYPE, "HTTP") == 0) {
27
proxy_type = 1;
28
} else if (strcmp(env_HTTP_PROXY_TYPE, "SOCKS4") == 0) {
29
proxy_type = 2;
30
} else if (strcmp(env_HTTP_PROXY_TYPE, "SOCKS5") == 0) {
31
proxy_type = 3;
32
}
33
}
34
}
35
36
curl = curl_easy_init();
37
if (!curl) {
38
fprintf(stderr, "curl_easy_init failed\n");
39
return 1;
40
}
41
42
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
43
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
44
45
if (proxy_type > 0) {
46
curl_easy_setopt(curl, CURLOPT_PROXY, proxy);
47
switch (proxy_type) {
48
case 2:
49
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
50
break;
51
case 3:
52
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
53
break;
54
default:
55
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
56
}
57
}
58
59
curl_easy_setopt(curl, CURLOPT_URL, url);
60
r = curl_easy_perform(curl);
61
curl_easy_cleanup(curl);
62
63
if (r != CURLE_OK) {
64
fprintf(stderr, "error: fetching '%s' failed: %s\n", url,
65
curl_easy_strerror(r));
66
return 1;
67
}
68
69
return 0;
70
}
71
72
int main(int argc, char const* argv[])
73
{
74
int r;
75
curl_global_init(CURL_GLOBAL_DEFAULT);
76
if (argc == 2) {
77
r = test_curl(argv[1]);
78
} else {
79
fprintf(stderr, "error: no URL given as first argument\n");
80
r = 1;
81
}
82
curl_global_cleanup();
83
return r;
84
}
85
86