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 Upgrade test
26  * </DESC>
27  */
28 #include <curl/curl.h>
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 /* #include <error.h> */
33 #include <errno.h>
34 
log_line_start(FILE * log,const char * idsbuf,curl_infotype type)35 static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
36 {
37   /*
38    * This is the trace look that is similar to what libcurl makes on its
39    * own.
40    */
41   static const char * const s_infotype[] = {
42     "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
43   };
44   if(idsbuf && *idsbuf)
45     fprintf(log, "%s%s", idsbuf, s_infotype[type]);
46   else
47     fputs(s_infotype[type], log);
48 }
49 
50 #define TRC_IDS_FORMAT_IDS_1  "[%" CURL_FORMAT_CURL_OFF_T "-x] "
51 #define TRC_IDS_FORMAT_IDS_2  "[%" CURL_FORMAT_CURL_OFF_T "-%" \
52                                    CURL_FORMAT_CURL_OFF_T "] "
53 /*
54 ** callback for CURLOPT_DEBUGFUNCTION
55 */
debug_cb(CURL * handle,curl_infotype type,char * data,size_t size,void * userdata)56 static int debug_cb(CURL *handle, curl_infotype type,
57                     char *data, size_t size,
58                     void *userdata)
59 {
60   FILE *output = stderr;
61   static int newl = 0;
62   static int traced_data = 0;
63   char idsbuf[60];
64   curl_off_t xfer_id, conn_id;
65 
66   (void)handle; /* not used */
67   (void)userdata;
68 
69   if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
70     if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
71         conn_id >= 0) {
72       curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2, xfer_id,
73                      conn_id);
74     }
75     else {
76       curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
77     }
78   }
79   else
80     idsbuf[0] = 0;
81 
82   switch(type) {
83   case CURLINFO_HEADER_OUT:
84     if(size > 0) {
85       size_t st = 0;
86       size_t i;
87       for(i = 0; i < size - 1; i++) {
88         if(data[i] == '\n') { /* LF */
89           if(!newl) {
90             log_line_start(output, idsbuf, type);
91           }
92           (void)fwrite(data + st, i - st + 1, 1, output);
93           st = i + 1;
94           newl = 0;
95         }
96       }
97       if(!newl)
98         log_line_start(output, idsbuf, type);
99       (void)fwrite(data + st, i - st + 1, 1, output);
100     }
101     newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
102     traced_data = 0;
103     break;
104   case CURLINFO_TEXT:
105   case CURLINFO_HEADER_IN:
106     if(!newl)
107       log_line_start(output, idsbuf, type);
108     (void)fwrite(data, size, 1, output);
109     newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
110     traced_data = 0;
111     break;
112   case CURLINFO_DATA_OUT:
113   case CURLINFO_DATA_IN:
114   case CURLINFO_SSL_DATA_IN:
115   case CURLINFO_SSL_DATA_OUT:
116     if(!traced_data) {
117       if(!newl)
118         log_line_start(output, idsbuf, type);
119       fprintf(output, "[%ld bytes data]\n", (long)size);
120       newl = 0;
121       traced_data = 1;
122     }
123     break;
124   default: /* nada */
125     newl = 0;
126     traced_data = 1;
127     break;
128   }
129 
130   return 0;
131 }
132 
write_cb(char * ptr,size_t size,size_t nmemb,void * opaque)133 static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *opaque)
134 {
135   (void)ptr;
136   (void)opaque;
137   return size * nmemb;
138 }
139 
main(int argc,char * argv[])140 int main(int argc, char *argv[])
141 {
142   const char *url;
143   CURLM *multi;
144   CURL *easy;
145   CURLMcode mc;
146   int running_handles = 0, start_count, numfds;
147   CURLMsg *msg;
148   int msgs_in_queue;
149   char range[128];
150 
151   if(argc != 2) {
152     fprintf(stderr, "%s URL\n", argv[0]);
153     exit(2);
154   }
155 
156   url = argv[1];
157   multi = curl_multi_init();
158   if(!multi) {
159     fprintf(stderr, "curl_multi_init failed\n");
160     exit(1);
161   }
162 
163   start_count = 200;
164   do {
165     if(start_count) {
166       easy = curl_easy_init();
167       if(!easy) {
168         fprintf(stderr, "curl_easy_init failed\n");
169         exit(1);
170       }
171       curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
172       curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, debug_cb);
173       curl_easy_setopt(easy, CURLOPT_URL, url);
174       curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1L);
175       curl_easy_setopt(easy, CURLOPT_AUTOREFERER, 1L);
176       curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
177       curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
178       curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb);
179       curl_easy_setopt(easy, CURLOPT_WRITEDATA, NULL);
180       curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
181       curl_msnprintf(range, sizeof(range),
182                      "%" CURL_FORMAT_CURL_OFF_TU "-"
183                      "%" CURL_FORMAT_CURL_OFF_TU,
184                      (curl_off_t)0,
185                      (curl_off_t)16384);
186       curl_easy_setopt(easy, CURLOPT_RANGE, range);
187 
188       mc = curl_multi_add_handle(multi, easy);
189       if(mc != CURLM_OK) {
190         fprintf(stderr, "curl_multi_add_handle: %s\n",
191                curl_multi_strerror(mc));
192         exit(1);
193       }
194       --start_count;
195     }
196 
197     mc = curl_multi_perform(multi, &running_handles);
198     if(mc != CURLM_OK) {
199       fprintf(stderr, "curl_multi_perform: %s\n",
200              curl_multi_strerror(mc));
201       exit(1);
202     }
203 
204     if(running_handles) {
205       mc = curl_multi_poll(multi, NULL, 0, 1000000, &numfds);
206       if(mc != CURLM_OK) {
207         fprintf(stderr, "curl_multi_poll: %s\n",
208                curl_multi_strerror(mc));
209         exit(1);
210       }
211     }
212 
213     /* Check for finished handles and remove. */
214     /* !checksrc! disable EQUALSNULL 1 */
215     while((msg = curl_multi_info_read(multi, &msgs_in_queue)) != NULL) {
216       if(msg->msg == CURLMSG_DONE) {
217         long status = 0;
218         curl_off_t xfer_id;
219         curl_easy_getinfo(msg->easy_handle, CURLINFO_XFER_ID, &xfer_id);
220         curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &status);
221         if(msg->data.result == CURLE_SEND_ERROR ||
222             msg->data.result == CURLE_RECV_ERROR) {
223           /* We get these if the server had a GOAWAY in transit on
224            * re-using a connection */
225         }
226         else if(msg->data.result) {
227           fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
228                   ": failed with %d\n", xfer_id, msg->data.result);
229           exit(1);
230         }
231         else if(status != 206) {
232           fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
233                   ": wrong http status %ld (expected 206)\n", xfer_id, status);
234           exit(1);
235         }
236         curl_multi_remove_handle(multi, msg->easy_handle);
237         curl_easy_cleanup(msg->easy_handle);
238         fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T" retiring "
239                 "(%d now running)\n", xfer_id, running_handles);
240       }
241     }
242 
243     fprintf(stderr, "running_handles=%d, yet_to_start=%d\n",
244             running_handles, start_count);
245 
246   } while(running_handles > 0 || start_count);
247 
248   fprintf(stderr, "exiting\n");
249   exit(EXIT_SUCCESS);
250 }
251