lib579.c 4.3 KB

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