lib1533.c 5.1 KB

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