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