xref: /curl/docs/examples/http2-serverpush.c (revision 45202cbb)
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 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 /* curl stuff */
33 #include <curl/curl.h>
34 #include <curl/mprintf.h>
35 
36 #ifndef CURLPIPE_MULTIPLEX
37 #error "too old libcurl, cannot do HTTP/2 server push!"
38 #endif
39 
40 static
dump(const char * text,unsigned char * ptr,size_t size,char nohex)41 void dump(const char *text, unsigned char *ptr, size_t size,
42           char nohex)
43 {
44   size_t i;
45   size_t c;
46 
47   unsigned int width = 0x10;
48 
49   if(nohex)
50     /* without the hex output, we can fit more on screen */
51     width = 0x40;
52 
53   fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
54           text, (unsigned long)size, (unsigned long)size);
55 
56   for(i = 0; i < size; i += width) {
57 
58     fprintf(stderr, "%4.4lx: ", (unsigned long)i);
59 
60     if(!nohex) {
61       /* hex not disabled, show it */
62       for(c = 0; c < width; c++)
63         if(i + c < size)
64           fprintf(stderr, "%02x ", ptr[i + c]);
65         else
66           fputs("   ", stderr);
67     }
68 
69     for(c = 0; (c < width) && (i + c < size); c++) {
70       /* check for 0D0A; if found, skip past and start a new line of output */
71       if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
72          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 &&
80          ptr[i + c + 2] == 0x0A) {
81         i += (c + 3 - width);
82         break;
83       }
84     }
85     fputc('\n', stderr); /* newline */
86   }
87 }
88 
89 static
my_trace(CURL * handle,curl_infotype type,char * data,size_t size,void * userp)90 int my_trace(CURL *handle, curl_infotype type,
91              char *data, size_t size,
92              void *userp)
93 {
94   const char *text;
95   (void)handle; /* prevent compiler warning */
96   (void)userp;
97   switch(type) {
98   case CURLINFO_TEXT:
99     fprintf(stderr, "== Info: %s", data);
100     return 0;
101   case CURLINFO_HEADER_OUT:
102     text = "=> Send header";
103     break;
104   case CURLINFO_DATA_OUT:
105     text = "=> Send data";
106     break;
107   case CURLINFO_SSL_DATA_OUT:
108     text = "=> Send SSL data";
109     break;
110   case CURLINFO_HEADER_IN:
111     text = "<= Recv header";
112     break;
113   case CURLINFO_DATA_IN:
114     text = "<= Recv data";
115     break;
116   case CURLINFO_SSL_DATA_IN:
117     text = "<= Recv SSL data";
118     break;
119   default: /* in case a new one is introduced to shock us */
120     return 0;
121   }
122 
123   dump(text, (unsigned char *)data, size, 1);
124   return 0;
125 }
126 
127 #define OUTPUTFILE "dl"
128 
setup(CURL * hnd,const char * url)129 static int setup(CURL *hnd, const char *url)
130 {
131   FILE *out = fopen(OUTPUTFILE, "wb");
132   if(!out)
133     /* failed */
134     return 1;
135 
136   /* write to this file */
137   curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
138 
139   /* set the same URL */
140   curl_easy_setopt(hnd, CURLOPT_URL, url);
141 
142   /* please be verbose */
143   curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
144   curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
145 
146   /* HTTP/2 please */
147   curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
148 
149   /* we use a self-signed test server, skip verification during debugging */
150   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
151   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
152 
153 #if (CURLPIPE_MULTIPLEX > 0)
154   /* wait for pipe connection to confirm */
155   curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
156 #endif
157   return 0; /* all is good */
158 }
159 
160 /* called when there is an incoming push */
server_push_callback(CURL * parent,CURL * easy,size_t num_headers,struct curl_pushheaders * headers,void * userp)161 static int server_push_callback(CURL *parent,
162                                 CURL *easy,
163                                 size_t num_headers,
164                                 struct curl_pushheaders *headers,
165                                 void *userp)
166 {
167   char *headp;
168   size_t i;
169   int *transfers = (int *)userp;
170   char filename[128];
171   FILE *out;
172   static unsigned int count = 0;
173 
174   (void)parent; /* we have no use for this */
175 
176   curl_msnprintf(filename, 128, "push%u", count++);
177 
178   /* here's a new stream, save it in a new file for each new push */
179   out = fopen(filename, "wb");
180   if(!out) {
181     /* if we cannot save it, deny it */
182     fprintf(stderr, "Failed to create output file for push\n");
183     return CURL_PUSH_DENY;
184   }
185 
186   /* write to this file */
187   curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
188 
189   fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
190           count, (unsigned long)num_headers);
191 
192   for(i = 0; i < num_headers; i++) {
193     headp = curl_pushheader_bynum(headers, i);
194     fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
195   }
196 
197   headp = curl_pushheader_byname(headers, ":path");
198   if(headp) {
199     fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
200   }
201 
202   (*transfers)++; /* one more */
203   return CURL_PUSH_OK;
204 }
205 
206 
207 /*
208  * Download a file over HTTP/2, take care of server push.
209  */
main(int argc,char * argv[])210 int main(int argc, char *argv[])
211 {
212   CURL *easy;
213   CURLM *multi_handle;
214   int transfers = 1; /* we start with one */
215   struct CURLMsg *m;
216   const char *url = "https://localhost:8443/index.html";
217 
218   if(argc == 2)
219     url = argv[1];
220 
221   /* init a multi stack */
222   multi_handle = curl_multi_init();
223 
224   easy = curl_easy_init();
225 
226   /* set options */
227   if(setup(easy, url)) {
228     fprintf(stderr, "failed\n");
229     return 1;
230   }
231 
232   /* add the easy transfer */
233   curl_multi_add_handle(multi_handle, easy);
234 
235   curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
236   curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
237   curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
238 
239   do {
240     int still_running; /* keep number of running handles */
241     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
242 
243     if(still_running)
244       /* wait for activity, timeout or "nothing" */
245       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
246 
247     if(mc)
248       break;
249 
250     /*
251      * A little caution when doing server push is that libcurl itself has
252      * created and added one or more easy handles but we need to clean them up
253      * when we are done.
254      */
255 
256     do {
257       int msgq = 0;
258       m = curl_multi_info_read(multi_handle, &msgq);
259       if(m && (m->msg == CURLMSG_DONE)) {
260         CURL *e = m->easy_handle;
261         transfers--;
262         curl_multi_remove_handle(multi_handle, e);
263         curl_easy_cleanup(e);
264       }
265     } while(m);
266 
267   } while(transfers); /* as long as we have transfers going */
268 
269   curl_multi_cleanup(multi_handle);
270 
271 
272   return 0;
273 }
274