master
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 * Multiplexed HTTP/2 uploads over a single connection
26 * </DESC>
27 */
28#ifdef _MSC_VER
29#ifndef _CRT_SECURE_NO_WARNINGS
30#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), localtime(),
31 strerror() */
32#endif
33#endif
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <fcntl.h>
39#include <sys/stat.h>
40#include <errno.h>
42/* somewhat Unix-specific */
43#ifndef _MSC_VER
44#include <sys/time.h>
45#include <unistd.h>
46#endif
48#include <curl/curl.h>
50#ifndef CURLPIPE_MULTIPLEX
51/* This little trick makes sure that we do not enable pipelining for libcurls
52 old enough to not have this symbol. It is _not_ defined to zero in a recent
53 libcurl header. */
54#define CURLPIPE_MULTIPLEX 0L
55#endif
57#ifdef _WIN32
58#undef stat
59#define stat _stati64
60#undef fstat
61#define fstat _fstati64
62#define fileno _fileno
63#endif
65#if defined(_MSC_VER) && (_MSC_VER < 1900)
66#define snprintf _snprintf
67#endif
69#ifdef _MSC_VER
70static int gettimeofday(struct timeval *tp, void *tzp)
71{
72 (void)tzp;
73 if(tp) {
74 union {
75 CURL_TYPEOF_CURL_OFF_T ns100; /* time since 1 Jan 1601 in 100ns units */
76 FILETIME ft;
77 } now;
78 GetSystemTimeAsFileTime(&now.ft);
79 tp->tv_usec = (long)((now.ns100 / 10) % 1000000);
80 /* subtract offset between 1601-01-01 and 1970-01-01 in 100ns units */
81 tp->tv_sec = (long)((now.ns100 - 116444736000000000) / 10000000);
82 }
83 return 0;
84}
85#endif
87struct input {
88 FILE *in;
89 FILE *out;
90 size_t bytes_read; /* count up */
91 CURL *curl;
92 int num;
93};
95static void dump(const char *text, int num, const unsigned char *ptr,
96 size_t size, char nohex)
97{
98 size_t i;
99 size_t c;
100 unsigned int width = 0x10;
102 if(nohex)
103 /* without the hex output, we can fit more on screen */
104 width = 0x40;
106 fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
107 num, text, (unsigned long)size, (unsigned long)size);
109 for(i = 0; i < size; i += width) {
111 fprintf(stderr, "%4.4lx: ", (unsigned long)i);
113 if(!nohex) {
114 /* hex not disabled, show it */
115 for(c = 0; c < width; c++)
116 if(i + c < size)
117 fprintf(stderr, "%02x ", ptr[i + c]);
118 else
119 fputs(" ", stderr);
120 }
122 for(c = 0; (c < width) && (i + c < size); c++) {
123 /* check for 0D0A; if found, skip past and start a new line of output */
124 if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
125 ptr[i + c + 1] == 0x0A) {
126 i += (c + 2 - width);
127 break;
128 }
129 fprintf(stderr, "%c",
130 (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
131 /* check again for 0D0A, to avoid an extra \n if it is at width */
132 if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
133 ptr[i + c + 2] == 0x0A) {
134 i += (c + 3 - width);
135 break;
136 }
137 }
138 fputc('\n', stderr); /* newline */
139 }
140}
142static int my_trace(CURL *curl, curl_infotype type,
143 char *data, size_t size, void *userp)
144{
145 char timebuf[60];
146 const char *text;
147 struct input *i = (struct input *)userp;
148 int num = i->num;
149 static time_t epoch_offset;
150 static int known_offset;
151 struct timeval tv;
152 time_t secs;
153 struct tm *now;
154 (void)curl;
156 gettimeofday(&tv, NULL);
157 if(!known_offset) {
158 epoch_offset = time(NULL) - tv.tv_sec;
159 known_offset = 1;
160 }
161 secs = epoch_offset + tv.tv_sec;
162 now = localtime(&secs); /* not thread-safe but we do not care */
163 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
164 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
166 switch(type) {
167 case CURLINFO_TEXT:
168 fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
169 return 0;
170 case CURLINFO_HEADER_OUT:
171 text = "=> Send header";
172 break;
173 case CURLINFO_DATA_OUT:
174 text = "=> Send data";
175 break;
176 case CURLINFO_SSL_DATA_OUT:
177 text = "=> Send SSL data";
178 break;
179 case CURLINFO_HEADER_IN:
180 text = "<= Recv header";
181 break;
182 case CURLINFO_DATA_IN:
183 text = "<= Recv data";
184 break;
185 case CURLINFO_SSL_DATA_IN:
186 text = "<= Recv SSL data";
187 break;
188 default: /* in case a new one is introduced to shock us */
189 return 0;
190 }
192 dump(text, num, (const unsigned char *)data, size, 1);
193 return 0;
194}
196static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
197{
198 struct input *i = userp;
199 size_t retcode = fread(ptr, size, nmemb, i->in);
200 i->bytes_read += retcode;
201 return retcode;
202}
204static int setup(struct input *t, int num, const char *upload)
205{
206 char upload_url[256];
207 char filename[128];
208 struct stat file_info;
209 curl_off_t uploadsize;
210 CURL *curl;
212 curl = t->curl = NULL;
214 t->num = num;
215 snprintf(filename, sizeof(filename), "dl-%d", num);
216 t->out = fopen(filename, "wb");
217 if(!t->out) {
218 fprintf(stderr, "error: could not open file %s for writing: %s\n", upload,
219 strerror(errno));
220 return 1;
221 }
223 snprintf(upload_url, sizeof(upload_url), "https://localhost:8443/upload-%d",
224 num);
226 t->in = fopen(upload, "rb");
227 if(!t->in) {
228 fprintf(stderr, "error: could not open file %s for reading: %s\n", upload,
229 strerror(errno));
230 fclose(t->out);
231 t->out = NULL;
232 return 1;
233 }
235 if(fstat(fileno(t->in), &file_info)) {
236 fprintf(stderr, "error: could not stat file %s: %s\n", upload,
237 strerror(errno));
238 fclose(t->out);
239 t->out = NULL;
240 return 1;
241 }
243 uploadsize = file_info.st_size;
245 curl = t->curl = curl_easy_init();
246 if(curl) {
248 /* write to this file */
249 curl_easy_setopt(curl, CURLOPT_WRITEDATA, t->out);
251 /* we want to use our own read function */
252 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
253 /* read from this file */
254 curl_easy_setopt(curl, CURLOPT_READDATA, t);
255 /* provide the size of the upload */
256 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, uploadsize);
258 /* send in the URL to store the upload as */
259 curl_easy_setopt(curl, CURLOPT_URL, upload_url);
261 /* upload please */
262 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
264 /* please be verbose */
265 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
266 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
267 curl_easy_setopt(curl, CURLOPT_DEBUGDATA, t);
269 /* HTTP/2 please */
270 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
272 /* we use a self-signed test server, skip verification during debugging */
273 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
274 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
276#if CURLPIPE_MULTIPLEX > 0
277 /* wait for pipe connection to confirm */
278 curl_easy_setopt(curl, CURLOPT_PIPEWAIT, 1L);
279#endif
280 }
281 return 0;
282}
284/*
285 * Upload all files over HTTP/2, using the same physical connection!
286 */
287int main(int argc, const char *argv[])
288{
289 CURLcode result;
290 struct input *trans;
291 CURLM *multi = NULL;
292 int i;
293 const char *filename = "index.html";
294 int still_running = 0; /* keep number of running handles */
295 int num_transfers;
297 if(argc > 1) {
298 /* if given a number, do that many transfers */
299 num_transfers = atoi(argv[1]);
300 if((num_transfers < 1) || (num_transfers > 1000))
301 num_transfers = 3; /* a suitable low default */
303 if(argc > 2)
304 /* if given a filename, upload this! */
305 filename = argv[2];
306 }
307 else
308 num_transfers = 3; /* a suitable low default */
310 result = curl_global_init(CURL_GLOBAL_ALL);
311 if(result != CURLE_OK)
312 return (int)result;
314 trans = calloc(num_transfers, sizeof(*trans));
315 if(!trans) {
316 fprintf(stderr, "error allocating transfer structs\n");
317 goto error;
318 }
320 /* init a multi stack */
321 multi = curl_multi_init();
322 if(!multi)
323 goto error;
325 for(i = 0; i < num_transfers; i++) {
326 if(setup(&trans[i], i, filename)) {
327 goto error;
328 }
330 /* add the individual transfer */
331 curl_multi_add_handle(multi, trans[i].curl);
332 }
334 curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
336 /* We do HTTP/2 so let's stick to one connection per host */
337 curl_multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
339 do {
340 CURLMcode mresult = curl_multi_perform(multi, &still_running);
342 if(still_running)
343 /* wait for activity, timeout or "nothing" */
344 mresult = curl_multi_poll(multi, NULL, 0, 1000, NULL);
346 if(mresult)
347 break;
349 } while(still_running);
351error:
353 if(multi) {
354 for(i = 0; i < num_transfers; i++) {
355 curl_multi_remove_handle(multi, trans[i].curl);
356 curl_easy_cleanup(trans[i].curl);
358 if(trans[i].in)
359 fclose(trans[i].in);
360 if(trans[i].out)
361 fclose(trans[i].out);
362 }
363 curl_multi_cleanup(multi);
364 }
366 free(trans);
368 curl_global_cleanup();
370 return 0;
371}
Diagnostics4 git calls · 30 ms
CallStatusSizeTime030 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=22 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2003.7 KiB · hit22 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3812 git.response.cache=HIT git.response.duration_ms=22 git.fetch.wants=9acd7198f1c4dfd066acf8e32e06f736759fab5c git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3812 git.pkt.payload.max=3791 git.sideband.band1.bytes=3790 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e265fbea59df63
- Request
- ebc6c15f…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format