2
0

lib1533.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /*
  25. * This test sends data with CURLOPT_KEEP_SENDING_ON_ERROR.
  26. * The server responds with an early error response.
  27. * The test is successful if the connection can be reused for the next request,
  28. * because this implies that the data has been sent completely to the server.
  29. */
  30. #include "test.h"
  31. #include "memdebug.h"
  32. struct cb_data {
  33. CURL *easy_handle;
  34. int response_received;
  35. int paused;
  36. size_t remaining_bytes;
  37. };
  38. static void reset_data(struct cb_data *data, CURL *curl)
  39. {
  40. data->easy_handle = curl;
  41. data->response_received = 0;
  42. data->paused = 0;
  43. data->remaining_bytes = 3;
  44. }
  45. static size_t read_callback(char *ptr, size_t size, size_t nitems,
  46. void *userdata)
  47. {
  48. struct cb_data *data = (struct cb_data *)userdata;
  49. /* wait until the server has sent all response headers */
  50. if(data->response_received) {
  51. size_t totalsize = nitems * size;
  52. size_t bytes_to_send = data->remaining_bytes;
  53. if(bytes_to_send > totalsize) {
  54. bytes_to_send = totalsize;
  55. }
  56. memset(ptr, 'a', bytes_to_send);
  57. data->remaining_bytes -= bytes_to_send;
  58. return bytes_to_send;
  59. }
  60. else {
  61. data->paused = 1;
  62. return CURL_READFUNC_PAUSE;
  63. }
  64. }
  65. static size_t write_callback(char *ptr, size_t size, size_t nmemb,
  66. void *userdata)
  67. {
  68. struct cb_data *data = (struct cb_data *)userdata;
  69. size_t totalsize = nmemb * size;
  70. /* unused parameter */
  71. (void)ptr;
  72. /* all response headers have been received */
  73. data->response_received = 1;
  74. if(data->paused) {
  75. /* continue to send request body data */
  76. data->paused = 0;
  77. curl_easy_pause(data->easy_handle, CURLPAUSE_CONT);
  78. }
  79. return totalsize;
  80. }
  81. static int perform_and_check_connections(CURL *curl, const char *description,
  82. long expected_connections)
  83. {
  84. CURLcode res;
  85. long connections = 0;
  86. res = curl_easy_perform(curl);
  87. if(res != CURLE_OK) {
  88. fprintf(stderr, "curl_easy_perform() failed with %d\n", res);
  89. return TEST_ERR_MAJOR_BAD;
  90. }
  91. res = curl_easy_getinfo(curl, CURLINFO_NUM_CONNECTS, &connections);
  92. if(res != CURLE_OK) {
  93. fprintf(stderr, "curl_easy_getinfo() failed\n");
  94. return TEST_ERR_MAJOR_BAD;
  95. }
  96. fprintf(stderr, "%s: expected: %ld connections; actual: %ld connections\n",
  97. description, expected_connections, connections);
  98. if(connections != expected_connections) {
  99. return TEST_ERR_FAILURE;
  100. }
  101. return TEST_ERR_SUCCESS;
  102. }
  103. CURLcode test(char *URL)
  104. {
  105. struct cb_data data;
  106. CURL *curl = NULL;
  107. CURLcode res = TEST_ERR_FAILURE;
  108. int result;
  109. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  110. fprintf(stderr, "curl_global_init() failed\n");
  111. return TEST_ERR_MAJOR_BAD;
  112. }
  113. curl = curl_easy_init();
  114. if(!curl) {
  115. fprintf(stderr, "curl_easy_init() failed\n");
  116. curl_global_cleanup();
  117. return TEST_ERR_MAJOR_BAD;
  118. }
  119. reset_data(&data, curl);
  120. test_setopt(curl, CURLOPT_URL, URL);
  121. test_setopt(curl, CURLOPT_POST, 1L);
  122. test_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
  123. (curl_off_t)data.remaining_bytes);
  124. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  125. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  126. test_setopt(curl, CURLOPT_READDATA, &data);
  127. test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
  128. test_setopt(curl, CURLOPT_WRITEDATA, &data);
  129. result = perform_and_check_connections(curl,
  130. "First request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
  131. if(result != TEST_ERR_SUCCESS) {
  132. res = (CURLcode) result;
  133. goto test_cleanup;
  134. }
  135. reset_data(&data, curl);
  136. result = perform_and_check_connections(curl,
  137. "Second request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
  138. if(result != TEST_ERR_SUCCESS) {
  139. res = (CURLcode) result;
  140. goto test_cleanup;
  141. }
  142. test_setopt(curl, CURLOPT_KEEP_SENDING_ON_ERROR, 1L);
  143. reset_data(&data, curl);
  144. result = perform_and_check_connections(curl,
  145. "First request with CURLOPT_KEEP_SENDING_ON_ERROR", 1);
  146. if(result != TEST_ERR_SUCCESS) {
  147. res = (CURLcode) result;
  148. goto test_cleanup;
  149. }
  150. reset_data(&data, curl);
  151. result = perform_and_check_connections(curl,
  152. "Second request with CURLOPT_KEEP_SENDING_ON_ERROR", 0);
  153. if(result != TEST_ERR_SUCCESS) {
  154. res = (CURLcode) result;
  155. goto test_cleanup;
  156. }
  157. res = TEST_ERR_SUCCESS;
  158. test_cleanup:
  159. curl_easy_cleanup(curl);
  160. curl_global_cleanup();
  161. return res;
  162. }