lib540.c 5.5 KB

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