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 API using libevent
26 * </DESC>
27 */
28#include <stdio.h>
29#include <stdlib.h>
31#include <event2/event.h>
33#include <curl/curl.h>
35static struct event_base *base;
36static CURLM *multi;
37static struct event *timeout;
39struct curl_context {
40 struct event *event;
41 curl_socket_t sockfd;
42};
44static void check_multi_info(void)
45{
46 char *done_url;
47 CURLMsg *message;
48 int pending;
49 CURL *curl;
50 FILE *file;
52 while((message = curl_multi_info_read(multi, &pending))) {
53 switch(message->msg) {
54 case CURLMSG_DONE:
55 /* Do not use message data after calling curl_multi_remove_handle() and
56 curl_easy_cleanup(). As per curl_multi_info_read() docs:
57 "WARNING: The data the returned pointer points to does not survive
58 calling curl_multi_cleanup, curl_multi_remove_handle or
59 curl_easy_cleanup." */
60 curl = message->easy_handle;
62 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &done_url);
63 curl_easy_getinfo(curl, CURLINFO_PRIVATE, &file);
64 printf("%s DONE\n", done_url);
66 curl_multi_remove_handle(multi, curl);
67 curl_easy_cleanup(curl);
68 if(file) {
69 fclose(file);
70 }
71 break;
73 default:
74 fprintf(stderr, "CURLMSG default\n");
75 break;
76 }
77 }
78}
80static void curl_perform(int fd, short event, void *arg)
81{
82 int running_handles;
83 int flags = 0;
84 struct curl_context *context;
86 (void)fd;
88 if(event & EV_READ)
89 flags |= CURL_CSELECT_IN;
90 if(event & EV_WRITE)
91 flags |= CURL_CSELECT_OUT;
93 context = (struct curl_context *)arg;
95 curl_multi_socket_action(multi, context->sockfd, flags, &running_handles);
97 check_multi_info();
98}
100static struct curl_context *create_curl_context(curl_socket_t sockfd)
101{
102 struct curl_context *context = malloc(sizeof(*context));
104 context->sockfd = sockfd;
106 context->event = event_new(base, sockfd, 0, curl_perform, context);
108 return context;
109}
111static void destroy_curl_context(struct curl_context *context)
112{
113 event_del(context->event);
114 event_free(context->event);
115 free(context);
116}
118static void add_download(const char *url, int num)
119{
120 char filename[50];
121 FILE *file;
122 CURL *curl;
124 snprintf(filename, sizeof(filename), "%d.download", num);
126 file = fopen(filename, "wb");
127 if(!file) {
128 fprintf(stderr, "Error opening %s\n", filename);
129 return;
130 }
132 curl = curl_easy_init();
133 curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
134 curl_easy_setopt(curl, CURLOPT_PRIVATE, file);
135 curl_easy_setopt(curl, CURLOPT_URL, url);
136 curl_multi_add_handle(multi, curl);
137 fprintf(stderr, "Added download %s -> %s\n", url, filename);
138}
140static void on_timeout(evutil_socket_t fd, short events, void *arg)
141{
142 int running_handles;
143 (void)fd;
144 (void)events;
145 (void)arg;
146 curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running_handles);
147 check_multi_info();
148}
150static int start_timeout(CURLM *multi, long timeout_ms, void *userp)
151{
152 (void)multi;
153 (void)userp;
154 if(timeout_ms < 0) {
155 evtimer_del(timeout);
156 }
157 else {
158 struct timeval tv;
159 if(timeout_ms == 0)
160 timeout_ms = 1; /* 0 means call socket_action asap */
161 tv.tv_sec = timeout_ms / 1000;
162 tv.tv_usec = (timeout_ms % 1000) * 1000;
163 evtimer_del(timeout);
164 evtimer_add(timeout, &tv);
165 }
166 return 0;
167}
169static int handle_socket(CURL *curl, curl_socket_t s, int action, void *userp,
170 void *socketp)
171{
172 struct curl_context *curl_context;
173 int events = 0;
175 (void)curl;
176 (void)userp;
178 switch(action) {
179 case CURL_POLL_IN:
180 case CURL_POLL_OUT:
181 case CURL_POLL_INOUT:
182 curl_context =
183 socketp ? (struct curl_context *)socketp : create_curl_context(s);
185 curl_multi_assign(multi, s, (void *)curl_context);
187 if(action != CURL_POLL_IN)
188 events |= EV_WRITE;
189 if(action != CURL_POLL_OUT)
190 events |= EV_READ;
192 events |= EV_PERSIST;
194 event_del(curl_context->event);
195 event_assign(curl_context->event, base, curl_context->sockfd,
196 (short)events, curl_perform, curl_context);
197 event_add(curl_context->event, NULL);
199 break;
200 case CURL_POLL_REMOVE:
201 if(socketp) {
202 event_del(((struct curl_context *)socketp)->event);
203 destroy_curl_context((struct curl_context *)socketp);
204 curl_multi_assign(multi, s, NULL);
205 }
206 break;
207 default:
208 return -1; /* unknown */
209 }
211 return 0;
212}
214int main(int argc, const char *argv[])
215{
216 CURLcode result;
218 if(argc <= 1)
219 return 0;
221 result = curl_global_init(CURL_GLOBAL_ALL);
222 if(result != CURLE_OK) {
223 fprintf(stderr, "Could not init curl\n");
224 return (int)result;
225 }
227 base = event_base_new();
228 timeout = evtimer_new(base, on_timeout, NULL);
230 multi = curl_multi_init();
231 if(multi) {
232 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, handle_socket);
233 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, start_timeout);
235 while(argc-- > 1) {
236 add_download(argv[argc], argc);
237 }
239 event_base_dispatch(base);
241 curl_multi_cleanup(multi);
242 }
243 event_free(timeout);
244 event_base_free(base);
246 libevent_global_shutdown();
247 curl_global_cleanup();
249 return 0;
250}
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 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=3 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=4 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
fetch2002.3 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2355 git.response.cache=HIT git.response.duration_ms=20 git.fetch.wants=d10c93d3e0668bdf1da2722a3edfceaf49698087 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=2355 git.pkt.payload.max=2334 git.sideband.band1.bytes=2333 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
- a5e21ee91f0ba87d
- Request
- cf8fa1f5…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format