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 * HTTP/2 server push
26 * </DESC>
27 */
28#ifdef _MSC_VER
29#ifndef _CRT_SECURE_NO_WARNINGS
30#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen() */
31#endif
32#endif
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
38#include <curl/curl.h>
40#ifndef CURLPIPE_MULTIPLEX
41#error "too old libcurl, cannot do HTTP/2 server push"
42#endif
44#if defined(_MSC_VER) && (_MSC_VER < 1900)
45#define snprintf _snprintf
46#endif
48static FILE *out_download;
50static void dump(const char *text, const unsigned char *ptr,
51 size_t size, char nohex)
52{
53 size_t i;
54 size_t c;
56 unsigned int width = 0x10;
58 if(nohex)
59 /* without the hex output, we can fit more on screen */
60 width = 0x40;
62 fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
63 text, (unsigned long)size, (unsigned long)size);
65 for(i = 0; i < size; i += width) {
67 fprintf(stderr, "%4.4lx: ", (unsigned long)i);
69 if(!nohex) {
70 /* hex not disabled, show it */
71 for(c = 0; c < width; c++)
72 if(i + c < size)
73 fprintf(stderr, "%02x ", ptr[i + c]);
74 else
75 fputs(" ", stderr);
76 }
78 for(c = 0; (c < width) && (i + c < size); c++) {
79 /* check for 0D0A; if found, skip past and start a new line of output */
80 if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
81 ptr[i + c + 1] == 0x0A) {
82 i += (c + 2 - width);
83 break;
84 }
85 fprintf(stderr, "%c",
86 (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
87 /* check again for 0D0A, to avoid an extra \n if it is at width */
88 if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
89 ptr[i + c + 2] == 0x0A) {
90 i += (c + 3 - width);
91 break;
92 }
93 }
94 fputc('\n', stderr); /* newline */
95 }
96}
98static int my_trace(CURL *curl, curl_infotype type,
99 char *data, size_t size, void *userp)
100{
101 const char *text;
102 (void)curl;
103 (void)userp;
104 switch(type) {
105 case CURLINFO_TEXT:
106 fprintf(stderr, "== Info: %s", data);
107 return 0;
108 case CURLINFO_HEADER_OUT:
109 text = "=> Send header";
110 break;
111 case CURLINFO_DATA_OUT:
112 text = "=> Send data";
113 break;
114 case CURLINFO_SSL_DATA_OUT:
115 text = "=> Send SSL data";
116 break;
117 case CURLINFO_HEADER_IN:
118 text = "<= Recv header";
119 break;
120 case CURLINFO_DATA_IN:
121 text = "<= Recv data";
122 break;
123 case CURLINFO_SSL_DATA_IN:
124 text = "<= Recv SSL data";
125 break;
126 default: /* in case a new one is introduced to shock us */
127 return 0;
128 }
130 dump(text, (const unsigned char *)data, size, 1);
131 return 0;
132}
134#define OUTPUTFILE "dl"
136static int setup(CURL *curl, const char *url)
137{
138 out_download = fopen(OUTPUTFILE, "wb");
139 if(!out_download)
140 return 1; /* failed */
142 /* set the same URL */
143 curl_easy_setopt(curl, CURLOPT_URL, url);
145 /* HTTP/2 please */
146 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
148 /* we use a self-signed test server, skip verification during debugging */
149 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
150 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
152 /* write to this file */
153 curl_easy_setopt(curl, CURLOPT_WRITEDATA, out_download);
155 /* please be verbose */
156 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
157 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
159#if CURLPIPE_MULTIPLEX > 0
160 /* wait for pipe connection to confirm */
161 curl_easy_setopt(curl, CURLOPT_PIPEWAIT, 1L);
162#endif
163 return 0; /* all is good */
164}
166static FILE *out_push;
168/* called when there is an incoming push */
169static int server_push_callback(CURL *parent,
170 CURL *curl,
171 size_t num_headers,
172 struct curl_pushheaders *headers,
173 void *userp)
174{
175 const char *headp;
176 size_t i;
177 int *transfers = (int *)userp;
178 char filename[128];
179 static unsigned int count = 0;
181 (void)parent;
183 snprintf(filename, sizeof(filename), "push%u", count++);
185 /* here's a new stream, save it in a new file for each new push */
186 out_push = fopen(filename, "wb");
187 if(!out_push) {
188 /* if we cannot save it, deny it */
189 fprintf(stderr, "Failed to create output file for push\n");
190 return CURL_PUSH_DENY;
191 }
193 /* write to this file */
194 curl_easy_setopt(curl, CURLOPT_WRITEDATA, out_push);
196 fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
197 count, (unsigned long)num_headers);
199 for(i = 0; i < num_headers; i++) {
200 headp = curl_pushheader_bynum(headers, i);
201 fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
202 }
204 headp = curl_pushheader_byname(headers, ":path");
205 if(headp) {
206 fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
207 }
209 (*transfers)++; /* one more */
211 return CURL_PUSH_OK;
212}
214/*
215 * Download a file over HTTP/2, take care of server push.
216 */
217int main(int argc, const char *argv[])
218{
219 CURLcode result;
220 CURL *curl;
221 CURLM *multi;
222 int transfers = 1; /* we start with one */
223 const char *url = "https://localhost:8443/index.html";
225 if(argc == 2)
226 url = argv[1];
228 result = curl_global_init(CURL_GLOBAL_ALL);
229 if(result != CURLE_OK)
230 return (int)result;
232 /* init a multi stack */
233 multi = curl_multi_init();
234 if(!multi)
235 goto error;
237 curl = curl_easy_init();
239 /* set options */
240 if(!curl || setup(curl, url)) {
241 fprintf(stderr, "failed\n");
242 goto error;
243 }
245 curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
246 curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, server_push_callback);
247 curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &transfers);
249 /* add the easy transfer */
250 curl_multi_add_handle(multi, curl);
252 do {
253 struct CURLMsg *m;
254 int still_running; /* keep number of running handles */
255 CURLMcode mresult = curl_multi_perform(multi, &still_running);
257 if(still_running)
258 /* wait for activity, timeout or "nothing" */
259 mresult = curl_multi_poll(multi, NULL, 0, 1000, NULL);
261 if(mresult)
262 break;
264 /*
265 * A little caution when doing server push is that libcurl itself has
266 * created and added one or more easy handles but we need to clean them up
267 * when we are done.
268 */
269 do {
270 int msgq = 0;
271 m = curl_multi_info_read(multi, &msgq);
272 if(m && (m->msg == CURLMSG_DONE)) {
273 curl = m->easy_handle;
274 transfers--;
275 curl_multi_remove_handle(multi, curl);
276 curl_easy_cleanup(curl);
277 }
278 } while(m);
280 } while(transfers); /* as long as we have transfers going */
282error:
284 if(multi)
285 curl_multi_cleanup(multi);
287 curl_global_cleanup();
289 fclose(out_download);
290 if(out_push)
291 fclose(out_push);
293 return 0;
294}
Diagnostics4 git calls · 429 ms
CallStatusSizeTime0429 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 · hit4 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=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
fetch2003.0 KiB423 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=423 git.response.bytes=3090 git.response.cache=MISS git.response.duration_ms=423 git.fetch.wants=0d97c1e64a85a87316d7aa28801b23d2e90d8ccc 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=3090 git.pkt.payload.max=3069 git.sideband.band1.bytes=3068 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
- a5e1f5a8dca7efe9
- Request
- 5b23a4a6…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format