lib579.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include "test.h"
  23. #include "memdebug.h"
  24. static const char * const post[]={
  25. "one",
  26. "two",
  27. "three",
  28. "and a final longer crap: four",
  29. NULL
  30. };
  31. struct WriteThis {
  32. int counter;
  33. };
  34. static int progress_callback(void *clientp, double dltotal, double dlnow,
  35. double ultotal, double ulnow)
  36. {
  37. FILE *moo;
  38. static int prev_ultotal = -1;
  39. static int prev_ulnow = -1;
  40. (void)clientp; /* UNUSED */
  41. (void)dltotal; /* UNUSED */
  42. (void)dlnow; /* UNUSED */
  43. /* to avoid depending on timing, which will cause this progress function to
  44. get called a different number of times depending on circumstances, we
  45. only log these lines if the numbers are different from the previous
  46. invoke */
  47. if((prev_ultotal != (int)ultotal) ||
  48. (prev_ulnow != (int)ulnow)) {
  49. moo = fopen(libtest_arg2, "ab");
  50. if(moo) {
  51. fprintf(moo, "Progress callback called with UL %d out of %d\n",
  52. (int)ulnow, (int)ultotal);
  53. fclose(moo);
  54. }
  55. prev_ulnow = (int) ulnow;
  56. prev_ultotal = (int) ultotal;
  57. }
  58. return 0;
  59. }
  60. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  61. {
  62. struct WriteThis *pooh = (struct WriteThis *)userp;
  63. const char *data;
  64. if(size*nmemb < 1)
  65. return 0;
  66. data = post[pooh->counter];
  67. if(data) {
  68. size_t len = strlen(data);
  69. memcpy(ptr, data, len);
  70. pooh->counter++; /* advance pointer */
  71. return len;
  72. }
  73. return 0; /* no more data left to deliver */
  74. }
  75. int test(char *URL)
  76. {
  77. CURL *curl;
  78. CURLcode res=CURLE_OK;
  79. struct curl_slist *slist = NULL;
  80. struct WriteThis pooh;
  81. pooh.counter = 0;
  82. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  83. fprintf(stderr, "curl_global_init() failed\n");
  84. return TEST_ERR_MAJOR_BAD;
  85. }
  86. if ((curl = curl_easy_init()) == NULL) {
  87. fprintf(stderr, "curl_easy_init() failed\n");
  88. curl_global_cleanup();
  89. return TEST_ERR_MAJOR_BAD;
  90. }
  91. slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
  92. if (slist == NULL) {
  93. fprintf(stderr, "curl_slist_append() failed\n");
  94. curl_easy_cleanup(curl);
  95. curl_global_cleanup();
  96. return TEST_ERR_MAJOR_BAD;
  97. }
  98. /* First set the URL that is about to receive our POST. */
  99. test_setopt(curl, CURLOPT_URL, URL);
  100. /* Now specify we want to POST data */
  101. test_setopt(curl, CURLOPT_POST, 1L);
  102. #ifdef CURL_DOES_CONVERSIONS
  103. /* Convert the POST data to ASCII */
  104. test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
  105. #endif
  106. /* we want to use our own read function */
  107. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  108. /* pointer to pass to our read function */
  109. test_setopt(curl, CURLOPT_INFILE, &pooh);
  110. /* get verbose debug output please */
  111. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  112. /* include headers in the output */
  113. test_setopt(curl, CURLOPT_HEADER, 1L);
  114. /* enforce chunked transfer by setting the header */
  115. test_setopt(curl, CURLOPT_HTTPHEADER, slist);
  116. test_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  117. test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
  118. /* we want to use our own progress function */
  119. test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  120. test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  121. /* Perform the request, res will get the return code */
  122. res = curl_easy_perform(curl);
  123. test_cleanup:
  124. /* clean up the headers list */
  125. if(slist)
  126. curl_slist_free_all(slist);
  127. /* always cleanup */
  128. curl_easy_cleanup(curl);
  129. curl_global_cleanup();
  130. return res;
  131. }