xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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 https://curl.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 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | /* This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl |
| 25 | * mailing list on 10 Jul 2007, converted to a test case. |
| 26 | * |
| 27 | * argv1 = URL |
| 28 | * argv2 = proxy |
| 29 | * argv3 = proxyuser:password |
| 30 | * argv4 = host name to use for the custom Host: header |
| 31 | */ |
| 32 | |
| 33 | #include "test.h" |
| 34 | |
| 35 | #include <limits.h> |
| 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 | |
| 49 | static CURL *eh[NUM_HANDLES]; |
| 50 | |
| 51 | static 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 | |
| 94 | init_failed: |
| 95 | |
| 96 | curl_easy_cleanup(eh[num]); |
| 97 | eh[num] = NULL; |
| 98 | |
| 99 | return res; /* failure */ |
| 100 | } |
| 101 | |
| 102 | static 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(1) { |
| 161 | msg = curl_multi_info_read(cm, &Q); |
| 162 | if(!msg) |
| 163 | break; |
| 164 | if(msg->msg == CURLMSG_DONE) { |
| 165 | int i; |
| 166 | CURL *e = msg->easy_handle; |
| 167 | fprintf(stderr, "R: %d - %s\n", (int)msg->data.result, |
| 168 | curl_easy_strerror(msg->data.result)); |
| 169 | curl_multi_remove_handle(cm, e); |
| 170 | curl_easy_cleanup(e); |
| 171 | for(i = 0; i < NUM_HANDLES; i++) { |
| 172 | if(eh[i] == e) { |
| 173 | eh[i] = NULL; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | else |
| 179 | fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg); |
| 180 | } |
| 181 | |
| 182 | res_test_timedout(); |
| 183 | if(res) |
| 184 | return res; |
| 185 | } |
| 186 | |
| 187 | return 0; /* success */ |
| 188 | } |
| 189 | |
| 190 | int test(char *URL) |
| 191 | { |
| 192 | CURLM *cm = NULL; |
| 193 | struct curl_slist *headers = NULL; |
| 194 | char buffer[246]; /* naively fixed-size */ |
| 195 | int res = 0; |
| 196 | int i; |
| 197 | |
| 198 | for(i = 0; i < NUM_HANDLES; i++) |
| 199 | eh[i] = NULL; |
| 200 | |
| 201 | start_test_timing(); |
| 202 | |
| 203 | if(test_argc < 4) |
| 204 | return 99; |
| 205 | |
| 206 | msnprintf(buffer, sizeof(buffer), "Host: %s", HOST); |
| 207 | |
| 208 | /* now add a custom Host: header */ |
| 209 | headers = curl_slist_append(headers, buffer); |
| 210 | if(!headers) { |
| 211 | fprintf(stderr, "curl_slist_append() failed\n"); |
| 212 | return TEST_ERR_MAJOR_BAD; |
| 213 | } |
| 214 | |
| 215 | res_global_init(CURL_GLOBAL_ALL); |
| 216 | if(res) { |
| 217 | curl_slist_free_all(headers); |
| 218 | return res; |
| 219 | } |
| 220 | |
| 221 | res_multi_init(cm); |
| 222 | if(res) { |
| 223 | curl_global_cleanup(); |
| 224 | curl_slist_free_all(headers); |
| 225 | return res; |
| 226 | } |
| 227 | |
| 228 | res = loop(0, cm, URL, PROXYUSERPWD, headers); |
| 229 | if(res) |
| 230 | goto test_cleanup; |
| 231 | |
| 232 | fprintf(stderr, "lib540: now we do the request again\n"); |
| 233 | |
| 234 | res = loop(1, cm, URL, PROXYUSERPWD, headers); |
| 235 | |
| 236 | test_cleanup: |
| 237 | |
| 238 | /* proper cleanup sequence - type PB */ |
| 239 | |
| 240 | for(i = 0; i < NUM_HANDLES; i++) { |
| 241 | curl_multi_remove_handle(cm, eh[i]); |
| 242 | curl_easy_cleanup(eh[i]); |
| 243 | } |
| 244 | |
| 245 | curl_multi_cleanup(cm); |
| 246 | curl_global_cleanup(); |
| 247 | |
| 248 | curl_slist_free_all(headers); |
| 249 | |
| 250 | return res; |
| 251 | } |