Debian Codegraph @

docs/samples/libcurl/curlget.c

raw
1 /*******************************************************************************
2 * libproxy - A library for proxy configuration
3 * Copyright (C) 2006 Nathaniel McCallum <[email protected]>
4 * Copyright (C) 2008 Dominique Leuenberger <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ******************************************************************************/
20
21 #include "proxy.h"
22 #include "curl/curl.h"
23 #include <string.h>
24 #include <stdlib.h>
25
26 int main(int argc, char * argv[]) {
27 pxProxyFactory *pf = NULL;
28 CURL *curl = NULL;
29 CURLcode result = 1;
30
31 /* Check if we have a parameter passed, otherwise bail out... I need one */
32 if (argc < 2)
33 {
34 printf("libcurl / libproxy example implementation\n");
35 printf("=========================================\n");
36 printf("The program has to be started with one parameter\n");
37 printf("Defining the URL that should be downloaded\n\n");
38 printf("Example: %s http://code.google.com/p/libproxy/\n", argv[0]);
39 return -1;
40 }
41
42 /* Initializing curl... has to happen exactly once in the program */
43 if (curl_global_init( CURL_GLOBAL_ALL )) return -4;
44
45 /* Create pxProxyFactory object */
46 if (!(pf = px_proxy_factory_new()))
47 {
48 printf("Unable to create pxProxyFactory object!\n");
49 curl_global_cleanup();
50 return -2;
51 }
52
53 /* Create curl handle */
54 if (!(curl = curl_easy_init()))
55 {
56 printf("Unable to get libcurl handle!\n");
57 px_proxy_factory_free(pf);
58 curl_global_cleanup();
59 return -3;
60 }
61
62 /* Get the array of proxies to try */
63 char **proxies = px_proxy_factory_get_proxies(pf, argv[1]);
64
65 /* Set the URL into the curl handle */
66 curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
67
68 /* Try to fetch our url using each proxy */
69 for (int i=0 ; proxies[i] ; i++)
70 {
71 if (result != 0)
72 {
73 /* Setup our proxy's config into curl */
74 curl_easy_setopt(curl, CURLOPT_PROXY, proxies[i]);
75
76 /* HTTP Proxy */
77 if (!strncmp("http", proxies[i], 4))
78 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
79
80 /* SOCKS Proxy, is this correct??? */
81 /* What about SOCKS 4A, 5 and 5_HOSTNAME??? */
82 else if (!strncmp("socks", proxies[i], 4))
83 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
84
85 /* Attempt to fetch the page */
86 result = curl_easy_perform(curl);
87 }
88
89 /* If we reached the end of the proxies array and still did not
90 succeed to conntect, let's inform the user that we failed. */
91 if (proxies[i+1] == NULL && result != 0)
92 printf ("The requested URL (%s) could not be retrieved using the current setup\n", argv[1]);
93
94 }
95
96 /* Free the proxy array */
97 px_proxy_factory_free_proxies(proxies);
98
99 /* Free the curl and libproxy handles */
100 px_proxy_factory_free(pf);
101 curl_easy_cleanup(curl);
102
103 /* Cleanup the libcurl library */
104 curl_global_cleanup();
105 return 0;
106
107 }
108