lib540.c 5.3 KB

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