blob: b497e76a9afa22963481cb01a96e3a933b54c44b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2017, 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.haxx.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 ***************************************************************************/
22/* <DESC>
23 * HTTP/2 server push
24 * </DESC>
25 */
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30/* somewhat unix-specific */
31#include <sys/time.h>
32#include <unistd.h>
33
34/* curl stuff */
35#include <curl/curl.h>
36
37#ifndef CURLPIPE_MULTIPLEX
38#error "too old libcurl, can't do HTTP/2 server push!"
39#endif
40
41static
42void dump(const char *text, unsigned char *ptr, size_t size,
43 char nohex)
44{
45 size_t i;
46 size_t c;
47
48 unsigned int width=0x10;
49
50 if(nohex)
51 /* without the hex output, we can fit more on screen */
52 width = 0x40;
53
54 fprintf(stderr, "%s, %ld bytes (0x%lx)\n",
55 text, (long)size, (long)size);
56
57 for(i=0; i<size; i+= width) {
58
59 fprintf(stderr, "%4.4lx: ", (long)i);
60
61 if(!nohex) {
62 /* hex not disabled, show it */
63 for(c = 0; c < width; c++)
64 if(i+c < size)
65 fprintf(stderr, "%02x ", ptr[i+c]);
66 else
67 fputs(" ", stderr);
68 }
69
70 for(c = 0; (c < width) && (i+c < size); c++) {
71 /* check for 0D0A; if found, skip past and start a new line of output */
72 if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
73 i+=(c+2-width);
74 break;
75 }
76 fprintf(stderr, "%c",
77 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
78 /* check again for 0D0A, to avoid an extra \n if it's at width */
79 if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
80 i+=(c+3-width);
81 break;
82 }
83 }
84 fputc('\n', stderr); /* newline */
85 }
86}
87
88static
89int my_trace(CURL *handle, curl_infotype type,
90 char *data, size_t size,
91 void *userp)
92{
93 const char *text;
94 (void)handle; /* prevent compiler warning */
95 (void)userp;
96 switch(type) {
97 case CURLINFO_TEXT:
98 fprintf(stderr, "== Info: %s", data);
99 /* FALLTHROUGH */
100 default: /* in case a new one is introduced to shock us */
101 return 0;
102
103 case CURLINFO_HEADER_OUT:
104 text = "=> Send header";
105 break;
106 case CURLINFO_DATA_OUT:
107 text = "=> Send data";
108 break;
109 case CURLINFO_SSL_DATA_OUT:
110 text = "=> Send SSL data";
111 break;
112 case CURLINFO_HEADER_IN:
113 text = "<= Recv header";
114 break;
115 case CURLINFO_DATA_IN:
116 text = "<= Recv data";
117 break;
118 case CURLINFO_SSL_DATA_IN:
119 text = "<= Recv SSL data";
120 break;
121 }
122
123 dump(text, (unsigned char *)data, size, 1);
124 return 0;
125}
126
127#define OUTPUTFILE "dl"
128
129static void setup(CURL *hnd)
130{
131 FILE *out = fopen(OUTPUTFILE, "wb");
132
133 /* write to this file */
134 curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
135
136 /* set the same URL */
137 curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
138
139 /* please be verbose */
140 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
141 curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
142
143 /* HTTP/2 please */
144 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
145
146 /* we use a self-signed test server, skip verification during debugging */
147 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
148 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
149
150#if (CURLPIPE_MULTIPLEX > 0)
151 /* wait for pipe connection to confirm */
152 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
153#endif
154
155}
156
157/* called when there's an incoming push */
158static int server_push_callback(CURL *parent,
159 CURL *easy,
160 size_t num_headers,
161 struct curl_pushheaders *headers,
162 void *userp)
163{
164 char *headp;
165 size_t i;
166 int *transfers = (int *)userp;
167 char filename[128];
168 FILE *out;
169 static unsigned int count = 0;
170
171 (void)parent; /* we have no use for this */
172
173 snprintf(filename, 128, "push%u", count++);
174
175 /* here's a new stream, save it in a new file for each new push */
176 out = fopen(filename, "wb");
177
178 /* write to this file */
179 curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
180
181 fprintf(stderr, "**** push callback approves stream %u, got %d headers!\n",
182 count, (int)num_headers);
183
184 for(i=0; i<num_headers; i++) {
185 headp = curl_pushheader_bynum(headers, i);
186 fprintf(stderr, "**** header %u: %s\n", (int)i, headp);
187 }
188
189 headp = curl_pushheader_byname(headers, ":path");
190 if(headp) {
191 fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
192 }
193
194 (*transfers)++; /* one more */
195 return CURL_PUSH_OK;
196}
197
198
199/*
200 * Download a file over HTTP/2, take care of server push.
201 */
202int main(void)
203{
204 CURL *easy;
205 CURLM *multi_handle;
206 int still_running; /* keep number of running handles */
207 int transfers=1; /* we start with one */
208 struct CURLMsg *m;
209
210 /* init a multi stack */
211 multi_handle = curl_multi_init();
212
213 easy = curl_easy_init();
214
215 /* set options */
216 setup(easy);
217
218 /* add the easy transfer */
219 curl_multi_add_handle(multi_handle, easy);
220
221 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
222 curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
223 curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
224
225 /* we start some action by calling perform right away */
226 curl_multi_perform(multi_handle, &still_running);
227
228 do {
229 struct timeval timeout;
230 int rc; /* select() return code */
231 CURLMcode mc; /* curl_multi_fdset() return code */
232
233 fd_set fdread;
234 fd_set fdwrite;
235 fd_set fdexcep;
236 int maxfd = -1;
237
238 long curl_timeo = -1;
239
240 FD_ZERO(&fdread);
241 FD_ZERO(&fdwrite);
242 FD_ZERO(&fdexcep);
243
244 /* set a suitable timeout to play around with */
245 timeout.tv_sec = 1;
246 timeout.tv_usec = 0;
247
248 curl_multi_timeout(multi_handle, &curl_timeo);
249 if(curl_timeo >= 0) {
250 timeout.tv_sec = curl_timeo / 1000;
251 if(timeout.tv_sec > 1)
252 timeout.tv_sec = 1;
253 else
254 timeout.tv_usec = (curl_timeo % 1000) * 1000;
255 }
256
257 /* get file descriptors from the transfers */
258 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
259
260 if(mc != CURLM_OK) {
261 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
262 break;
263 }
264
265 /* On success the value of maxfd is guaranteed to be >= -1. We call
266 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
267 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
268 to sleep 100ms, which is the minimum suggested value in the
269 curl_multi_fdset() doc. */
270
271 if(maxfd == -1) {
272#ifdef _WIN32
273 Sleep(100);
274 rc = 0;
275#else
276 /* Portable sleep for platforms other than Windows. */
277 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
278 rc = select(0, NULL, NULL, NULL, &wait);
279#endif
280 }
281 else {
282 /* Note that on some platforms 'timeout' may be modified by select().
283 If you need access to the original value save a copy beforehand. */
284 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
285 }
286
287 switch(rc) {
288 case -1:
289 /* select error */
290 break;
291 case 0:
292 default:
293 /* timeout or readable/writable sockets */
294 curl_multi_perform(multi_handle, &still_running);
295 break;
296 }
297
298 /*
299 * A little caution when doing server push is that libcurl itself has
300 * created and added one or more easy handles but we need to clean them up
301 * when we are done.
302 */
303
304 do {
305 int msgq = 0;;
306 m = curl_multi_info_read(multi_handle, &msgq);
307 if(m && (m->msg == CURLMSG_DONE)) {
308 CURL *e = m->easy_handle;
309 transfers--;
310 curl_multi_remove_handle(multi_handle, e);
311 curl_easy_cleanup(e);
312 }
313 } while(m);
314
315 } while(transfers); /* as long as we have transfers going */
316
317 curl_multi_cleanup(multi_handle);
318
319
320 return 0;
321}