lib547.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * argv1 = URL
  11. * argv2 = proxy
  12. * argv3 = proxyuser:password
  13. */
  14. #include "test.h"
  15. #define UPLOADTHIS "this is the blurb we want to upload\n"
  16. #ifndef LIB548
  17. static size_t readcallback(void *ptr,
  18. size_t size,
  19. size_t nmemb,
  20. void *clientp)
  21. {
  22. int *counter = (int *)clientp;
  23. if(*counter) {
  24. /* only do this once and then require a clearing of this */
  25. fprintf(stderr, "READ ALREADY DONE!\n");
  26. return 0;
  27. }
  28. (*counter)++; /* bump */
  29. if(size * nmemb > strlen(UPLOADTHIS)) {
  30. fprintf(stderr, "READ!\n");
  31. strcpy(ptr, UPLOADTHIS);
  32. return strlen(UPLOADTHIS);
  33. }
  34. fprintf(stderr, "READ NOT FINE!\n");
  35. return 0;
  36. }
  37. static curlioerr ioctlcallback(CURL *handle,
  38. int cmd,
  39. void *clientp)
  40. {
  41. int *counter = (int *)clientp;
  42. (void)handle; /* unused */
  43. if(cmd == CURLIOCMD_RESTARTREAD) {
  44. fprintf(stderr, "REWIND!\n");
  45. *counter = 0; /* clear counter to make the read callback restart */
  46. }
  47. return CURLIOE_OK;
  48. }
  49. #endif
  50. int test(char *URL)
  51. {
  52. CURLcode res;
  53. CURL *curl;
  54. #ifndef LIB548
  55. int counter=0;
  56. #endif
  57. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  58. fprintf(stderr, "curl_global_init() failed\n");
  59. return TEST_ERR_MAJOR_BAD;
  60. }
  61. if ((curl = curl_easy_init()) == NULL) {
  62. fprintf(stderr, "curl_easy_init() failed\n");
  63. curl_global_cleanup();
  64. return TEST_ERR_MAJOR_BAD;
  65. }
  66. curl_easy_setopt(curl, CURLOPT_URL, URL);
  67. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  68. curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
  69. #ifdef LIB548
  70. /* set the data to POST with a mere pointer to a zero-terminated string */
  71. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, UPLOADTHIS);
  72. #else
  73. /* 547 style, which means reading the POST data from a callback */
  74. curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctlcallback);
  75. curl_easy_setopt(curl, CURLOPT_IOCTLDATA, &counter);
  76. curl_easy_setopt(curl, CURLOPT_READFUNCTION, readcallback);
  77. curl_easy_setopt(curl, CURLOPT_READDATA, &counter);
  78. /* We CANNOT do the POST fine without setting the size (or choose chunked)! */
  79. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(UPLOADTHIS));
  80. #endif
  81. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  82. curl_easy_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  83. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, libtest_arg3);
  84. curl_easy_setopt(curl, CURLOPT_PROXYAUTH,
  85. (long) (CURLAUTH_NTLM | CURLAUTH_DIGEST | CURLAUTH_BASIC) );
  86. res = curl_easy_perform(curl);
  87. curl_easy_cleanup(curl);
  88. curl_global_cleanup();
  89. return (int)res;
  90. }