1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
10 * mailing list on 10 Jul 2007, converted to a test case.
14 * argv3 = proxyuser:password
15 * argv4 = host name to use for the custom Host: header
22 #define PROXY libtest_arg2
23 #define PROXYUSERPWD libtest_arg3
24 #define HOST test_argv[4]
26 static int init(CURLM *cm, const char* url, const char* userpwd,
27 struct curl_slist *headers)
32 if ((eh = curl_easy_init()) == NULL) {
33 fprintf(stderr, "curl_easy_init() failed\n");
34 return 1; /* failure */
37 res = curl_easy_setopt(eh, CURLOPT_URL, url);
39 res = curl_easy_setopt(eh, CURLOPT_PROXY, PROXY);
41 res = curl_easy_setopt(eh, CURLOPT_PROXYUSERPWD, userpwd);
43 res = curl_easy_setopt(eh, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
45 res = curl_easy_setopt(eh, CURLOPT_VERBOSE, 1L);
47 res = curl_easy_setopt(eh, CURLOPT_HEADER, 1L);
49 res = curl_easy_setopt(eh, CURLOPT_HTTPHEADER, headers); /* custom Host: */
52 if ((res = (int)curl_multi_add_handle(cm, eh)) != CURLM_OK) {
53 fprintf(stderr, "curl_multi_add_handle() failed, "
54 "with code %d\n", res);
55 return 1; /* failure */
58 return 0; /* success */
61 static int loop(CURLM *cm, const char* url, const char* userpwd,
62 struct curl_slist *headers)
71 if(init(cm, url, userpwd, headers))
72 return 1; /* failure */
77 code = curl_multi_perform(cm, &U);
78 } while (code == CURLM_CALL_MULTI_PERFORM);
85 if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
86 fprintf(stderr, "E: curl_multi_fdset\n");
87 return 1; /* failure */
90 /* In a real-world program you OF COURSE check the return that maxfd is
91 bigger than -1 so that the call to select() below makes sense! */
93 if (curl_multi_timeout(cm, &L)) {
94 fprintf(stderr, "E: curl_multi_timeout\n");
95 return 1; /* failure */
100 T.tv_usec = (L%1000)*1000;
107 if (0 > select(M+1, &R, &W, &E, &T)) {
108 fprintf(stderr, "E: select\n");
109 return 1; /* failure */
113 while ((msg = curl_multi_info_read(cm, &Q))) {
114 if (msg->msg == CURLMSG_DONE) {
115 CURL *e = msg->easy_handle;
116 fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
117 curl_easy_strerror(msg->data.result));
118 curl_multi_remove_handle(cm, e);
119 curl_easy_cleanup(e);
122 fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
127 return 0; /* success */
133 struct curl_slist *headers = NULL;
134 char buffer[246]; /* naively fixed-size */
140 sprintf(buffer, "Host: %s", HOST);
142 /* now add a custom Host: header */
143 headers = curl_slist_append(headers, buffer);
145 fprintf(stderr, "curl_slist_append() failed\n");
146 return TEST_ERR_MAJOR_BAD;
149 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
150 fprintf(stderr, "curl_global_init() failed\n");
151 curl_slist_free_all(headers);
152 return TEST_ERR_MAJOR_BAD;
155 if ((cm = curl_multi_init()) == NULL) {
156 fprintf(stderr, "curl_multi_init() failed\n");
157 curl_slist_free_all(headers);
158 curl_global_cleanup();
159 return TEST_ERR_MAJOR_BAD;
162 res = loop(cm, URL, PROXYUSERPWD, headers);
166 fprintf(stderr, "lib540: now we do the request again\n");
167 res = loop(cm, URL, PROXYUSERPWD, headers);
171 curl_multi_cleanup(cm);
173 curl_global_cleanup();
175 curl_slist_free_all(headers);