00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "test.h"
00032
00033 #ifdef HAVE_LIMITS_H
00034 #include <limits.h>
00035 #endif
00036
00037 #include "testutil.h"
00038 #include "warnless.h"
00039 #include "memdebug.h"
00040
00041 #define TEST_HANG_TIMEOUT 60 * 1000
00042
00043 #define PROXY libtest_arg2
00044 #define PROXYUSERPWD libtest_arg3
00045 #define HOST test_argv[4]
00046
00047 #define NUM_HANDLES 2
00048
00049 CURL *eh[NUM_HANDLES];
00050
00051 static int init(int num, CURLM *cm, const char *url, const char *userpwd,
00052 struct curl_slist *headers)
00053 {
00054 int res = 0;
00055
00056 res_easy_init(eh[num]);
00057 if(res)
00058 goto init_failed;
00059
00060 res_easy_setopt(eh[num], CURLOPT_URL, url);
00061 if(res)
00062 goto init_failed;
00063
00064 res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
00065 if(res)
00066 goto init_failed;
00067
00068 res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
00069 if(res)
00070 goto init_failed;
00071
00072 res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
00073 if(res)
00074 goto init_failed;
00075
00076 res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
00077 if(res)
00078 goto init_failed;
00079
00080 res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
00081 if(res)
00082 goto init_failed;
00083
00084 res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers);
00085 if(res)
00086 goto init_failed;
00087
00088 res_multi_add_handle(cm, eh[num]);
00089 if(res)
00090 goto init_failed;
00091
00092 return 0;
00093
00094 init_failed:
00095
00096 curl_easy_cleanup(eh[num]);
00097 eh[num] = NULL;
00098
00099 return res;
00100 }
00101
00102 static int loop(int num, CURLM *cm, const char *url, const char *userpwd,
00103 struct curl_slist *headers)
00104 {
00105 CURLMsg *msg;
00106 long L;
00107 int Q, U = -1;
00108 fd_set R, W, E;
00109 struct timeval T;
00110 int res = 0;
00111
00112 res = init(num, cm, url, userpwd, headers);
00113 if(res)
00114 return res;
00115
00116 while(U) {
00117
00118 int M = -99;
00119
00120 res_multi_perform(cm, &U);
00121 if(res)
00122 return res;
00123
00124 res_test_timedout();
00125 if(res)
00126 return res;
00127
00128 if(U) {
00129 FD_ZERO(&R);
00130 FD_ZERO(&W);
00131 FD_ZERO(&E);
00132
00133 res_multi_fdset(cm, &R, &W, &E, &M);
00134 if(res)
00135 return res;
00136
00137
00138
00139 res_multi_timeout(cm, &L);
00140 if(res)
00141 return res;
00142
00143
00144
00145 if(L != -1) {
00146 int itimeout = (L > (long)INT_MAX) ? INT_MAX : (int)L;
00147 T.tv_sec = itimeout/1000;
00148 T.tv_usec = (itimeout%1000)*1000;
00149 }
00150 else {
00151 T.tv_sec = 5;
00152 T.tv_usec = 0;
00153 }
00154
00155 res_select_test(M+1, &R, &W, &E, &T);
00156 if(res)
00157 return res;
00158 }
00159
00160 while((msg = curl_multi_info_read(cm, &Q)) != NULL) {
00161 if(msg->msg == CURLMSG_DONE) {
00162 int i;
00163 CURL *e = msg->easy_handle;
00164 fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
00165 curl_easy_strerror(msg->data.result));
00166 curl_multi_remove_handle(cm, e);
00167 curl_easy_cleanup(e);
00168 for(i=0; i < NUM_HANDLES; i++) {
00169 if(eh[i] == e) {
00170 eh[i] = NULL;
00171 break;
00172 }
00173 }
00174 }
00175 else
00176 fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
00177 }
00178
00179 res_test_timedout();
00180 if(res)
00181 return res;
00182 }
00183
00184 return 0;
00185 }
00186
00187 int test(char *URL)
00188 {
00189 CURLM *cm = NULL;
00190 struct curl_slist *headers = NULL;
00191 char buffer[246];
00192 int res = 0;
00193 int i;
00194
00195 for(i=0; i < NUM_HANDLES; i++)
00196 eh[i] = NULL;
00197
00198 start_test_timing();
00199
00200 if(test_argc < 4)
00201 return 99;
00202
00203 snprintf(buffer, sizeof(buffer), "Host: %s", HOST);
00204
00205
00206 headers = curl_slist_append(headers, buffer);
00207 if(!headers) {
00208 fprintf(stderr, "curl_slist_append() failed\n");
00209 return TEST_ERR_MAJOR_BAD;
00210 }
00211
00212 res_global_init(CURL_GLOBAL_ALL);
00213 if(res) {
00214 curl_slist_free_all(headers);
00215 return res;
00216 }
00217
00218 res_multi_init(cm);
00219 if(res) {
00220 curl_global_cleanup();
00221 curl_slist_free_all(headers);
00222 return res;
00223 }
00224
00225 res = loop(0, cm, URL, PROXYUSERPWD, headers);
00226 if(res)
00227 goto test_cleanup;
00228
00229 fprintf(stderr, "lib540: now we do the request again\n");
00230
00231 res = loop(1, cm, URL, PROXYUSERPWD, headers);
00232
00233 test_cleanup:
00234
00235
00236
00237 for(i=0; i < NUM_HANDLES; i++) {
00238 curl_multi_remove_handle(cm, eh[i]);
00239 curl_easy_cleanup(eh[i]);
00240 }
00241
00242 curl_multi_cleanup(cm);
00243 curl_global_cleanup();
00244
00245 curl_slist_free_all(headers);
00246
00247 return res;
00248 }