Artifactspublic-v0

curl

master
ply4 git calls36 ms289 KiB3 cachedprotocol v2
261 lines6.9 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 API using libuv
26 * </DESC>
27 */
28/* Use the socket_action interface to download multiple files in parallel,
29 * powered by libuv.
30 *
31 * Requires libuv and (of course) libcurl.
32 *
33 * See https://docs.libuv.org/en/v1.x/index.html libuv API documentation
34 */
36/* Requires: USE_LIBUV */
38#include <stdio.h>
39#include <stdlib.h>
41#include <uv.h>
43#include <curl/curl.h>
45/* object to pass to the callbacks */
46struct datauv {
47 uv_timer_t timeout;
48 uv_loop_t *loop;
49 CURLM *multi;
50};
52struct curl_context {
53 uv_poll_t poll_handle;
54 curl_socket_t sockfd;
55 struct datauv *uv;
56};
58static struct curl_context *create_curl_context(curl_socket_t sockfd,
59 struct datauv *uv)
61 struct curl_context *context = malloc(sizeof(*context));
63 context->sockfd = sockfd;
64 context->uv = uv;
66 uv_poll_init_socket(uv->loop, &context->poll_handle, sockfd);
67 context->poll_handle.data = context;
69 return context;
72static void curl_close_cb(uv_handle_t *handle)
74 struct curl_context *context = (struct curl_context *)handle->data;
75 free(context);
78static void destroy_curl_context(struct curl_context *context)
80 uv_close((uv_handle_t *)&context->poll_handle, curl_close_cb);
83static void add_download(const char *url, int num, CURLM *multi)
85 char filename[50];
86 FILE *file;
87 CURL *curl;
89 snprintf(filename, sizeof(filename), "%d.download", num);
91 file = fopen(filename, "wb");
92 if(!file) {
93 fprintf(stderr, "Error opening %s\n", filename);
94 return;
95 }
97 curl = curl_easy_init();
98 curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
99 curl_easy_setopt(curl, CURLOPT_PRIVATE, file);
100 curl_easy_setopt(curl, CURLOPT_URL, url);
101 curl_multi_add_handle(multi, curl);
102 fprintf(stderr, "Added download %s -> %s\n", url, filename);
105static void check_multi_info(struct datauv *uv)
107 char *done_url;
108 CURLMsg *message;
109 int pending;
110 CURL *curl;
111 FILE *file;
113 while((message = curl_multi_info_read(uv->multi, &pending))) {
114 switch(message->msg) {
115 case CURLMSG_DONE:
116 /* Do not use message data after calling curl_multi_remove_handle() and
117 curl_easy_cleanup(). As per curl_multi_info_read() docs:
118 "WARNING: The data the returned pointer points to does not survive
119 calling curl_multi_cleanup, curl_multi_remove_handle or
120 curl_easy_cleanup." */
121 curl = message->easy_handle;
123 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &done_url);
124 curl_easy_getinfo(curl, CURLINFO_PRIVATE, &file);
125 printf("%s DONE\n", done_url);
127 curl_multi_remove_handle(uv->multi, curl);
128 curl_easy_cleanup(curl);
129 if(file) {
130 fclose(file);
131 }
132 break;
134 default:
135 fprintf(stderr, "CURLMSG default\n");
136 break;
137 }
138 }
141/* callback from libuv on socket activity */
142static void on_uv_socket(uv_poll_t *req, int status, int events)
144 int running_handles;
145 int flags = 0;
146 struct curl_context *context = (struct curl_context *)req->data;
147 (void)status;
148 if(events & UV_READABLE)
149 flags |= CURL_CSELECT_IN;
150 if(events & UV_WRITABLE)
151 flags |= CURL_CSELECT_OUT;
153 curl_multi_socket_action(context->uv->multi, context->sockfd, flags,
154 &running_handles);
155 check_multi_info(context->uv);
158/* callback from libuv when timeout expires */
159static void on_uv_timeout(uv_timer_t *req)
161 /* get the datauv struct from the timer handle */
162 struct datauv *uv = (struct datauv *)req;
163 int running_handles;
165 curl_multi_socket_action(uv->multi, CURL_SOCKET_TIMEOUT, 0,
166 &running_handles);
168 if(running_handles)
169 check_multi_info(uv);
172/* callback from libcurl to update the timeout expiry */
173static int cb_timeout(CURLM *multi, long timeout_ms, void *userp)
175 struct datauv *uv = (struct datauv *)userp;
176 (void)multi;
177 if(timeout_ms < 0)
178 uv_timer_stop(&uv->timeout);
179 else {
180 uv_timer_start(&uv->timeout, on_uv_timeout, (uint64_t)timeout_ms,
181 0); /* do not repeat */
182 }
183 return 0;
186/* callback from libcurl to update socket activity to wait for */
187static int cb_socket(CURL *curl, curl_socket_t s, int action,
188 void *userp, void *socketp)
190 struct datauv *uv = (struct datauv *)userp;
191 struct curl_context *curl_context;
192 int events = 0;
193 (void)curl;
195 switch(action) {
196 case CURL_POLL_IN:
197 case CURL_POLL_OUT:
198 case CURL_POLL_INOUT:
199 curl_context =
200 socketp ? (struct curl_context *)socketp : create_curl_context(s, uv);
202 curl_multi_assign(uv->multi, s, (void *)curl_context);
204 if(action != CURL_POLL_IN)
205 events |= UV_WRITABLE;
206 if(action != CURL_POLL_OUT)
207 events |= UV_READABLE;
209 uv_poll_start(&curl_context->poll_handle, events, on_uv_socket);
210 break;
211 case CURL_POLL_REMOVE:
212 if(socketp) {
213 uv_poll_stop(&((struct curl_context *)socketp)->poll_handle);
214 destroy_curl_context((struct curl_context *)socketp);
215 curl_multi_assign(uv->multi, s, NULL);
216 }
217 break;
218 default:
219 return -1; /* unknown */
220 }
222 return 0;
225int main(int argc, const char *argv[])
227 CURLcode result;
228 struct datauv uv = { 0 };
229 int running_handles;
231 if(argc <= 1)
232 return 0;
234 result = curl_global_init(CURL_GLOBAL_ALL);
235 if(result != CURLE_OK)
236 return (int)result;
238 uv.loop = uv_default_loop();
239 uv_timer_init(uv.loop, &uv.timeout);
241 uv.multi = curl_multi_init();
242 if(uv.multi) {
243 curl_multi_setopt(uv.multi, CURLMOPT_SOCKETFUNCTION, cb_socket);
244 curl_multi_setopt(uv.multi, CURLMOPT_SOCKETDATA, &uv);
245 curl_multi_setopt(uv.multi, CURLMOPT_TIMERFUNCTION, cb_timeout);
246 curl_multi_setopt(uv.multi, CURLMOPT_TIMERDATA, &uv);
248 while(argc-- > 1) {
249 add_download(argv[argc], argc, uv.multi);
250 }
252 /* kickstart the thing */
253 curl_multi_socket_action(uv.multi, CURL_SOCKET_TIMEOUT, 0,
254 &running_handles);
255 uv_run(uv.loop, UV_RUN_DEFAULT);
256 curl_multi_cleanup(uv.multi);
257 }
258 curl_global_cleanup();
260 return 0;
Diagnostics4 git calls · 36 ms
CallStatusSizeTime036 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=30
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
fetch2002.6 KiB · hit30 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2646
git.response.cache=HIT
git.response.duration_ms=30
git.fetch.wants=4089ceeda800b8342ce8a3091a5062ae18333344
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=2646
git.pkt.payload.max=2625
git.sideband.band1.bytes=2624
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
a5e2236dcb5da87d
Request
bb433f9c…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format