1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 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. 13 * 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. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 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. 24 * 25 * argv1 = URL 26 * argv2 = proxy 27 * argv3 = proxyuser:password 28 * argv4 = host name to use for the custom Host: header 29 */ 30 31#include "test.h" 32 33#ifdef HAVE_LIMITS_H 34#include <limits.h> 35#endif 36 37#include "testutil.h" 38#include "warnless.h" 39#include "memdebug.h" 40 41#define TEST_HANG_TIMEOUT 60 * 1000 42 43#define PROXY libtest_arg2 44#define PROXYUSERPWD libtest_arg3 45#define HOST test_argv[4] 46 47#define NUM_HANDLES 2 48 49CURL *eh[NUM_HANDLES]; 50 51static int init(int num, CURLM *cm, const char* url, const char* userpwd, 52 struct curl_slist *headers) 53{ 54 int res = 0; 55 56 res_easy_init(eh[num]); 57 if(res) 58 goto init_failed; 59 60 res_easy_setopt(eh[num], CURLOPT_URL, url); 61 if(res) 62 goto init_failed; 63 64 res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY); 65 if(res) 66 goto init_failed; 67 68 res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd); 69 if(res) 70 goto init_failed; 71 72 res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY); 73 if(res) 74 goto init_failed; 75 76 res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L); 77 if(res) 78 goto init_failed; 79 80 res_easy_setopt(eh[num], CURLOPT_HEADER, 1L); 81 if(res) 82 goto init_failed; 83 84 res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */ 85 if(res) 86 goto init_failed; 87 88 res_multi_add_handle(cm, eh[num]); 89 if(res) 90 goto init_failed; 91 92 return 0; /* success */ 93 94init_failed: 95 96 curl_easy_cleanup(eh[num]); 97 eh[num] = NULL; 98 99 return res; /* failure */ 100} 101 102static int loop(int num, CURLM *cm, const char* url, const char* userpwd, 103 struct curl_slist *headers) 104{ 105 CURLMsg *msg; 106 long L; 107 int Q, U = -1; 108 fd_set R, W, E; 109 struct timeval T; 110 int res = 0; 111 112 res = init(num, cm, url, userpwd, headers); 113 if(res) 114 return res; 115 116 while (U) { 117 118 int M = -99; 119 120 res_multi_perform(cm, &U); 121 if(res) 122 return res; 123 124 res_test_timedout(); 125 if(res) 126 return res; 127 128 if (U) { 129 FD_ZERO(&R); 130 FD_ZERO(&W); 131 FD_ZERO(&E); 132 133 res_multi_fdset(cm, &R, &W, &E, &M); 134 if(res) 135 return res; 136 137 /* At this point, M is guaranteed to be greater or equal than -1. */ 138 139 res_multi_timeout(cm, &L); 140 if(res) 141 return res; 142 143 /* At this point, L is guaranteed to be greater or equal than -1. */ 144 145 if(L != -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; 149 } 150 else { 151 T.tv_sec = 5; 152 T.tv_usec = 0; 153 } 154 155 res_select_test(M+1, &R, &W, &E, &T); 156 if(res) 157 return res; 158 } 159 160 while((msg = curl_multi_info_read(cm, &Q)) != NULL) { 161 if(msg->msg == CURLMSG_DONE) { 162 int i; 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++) { 169 if(eh[i] == e) { 170 eh[i] = NULL; 171 break; 172 } 173 } 174 } 175 else 176 fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg); 177 } 178 179 res_test_timedout(); 180 if(res) 181 return res; 182 } 183 184 return 0; /* success */ 185} 186 187int test(char *URL) 188{ 189 CURLM *cm = NULL; 190 struct curl_slist *headers = NULL; 191 char buffer[246]; /* naively fixed-size */ 192 int res = 0; 193 int i; 194 195 for(i=0; i < NUM_HANDLES; i++) 196 eh[i] = NULL; 197 198 start_test_timing(); 199 200 if(test_argc < 4) 201 return 99; 202 203 sprintf(buffer, "Host: %s", HOST); 204 205 /* now add a custom Host: header */ 206 headers = curl_slist_append(headers, buffer); 207 if(!headers) { 208 fprintf(stderr, "curl_slist_append() failed\n"); 209 return TEST_ERR_MAJOR_BAD; 210 } 211 212 res_global_init(CURL_GLOBAL_ALL); 213 if(res) { 214 curl_slist_free_all(headers); 215 return res; 216 } 217 218 res_multi_init(cm); 219 if(res) { 220 curl_global_cleanup(); 221 curl_slist_free_all(headers); 222 return res; 223 } 224 225 res = loop(0, cm, URL, PROXYUSERPWD, headers); 226 if(res) 227 goto test_cleanup; 228 229 fprintf(stderr, "lib540: now we do the request again\n"); 230 231 res = loop(1, cm, URL, PROXYUSERPWD, headers); 232 233test_cleanup: 234 235 /* proper cleanup sequence - type PB */ 236 237 for(i=0; i < NUM_HANDLES; i++) { 238 curl_multi_remove_handle(cm, eh[i]); 239 curl_easy_cleanup(eh[i]); 240 } 241 242 curl_multi_cleanup(cm); 243 curl_global_cleanup(); 244 245 curl_slist_free_all(headers); 246 247 return res; 248} 249