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