lib1533.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 /*
00024  * This test sends data with CURLOPT_KEEP_SENDING_ON_ERROR.
00025  * The server responds with an early error response.
00026  * The test is successful if the connection can be reused for the next request,
00027  * because this implies that the data has been sent completely to the server.
00028  */
00029 
00030 #include "test.h"
00031 
00032 #include "memdebug.h"
00033 
00034 struct cb_data {
00035   CURL *easy_handle;
00036   int response_received;
00037   int paused;
00038   size_t remaining_bytes;
00039 };
00040 
00041 
00042 static void reset_data(struct cb_data *data, CURL *curl)
00043 {
00044   data->easy_handle = curl;
00045   data->response_received = 0;
00046   data->paused = 0;
00047   data->remaining_bytes = 3;
00048 }
00049 
00050 
00051 static size_t read_callback(void *ptr, size_t size, size_t nitems,
00052                             void *userdata)
00053 {
00054   struct cb_data *data = (struct cb_data *)userdata;
00055 
00056   /* wait until the server has sent all response headers */
00057   if(data->response_received) {
00058     size_t totalsize = nitems * size;
00059 
00060     size_t bytes_to_send = data->remaining_bytes;
00061     if(bytes_to_send > totalsize) {
00062       bytes_to_send = totalsize;
00063     }
00064 
00065     memset(ptr, 'a', bytes_to_send);
00066     data->remaining_bytes -= bytes_to_send;
00067 
00068     return bytes_to_send;
00069   }
00070   else {
00071     data->paused = 1;
00072     return CURL_READFUNC_PAUSE;
00073   }
00074 }
00075 
00076 
00077 static size_t write_callback(char *ptr, size_t size, size_t nmemb,
00078                              void *userdata)
00079 {
00080   struct cb_data *data = (struct cb_data *)userdata;
00081   size_t totalsize = nmemb * size;
00082 
00083   /* unused parameter */
00084   (void)ptr;
00085 
00086   /* all response headers have been received */
00087   data->response_received = 1;
00088 
00089   if(data->paused) {
00090     /* continue to send request body data */
00091     data->paused = 0;
00092     curl_easy_pause(data->easy_handle, CURLPAUSE_CONT);
00093   }
00094 
00095   return totalsize;
00096 }
00097 
00098 
00099 static int perform_and_check_connections(CURL *curl, const char *description,
00100                                          long expected_connections)
00101 {
00102   CURLcode res;
00103   long connections = 0;
00104 
00105   res = curl_easy_perform(curl);
00106   if(res != CURLE_OK) {
00107     fprintf(stderr, "curl_easy_perform() failed\n");
00108     return TEST_ERR_MAJOR_BAD;
00109   }
00110 
00111   res = curl_easy_getinfo(curl, CURLINFO_NUM_CONNECTS, &connections);
00112   if(res != CURLE_OK) {
00113     fprintf(stderr, "curl_easy_getinfo() failed\n");
00114     return TEST_ERR_MAJOR_BAD;
00115   }
00116 
00117   fprintf(stderr, "%s: expected: %ld connections; actual: %ld connections\n",
00118           description, expected_connections, connections);
00119 
00120   if(connections != expected_connections) {
00121     return TEST_ERR_FAILURE;
00122   }
00123 
00124   return TEST_ERR_SUCCESS;
00125 }
00126 
00127 
00128 int test(char *URL)
00129 {
00130   struct cb_data data;
00131   CURL *curl = NULL;
00132   CURLcode res = CURLE_FAILED_INIT;
00133 
00134   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
00135     fprintf(stderr, "curl_global_init() failed\n");
00136     return TEST_ERR_MAJOR_BAD;
00137   }
00138 
00139   curl = curl_easy_init();
00140   if(curl == NULL) {
00141     fprintf(stderr, "curl_easy_init() failed\n");
00142     curl_global_cleanup();
00143     return TEST_ERR_MAJOR_BAD;
00144   }
00145 
00146   reset_data(&data, curl);
00147 
00148   test_setopt(curl, CURLOPT_URL, URL);
00149   test_setopt(curl, CURLOPT_POST, 1L);
00150   test_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
00151               (curl_off_t)data.remaining_bytes);
00152   test_setopt(curl, CURLOPT_VERBOSE, 1L);
00153   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00154   test_setopt(curl, CURLOPT_READDATA, &data);
00155   test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
00156   test_setopt(curl, CURLOPT_WRITEDATA, &data);
00157 
00158   res = perform_and_check_connections(curl,
00159     "First request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
00160   if(res != TEST_ERR_SUCCESS) {
00161     goto test_cleanup;
00162   }
00163 
00164   reset_data(&data, curl);
00165 
00166   res = perform_and_check_connections(curl,
00167     "Second request without CURLOPT_KEEP_SENDING_ON_ERROR", 1);
00168   if(res != TEST_ERR_SUCCESS) {
00169     goto test_cleanup;
00170   }
00171 
00172   test_setopt(curl, CURLOPT_KEEP_SENDING_ON_ERROR, 1L);
00173 
00174   reset_data(&data, curl);
00175 
00176   res = perform_and_check_connections(curl,
00177     "First request with CURLOPT_KEEP_SENDING_ON_ERROR", 1);
00178   if(res != TEST_ERR_SUCCESS) {
00179     goto test_cleanup;
00180   }
00181 
00182   reset_data(&data, curl);
00183 
00184   res = perform_and_check_connections(curl,
00185     "Second request with CURLOPT_KEEP_SENDING_ON_ERROR", 0);
00186   if(res != TEST_ERR_SUCCESS) {
00187     goto test_cleanup;
00188   }
00189 
00190   res = TEST_ERR_SUCCESS;
00191 
00192 test_cleanup:
00193 
00194   curl_easy_cleanup(curl);
00195 
00196   curl_global_cleanup();
00197 
00198   return (int)res;
00199 }
00200 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05