2
0

lib579.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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.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. static int prev_ultotal = -1;
  38. static int prev_ulnow = -1;
  39. (void)clientp; /* UNUSED */
  40. (void)dltotal; /* UNUSED */
  41. (void)dlnow; /* UNUSED */
  42. /* to avoid depending on timing, which will cause this progress function to
  43. get called a different number of times depending on circumstances, we
  44. only log these lines if the numbers are different from the previous
  45. invoke */
  46. if((prev_ultotal != (int)ultotal) ||
  47. (prev_ulnow != (int)ulnow)) {
  48. FILE *moo = fopen(libtest_arg2, "ab");
  49. if(moo) {
  50. fprintf(moo, "Progress callback called with UL %d out of %d\n",
  51. (int)ulnow, (int)ultotal);
  52. fclose(moo);
  53. }
  54. prev_ulnow = (int) ulnow;
  55. prev_ultotal = (int) ultotal;
  56. }
  57. return 0;
  58. }
  59. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  60. {
  61. struct WriteThis *pooh = (struct WriteThis *)userp;
  62. const char *data;
  63. if(size*nmemb < 1)
  64. return 0;
  65. data = post[pooh->counter];
  66. if(data) {
  67. size_t len = strlen(data);
  68. memcpy(ptr, data, len);
  69. pooh->counter++; /* advance pointer */
  70. return len;
  71. }
  72. return 0; /* no more data left to deliver */
  73. }
  74. int test(char *URL)
  75. {
  76. CURL *curl;
  77. CURLcode res = CURLE_OK;
  78. struct curl_slist *slist = NULL;
  79. struct WriteThis pooh;
  80. pooh.counter = 0;
  81. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  82. fprintf(stderr, "curl_global_init() failed\n");
  83. return TEST_ERR_MAJOR_BAD;
  84. }
  85. curl = curl_easy_init();
  86. if(!curl) {
  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) {
  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. /* we want to use our own read function */
  103. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  104. /* pointer to pass to our read function */
  105. test_setopt(curl, CURLOPT_READDATA, &pooh);
  106. /* get verbose debug output please */
  107. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  108. /* include headers in the output */
  109. test_setopt(curl, CURLOPT_HEADER, 1L);
  110. /* enforce chunked transfer by setting the header */
  111. test_setopt(curl, CURLOPT_HTTPHEADER, slist);
  112. test_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
  113. test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
  114. /* we want to use our own progress function */
  115. test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  116. test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  117. /* Perform the request, res will get the return code */
  118. res = curl_easy_perform(curl);
  119. test_cleanup:
  120. /* clean up the headers list */
  121. if(slist)
  122. curl_slist_free_all(slist);
  123. /* always cleanup */
  124. curl_easy_cleanup(curl);
  125. curl_global_cleanup();
  126. return res;
  127. }