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
43static
44void 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
92static
93int 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    return 0;
104  case CURLINFO_HEADER_OUT:
105    text = "=> Send header";
106    break;
107  case CURLINFO_DATA_OUT:
108    text = "=> Send data";
109    break;
110  case CURLINFO_SSL_DATA_OUT:
111    text = "=> Send SSL data";
112    break;
113  case CURLINFO_HEADER_IN:
114    text = "<= Recv header";
115    break;
116  case CURLINFO_DATA_IN:
117    text = "<= Recv data";
118    break;
119  case CURLINFO_SSL_DATA_IN:
120    text = "<= Recv SSL data";
121    break;
122  default: /* in case a new one is introduced to shock us */
123    return 0;
124  }
125
126  dump(text, (unsigned char *)data, size, 1);
127  return 0;
128}
129
130#define OUTPUTFILE "dl"
131
132static int setup(CURL *hnd, const char *url)
133{
134  FILE *out = fopen(OUTPUTFILE, "wb");
135  if(!out)
136    /* failed */
137    return 1;
138
139  /* write to this file */
140  curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
141
142  /* set the same URL */
143  curl_easy_setopt(hnd, CURLOPT_URL, url);
144
145  /* please be verbose */
146  curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
147  curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
148
149  /* HTTP/2 please */
150  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
151
152  /* we use a self-signed test server, skip verification during debugging */
153  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
154  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
155
156#if (CURLPIPE_MULTIPLEX > 0)
157  /* wait for pipe connection to confirm */
158  curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
159#endif
160  return 0; /* all is good */
161}
162
163/* called when there is an incoming push */
164static int server_push_callback(CURL *parent,
165                                CURL *easy,
166                                size_t num_headers,
167                                struct curl_pushheaders *headers,
168                                void *userp)
169{
170  char *headp;
171  size_t i;
172  int *transfers = (int *)userp;
173  char filename[128];
174  FILE *out;
175  static unsigned int count = 0;
176
177  (void)parent; /* we have no use for this */
178
179  snprintf(filename, 128, "push%u", count++);
180
181  /* here's a new stream, save it in a new file for each new push */
182  out = fopen(filename, "wb");
183  if(!out) {
184    /* if we cannot save it, deny it */
185    fprintf(stderr, "Failed to create output file for push\n");
186    return CURL_PUSH_DENY;
187  }
188
189  /* write to this file */
190  curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
191
192  fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
193          count, (unsigned long)num_headers);
194
195  for(i = 0; i<num_headers; i++) {
196    headp = curl_pushheader_bynum(headers, i);
197    fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
198  }
199
200  headp = curl_pushheader_byname(headers, ":path");
201  if(headp) {
202    fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
203  }
204
205  (*transfers)++; /* one more */
206  return CURL_PUSH_OK;
207}
208
209
210/*
211 * Download a file over HTTP/2, take care of server push.
212 */
213int main(int argc, char *argv[])
214{
215  CURL *easy;
216  CURLM *multi_handle;
217  int transfers = 1; /* we start with one */
218  struct CURLMsg *m;
219  const char *url = "https://localhost:8443/index.html";
220
221  if(argc == 2)
222    url = argv[1];
223
224  /* init a multi stack */
225  multi_handle = curl_multi_init();
226
227  easy = curl_easy_init();
228
229  /* set options */
230  if(setup(easy, url)) {
231    fprintf(stderr, "failed\n");
232    return 1;
233  }
234
235  /* add the easy transfer */
236  curl_multi_add_handle(multi_handle, easy);
237
238  curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
239  curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
240  curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
241
242  do {
243    int still_running; /* keep number of running handles */
244    CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
245
246    if(still_running)
247      /* wait for activity, timeout or "nothing" */
248      mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
249
250    if(mc)
251      break;
252
253    /*
254     * A little caution when doing server push is that libcurl itself has
255     * created and added one or more easy handles but we need to clean them up
256     * when we are done.
257     */
258
259    do {
260      int msgq = 0;
261      m = curl_multi_info_read(multi_handle, &msgq);
262      if(m && (m->msg == CURLMSG_DONE)) {
263        CURL *e = m->easy_handle;
264        transfers--;
265        curl_multi_remove_handle(multi_handle, e);
266        curl_easy_cleanup(e);
267      }
268    } while(m);
269
270  } while(transfers); /* as long as we have transfers going */
271
272  curl_multi_cleanup(multi_handle);
273
274
275  return 0;
276}
277