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 * multi socket interface with glib2
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 glib-2.x 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, adapted to use glib's g_io_channel in place of libevent.
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 * This is purely a demo app, all retrieved data is discarded by
54 * the write callback.
55 *
56 */
57#include <glib.h>
59#include <unistd.h>
60#include <fcntl.h>
61#include <stdlib.h>
62#include <stdio.h>
63#include <errno.h>
64#include <sys/stat.h>
66#include <curl/curl.h>
68#define MSG_OUT g_print /* Change to "g_error" to write to stderr */
69#define SHOW_VERBOSE 0L /* Set to non-zero for libcurl messages */
70#define SHOW_PROGRESS 0 /* Set to non-zero to enable progress callback */
72/* Global information, common to all connections */
73struct GlobalInfo {
74 CURLM *multi;
75 guint timer_event;
76 int still_running;
77};
79/* Information associated with a specific easy handle */
80struct ConnInfo {
81 CURL *curl;
82 char *url;
83 struct GlobalInfo *global;
84 char error[CURL_ERROR_SIZE];
85};
87/* Information associated with a specific socket */
88struct SockInfo {
89 curl_socket_t sockfd;
90 CURL *curl;
91 int action;
92 long timeout;
93 GIOChannel *ch;
94 guint ev;
95 struct GlobalInfo *global;
96};
98/* Die if we get a bad CURLMcode somewhere */
99static void mcode_or_die(const char *where, CURLMcode code)
100{
101 if(CURLM_OK != code) {
102 const char *s;
103 switch(code) {
104 case CURLM_BAD_HANDLE:
105 s = "CURLM_BAD_HANDLE";
106 break;
107 case CURLM_BAD_EASY_HANDLE:
108 s = "CURLM_BAD_EASY_HANDLE";
109 break;
110 case CURLM_OUT_OF_MEMORY:
111 s = "CURLM_OUT_OF_MEMORY";
112 break;
113 case CURLM_INTERNAL_ERROR:
114 s = "CURLM_INTERNAL_ERROR";
115 break;
116 case CURLM_BAD_SOCKET:
117 s = "CURLM_BAD_SOCKET";
118 break;
119 case CURLM_UNKNOWN_OPTION:
120 s = "CURLM_UNKNOWN_OPTION";
121 break;
122 default:
123 s = "CURLM_unknown";
124 break;
125 }
126 MSG_OUT("ERROR: %s returns %s\n", where, s);
127 exit(code);
128 }
129}
131/* Check for completed transfers, and remove their easy handles */
132static void check_multi_info(struct GlobalInfo *g)
133{
134 CURLMsg *msg;
135 int msgs_left;
137 MSG_OUT("REMAINING: %d\n", g->still_running);
138 while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
139 if(msg->msg == CURLMSG_DONE) {
140 CURL *curl = msg->easy_handle;
141 CURLcode result = msg->data.result;
142 char *eff_url;
143 struct ConnInfo *conn;
144 curl_easy_getinfo(curl, CURLINFO_PRIVATE, &conn);
145 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &eff_url);
146 MSG_OUT("DONE: %s => (%d) %s\n", eff_url, result, conn->error);
147 curl_multi_remove_handle(g->multi, curl);
148 free(conn->url);
149 curl_easy_cleanup(curl);
150 free(conn);
151 }
152 }
153}
155/* Called by glib when our timeout expires */
156static gboolean timer_cb(gpointer data)
157{
158 struct GlobalInfo *g = (struct GlobalInfo *)data;
159 CURLMcode mresult;
161 mresult = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
162 &g->still_running);
163 mcode_or_die("timer_cb: curl_multi_socket_action", mresult);
164 check_multi_info(g);
165 return FALSE;
166}
168/* Update the event timer after curl_multi library calls */
169static int update_timeout_cb(CURLM *multi, long timeout_ms, void *userp)
170{
171 struct timeval timeout;
172 struct GlobalInfo *g = (struct GlobalInfo *)userp;
173 timeout.tv_sec = timeout_ms / 1000;
174 timeout.tv_usec = (timeout_ms % 1000) * 1000;
176 MSG_OUT("*** update_timeout_cb %ld => %ld:%ld ***\n",
177 timeout_ms, timeout.tv_sec, timeout.tv_usec);
179 /*
180 * if timeout_ms is -1, delete the timer
181 *
182 * For other values of timeout_ms, this should set or *update* the timer to
183 * the new value
184 */
185 if(timeout_ms >= 0)
186 g->timer_event = g_timeout_add(timeout_ms, timer_cb, g);
187 return 0;
188}
190/* Called by glib when we get action on a multi socket */
191static gboolean event_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
192{
193 struct GlobalInfo *g = (struct GlobalInfo *)data;
194 CURLMcode mresult;
195 int fd = g_io_channel_unix_get_fd(ch);
197 int action = ((condition & G_IO_IN) ? CURL_CSELECT_IN : 0) |
198 ((condition & G_IO_OUT) ? CURL_CSELECT_OUT : 0);
200 mresult = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
201 mcode_or_die("event_cb: curl_multi_socket_action", mresult);
203 check_multi_info(g);
204 if(g->still_running) {
205 return TRUE;
206 }
207 else {
208 MSG_OUT("last transfer done, kill timeout\n");
209 if(g->timer_event) {
210 g_source_remove(g->timer_event);
211 }
212 return FALSE;
213 }
214}
216/* Clean up the SockInfo structure */
217static void remsock(struct SockInfo *f)
218{
219 if(!f) {
220 return;
221 }
222 if(f->ev) {
223 g_source_remove(f->ev);
224 }
225 g_free(f);
226}
228/* Assign information to a SockInfo structure */
229static void setsock(struct SockInfo *f, curl_socket_t s, CURL *e, int act,
230 struct GlobalInfo *g)
231{
232 GIOCondition kind = ((act & CURL_POLL_IN) ? G_IO_IN : 0) |
233 ((act & CURL_POLL_OUT) ? G_IO_OUT : 0);
235 f->sockfd = s;
236 f->action = act;
237 f->curl = e;
238 if(f->ev) {
239 g_source_remove(f->ev);
240 }
241 f->ev = g_io_add_watch(f->ch, kind, event_cb, g);
242}
244/* Initialize a new SockInfo structure */
245static void addsock(curl_socket_t s, CURL *curl, int action,
246 struct GlobalInfo *g)
247{
248 struct SockInfo *fdp = g_malloc0(sizeof(struct SockInfo));
250 fdp->global = g;
251 fdp->ch = g_io_channel_unix_new(s);
252 setsock(fdp, s, curl, action, g);
253 curl_multi_assign(g->multi, s, fdp);
254}
256/* CURLMOPT_SOCKETFUNCTION */
257static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
258{
259 struct GlobalInfo *g = (struct GlobalInfo *)cbp;
260 struct SockInfo *fdp = (struct SockInfo *)sockp;
261 static const char * const whatstr[] = {
262 "none", "IN", "OUT", "INOUT", "REMOVE"
263 };
265 MSG_OUT("socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
266 if(what == CURL_POLL_REMOVE) {
267 MSG_OUT("\n");
268 remsock(fdp);
269 }
270 else {
271 if(!fdp) {
272 MSG_OUT("Adding data: %s%s\n",
273 (what & CURL_POLL_IN) ? "READ" : "",
274 (what & CURL_POLL_OUT) ? "WRITE" : "");
275 addsock(s, e, what, g);
276 }
277 else {
278 MSG_OUT("Changing action from %d to %d\n", fdp->action, what);
279 setsock(fdp, s, e, what, g);
280 }
281 }
282 return 0;
283}
285/* CURLOPT_WRITEFUNCTION */
286static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *data)
287{
288 size_t realsize = size * nmemb;
289 struct ConnInfo *conn = (struct ConnInfo *)data;
290 (void)ptr;
291 (void)conn;
292 return realsize;
293}
295/* CURLOPT_XFERINFOFUNCTION */
296static int xferinfo_cb(void *p, curl_off_t dltotal, curl_off_t dlnow,
297 curl_off_t ult, curl_off_t uln)
298{
299 struct ConnInfo *conn = (struct ConnInfo *)p;
300 (void)ult;
301 (void)uln;
303 fprintf(MSG_OUT, "Progress: %s (%" CURL_FORMAT_CURL_OFF_T "/"
304 "%" CURL_FORMAT_CURL_OFF_T ")\n", conn->url, dlnow, dltotal);
305 return 0;
306}
308/* Create a new easy handle, and add it to the global curl_multi */
309static void new_conn(const char *url, struct GlobalInfo *g)
310{
311 struct ConnInfo *conn;
312 CURLMcode mresult;
314 conn = g_malloc0(sizeof(*conn));
315 conn->error[0] = '\0';
316 conn->curl = curl_easy_init();
317 if(!conn->curl) {
318 MSG_OUT("curl_easy_init() failed, exiting!\n");
319 exit(2);
320 }
321 conn->global = g;
322 conn->url = g_strdup(url);
323 curl_easy_setopt(conn->curl, CURLOPT_URL, conn->url);
324 curl_easy_setopt(conn->curl, CURLOPT_WRITEFUNCTION, write_cb);
325 curl_easy_setopt(conn->curl, CURLOPT_WRITEDATA, &conn);
326 curl_easy_setopt(conn->curl, CURLOPT_VERBOSE, SHOW_VERBOSE);
327 curl_easy_setopt(conn->curl, CURLOPT_ERRORBUFFER, conn->error);
328 curl_easy_setopt(conn->curl, CURLOPT_PRIVATE, conn);
329 curl_easy_setopt(conn->curl, CURLOPT_NOPROGRESS, SHOW_PROGRESS ? 0L : 1L);
330 curl_easy_setopt(conn->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
331 curl_easy_setopt(conn->curl, CURLOPT_PROGRESSDATA, conn);
332 curl_easy_setopt(conn->curl, CURLOPT_FOLLOWLOCATION, 1L);
333 curl_easy_setopt(conn->curl, CURLOPT_CONNECTTIMEOUT, 30L);
334 curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
335 curl_easy_setopt(conn->curl, CURLOPT_LOW_SPEED_TIME, 30L);
337 MSG_OUT("Adding easy %p to multi %p (%s)\n", conn->curl, g->multi, url);
338 mresult = curl_multi_add_handle(g->multi, conn->curl);
339 mcode_or_die("new_conn: curl_multi_add_handle", mresult);
341 /* note that add_handle() sets a timeout to trigger soon so that the
342 necessary socket_action() gets called */
343}
345/* This gets called by glib whenever data is received from the fifo */
346static gboolean fifo_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
347{
348#define BUF_SIZE 1024
349 gsize len, tp;
350 gchar *buf, *tmp, *all = NULL;
351 GIOStatus rv;
353 do {
354 GError *err = NULL;
355 rv = g_io_channel_read_line(ch, &buf, &len, &tp, &err);
356 if(buf) {
357 if(tp) {
358 buf[tp] = '\0';
359 }
360 new_conn(buf, (struct GlobalInfo *)data);
361 g_free(buf);
362 }
363 else {
364 buf = g_malloc(BUF_SIZE + 1);
365 for(;;) {
366 buf[BUF_SIZE] = '\0';
367 g_io_channel_read_chars(ch, buf, BUF_SIZE, &len, &err);
368 if(len) {
369 buf[len] = '\0';
370 if(all) {
371 tmp = all;
372 all = g_strdup_printf("%s%s", tmp, buf);
373 g_free(tmp);
374 }
375 else {
376 all = g_strdup(buf);
377 }
378 }
379 else {
380 break;
381 }
382 }
383 if(all) {
384 new_conn(all, (struct GlobalInfo *)data);
385 g_free(all);
386 }
387 g_free(buf);
388 }
389 if(err) {
390 g_error("fifo_cb: %s", err->message);
391 g_free(err);
392 break;
393 }
394 } while((len) && (rv == G_IO_STATUS_NORMAL));
395 return TRUE;
396}
398int init_fifo(void)
399{
400 static const char fifo[] = "hiper.fifo";
401 struct stat st;
402 int socket;
404 if(!lstat(fifo, &st)) {
405 if((st.st_mode & S_IFMT) == S_IFREG) {
406 errno = EEXIST;
407 perror("lstat");
408 return CURL_SOCKET_BAD;
409 }
410 }
412 unlink(fifo);
413 if(mkfifo(fifo, 0600) == -1) {
414 perror("mkfifo");
415 return CURL_SOCKET_BAD;
416 }
418 socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
420 if(socket == CURL_SOCKET_BAD) {
421 perror("open");
422 return socket;
423 }
424 MSG_OUT("Now, pipe some URL's into > %s\n", fifo);
426 return socket;
427}
429int main(void)
430{
431 struct GlobalInfo *g = g_malloc0(sizeof(struct GlobalInfo));
432 GMainLoop *gmain;
433 int fd;
434 GIOChannel *ch;
436 CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
437 if(result != CURLE_OK)
438 return (int)result;
440 fd = init_fifo();
441 if(fd == CURL_SOCKET_BAD) {
442 curl_global_cleanup();
443 return 1;
444 }
445 ch = g_io_channel_unix_new(fd);
446 g_io_add_watch(ch, G_IO_IN, fifo_cb, g);
447 gmain = g_main_loop_new(NULL, FALSE);
448 g->multi = curl_multi_init();
449 if(g->multi) {
450 curl_multi_setopt(g->multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
451 curl_multi_setopt(g->multi, CURLMOPT_SOCKETDATA, g);
452 curl_multi_setopt(g->multi, CURLMOPT_TIMERFUNCTION, update_timeout_cb);
453 curl_multi_setopt(g->multi, CURLMOPT_TIMERDATA, g);
455 /* we do not call any curl_multi_socket*() function yet as we have no
456 handles added! */
458 g_main_loop_run(gmain);
459 curl_multi_cleanup(g->multi);
460 }
461 curl_global_cleanup();
462 return 0;
463}
Diagnostics4 git calls · 54 ms
CallStatusSizeTime054 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 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 · hit28 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=28 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
fetch2004.6 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=23 git.response.bytes=4748 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=79516ccd82c53b19a2f0ab8f19cfadc4fc3bdf93 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=4748 git.pkt.payload.max=4727 git.sideband.band1.bytes=4726 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
- a5e2c68178624afc
- Request
- 9dffb9ab…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format