1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 /* This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
23 * mailing list on 10 Jul 2007, converted to a test case.
27 * argv3 = proxyuser:password
28 * argv4 = host name to use for the custom Host: header
41 #define TEST_HANG_TIMEOUT 60 * 1000
43 #define PROXY libtest_arg2
44 #define PROXYUSERPWD libtest_arg3
45 #define HOST test_argv[4]
49 CURL *eh[NUM_HANDLES];
51 static int init(int num, CURLM *cm, const char* url, const char* userpwd,
52 struct curl_slist *headers)
56 res_easy_init(eh[num]);
60 res_easy_setopt(eh[num], CURLOPT_URL, url);
64 res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
68 res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
72 res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
76 res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
80 res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
84 res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
88 res_multi_add_handle(cm, eh[num]);
92 return 0; /* success */
96 curl_easy_cleanup(eh[num]);
99 return res; /* failure */
102 static int loop(int num, CURLM *cm, const char* url, const char* userpwd,
103 struct curl_slist *headers)
112 res = init(num, cm, url, userpwd, headers);
120 res_multi_perform(cm, &U);
133 res_multi_fdset(cm, &R, &W, &E, &M);
137 /* At this point, M is guaranteed to be greater or equal than -1. */
139 res_multi_timeout(cm, &L);
143 /* At this point, L is guaranteed to be greater or equal than -1. */
146 int itimeout = (L > (long)INT_MAX) ? INT_MAX : (int)L;
147 T.tv_sec = itimeout/1000;
148 T.tv_usec = (itimeout%1000)*1000;
155 res_select_test(M+1, &R, &W, &E, &T);
160 while((msg = curl_multi_info_read(cm, &Q)) != NULL) {
161 if(msg->msg == CURLMSG_DONE) {
163 CURL *e = msg->easy_handle;
164 fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
165 curl_easy_strerror(msg->data.result));
166 curl_multi_remove_handle(cm, e);
167 curl_easy_cleanup(e);
168 for(i=0; i < NUM_HANDLES; i++) {
176 fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
184 return 0; /* success */
190 struct curl_slist *headers = NULL;
191 char buffer[246]; /* naively fixed-size */
195 for(i=0; i < NUM_HANDLES; i++)
203 sprintf(buffer, "Host: %s", HOST);
205 /* now add a custom Host: header */
206 headers = curl_slist_append(headers, buffer);
208 fprintf(stderr, "curl_slist_append() failed\n");
209 return TEST_ERR_MAJOR_BAD;
212 res_global_init(CURL_GLOBAL_ALL);
214 curl_slist_free_all(headers);
220 curl_global_cleanup();
221 curl_slist_free_all(headers);
225 res = loop(0, cm, URL, PROXYUSERPWD, headers);
229 fprintf(stderr, "lib540: now we do the request again\n");
231 res = loop(1, cm, URL, PROXYUSERPWD, headers);
235 /* proper cleanup sequence - type PB */
237 for(i=0; i < NUM_HANDLES; i++) {
238 curl_multi_remove_handle(cm, eh[i]);
239 curl_easy_cleanup(eh[i]);
242 curl_multi_cleanup(cm);
243 curl_global_cleanup();
245 curl_slist_free_all(headers);