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