lib540.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
  11. * mailing list on 10 Jul 2007, converted to a test case.
  12. *
  13. * argv1 = URL
  14. * argv2 = proxy
  15. * argv3 = proxyuser:password
  16. * argv4 = host name to use for the custom Host: header
  17. */
  18. #include "test.h"
  19. #define PROXY libtest_arg2
  20. #define PROXYUSERPWD libtest_arg3
  21. #define HOST test_argv[4]
  22. static void init(CURLM *cm, const char* url, const char* userpwd,
  23. struct curl_slist *headers)
  24. {
  25. CURL *eh = curl_easy_init();
  26. curl_easy_setopt(eh, CURLOPT_URL, url);
  27. curl_easy_setopt(eh, CURLOPT_PROXY, PROXY);
  28. curl_easy_setopt(eh, CURLOPT_PROXYUSERPWD, userpwd);
  29. curl_easy_setopt(eh, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
  30. curl_easy_setopt(eh, CURLOPT_VERBOSE, 1L);
  31. curl_easy_setopt(eh, CURLOPT_HEADER, 1L);
  32. curl_easy_setopt(eh, CURLOPT_HTTPHEADER, headers); /* custom Host: */
  33. curl_multi_add_handle(cm, eh);
  34. }
  35. static int loop(CURLM *cm, const char* url, const char* userpwd,
  36. struct curl_slist *headers)
  37. {
  38. CURLMsg *msg;
  39. long L;
  40. int M, Q, U = -1;
  41. fd_set R, W, E;
  42. struct timeval T;
  43. init(cm, url, userpwd, headers);
  44. while (U) {
  45. while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(cm, &U));
  46. if (U) {
  47. FD_ZERO(&R);
  48. FD_ZERO(&W);
  49. FD_ZERO(&E);
  50. if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
  51. fprintf(stderr, "E: curl_multi_fdset\n");
  52. return EXIT_FAILURE;
  53. }
  54. /* In a real-world program you OF COURSE check the return that maxfd is
  55. bigger than -1 so that the call to select() below makes sense! */
  56. if (curl_multi_timeout(cm, &L)) {
  57. fprintf(stderr, "E: curl_multi_timeout\n");
  58. return EXIT_FAILURE;
  59. }
  60. if(L != -1) {
  61. T.tv_sec = L/1000;
  62. T.tv_usec = (L%1000)*1000;
  63. }
  64. else {
  65. T.tv_sec = 5;
  66. T.tv_usec = 0;
  67. }
  68. if (0 > select(M+1, &R, &W, &E, &T)) {
  69. fprintf(stderr, "E: select\n");
  70. return EXIT_FAILURE;
  71. }
  72. }
  73. while ((msg = curl_multi_info_read(cm, &Q))) {
  74. if (msg->msg == CURLMSG_DONE) {
  75. CURL *e = msg->easy_handle;
  76. fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
  77. curl_easy_strerror(msg->data.result));
  78. curl_multi_remove_handle(cm, e);
  79. curl_easy_cleanup(e);
  80. }
  81. else {
  82. fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
  83. }
  84. }
  85. }
  86. return 1;
  87. }
  88. int test(char *URL)
  89. {
  90. CURLM *cm;
  91. struct curl_slist *headers = NULL;
  92. char buffer[246]; /* naively fixed-size */
  93. if(test_argc < 4)
  94. return 99;
  95. sprintf(buffer, "Host: %s", HOST);
  96. /* now add a custom Host: header */
  97. headers = curl_slist_append(headers, buffer);
  98. curl_global_init(CURL_GLOBAL_ALL);
  99. cm = curl_multi_init();
  100. loop(cm, URL, PROXYUSERPWD, headers);
  101. fprintf(stderr, "lib540: now we do the request again\n");
  102. loop(cm, URL, PROXYUSERPWD, headers);
  103. curl_multi_cleanup(cm);
  104. curl_global_cleanup();
  105. curl_slist_free_all(headers);
  106. return EXIT_SUCCESS;
  107. }