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


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15