xref: /curl/tests/http/clients/h2-pausing.c (revision 270a25c0)
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  * HTTP/2 download pausing
26  * </DESC>
27  */
28 /* This is based on the poc client of issue #11982
29  */
30 #include <assert.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <curl/curl.h>
37 #include <curl/mprintf.h>
38 
39 #define HANDLECOUNT 2
40 
log_line_start(FILE * log,const char * idsbuf,curl_infotype type)41 static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
42 {
43   /*
44    * This is the trace look that is similar to what libcurl makes on its
45    * own.
46    */
47   static const char * const s_infotype[] = {
48     "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
49   };
50   if(idsbuf && *idsbuf)
51     fprintf(log, "%s%s", idsbuf, s_infotype[type]);
52   else
53     fputs(s_infotype[type], log);
54 }
55 
56 #define TRC_IDS_FORMAT_IDS_1  "[%" CURL_FORMAT_CURL_OFF_T "-x] "
57 #define TRC_IDS_FORMAT_IDS_2  "[%" CURL_FORMAT_CURL_OFF_T "-%" \
58                                    CURL_FORMAT_CURL_OFF_T "] "
59 /*
60 ** callback for CURLOPT_DEBUGFUNCTION
61 */
debug_cb(CURL * handle,curl_infotype type,char * data,size_t size,void * userdata)62 static int debug_cb(CURL *handle, curl_infotype type,
63                     char *data, size_t size,
64                     void *userdata)
65 {
66   FILE *output = stderr;
67   static int newl = 0;
68   static int traced_data = 0;
69   char idsbuf[60];
70   curl_off_t xfer_id, conn_id;
71 
72   (void)handle; /* not used */
73   (void)userdata;
74 
75   if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
76     if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
77         conn_id >= 0) {
78       curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
79                      xfer_id, conn_id);
80     }
81     else {
82       curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
83     }
84   }
85   else
86     idsbuf[0] = 0;
87 
88   switch(type) {
89   case CURLINFO_HEADER_OUT:
90     if(size > 0) {
91       size_t st = 0;
92       size_t i;
93       for(i = 0; i < size - 1; i++) {
94         if(data[i] == '\n') { /* LF */
95           if(!newl) {
96             log_line_start(output, idsbuf, type);
97           }
98           (void)fwrite(data + st, i - st + 1, 1, output);
99           st = i + 1;
100           newl = 0;
101         }
102       }
103       if(!newl)
104         log_line_start(output, idsbuf, type);
105       (void)fwrite(data + st, i - st + 1, 1, output);
106     }
107     newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
108     traced_data = 0;
109     break;
110   case CURLINFO_TEXT:
111   case CURLINFO_HEADER_IN:
112     if(!newl)
113       log_line_start(output, idsbuf, type);
114     (void)fwrite(data, size, 1, output);
115     newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
116     traced_data = 0;
117     break;
118   case CURLINFO_DATA_OUT:
119   case CURLINFO_DATA_IN:
120   case CURLINFO_SSL_DATA_IN:
121   case CURLINFO_SSL_DATA_OUT:
122     if(!traced_data) {
123       if(!newl)
124         log_line_start(output, idsbuf, type);
125       fprintf(output, "[%ld bytes data]\n", (long)size);
126       newl = 0;
127       traced_data = 1;
128     }
129     break;
130   default: /* nada */
131     newl = 0;
132     traced_data = 1;
133     break;
134   }
135 
136   return 0;
137 }
138 
err(void)139 static int err(void)
140 {
141   fprintf(stderr, "something unexpected went wrong - bailing out!\n");
142   exit(2);
143 }
144 
usage(const char * msg)145 static void usage(const char *msg)
146 {
147   if(msg)
148     fprintf(stderr, "%s\n", msg);
149   fprintf(stderr,
150     "usage: [options] url\n"
151     "  pause downloads with following options:\n"
152     "  -V http_version (http/1.1, h2, h3) http version to use\n"
153   );
154 }
155 
156 struct handle
157 {
158   int idx;
159   int paused;
160   int resumed;
161   int errored;
162   int fail_write;
163   CURL *h;
164 };
165 
cb(void * data,size_t size,size_t nmemb,void * clientp)166 static size_t cb(void *data, size_t size, size_t nmemb, void *clientp)
167 {
168   size_t realsize = size * nmemb;
169   struct handle *handle = (struct handle *) clientp;
170   curl_off_t totalsize;
171 
172   (void)data;
173   if(curl_easy_getinfo(handle->h, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
174                        &totalsize) == CURLE_OK)
175     fprintf(stderr, "INFO: [%d] write, Content-Length %"CURL_FORMAT_CURL_OFF_T
176             "\n", handle->idx, totalsize);
177 
178   if(!handle->resumed) {
179     ++handle->paused;
180     fprintf(stderr, "INFO: [%d] write, PAUSING %d time on %lu bytes\n",
181             handle->idx, handle->paused, (long)realsize);
182     assert(handle->paused == 1);
183     return CURL_WRITEFUNC_PAUSE;
184   }
185   if(handle->fail_write) {
186     ++handle->errored;
187     fprintf(stderr, "INFO: [%d] FAIL write of %lu bytes, %d time\n",
188             handle->idx, (long)realsize, handle->errored);
189     return CURL_WRITEFUNC_ERROR;
190   }
191   fprintf(stderr, "INFO: [%d] write, accepting %lu bytes\n",
192           handle->idx, (long)realsize);
193   return realsize;
194 }
195 
main(int argc,char * argv[])196 int main(int argc, char *argv[])
197 {
198   struct handle handles[HANDLECOUNT];
199   CURLM *multi_handle;
200   int i, still_running = 1, msgs_left, numfds;
201   CURLMsg *msg;
202   int rounds = 0;
203   int rc = 0;
204   CURLU *cu;
205   struct curl_slist *resolve = NULL;
206   char resolve_buf[1024];
207   char *url, *host = NULL, *port = NULL;
208   int all_paused = 0;
209   int resume_round = -1;
210   int http_version = CURL_HTTP_VERSION_2_0;
211   int ch;
212 
213   while((ch = getopt(argc, argv, "hV:")) != -1) {
214     switch(ch) {
215     case 'h':
216       usage(NULL);
217       return 2;
218     case 'V': {
219       if(!strcmp("http/1.1", optarg))
220         http_version = CURL_HTTP_VERSION_1_1;
221       else if(!strcmp("h2", optarg))
222         http_version = CURL_HTTP_VERSION_2_0;
223       else if(!strcmp("h3", optarg))
224         http_version = CURL_HTTP_VERSION_3ONLY;
225       else {
226         usage("invalid http version");
227         return 1;
228       }
229       break;
230     }
231     default:
232      usage("invalid option");
233      return 1;
234     }
235   }
236   argc -= optind;
237   argv += optind;
238 
239   if(argc != 1) {
240     fprintf(stderr, "ERROR: need URL as argument\n");
241     return 2;
242   }
243   url = argv[0];
244 
245   curl_global_init(CURL_GLOBAL_DEFAULT);
246   curl_global_trace("ids,time,http/2,http/3");
247 
248   cu = curl_url();
249   if(!cu) {
250     fprintf(stderr, "out of memory\n");
251     exit(1);
252   }
253   if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
254     fprintf(stderr, "not a URL: '%s'\n", url);
255     exit(1);
256   }
257   if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
258     fprintf(stderr, "could not get host of '%s'\n", url);
259     exit(1);
260   }
261   if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
262     fprintf(stderr, "could not get port of '%s'\n", url);
263     exit(1);
264   }
265   memset(&resolve, 0, sizeof(resolve));
266   curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1,
267                  "%s:%s:127.0.0.1", host, port);
268   resolve = curl_slist_append(resolve, resolve_buf);
269 
270   for(i = 0; i<HANDLECOUNT; i++) {
271     handles[i].idx = i;
272     handles[i].paused = 0;
273     handles[i].resumed = 0;
274     handles[i].errored = 0;
275     handles[i].fail_write = 1;
276     handles[i].h = curl_easy_init();
277     if(!handles[i].h ||
278       curl_easy_setopt(handles[i].h, CURLOPT_WRITEFUNCTION, cb) != CURLE_OK ||
279       curl_easy_setopt(handles[i].h, CURLOPT_WRITEDATA, &handles[i])
280         != CURLE_OK ||
281       curl_easy_setopt(handles[i].h, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK ||
282       curl_easy_setopt(handles[i].h, CURLOPT_VERBOSE, 1L) != CURLE_OK ||
283       curl_easy_setopt(handles[i].h, CURLOPT_DEBUGFUNCTION, debug_cb)
284         != CURLE_OK ||
285       curl_easy_setopt(handles[i].h, CURLOPT_SSL_VERIFYPEER, 0L) != CURLE_OK ||
286       curl_easy_setopt(handles[i].h, CURLOPT_RESOLVE, resolve) != CURLE_OK ||
287       curl_easy_setopt(handles[i].h, CURLOPT_PIPEWAIT, 1L) ||
288       curl_easy_setopt(handles[i].h, CURLOPT_URL, url) != CURLE_OK) {
289       err();
290     }
291     curl_easy_setopt(handles[i].h, CURLOPT_HTTP_VERSION, (long)http_version);
292   }
293 
294   multi_handle = curl_multi_init();
295   if(!multi_handle)
296     err();
297 
298   for(i = 0; i<HANDLECOUNT; i++) {
299     if(curl_multi_add_handle(multi_handle, handles[i].h) != CURLM_OK)
300       err();
301   }
302 
303   for(rounds = 0;; rounds++) {
304     fprintf(stderr, "INFO: multi_perform round %d\n", rounds);
305     if(curl_multi_perform(multi_handle, &still_running) != CURLM_OK)
306       err();
307 
308     if(!still_running) {
309       int as_expected = 1;
310       fprintf(stderr, "INFO: no more handles running\n");
311       for(i = 0; i<HANDLECOUNT; i++) {
312         if(!handles[i].paused) {
313           fprintf(stderr, "ERROR: [%d] NOT PAUSED\n", i);
314           as_expected = 0;
315         }
316         else if(handles[i].paused != 1) {
317           fprintf(stderr, "ERROR: [%d] PAUSED %d times!\n",
318                   i, handles[i].paused);
319           as_expected = 0;
320         }
321         else if(!handles[i].resumed) {
322           fprintf(stderr, "ERROR: [%d] NOT resumed!\n", i);
323           as_expected = 0;
324         }
325         else if(handles[i].errored != 1) {
326           fprintf(stderr, "ERROR: [%d] NOT errored once, %d instead!\n",
327                   i, handles[i].errored);
328           as_expected = 0;
329         }
330       }
331       if(!as_expected) {
332         fprintf(stderr, "ERROR: handles not in expected state "
333                 "after %d rounds\n", rounds);
334         rc = 1;
335       }
336       break;
337     }
338 
339     if(curl_multi_poll(multi_handle, NULL, 0, 100, &numfds) != CURLM_OK)
340       err();
341 
342     while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
343       if(msg->msg == CURLMSG_DONE) {
344         for(i = 0; i<HANDLECOUNT; i++) {
345           if(msg->easy_handle == handles[i].h) {
346             if(handles[i].paused != 1 || !handles[i].resumed) {
347               fprintf(stderr, "ERROR: [%d] done, pauses=%d, resumed=%d, "
348                       "result %d - wtf?\n", i, handles[i].paused,
349                       handles[i].resumed, msg->data.result);
350               rc = 1;
351               goto out;
352             }
353           }
354         }
355       }
356     }
357 
358     /* Successfully paused? */
359     if(!all_paused) {
360       for(i = 0; i<HANDLECOUNT; i++) {
361         if(!handles[i].paused) {
362           break;
363         }
364       }
365       all_paused = (i == HANDLECOUNT);
366       if(all_paused) {
367         fprintf(stderr, "INFO: all transfers paused\n");
368         /* give transfer some rounds to mess things up */
369         resume_round = rounds + 2;
370       }
371     }
372     if(resume_round > 0 && rounds == resume_round) {
373       /* time to resume */
374       for(i = 0; i<HANDLECOUNT; i++) {
375         fprintf(stderr, "INFO: [%d] resumed\n", i);
376         handles[i].resumed = 1;
377         curl_easy_pause(handles[i].h, CURLPAUSE_CONT);
378       }
379     }
380   }
381 
382 out:
383   for(i = 0; i<HANDLECOUNT; i++) {
384     curl_multi_remove_handle(multi_handle, handles[i].h);
385     curl_easy_cleanup(handles[i].h);
386   }
387 
388 
389   curl_slist_free_all(resolve);
390   curl_free(host);
391   curl_free(port);
392   curl_url_cleanup(cu);
393   curl_multi_cleanup(multi_handle);
394   curl_global_cleanup();
395 
396   return rc;
397 }
398