Artifactspublic-v0

curl

master
ply4 git calls576 ms292 KiB2 cachedprotocol v2
459 lines13 KiBRaw
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 * multi socket interface with libevent 2
26 * </DESC>
27 */
28/* Example application source code using the multi socket interface to
29 * download many files at once.
30 *
31 * Written by Jeff Pohlmeyer
32 *
33 * Requires libevent version 2 and a (POSIX?) system that has mkfifo().
34 *
35 * This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
36 * sample programs.
37 *
38 * When running, the program creates the named pipe "hiper.fifo"
39 *
40 * Whenever there is input into the fifo, the program reads the input as a list
41 * of URL's and creates some new easy handles to fetch each URL via the
42 * curl_multi "hiper" API.
43 *
44 * Thus, you can try a single URL:
45 * % echo http://www.example.com > hiper.fifo
46 *
47 * Or a whole bunch of them:
48 * % cat my-url-list > hiper.fifo
49 *
50 * The fifo buffer is handled almost instantly, so you can even add more URL's
51 * while the previous requests are still being downloaded.
52 *
53 * Note:
54 * For the sake of simplicity, URL length is limited to 1023 chars.
55 *
56 * This is purely a demo app, all retrieved data is discarded by
57 * the write callback.
58 */
59#include <errno.h>
60#include <fcntl.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <sys/cdefs.h>
65#include <sys/poll.h>
66#include <sys/stat.h>
67#include <sys/time.h>
68#include <time.h>
69#include <unistd.h>
71#include <curl/curl.h>
73#include <event2/event.h>
74#include <event2/event_struct.h>
76#define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
78/* Global information, common to all connections */
79struct GlobalInfo {
80 struct event_base *evbase;
81 struct event fifo_event;
82 struct event timer_event;
83 CURLM *multi;
84 int still_running;
85 FILE *input;
86 int stopped;
87};
89/* Information associated with a specific easy handle */
90struct ConnInfo {
91 CURL *curl;
92 char *url;
93 struct GlobalInfo *global;
94 char error[CURL_ERROR_SIZE];
95};
97/* Information associated with a specific socket */
98struct SockInfo {
99 curl_socket_t sockfd;
100 CURL *curl;
101 int action;
102 long timeout;
103 struct event ev;
104 struct GlobalInfo *global;
105};
107/* Die if we get a bad CURLMcode somewhere */
108static void mcode_or_die(const char *where, CURLMcode code)
110 if(CURLM_OK != code) {
111 const char *s;
112 switch(code) {
113 case CURLM_BAD_HANDLE:
114 s = "CURLM_BAD_HANDLE";
115 break;
116 case CURLM_BAD_EASY_HANDLE:
117 s = "CURLM_BAD_EASY_HANDLE";
118 break;
119 case CURLM_OUT_OF_MEMORY:
120 s = "CURLM_OUT_OF_MEMORY";
121 break;
122 case CURLM_INTERNAL_ERROR:
123 s = "CURLM_INTERNAL_ERROR";
124 break;
125 case CURLM_UNKNOWN_OPTION:
126 s = "CURLM_UNKNOWN_OPTION";
127 break;
128 case CURLM_LAST:
129 s = "CURLM_LAST";
130 break;
131 default:
132 s = "CURLM_unknown";
133 break;
134 case CURLM_BAD_SOCKET:
135 s = "CURLM_BAD_SOCKET";
136 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
137 /* ignore this error */
138 return;
139 }
140 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
141 exit(code);
142 }
145/* Update the event timer after curl_multi library calls */
146static int multi_timer_cb(CURLM *multi, long timeout_ms, struct GlobalInfo *g)
148 struct timeval timeout;
149 (void)multi;
151 timeout.tv_sec = timeout_ms / 1000;
152 timeout.tv_usec = (timeout_ms % 1000) * 1000;
153 fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
155 /*
156 * if timeout_ms is -1, delete the timer
157 *
158 * For all other values of timeout_ms, this should set or *update* the timer
159 * to the new value
160 */
161 if(timeout_ms == -1)
162 evtimer_del(&g->timer_event);
163 else /* includes timeout zero */
164 evtimer_add(&g->timer_event, &timeout);
165 return 0;
168/* Check for completed transfers, and remove their easy handles */
169static void check_multi_info(struct GlobalInfo *g)
171 char *eff_url;
172 CURLMsg *msg;
173 int msgs_left;
174 struct ConnInfo *conn;
176 fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
177 while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
178 if(msg->msg == CURLMSG_DONE) {
179 CURL *curl = msg->easy_handle;
180 CURLcode result = msg->data.result;
181 curl_easy_getinfo(curl, CURLINFO_PRIVATE, &conn);
182 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &eff_url);
183 fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, result, conn->error);
184 curl_multi_remove_handle(g->multi, curl);
185 free(conn->url);
186 curl_easy_cleanup(curl);
187 free(conn);
188 }
189 }
190 if(g->still_running == 0 && g->stopped)
191 event_base_loopbreak(g->evbase);
194/* Called by libevent when we get action on a multi socket */
195static void event_cb(int fd, short kind, void *userp)
197 struct GlobalInfo *g = (struct GlobalInfo *)userp;
198 CURLMcode mresult;
200 int action = ((kind & EV_READ) ? CURL_CSELECT_IN : 0) |
201 ((kind & EV_WRITE) ? CURL_CSELECT_OUT : 0);
203 mresult = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
204 mcode_or_die("event_cb: curl_multi_socket_action", mresult);
206 check_multi_info(g);
207 if(g->still_running <= 0) {
208 fprintf(MSG_OUT, "last transfer done, kill timeout\n");
209 if(evtimer_pending(&g->timer_event, NULL)) {
210 evtimer_del(&g->timer_event);
211 }
212 }
215/* Called by libevent when our timeout expires */
216static void timer_cb(int fd, short kind, void *userp)
218 struct GlobalInfo *g = (struct GlobalInfo *)userp;
219 CURLMcode mresult;
220 (void)fd;
221 (void)kind;
223 mresult = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
224 &g->still_running);
225 mcode_or_die("timer_cb: curl_multi_socket_action", mresult);
226 check_multi_info(g);
229/* Clean up the SockInfo structure */
230static void remsock(struct SockInfo *f)
232 if(f) {
233 if(event_initialized(&f->ev)) {
234 event_del(&f->ev);
235 }
236 free(f);
237 }
240/* Assign information to a SockInfo structure */
241static void setsock(struct SockInfo *f, curl_socket_t s, CURL *e, int act,
242 struct GlobalInfo *g)
244 int kind = ((act & CURL_POLL_IN) ? EV_READ : 0) |
245 ((act & CURL_POLL_OUT) ? EV_WRITE : 0) | EV_PERSIST;
247 f->sockfd = s;
248 f->action = act;
249 f->curl = e;
250 if(event_initialized(&f->ev)) {
251 event_del(&f->ev);
252 }
253 event_assign(&f->ev, g->evbase, f->sockfd, (short)kind, event_cb, g);
254 event_add(&f->ev, NULL);
257/* Initialize a new SockInfo structure */
258static void addsock(curl_socket_t s, CURL *curl, int action,
259 struct GlobalInfo *g)
261 struct SockInfo *fdp = calloc(1, sizeof(struct SockInfo));
263 fdp->global = g;
264 setsock(fdp, s, curl, action, g);
265 curl_multi_assign(g->multi, s, fdp);
268/* CURLMOPT_SOCKETFUNCTION */
269static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
271 struct GlobalInfo *g = (struct GlobalInfo *)cbp;
272 struct SockInfo *fdp = (struct SockInfo *)sockp;
273 static const char * const whatstr[] = {
274 "none", "IN", "OUT", "INOUT", "REMOVE"
275 };
277 fprintf(MSG_OUT, "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
278 if(what == CURL_POLL_REMOVE) {
279 fprintf(MSG_OUT, "\n");
280 remsock(fdp);
281 }
282 else {
283 if(!fdp) {
284 fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
285 addsock(s, e, what, g);
286 }
287 else {
288 fprintf(MSG_OUT, "Changing action from %s to %s\n",
289 whatstr[fdp->action], whatstr[what]);
290 setsock(fdp, s, e, what, g);
291 }
292 }
293 return 0;
296/* CURLOPT_WRITEFUNCTION */
297static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *data)
299 (void)ptr;
300 (void)data;
301 return size * nmemb;
304/* CURLOPT_PROGRESSFUNCTION */
305static int xferinfo_cb(void *p, curl_off_t dltotal, curl_off_t dlnow,
306 curl_off_t ult, curl_off_t uln)
308 struct ConnInfo *conn = (struct ConnInfo *)p;
309 (void)ult;
310 (void)uln;
312 fprintf(MSG_OUT, "Progress: %s (%" CURL_FORMAT_CURL_OFF_T "/"
313 "%" CURL_FORMAT_CURL_OFF_T ")\n", conn->url, dlnow, dltotal);
314 return 0;
317/* Create a new easy handle, and add it to the global curl_multi */
318static void new_conn(const char *url, struct GlobalInfo *g)
320 struct ConnInfo *conn;
321 CURLMcode mresult;
323 conn = calloc(1, sizeof(*conn));
324 conn->error[0] = '\0';
326 conn->curl = curl_easy_init();
327 if(!conn->curl) {
328 fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
329 exit(2);
330 }
331 conn->global = g;
332 conn->url = strdup(url);
333 curl_easy_setopt(conn->curl, CURLOPT_URL, conn->url);
334 curl_easy_setopt(conn->curl, CURLOPT_WRITEFUNCTION, write_cb);
335 curl_easy_setopt(conn->curl, CURLOPT_WRITEDATA, conn);
336 curl_easy_setopt(conn->curl, CURLOPT_VERBOSE, 1L);
337 curl_easy_setopt(conn->curl, CURLOPT_ERRORBUFFER, conn->error);
338 curl_easy_setopt(conn->curl, CURLOPT_PRIVATE, conn);
339 curl_easy_setopt(conn->curl, CURLOPT_NOPROGRESS, 0L);
340 curl_easy_setopt(conn->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
341 curl_easy_setopt(conn->curl, CURLOPT_PROGRESSDATA, conn);
342 curl_easy_setopt(conn->curl, CURLOPT_FOLLOWLOCATION, 1L);
343 fprintf(MSG_OUT, "Adding easy %p to multi %p (%s)\n",
344 conn->curl, g->multi, url);
345 mresult = curl_multi_add_handle(g->multi, conn->curl);
346 mcode_or_die("new_conn: curl_multi_add_handle", mresult);
348 /* note that the add_handle() sets a time-out to trigger soon so that
349 the necessary socket_action() gets called */
352/* This gets called whenever data is received from the fifo */
353static void fifo_cb(int fd, short event, void *arg)
355 char s[1024];
356 long int rv = 0;
357 int n = 0;
358 struct GlobalInfo *g = (struct GlobalInfo *)arg;
359 (void)fd;
360 (void)event;
362 do {
363 s[0] = '\0';
364 rv = fscanf(g->input, "%1023s%n", s, &n);
365 s[n] = '\0';
366 if(n && s[0]) {
367 if(!strcmp(s, "stop")) {
368 g->stopped = 1;
369 if(g->still_running == 0)
370 event_base_loopbreak(g->evbase);
371 }
372 else
373 new_conn(s, arg); /* if we read a URL, go get it! */
374 }
375 else
376 break;
377 } while(rv != EOF);
380/* Create a named pipe and tell libevent to monitor it */
381static const char fifo[] = "hiper.fifo";
382static int init_fifo(struct GlobalInfo *g)
384 struct stat st;
385 curl_socket_t sockfd;
387 fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
388 if(!lstat(fifo, &st)) {
389 if((st.st_mode & S_IFMT) == S_IFREG) {
390 errno = EEXIST;
391 perror("lstat");
392 return 1;
393 }
394 }
395 unlink(fifo);
396 if(mkfifo(fifo, 0600) == -1) {
397 perror("mkfifo");
398 return 1;
399 }
400 sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
401 if(sockfd == -1) {
402 perror("open");
403 return 1;
404 }
405 g->input = fdopen(sockfd, "r");
407 fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
408 event_assign(&g->fifo_event, g->evbase, sockfd, EV_READ | EV_PERSIST,
409 fifo_cb, g);
410 event_add(&g->fifo_event, NULL);
411 return 0;
414static void clean_fifo(struct GlobalInfo *g)
416 event_del(&g->fifo_event);
417 fclose(g->input);
418 unlink(fifo);
421int main(void)
423 CURLcode result;
424 struct GlobalInfo g;
426 result = curl_global_init(CURL_GLOBAL_ALL);
427 if(result != CURLE_OK)
428 return (int)result;
430 memset(&g, 0, sizeof(g));
431 g.evbase = event_base_new();
432 if(init_fifo(&g)) {
433 curl_global_cleanup();
434 return 1;
435 }
436 g.multi = curl_multi_init();
437 evtimer_assign(&g.timer_event, g.evbase, timer_cb, &g);
439 /* setup the generic multi interface options we want */
440 curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
441 curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
442 curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
443 curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
445 /* we do not call any curl_multi_socket*() function yet as we have no handles
446 added! */
448 event_base_dispatch(g.evbase);
450 /* this, of course, does not get called since the only way to stop this
451 program is via ctrl-C, but it is here to show how cleanup /would/ be
452 done. */
453 clean_fifo(&g);
454 event_del(&g.timer_event);
455 event_base_free(g.evbase);
456 curl_multi_cleanup(g.multi);
457 curl_global_cleanup();
458 return 0;
Diagnostics4 git calls · 576 ms
CallStatusSizeTime0576 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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=570
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
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=86
Open pack map
fetch2004.7 KiB570 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4775
git.response.cache=MISS
git.response.duration_ms=570
git.fetch.wants=cc27799012cd06f0ddd024167997750e94ea4309
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=4775
git.pkt.payload.max=4754
git.sideband.band1.bytes=4753
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
a5e1a4056eb15e44
Request
7140393d…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format