lib540.c 5.4 KB

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