2
0

lib1533.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\n");
  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. int test(char *URL)
  104. {
  105. struct cb_data data;
  106. CURL *curl = NULL;
  107. int res = TEST_ERR_FAILURE;
  108. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  109. fprintf(stderr, "curl_global_init() failed\n");
  110. return TEST_ERR_MAJOR_BAD;
  111. }
  112. curl = curl_easy_init();
  113. if(!curl) {
  114. fprintf(stderr, "curl_easy_init() failed\n");
  115. curl_global_cleanup();
  116. return TEST_ERR_MAJOR_BAD;
  117. }
  118. reset_data(&data, curl);
  119. test_setopt(curl, CURLOPT_URL, URL);
  120. test_setopt(curl, CURLOPT_POST, 1L);
  121. test_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
  122. (curl_off_t)data.remaining_bytes);
  123. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  124. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  125. test_setopt(curl, CURLOPT_READDATA, &data);
  126. test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
  127. test_setopt(curl, CURLOPT_WRITEDATA, &data);
  128. res = perform_and_check_connections(curl,
  129. "First request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
  130. if(res != TEST_ERR_SUCCESS) {
  131. goto test_cleanup;
  132. }
  133. reset_data(&data, curl);
  134. res = perform_and_check_connections(curl,
  135. "Second request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
  136. if(res != TEST_ERR_SUCCESS) {
  137. goto test_cleanup;
  138. }
  139. test_setopt(curl, CURLOPT_KEEP_SENDING_ON_ERROR, 1L);
  140. reset_data(&data, curl);
  141. res = perform_and_check_connections(curl,
  142. "First request with CURLOPT_KEEP_SENDING_ON_ERROR", 1);
  143. if(res != TEST_ERR_SUCCESS) {
  144. goto test_cleanup;
  145. }
  146. reset_data(&data, curl);
  147. res = perform_and_check_connections(curl,
  148. "Second request with CURLOPT_KEEP_SENDING_ON_ERROR", 0);
  149. if(res != TEST_ERR_SUCCESS) {
  150. goto test_cleanup;
  151. }
  152. res = TEST_ERR_SUCCESS;
  153. test_cleanup:
  154. curl_easy_cleanup(curl);
  155. curl_global_cleanup();
  156. return (int)res;
  157. }