1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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
39 #define TEST_HANG_TIMEOUT 60 * 1000
41 #define PROXY libtest_arg2
42 #define PROXYUSERPWD libtest_arg3
43 #define HOST test_argv[4]
47 static CURL *eh[NUM_HANDLES];
49 static int init(int num, CURLM *cm, const char *url, const char *userpwd,
50 struct curl_slist *headers)
54 res_easy_init(eh[num]);
58 res_easy_setopt(eh[num], CURLOPT_URL, url);
62 res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
66 res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
70 res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
74 res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
78 res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
82 res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
86 res_multi_add_handle(cm, eh[num]);
90 return 0; /* success */
94 curl_easy_cleanup(eh[num]);
97 return res; /* failure */
100 static int loop(int num, CURLM *cm, const char *url, const char *userpwd,
101 struct curl_slist *headers)
110 res = init(num, cm, url, userpwd, headers);
118 res_multi_perform(cm, &U);
131 res_multi_fdset(cm, &R, &W, &E, &M);
135 /* At this point, M is guaranteed to be greater or equal than -1. */
137 res_multi_timeout(cm, &L);
141 /* At this point, L is guaranteed to be greater or equal than -1. */
144 int itimeout = (L > (long)INT_MAX) ? INT_MAX : (int)L;
145 T.tv_sec = itimeout/1000;
146 T.tv_usec = (itimeout%1000)*1000;
153 res_select_test(M + 1, &R, &W, &E, &T);
158 while((msg = curl_multi_info_read(cm, &Q)) != NULL) {
159 if(msg->msg == CURLMSG_DONE) {
161 CURL *e = msg->easy_handle;
162 fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
163 curl_easy_strerror(msg->data.result));
164 curl_multi_remove_handle(cm, e);
165 curl_easy_cleanup(e);
166 for(i = 0; i < NUM_HANDLES; i++) {
174 fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
182 return 0; /* success */
188 struct curl_slist *headers = NULL;
189 char buffer[246]; /* naively fixed-size */
193 for(i = 0; i < NUM_HANDLES; i++)
201 snprintf(buffer, sizeof(buffer), "Host: %s", HOST);
203 /* now add a custom Host: header */
204 headers = curl_slist_append(headers, buffer);
206 fprintf(stderr, "curl_slist_append() failed\n");
207 return TEST_ERR_MAJOR_BAD;
210 res_global_init(CURL_GLOBAL_ALL);
212 curl_slist_free_all(headers);
218 curl_global_cleanup();
219 curl_slist_free_all(headers);
223 res = loop(0, cm, URL, PROXYUSERPWD, headers);
227 fprintf(stderr, "lib540: now we do the request again\n");
229 res = loop(1, cm, URL, PROXYUSERPWD, headers);
233 /* proper cleanup sequence - type PB */
235 for(i = 0; i < NUM_HANDLES; i++) {
236 curl_multi_remove_handle(cm, eh[i]);
237 curl_easy_cleanup(eh[i]);
240 curl_multi_cleanup(cm);
241 curl_global_cleanup();
243 curl_slist_free_all(headers);