xref: /curl/tests/http/clients/upload-pausing.c (revision cfc65fd1)
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 /* <DESC>
25  * upload pausing
26  * </DESC>
27  */
28 /* This is based on the poc client of issue #11769
29  */
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <curl/curl.h>
36 #include <curl/mprintf.h>
37 
log_line_start(FILE * log,const char * idsbuf,curl_infotype type)38 static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
39 {
40   /*
41    * This is the trace look that is similar to what libcurl makes on its
42    * own.
43    */
44   static const char * const s_infotype[] = {
45     "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
46   };
47   if(idsbuf && *idsbuf)
48     fprintf(log, "%s%s", idsbuf, s_infotype[type]);
49   else
50     fputs(s_infotype[type], log);
51 }
52 
53 #define TRC_IDS_FORMAT_IDS_1  "[%" CURL_FORMAT_CURL_OFF_T "-x] "
54 #define TRC_IDS_FORMAT_IDS_2  "[%" CURL_FORMAT_CURL_OFF_T "-%" \
55                                    CURL_FORMAT_CURL_OFF_T "] "
56 /*
57 ** callback for CURLOPT_DEBUGFUNCTION
58 */
debug_cb(CURL * handle,curl_infotype type,char * data,size_t size,void * userdata)59 static int debug_cb(CURL *handle, curl_infotype type,
60                     char *data, size_t size,
61                     void *userdata)
62 {
63   FILE *output = stderr;
64   static int newl = 0;
65   static int traced_data = 0;
66   char idsbuf[60];
67   curl_off_t xfer_id, conn_id;
68 
69   (void)handle; /* not used */
70   (void)userdata;
71 
72   if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
73     if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
74         conn_id >= 0) {
75       curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
76                      xfer_id, conn_id);
77     }
78     else {
79       curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
80     }
81   }
82   else
83     idsbuf[0] = 0;
84 
85   switch(type) {
86   case CURLINFO_HEADER_OUT:
87     if(size > 0) {
88       size_t st = 0;
89       size_t i;
90       for(i = 0; i < size - 1; i++) {
91         if(data[i] == '\n') { /* LF */
92           if(!newl) {
93             log_line_start(output, idsbuf, type);
94           }
95           (void)fwrite(data + st, i - st + 1, 1, output);
96           st = i + 1;
97           newl = 0;
98         }
99       }
100       if(!newl)
101         log_line_start(output, idsbuf, type);
102       (void)fwrite(data + st, i - st + 1, 1, output);
103     }
104     newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
105     traced_data = 0;
106     break;
107   case CURLINFO_TEXT:
108   case CURLINFO_HEADER_IN:
109     if(!newl)
110       log_line_start(output, idsbuf, type);
111     (void)fwrite(data, size, 1, output);
112     newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
113     traced_data = 0;
114     break;
115   case CURLINFO_DATA_OUT:
116   case CURLINFO_DATA_IN:
117   case CURLINFO_SSL_DATA_IN:
118   case CURLINFO_SSL_DATA_OUT:
119     if(!traced_data) {
120       if(!newl)
121         log_line_start(output, idsbuf, type);
122       fprintf(output, "[%ld bytes data]\n", (long)size);
123       newl = 0;
124       traced_data = 1;
125     }
126     break;
127   default: /* nada */
128     newl = 0;
129     traced_data = 1;
130     break;
131   }
132 
133   return 0;
134 }
135 
136 #define PAUSE_READ_AFTER  10
137 static size_t total_read = 0;
138 
read_callback(char * ptr,size_t size,size_t nmemb,void * userdata)139 static size_t read_callback(char *ptr, size_t size, size_t nmemb,
140                             void *userdata)
141 {
142   (void)size;
143   (void)nmemb;
144   (void)userdata;
145   if(total_read >= PAUSE_READ_AFTER) {
146     return CURL_READFUNC_PAUSE;
147   }
148   else {
149     ptr[0] = '\n';
150     ++total_read;
151     return 1;
152   }
153 }
154 
progress_callback(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)155 static int progress_callback(void *clientp,
156                              double dltotal,
157                              double dlnow,
158                              double ultotal,
159                              double ulnow)
160 {
161   CURL *curl;
162   (void)dltotal;
163   (void)dlnow;
164   (void)ultotal;
165   (void)ulnow;
166   curl = (CURL *)clientp;
167   curl_easy_pause(curl, CURLPAUSE_CONT);
168   return 0;
169 }
170 
err(void)171 static int err(void)
172 {
173   fprintf(stderr, "something unexpected went wrong - bailing out!\n");
174   exit(2);
175 }
176 
177 
178 
main(int argc,char * argv[])179 int main(int argc, char *argv[])
180 {
181   CURL *curl;
182   CURLcode rc = CURLE_OK;
183   CURLU *cu;
184   struct curl_slist *resolve = NULL;
185   char resolve_buf[1024];
186   char *url, *host = NULL, *port = NULL;
187 
188   if(argc != 2) {
189     fprintf(stderr, "ERROR: need URL as argument\n");
190     return 2;
191   }
192   url = argv[1];
193 
194   curl_global_init(CURL_GLOBAL_DEFAULT);
195   curl_global_trace("ids,time");
196 
197   cu = curl_url();
198   if(!cu) {
199     fprintf(stderr, "out of memory\n");
200     exit(1);
201   }
202   if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
203     fprintf(stderr, "not a URL: '%s'\n", url);
204     exit(1);
205   }
206   if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
207     fprintf(stderr, "could not get host of '%s'\n", url);
208     exit(1);
209   }
210   if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
211     fprintf(stderr, "could not get port of '%s'\n", url);
212     exit(1);
213   }
214   memset(&resolve, 0, sizeof(resolve));
215   curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1,
216                  "%s:%s:127.0.0.1", host, port);
217   resolve = curl_slist_append(resolve, resolve_buf);
218 
219   curl = curl_easy_init();
220   if(!curl) {
221     fprintf(stderr, "out of memory\n");
222     exit(1);
223   }
224   /* We want to use our own read function. */
225   curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
226 
227   /* It will help us to continue the read function. */
228   curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
229   curl_easy_setopt(curl, CURLOPT_XFERINFODATA, curl);
230   curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
231 
232   /* It will help us to ensure that keepalive does not help. */
233   curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
234   curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 1L);
235   curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 1L);
236 
237   /* Enable uploading. */
238   curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
239   curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
240 
241   if(curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L) != CURLE_OK ||
242      curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_cb)
243      != CURLE_OK ||
244      curl_easy_setopt(curl, CURLOPT_RESOLVE, resolve) != CURLE_OK)
245     err();
246 
247   curl_easy_setopt(curl, CURLOPT_URL, url);
248   rc = curl_easy_perform(curl);
249 
250   if(curl) {
251     curl_easy_cleanup(curl);
252   }
253 
254   curl_slist_free_all(resolve);
255   curl_free(host);
256   curl_free(port);
257   curl_url_cleanup(cu);
258   curl_global_cleanup();
259 
260   return (int)rc;
261 }
262