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