1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2011, 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#include "warnless.h" 34#include "memdebug.h" 35 36#define PROXY libtest_arg2 37#define PROXYUSERPWD libtest_arg3 38#define HOST test_argv[4] 39 40static int init(CURLM *cm, const char* url, const char* userpwd, 41 struct curl_slist *headers) 42{ 43 CURL *eh; 44 int res; 45 46 if ((eh = curl_easy_init()) == NULL) { 47 fprintf(stderr, "curl_easy_init() failed\n"); 48 return 1; /* failure */ 49 } 50 51 res = curl_easy_setopt(eh, CURLOPT_URL, url); 52 if(res) return 1; 53 res = curl_easy_setopt(eh, CURLOPT_PROXY, PROXY); 54 if(res) return 1; 55 res = curl_easy_setopt(eh, CURLOPT_PROXYUSERPWD, userpwd); 56 if(res) return 1; 57 res = curl_easy_setopt(eh, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY); 58 if(res) return 1; 59 res = curl_easy_setopt(eh, CURLOPT_VERBOSE, 1L); 60 if(res) return 1; 61 res = curl_easy_setopt(eh, CURLOPT_HEADER, 1L); 62 if(res) return 1; 63 res = curl_easy_setopt(eh, CURLOPT_HTTPHEADER, headers); /* custom Host: */ 64 if(res) return 1; 65 66 if ((res = (int)curl_multi_add_handle(cm, eh)) != CURLM_OK) { 67 fprintf(stderr, "curl_multi_add_handle() failed, " 68 "with code %d\n", res); 69 return 1; /* failure */ 70 } 71 72 return 0; /* success */ 73} 74 75static int loop(CURLM *cm, const char* url, const char* userpwd, 76 struct curl_slist *headers) 77{ 78 CURLMsg *msg; 79 CURLMcode code; 80 long L; 81 int M, Q, U = -1; 82 fd_set R, W, E; 83 struct timeval T; 84 85 if(init(cm, url, userpwd, headers)) 86 return 1; /* failure */ 87 88 while (U) { 89 90 do { 91 code = curl_multi_perform(cm, &U); 92 } while (code == CURLM_CALL_MULTI_PERFORM); 93 94 if (U) { 95 FD_ZERO(&R); 96 FD_ZERO(&W); 97 FD_ZERO(&E); 98 99 if (curl_multi_fdset(cm, &R, &W, &E, &M)) { 100 fprintf(stderr, "E: curl_multi_fdset\n"); 101 return 1; /* failure */ 102 } 103 104 /* In a real-world program you OF COURSE check the return that maxfd is 105 bigger than -1 so that the call to select() below makes sense! */ 106 107 if (curl_multi_timeout(cm, &L)) { 108 fprintf(stderr, "E: curl_multi_timeout\n"); 109 return 1; /* failure */ 110 } 111 112 if(L != -1) { 113 T.tv_sec = L/1000; 114 T.tv_usec = (L%1000)*1000; 115 } 116 else { 117 T.tv_sec = 5; 118 T.tv_usec = 0; 119 } 120 121 if (0 > select(M+1, &R, &W, &E, &T)) { 122 fprintf(stderr, "E: select\n"); 123 return 1; /* failure */ 124 } 125 } 126 127 while ((msg = curl_multi_info_read(cm, &Q))) { 128 if (msg->msg == CURLMSG_DONE) { 129 CURL *e = msg->easy_handle; 130 fprintf(stderr, "R: %d - %s\n", (int)msg->data.result, 131 curl_easy_strerror(msg->data.result)); 132 curl_multi_remove_handle(cm, e); 133 curl_easy_cleanup(e); 134 } 135 else { 136 fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg); 137 } 138 } 139 } 140 141 return 0; /* success */ 142} 143 144int test(char *URL) 145{ 146 CURLM *cm = NULL; 147 struct curl_slist *headers = NULL; 148 char buffer[246]; /* naively fixed-size */ 149 int res; 150 151 if(test_argc < 4) 152 return 99; 153 154 sprintf(buffer, "Host: %s", HOST); 155 156 /* now add a custom Host: header */ 157 headers = curl_slist_append(headers, buffer); 158 if(!headers) { 159 fprintf(stderr, "curl_slist_append() failed\n"); 160 return TEST_ERR_MAJOR_BAD; 161 } 162 163 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 164 fprintf(stderr, "curl_global_init() failed\n"); 165 curl_slist_free_all(headers); 166 return TEST_ERR_MAJOR_BAD; 167 } 168 169 if ((cm = curl_multi_init()) == NULL) { 170 fprintf(stderr, "curl_multi_init() failed\n"); 171 curl_slist_free_all(headers); 172 curl_global_cleanup(); 173 return TEST_ERR_MAJOR_BAD; 174 } 175 176 res = loop(cm, URL, PROXYUSERPWD, headers); 177 if(res) 178 goto test_cleanup; 179 180 fprintf(stderr, "lib540: now we do the request again\n"); 181 res = loop(cm, URL, PROXYUSERPWD, headers); 182 183test_cleanup: 184 185 curl_multi_cleanup(cm); 186 187 curl_global_cleanup(); 188 189 curl_slist_free_all(headers); 190 191 return res; 192} 193