2011-03-10 05:48:02 -05:00
|
|
|
/***************************************************************************
|
2007-07-14 18:39:22 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-04-01 04:14:06 -04:00
|
|
|
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2007-07-14 18:39:22 -04:00
|
|
|
*
|
2011-03-10 05:48:02 -05:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-03-10 05:48:02 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
/* This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
|
2007-07-14 18:39:22 -04:00
|
|
|
* mailing list on 10 Jul 2007, converted to a test case.
|
|
|
|
*
|
|
|
|
* argv1 = URL
|
|
|
|
* argv2 = proxy
|
|
|
|
* argv3 = proxyuser:password
|
2007-07-15 17:00:26 -04:00
|
|
|
* argv4 = host name to use for the custom Host: header
|
2007-07-14 18:39:22 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
|
2012-12-05 11:28:32 -05:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
#include "testutil.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "warnless.h"
|
|
|
|
#include "memdebug.h"
|
2008-09-20 00:26:55 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
#define TEST_HANG_TIMEOUT 60 * 1000
|
|
|
|
|
2007-10-02 12:05:28 -04:00
|
|
|
#define PROXY libtest_arg2
|
|
|
|
#define PROXYUSERPWD libtest_arg3
|
2007-07-15 17:00:26 -04:00
|
|
|
#define HOST test_argv[4]
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
#define NUM_HANDLES 2
|
|
|
|
|
2017-05-01 06:55:09 -04:00
|
|
|
static CURL *eh[NUM_HANDLES];
|
2011-10-10 06:27:17 -04:00
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
static int init(int num, CURLM *cm, const char *url, const char *userpwd,
|
2007-07-15 17:00:26 -04:00
|
|
|
struct curl_slist *headers)
|
2007-07-14 18:39:22 -04:00
|
|
|
{
|
2011-10-21 10:26:18 -04:00
|
|
|
int res = 0;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_init(eh[num]);
|
|
|
|
if(res)
|
2011-10-10 06:27:17 -04:00
|
|
|
goto init_failed;
|
2010-02-05 13:07:19 -05:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_setopt(eh[num], CURLOPT_URL, url);
|
2011-10-10 06:27:17 -04:00
|
|
|
if(res)
|
|
|
|
goto init_failed;
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
|
2011-10-10 06:27:17 -04:00
|
|
|
if(res)
|
|
|
|
goto init_failed;
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
|
2011-10-10 06:27:17 -04:00
|
|
|
if(res)
|
|
|
|
goto init_failed;
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
|
2011-10-10 06:27:17 -04:00
|
|
|
if(res)
|
|
|
|
goto init_failed;
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
|
2011-10-10 06:27:17 -04:00
|
|
|
if(res)
|
|
|
|
goto init_failed;
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
|
2011-10-10 06:27:17 -04:00
|
|
|
if(res)
|
|
|
|
goto init_failed;
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
|
2011-10-10 06:27:17 -04:00
|
|
|
if(res)
|
|
|
|
goto init_failed;
|
2010-02-05 13:07:19 -05:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_multi_add_handle(cm, eh[num]);
|
|
|
|
if(res)
|
2011-10-10 06:27:17 -04:00
|
|
|
goto init_failed;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2010-02-05 13:07:19 -05:00
|
|
|
return 0; /* success */
|
2011-10-10 06:27:17 -04:00
|
|
|
|
|
|
|
init_failed:
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
curl_easy_cleanup(eh[num]);
|
|
|
|
eh[num] = NULL;
|
|
|
|
|
|
|
|
return res; /* failure */
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
static int loop(int num, CURLM *cm, const char *url, const char *userpwd,
|
2007-07-15 17:00:26 -04:00
|
|
|
struct curl_slist *headers)
|
2007-07-14 18:39:22 -04:00
|
|
|
{
|
|
|
|
CURLMsg *msg;
|
|
|
|
long L;
|
2011-10-21 10:26:18 -04:00
|
|
|
int Q, U = -1;
|
2007-07-14 18:39:22 -04:00
|
|
|
fd_set R, W, E;
|
|
|
|
struct timeval T;
|
2011-10-21 10:26:18 -04:00
|
|
|
int res = 0;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res = init(num, cm, url, userpwd, headers);
|
|
|
|
if(res)
|
|
|
|
return res;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2016-04-03 05:57:34 -04:00
|
|
|
while(U) {
|
2009-10-10 08:29:32 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
int M = -99;
|
|
|
|
|
|
|
|
res_multi_perform(cm, &U);
|
|
|
|
if(res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res_test_timedout();
|
|
|
|
if(res)
|
|
|
|
return res;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2016-04-03 05:57:34 -04:00
|
|
|
if(U) {
|
2007-07-14 18:39:22 -04:00
|
|
|
FD_ZERO(&R);
|
|
|
|
FD_ZERO(&W);
|
|
|
|
FD_ZERO(&E);
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_multi_fdset(cm, &R, &W, &E, &M);
|
|
|
|
if(res)
|
|
|
|
return res;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
/* At this point, M is guaranteed to be greater or equal than -1. */
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_multi_timeout(cm, &L);
|
|
|
|
if(res)
|
|
|
|
return res;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-26 12:46:32 -04:00
|
|
|
/* At this point, L is guaranteed to be greater or equal than -1. */
|
|
|
|
|
2007-07-14 18:39:22 -04:00
|
|
|
if(L != -1) {
|
2012-12-05 11:28:32 -05:00
|
|
|
int itimeout = (L > (long)INT_MAX) ? INT_MAX : (int)L;
|
|
|
|
T.tv_sec = itimeout/1000;
|
|
|
|
T.tv_usec = (itimeout%1000)*1000;
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
T.tv_sec = 5;
|
|
|
|
T.tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_select_test(M+1, &R, &W, &E, &T);
|
|
|
|
if(res)
|
|
|
|
return res;
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
while((msg = curl_multi_info_read(cm, &Q)) != NULL) {
|
|
|
|
if(msg->msg == CURLMSG_DONE) {
|
|
|
|
int i;
|
2007-07-14 18:39:22 -04:00
|
|
|
CURL *e = msg->easy_handle;
|
2007-09-12 06:46:14 -04:00
|
|
|
fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
|
|
|
|
curl_easy_strerror(msg->data.result));
|
2007-07-14 18:39:22 -04:00
|
|
|
curl_multi_remove_handle(cm, e);
|
|
|
|
curl_easy_cleanup(e);
|
2011-10-21 10:26:18 -04:00
|
|
|
for(i=0; i < NUM_HANDLES; i++) {
|
|
|
|
if(eh[i] == e) {
|
|
|
|
eh[i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|
2011-10-21 10:26:18 -04:00
|
|
|
else
|
2007-08-06 20:10:27 -04:00
|
|
|
fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|
2011-10-21 10:26:18 -04:00
|
|
|
|
|
|
|
res_test_timedout();
|
|
|
|
if(res)
|
|
|
|
return res;
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|
|
|
|
|
2010-02-05 13:07:19 -05:00
|
|
|
return 0; /* success */
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int test(char *URL)
|
|
|
|
{
|
2010-02-05 13:07:19 -05:00
|
|
|
CURLM *cm = NULL;
|
2007-07-15 17:00:26 -04:00
|
|
|
struct curl_slist *headers = NULL;
|
|
|
|
char buffer[246]; /* naively fixed-size */
|
2011-10-21 10:26:18 -04:00
|
|
|
int res = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i < NUM_HANDLES; i++)
|
|
|
|
eh[i] = NULL;
|
|
|
|
|
|
|
|
start_test_timing();
|
2007-07-15 17:00:26 -04:00
|
|
|
|
|
|
|
if(test_argc < 4)
|
|
|
|
return 99;
|
|
|
|
|
2016-04-01 04:14:06 -04:00
|
|
|
snprintf(buffer, sizeof(buffer), "Host: %s", HOST);
|
2007-07-15 17:00:26 -04:00
|
|
|
|
|
|
|
/* now add a custom Host: header */
|
|
|
|
headers = curl_slist_append(headers, buffer);
|
2010-02-05 13:07:19 -05:00
|
|
|
if(!headers) {
|
|
|
|
fprintf(stderr, "curl_slist_append() failed\n");
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_global_init(CURL_GLOBAL_ALL);
|
|
|
|
if(res) {
|
2010-02-05 13:07:19 -05:00
|
|
|
curl_slist_free_all(headers);
|
2011-10-21 10:26:18 -04:00
|
|
|
return res;
|
2010-02-05 13:07:19 -05:00
|
|
|
}
|
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res_multi_init(cm);
|
|
|
|
if(res) {
|
2010-02-05 13:07:19 -05:00
|
|
|
curl_global_cleanup();
|
2011-10-21 10:26:18 -04:00
|
|
|
curl_slist_free_all(headers);
|
|
|
|
return res;
|
2010-02-05 13:07:19 -05:00
|
|
|
}
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res = loop(0, cm, URL, PROXYUSERPWD, headers);
|
2010-02-05 13:07:19 -05:00
|
|
|
if(res)
|
|
|
|
goto test_cleanup;
|
2007-07-14 18:39:22 -04:00
|
|
|
|
|
|
|
fprintf(stderr, "lib540: now we do the request again\n");
|
2010-02-05 13:07:19 -05:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
res = loop(1, cm, URL, PROXYUSERPWD, headers);
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
test_cleanup:
|
2011-10-10 06:27:17 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
/* proper cleanup sequence - type PB */
|
2011-10-10 06:27:17 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
for(i=0; i < NUM_HANDLES; i++) {
|
|
|
|
curl_multi_remove_handle(cm, eh[i]);
|
|
|
|
curl_easy_cleanup(eh[i]);
|
|
|
|
}
|
2007-07-14 18:39:22 -04:00
|
|
|
|
2011-10-21 10:26:18 -04:00
|
|
|
curl_multi_cleanup(cm);
|
2007-07-14 18:39:22 -04:00
|
|
|
curl_global_cleanup();
|
|
|
|
|
2007-07-15 17:00:26 -04:00
|
|
|
curl_slist_free_all(headers);
|
|
|
|
|
2010-02-05 13:07:19 -05:00
|
|
|
return res;
|
2007-07-14 18:39:22 -04:00
|
|
|
}
|