lib540.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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;
  114. #if LONG_MAX > INT_MAX
  115. itimeout = (L > INT_MAX) ? INT_MAX : (int)L;
  116. #else
  117. itimeout = (int)L;
  118. #endif
  119. T.tv_sec = itimeout/1000;
  120. T.tv_usec = (itimeout%1000)*1000;
  121. }
  122. else {
  123. T.tv_sec = 5;
  124. T.tv_usec = 0;
  125. }
  126. res_select_test(M + 1, &R, &W, &E, &T);
  127. if(res)
  128. return res;
  129. }
  130. while(1) {
  131. msg = curl_multi_info_read(cm, &Q);
  132. if(!msg)
  133. break;
  134. if(msg->msg == CURLMSG_DONE) {
  135. int i;
  136. CURL *e = msg->easy_handle;
  137. fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
  138. curl_easy_strerror(msg->data.result));
  139. curl_multi_remove_handle(cm, e);
  140. curl_easy_cleanup(e);
  141. for(i = 0; i < NUM_HANDLES; i++) {
  142. if(eh[i] == e) {
  143. eh[i] = NULL;
  144. break;
  145. }
  146. }
  147. }
  148. else
  149. fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
  150. }
  151. res_test_timedout();
  152. if(res)
  153. return res;
  154. }
  155. return 0; /* success */
  156. }
  157. int test(char *URL)
  158. {
  159. CURLM *cm = NULL;
  160. struct curl_slist *headers = NULL;
  161. char buffer[246]; /* naively fixed-size */
  162. int res = 0;
  163. int i;
  164. for(i = 0; i < NUM_HANDLES; i++)
  165. eh[i] = NULL;
  166. start_test_timing();
  167. if(test_argc < 4)
  168. return 99;
  169. msnprintf(buffer, sizeof(buffer), "Host: %s", HOST);
  170. /* now add a custom Host: header */
  171. headers = curl_slist_append(headers, buffer);
  172. if(!headers) {
  173. fprintf(stderr, "curl_slist_append() failed\n");
  174. return TEST_ERR_MAJOR_BAD;
  175. }
  176. res_global_init(CURL_GLOBAL_ALL);
  177. if(res) {
  178. curl_slist_free_all(headers);
  179. return res;
  180. }
  181. res_multi_init(cm);
  182. if(res) {
  183. curl_global_cleanup();
  184. curl_slist_free_all(headers);
  185. return res;
  186. }
  187. res = loop(0, cm, URL, PROXYUSERPWD, headers);
  188. if(res)
  189. goto test_cleanup;
  190. fprintf(stderr, "lib540: now we do the request again\n");
  191. res = loop(1, cm, URL, PROXYUSERPWD, headers);
  192. test_cleanup:
  193. /* proper cleanup sequence - type PB */
  194. for(i = 0; i < NUM_HANDLES; i++) {
  195. curl_multi_remove_handle(cm, eh[i]);
  196. curl_easy_cleanup(eh[i]);
  197. }
  198. curl_multi_cleanup(cm);
  199. curl_global_cleanup();
  200. curl_slist_free_all(headers);
  201. return res;
  202. }