xref: /curl/tests/libtest/lib1533.c (revision 25cbc2f7)
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 /*
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 
reset_data(struct cb_data * data,CURL * curl)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 
read_callback(char * ptr,size_t size,size_t nitems,void * userdata)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 
write_callback(char * ptr,size_t size,size_t nmemb,void * userdata)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 
perform_and_check_connections(CURL * curl,const char * description,long expected_connections)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 with %d\n", res);
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 
test(char * URL)130 CURLcode test(char *URL)
131 {
132   struct cb_data data;
133   CURL *curl = NULL;
134   CURLcode res = TEST_ERR_FAILURE;
135   int result;
136 
137   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
138     fprintf(stderr, "curl_global_init() failed\n");
139     return TEST_ERR_MAJOR_BAD;
140   }
141 
142   curl = curl_easy_init();
143   if(!curl) {
144     fprintf(stderr, "curl_easy_init() failed\n");
145     curl_global_cleanup();
146     return TEST_ERR_MAJOR_BAD;
147   }
148 
149   reset_data(&data, curl);
150 
151   test_setopt(curl, CURLOPT_URL, URL);
152   test_setopt(curl, CURLOPT_POST, 1L);
153   test_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
154               (curl_off_t)data.remaining_bytes);
155   test_setopt(curl, CURLOPT_VERBOSE, 1L);
156   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
157   test_setopt(curl, CURLOPT_READDATA, &data);
158   test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
159   test_setopt(curl, CURLOPT_WRITEDATA, &data);
160 
161   result = perform_and_check_connections(curl,
162     "First request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
163   if(result != TEST_ERR_SUCCESS) {
164     res = (CURLcode) result;
165     goto test_cleanup;
166   }
167 
168   reset_data(&data, curl);
169 
170   result = perform_and_check_connections(curl,
171     "Second request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
172   if(result != TEST_ERR_SUCCESS) {
173     res = (CURLcode) result;
174     goto test_cleanup;
175   }
176 
177   test_setopt(curl, CURLOPT_KEEP_SENDING_ON_ERROR, 1L);
178 
179   reset_data(&data, curl);
180 
181   result = perform_and_check_connections(curl,
182     "First request with CURLOPT_KEEP_SENDING_ON_ERROR", 1);
183   if(result != TEST_ERR_SUCCESS) {
184     res = (CURLcode) result;
185     goto test_cleanup;
186   }
187 
188   reset_data(&data, curl);
189 
190   result = perform_and_check_connections(curl,
191     "Second request with CURLOPT_KEEP_SENDING_ON_ERROR", 0);
192   if(result != TEST_ERR_SUCCESS) {
193     res = (CURLcode) result;
194     goto test_cleanup;
195   }
196 
197   res = TEST_ERR_SUCCESS;
198 
199 test_cleanup:
200 
201   curl_easy_cleanup(curl);
202 
203   curl_global_cleanup();
204 
205   return res;
206 }
207