lib579.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #include "memdebug.h"
  26. static const char * const testpost[]={
  27. "one",
  28. "two",
  29. "three",
  30. "and a final longer crap: four",
  31. NULL
  32. };
  33. struct WriteThis {
  34. int counter;
  35. };
  36. static bool started = FALSE;
  37. static size_t last_ul = 0;
  38. static size_t last_ul_total = 0;
  39. static void progress_final_report(void)
  40. {
  41. FILE *moo = fopen(libtest_arg2, "ab");
  42. fprintf(moo, "Progress: end UL %zu/%zu\n", last_ul, last_ul_total);
  43. started = FALSE;
  44. fclose(moo);
  45. }
  46. static int progress_callback(void *clientp, double dltotal, double dlnow,
  47. double ultotal, double ulnow)
  48. {
  49. (void)clientp; /* UNUSED */
  50. (void)dltotal; /* UNUSED */
  51. (void)dlnow; /* UNUSED */
  52. if(started && ulnow <= 0.0 && last_ul) {
  53. progress_final_report();
  54. }
  55. last_ul = (size_t)ulnow;
  56. last_ul_total = (size_t)ultotal;
  57. if(!started) {
  58. FILE *moo = fopen(libtest_arg2, "ab");
  59. fprintf(moo, "Progress: start UL %zu/%zu\n", last_ul, last_ul_total);
  60. started = TRUE;
  61. fclose(moo);
  62. }
  63. return 0;
  64. }
  65. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  66. {
  67. struct WriteThis *pooh = (struct WriteThis *)userp;
  68. const char *data;
  69. if(size*nmemb < 1)
  70. return 0;
  71. data = testpost[pooh->counter];
  72. if(data) {
  73. size_t len = strlen(data);
  74. memcpy(ptr, data, len);
  75. pooh->counter++; /* advance pointer */
  76. return len;
  77. }
  78. return 0; /* no more data left to deliver */
  79. }
  80. CURLcode test(char *URL)
  81. {
  82. CURL *curl;
  83. CURLcode res = CURLE_OK;
  84. struct curl_slist *slist = NULL;
  85. struct WriteThis pooh;
  86. pooh.counter = 0;
  87. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  88. fprintf(stderr, "curl_global_init() failed\n");
  89. return TEST_ERR_MAJOR_BAD;
  90. }
  91. curl = curl_easy_init();
  92. if(!curl) {
  93. fprintf(stderr, "curl_easy_init() failed\n");
  94. curl_global_cleanup();
  95. return TEST_ERR_MAJOR_BAD;
  96. }
  97. slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
  98. if(!slist) {
  99. fprintf(stderr, "curl_slist_append() failed\n");
  100. curl_easy_cleanup(curl);
  101. curl_global_cleanup();
  102. return TEST_ERR_MAJOR_BAD;
  103. }
  104. /* First set the URL that is about to receive our POST. */
  105. test_setopt(curl, CURLOPT_URL, URL);
  106. /* Now specify we want to POST data */
  107. test_setopt(curl, CURLOPT_POST, 1L);
  108. /* we want to use our own read function */
  109. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  110. /* pointer to pass to our read function */
  111. test_setopt(curl, CURLOPT_READDATA, &pooh);
  112. /* get verbose debug output please */
  113. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  114. /* include headers in the output */
  115. test_setopt(curl, CURLOPT_HEADER, 1L);
  116. /* enforce chunked transfer by setting the header */
  117. test_setopt(curl, CURLOPT_HTTPHEADER, slist);
  118. test_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
  119. test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
  120. /* we want to use our own progress function */
  121. test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  122. CURL_IGNORE_DEPRECATION(
  123. test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  124. )
  125. /* Perform the request, res will get the return code */
  126. res = curl_easy_perform(curl);
  127. progress_final_report();
  128. test_cleanup:
  129. /* clean up the headers list */
  130. if(slist)
  131. curl_slist_free_all(slist);
  132. /* always cleanup */
  133. curl_easy_cleanup(curl);
  134. curl_global_cleanup();
  135. return res;
  136. }