lib540.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #include "test.h"
  33. #include <limits.h>
  34. #include "testutil.h"
  35. #include "warnless.h"
  36. #include "memdebug.h"
  37. #define TEST_HANG_TIMEOUT 60 * 1000
  38. #define PROXY libtest_arg2
  39. #define PROXYUSERPWD libtest_arg3
  40. #define HOST test_argv[4]
  41. #define NUM_HANDLES 2
  42. static CURL *eh[NUM_HANDLES];
  43. static int init(int num, CURLM *cm, const char *url, const char *userpwd,
  44. struct curl_slist *headers)
  45. {
  46. int res = 0;
  47. res_easy_init(eh[num]);
  48. if(res)
  49. goto init_failed;
  50. res_easy_setopt(eh[num], CURLOPT_URL, url);
  51. if(res)
  52. goto init_failed;
  53. res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
  54. if(res)
  55. goto init_failed;
  56. res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
  57. if(res)
  58. goto init_failed;
  59. res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
  60. if(res)
  61. goto init_failed;
  62. res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
  63. if(res)
  64. goto init_failed;
  65. res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
  66. if(res)
  67. goto init_failed;
  68. res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
  69. if(res)
  70. goto init_failed;
  71. res_multi_add_handle(cm, eh[num]);
  72. if(res)
  73. goto init_failed;
  74. return 0; /* success */
  75. init_failed:
  76. curl_easy_cleanup(eh[num]);
  77. eh[num] = NULL;
  78. return res; /* failure */
  79. }
  80. static int loop(int num, CURLM *cm, const char *url, const char *userpwd,
  81. struct curl_slist *headers)
  82. {
  83. CURLMsg *msg;
  84. long L;
  85. int Q, U = -1;
  86. fd_set R, W, E;
  87. struct timeval T;
  88. int res = 0;
  89. res = init(num, cm, url, userpwd, headers);
  90. if(res)
  91. return res;
  92. while(U) {
  93. int M = -99;
  94. res_multi_perform(cm, &U);
  95. if(res)
  96. return res;
  97. res_test_timedout();
  98. if(res)
  99. return res;
  100. if(U) {
  101. FD_ZERO(&R);
  102. FD_ZERO(&W);
  103. FD_ZERO(&E);
  104. res_multi_fdset(cm, &R, &W, &E, &M);
  105. if(res)
  106. return res;
  107. /* At this point, M is guaranteed to be greater or equal than -1. */
  108. res_multi_timeout(cm, &L);
  109. if(res)
  110. return res;
  111. /* At this point, L is guaranteed to be greater or equal than -1. */
  112. if(L != -1) {
  113. int itimeout = (L > (long)INT_MAX) ? INT_MAX : (int)L;
  114. T.tv_sec = itimeout/1000;
  115. T.tv_usec = (itimeout%1000)*1000;
  116. }
  117. else {
  118. T.tv_sec = 5;
  119. T.tv_usec = 0;
  120. }
  121. res_select_test(M + 1, &R, &W, &E, &T);
  122. if(res)
  123. return res;
  124. }
  125. while(1) {
  126. msg = curl_multi_info_read(cm, &Q);
  127. if(!msg)
  128. break;
  129. if(msg->msg == CURLMSG_DONE) {
  130. int i;
  131. CURL *e = msg->easy_handle;
  132. fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
  133. curl_easy_strerror(msg->data.result));
  134. curl_multi_remove_handle(cm, e);
  135. curl_easy_cleanup(e);
  136. for(i = 0; i < NUM_HANDLES; i++) {
  137. if(eh[i] == e) {
  138. eh[i] = NULL;
  139. break;
  140. }
  141. }
  142. }
  143. else
  144. fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
  145. }
  146. res_test_timedout();
  147. if(res)
  148. return res;
  149. }
  150. return 0; /* success */
  151. }
  152. int test(char *URL)
  153. {
  154. CURLM *cm = NULL;
  155. struct curl_slist *headers = NULL;
  156. char buffer[246]; /* naively fixed-size */
  157. int res = 0;
  158. int i;
  159. for(i = 0; i < NUM_HANDLES; i++)
  160. eh[i] = NULL;
  161. start_test_timing();
  162. if(test_argc < 4)
  163. return 99;
  164. msnprintf(buffer, sizeof(buffer), "Host: %s", HOST);
  165. /* now add a custom Host: header */
  166. headers = curl_slist_append(headers, buffer);
  167. if(!headers) {
  168. fprintf(stderr, "curl_slist_append() failed\n");
  169. return TEST_ERR_MAJOR_BAD;
  170. }
  171. res_global_init(CURL_GLOBAL_ALL);
  172. if(res) {
  173. curl_slist_free_all(headers);
  174. return res;
  175. }
  176. res_multi_init(cm);
  177. if(res) {
  178. curl_global_cleanup();
  179. curl_slist_free_all(headers);
  180. return res;
  181. }
  182. res = loop(0, cm, URL, PROXYUSERPWD, headers);
  183. if(res)
  184. goto test_cleanup;
  185. fprintf(stderr, "lib540: now we do the request again\n");
  186. res = loop(1, cm, URL, PROXYUSERPWD, headers);
  187. test_cleanup:
  188. /* proper cleanup sequence - type PB */
  189. for(i = 0; i < NUM_HANDLES; i++) {
  190. curl_multi_remove_handle(cm, eh[i]);
  191. curl_easy_cleanup(eh[i]);
  192. }
  193. curl_multi_cleanup(cm);
  194. curl_global_cleanup();
  195. curl_slist_free_all(headers);
  196. return res;
  197. }