00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00084 (void)ptr;
00085
00086
00087 data->response_received = 1;
00088
00089 if(data->paused) {
00090
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