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