113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* <DESC>
2513498266Sopenharmony_ci * HTTP/2 server push
2613498266Sopenharmony_ci * </DESC>
2713498266Sopenharmony_ci */
2813498266Sopenharmony_ci#include <stdio.h>
2913498266Sopenharmony_ci#include <stdlib.h>
3013498266Sopenharmony_ci#include <string.h>
3113498266Sopenharmony_ci
3213498266Sopenharmony_ci/* somewhat unix-specific */
3313498266Sopenharmony_ci#include <sys/time.h>
3413498266Sopenharmony_ci#include <unistd.h>
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci/* curl stuff */
3713498266Sopenharmony_ci#include <curl/curl.h>
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci#ifndef CURLPIPE_MULTIPLEX
4013498266Sopenharmony_ci#error "too old libcurl, cannot do HTTP/2 server push!"
4113498266Sopenharmony_ci#endif
4213498266Sopenharmony_ci
4313498266Sopenharmony_cistatic
4413498266Sopenharmony_civoid dump(const char *text, unsigned char *ptr, size_t size,
4513498266Sopenharmony_ci          char nohex)
4613498266Sopenharmony_ci{
4713498266Sopenharmony_ci  size_t i;
4813498266Sopenharmony_ci  size_t c;
4913498266Sopenharmony_ci
5013498266Sopenharmony_ci  unsigned int width = 0x10;
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci  if(nohex)
5313498266Sopenharmony_ci    /* without the hex output, we can fit more on screen */
5413498266Sopenharmony_ci    width = 0x40;
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci  fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
5713498266Sopenharmony_ci          text, (unsigned long)size, (unsigned long)size);
5813498266Sopenharmony_ci
5913498266Sopenharmony_ci  for(i = 0; i<size; i += width) {
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci    fprintf(stderr, "%4.4lx: ", (unsigned long)i);
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci    if(!nohex) {
6413498266Sopenharmony_ci      /* hex not disabled, show it */
6513498266Sopenharmony_ci      for(c = 0; c < width; c++)
6613498266Sopenharmony_ci        if(i + c < size)
6713498266Sopenharmony_ci          fprintf(stderr, "%02x ", ptr[i + c]);
6813498266Sopenharmony_ci        else
6913498266Sopenharmony_ci          fputs("   ", stderr);
7013498266Sopenharmony_ci    }
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci    for(c = 0; (c < width) && (i + c < size); c++) {
7313498266Sopenharmony_ci      /* check for 0D0A; if found, skip past and start a new line of output */
7413498266Sopenharmony_ci      if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
7513498266Sopenharmony_ci         ptr[i + c + 1] == 0x0A) {
7613498266Sopenharmony_ci        i += (c + 2 - width);
7713498266Sopenharmony_ci        break;
7813498266Sopenharmony_ci      }
7913498266Sopenharmony_ci      fprintf(stderr, "%c",
8013498266Sopenharmony_ci              (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
8113498266Sopenharmony_ci      /* check again for 0D0A, to avoid an extra \n if it's at width */
8213498266Sopenharmony_ci      if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
8313498266Sopenharmony_ci         ptr[i + c + 2] == 0x0A) {
8413498266Sopenharmony_ci        i += (c + 3 - width);
8513498266Sopenharmony_ci        break;
8613498266Sopenharmony_ci      }
8713498266Sopenharmony_ci    }
8813498266Sopenharmony_ci    fputc('\n', stderr); /* newline */
8913498266Sopenharmony_ci  }
9013498266Sopenharmony_ci}
9113498266Sopenharmony_ci
9213498266Sopenharmony_cistatic
9313498266Sopenharmony_ciint my_trace(CURL *handle, curl_infotype type,
9413498266Sopenharmony_ci             char *data, size_t size,
9513498266Sopenharmony_ci             void *userp)
9613498266Sopenharmony_ci{
9713498266Sopenharmony_ci  const char *text;
9813498266Sopenharmony_ci  (void)handle; /* prevent compiler warning */
9913498266Sopenharmony_ci  (void)userp;
10013498266Sopenharmony_ci  switch(type) {
10113498266Sopenharmony_ci  case CURLINFO_TEXT:
10213498266Sopenharmony_ci    fprintf(stderr, "== Info: %s", data);
10313498266Sopenharmony_ci    return 0;
10413498266Sopenharmony_ci  case CURLINFO_HEADER_OUT:
10513498266Sopenharmony_ci    text = "=> Send header";
10613498266Sopenharmony_ci    break;
10713498266Sopenharmony_ci  case CURLINFO_DATA_OUT:
10813498266Sopenharmony_ci    text = "=> Send data";
10913498266Sopenharmony_ci    break;
11013498266Sopenharmony_ci  case CURLINFO_SSL_DATA_OUT:
11113498266Sopenharmony_ci    text = "=> Send SSL data";
11213498266Sopenharmony_ci    break;
11313498266Sopenharmony_ci  case CURLINFO_HEADER_IN:
11413498266Sopenharmony_ci    text = "<= Recv header";
11513498266Sopenharmony_ci    break;
11613498266Sopenharmony_ci  case CURLINFO_DATA_IN:
11713498266Sopenharmony_ci    text = "<= Recv data";
11813498266Sopenharmony_ci    break;
11913498266Sopenharmony_ci  case CURLINFO_SSL_DATA_IN:
12013498266Sopenharmony_ci    text = "<= Recv SSL data";
12113498266Sopenharmony_ci    break;
12213498266Sopenharmony_ci  default: /* in case a new one is introduced to shock us */
12313498266Sopenharmony_ci    return 0;
12413498266Sopenharmony_ci  }
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci  dump(text, (unsigned char *)data, size, 1);
12713498266Sopenharmony_ci  return 0;
12813498266Sopenharmony_ci}
12913498266Sopenharmony_ci
13013498266Sopenharmony_ci#define OUTPUTFILE "dl"
13113498266Sopenharmony_ci
13213498266Sopenharmony_cistatic int setup(CURL *hnd, const char *url)
13313498266Sopenharmony_ci{
13413498266Sopenharmony_ci  FILE *out = fopen(OUTPUTFILE, "wb");
13513498266Sopenharmony_ci  if(!out)
13613498266Sopenharmony_ci    /* failed */
13713498266Sopenharmony_ci    return 1;
13813498266Sopenharmony_ci
13913498266Sopenharmony_ci  /* write to this file */
14013498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
14113498266Sopenharmony_ci
14213498266Sopenharmony_ci  /* set the same URL */
14313498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_URL, url);
14413498266Sopenharmony_ci
14513498266Sopenharmony_ci  /* please be verbose */
14613498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
14713498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
14813498266Sopenharmony_ci
14913498266Sopenharmony_ci  /* HTTP/2 please */
15013498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
15113498266Sopenharmony_ci
15213498266Sopenharmony_ci  /* we use a self-signed test server, skip verification during debugging */
15313498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
15413498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
15513498266Sopenharmony_ci
15613498266Sopenharmony_ci#if (CURLPIPE_MULTIPLEX > 0)
15713498266Sopenharmony_ci  /* wait for pipe connection to confirm */
15813498266Sopenharmony_ci  curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
15913498266Sopenharmony_ci#endif
16013498266Sopenharmony_ci  return 0; /* all is good */
16113498266Sopenharmony_ci}
16213498266Sopenharmony_ci
16313498266Sopenharmony_ci/* called when there is an incoming push */
16413498266Sopenharmony_cistatic int server_push_callback(CURL *parent,
16513498266Sopenharmony_ci                                CURL *easy,
16613498266Sopenharmony_ci                                size_t num_headers,
16713498266Sopenharmony_ci                                struct curl_pushheaders *headers,
16813498266Sopenharmony_ci                                void *userp)
16913498266Sopenharmony_ci{
17013498266Sopenharmony_ci  char *headp;
17113498266Sopenharmony_ci  size_t i;
17213498266Sopenharmony_ci  int *transfers = (int *)userp;
17313498266Sopenharmony_ci  char filename[128];
17413498266Sopenharmony_ci  FILE *out;
17513498266Sopenharmony_ci  static unsigned int count = 0;
17613498266Sopenharmony_ci
17713498266Sopenharmony_ci  (void)parent; /* we have no use for this */
17813498266Sopenharmony_ci
17913498266Sopenharmony_ci  snprintf(filename, 128, "push%u", count++);
18013498266Sopenharmony_ci
18113498266Sopenharmony_ci  /* here's a new stream, save it in a new file for each new push */
18213498266Sopenharmony_ci  out = fopen(filename, "wb");
18313498266Sopenharmony_ci  if(!out) {
18413498266Sopenharmony_ci    /* if we cannot save it, deny it */
18513498266Sopenharmony_ci    fprintf(stderr, "Failed to create output file for push\n");
18613498266Sopenharmony_ci    return CURL_PUSH_DENY;
18713498266Sopenharmony_ci  }
18813498266Sopenharmony_ci
18913498266Sopenharmony_ci  /* write to this file */
19013498266Sopenharmony_ci  curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
19113498266Sopenharmony_ci
19213498266Sopenharmony_ci  fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
19313498266Sopenharmony_ci          count, (unsigned long)num_headers);
19413498266Sopenharmony_ci
19513498266Sopenharmony_ci  for(i = 0; i<num_headers; i++) {
19613498266Sopenharmony_ci    headp = curl_pushheader_bynum(headers, i);
19713498266Sopenharmony_ci    fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
19813498266Sopenharmony_ci  }
19913498266Sopenharmony_ci
20013498266Sopenharmony_ci  headp = curl_pushheader_byname(headers, ":path");
20113498266Sopenharmony_ci  if(headp) {
20213498266Sopenharmony_ci    fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
20313498266Sopenharmony_ci  }
20413498266Sopenharmony_ci
20513498266Sopenharmony_ci  (*transfers)++; /* one more */
20613498266Sopenharmony_ci  return CURL_PUSH_OK;
20713498266Sopenharmony_ci}
20813498266Sopenharmony_ci
20913498266Sopenharmony_ci
21013498266Sopenharmony_ci/*
21113498266Sopenharmony_ci * Download a file over HTTP/2, take care of server push.
21213498266Sopenharmony_ci */
21313498266Sopenharmony_ciint main(int argc, char *argv[])
21413498266Sopenharmony_ci{
21513498266Sopenharmony_ci  CURL *easy;
21613498266Sopenharmony_ci  CURLM *multi_handle;
21713498266Sopenharmony_ci  int transfers = 1; /* we start with one */
21813498266Sopenharmony_ci  struct CURLMsg *m;
21913498266Sopenharmony_ci  const char *url = "https://localhost:8443/index.html";
22013498266Sopenharmony_ci
22113498266Sopenharmony_ci  if(argc == 2)
22213498266Sopenharmony_ci    url = argv[1];
22313498266Sopenharmony_ci
22413498266Sopenharmony_ci  /* init a multi stack */
22513498266Sopenharmony_ci  multi_handle = curl_multi_init();
22613498266Sopenharmony_ci
22713498266Sopenharmony_ci  easy = curl_easy_init();
22813498266Sopenharmony_ci
22913498266Sopenharmony_ci  /* set options */
23013498266Sopenharmony_ci  if(setup(easy, url)) {
23113498266Sopenharmony_ci    fprintf(stderr, "failed\n");
23213498266Sopenharmony_ci    return 1;
23313498266Sopenharmony_ci  }
23413498266Sopenharmony_ci
23513498266Sopenharmony_ci  /* add the easy transfer */
23613498266Sopenharmony_ci  curl_multi_add_handle(multi_handle, easy);
23713498266Sopenharmony_ci
23813498266Sopenharmony_ci  curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
23913498266Sopenharmony_ci  curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
24013498266Sopenharmony_ci  curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
24113498266Sopenharmony_ci
24213498266Sopenharmony_ci  do {
24313498266Sopenharmony_ci    int still_running; /* keep number of running handles */
24413498266Sopenharmony_ci    CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
24513498266Sopenharmony_ci
24613498266Sopenharmony_ci    if(still_running)
24713498266Sopenharmony_ci      /* wait for activity, timeout or "nothing" */
24813498266Sopenharmony_ci      mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
24913498266Sopenharmony_ci
25013498266Sopenharmony_ci    if(mc)
25113498266Sopenharmony_ci      break;
25213498266Sopenharmony_ci
25313498266Sopenharmony_ci    /*
25413498266Sopenharmony_ci     * A little caution when doing server push is that libcurl itself has
25513498266Sopenharmony_ci     * created and added one or more easy handles but we need to clean them up
25613498266Sopenharmony_ci     * when we are done.
25713498266Sopenharmony_ci     */
25813498266Sopenharmony_ci
25913498266Sopenharmony_ci    do {
26013498266Sopenharmony_ci      int msgq = 0;
26113498266Sopenharmony_ci      m = curl_multi_info_read(multi_handle, &msgq);
26213498266Sopenharmony_ci      if(m && (m->msg == CURLMSG_DONE)) {
26313498266Sopenharmony_ci        CURL *e = m->easy_handle;
26413498266Sopenharmony_ci        transfers--;
26513498266Sopenharmony_ci        curl_multi_remove_handle(multi_handle, e);
26613498266Sopenharmony_ci        curl_easy_cleanup(e);
26713498266Sopenharmony_ci      }
26813498266Sopenharmony_ci    } while(m);
26913498266Sopenharmony_ci
27013498266Sopenharmony_ci  } while(transfers); /* as long as we have transfers going */
27113498266Sopenharmony_ci
27213498266Sopenharmony_ci  curl_multi_cleanup(multi_handle);
27313498266Sopenharmony_ci
27413498266Sopenharmony_ci
27513498266Sopenharmony_ci  return 0;
27613498266Sopenharmony_ci}
277