Blame examples/client.c

Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * nghttp2 - HTTP/2 C Library
Packit Bot 4c90e1
 *
Packit Bot 4c90e1
 * Copyright (c) 2013 Tatsuhiro Tsujikawa
Packit Bot 4c90e1
 *
Packit Bot 4c90e1
 * Permission is hereby granted, free of charge, to any person obtaining
Packit Bot 4c90e1
 * a copy of this software and associated documentation files (the
Packit Bot 4c90e1
 * "Software"), to deal in the Software without restriction, including
Packit Bot 4c90e1
 * without limitation the rights to use, copy, modify, merge, publish,
Packit Bot 4c90e1
 * distribute, sublicense, and/or sell copies of the Software, and to
Packit Bot 4c90e1
 * permit persons to whom the Software is furnished to do so, subject to
Packit Bot 4c90e1
 * the following conditions:
Packit Bot 4c90e1
 *
Packit Bot 4c90e1
 * The above copyright notice and this permission notice shall be
Packit Bot 4c90e1
 * included in all copies or substantial portions of the Software.
Packit Bot 4c90e1
 *
Packit Bot 4c90e1
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit Bot 4c90e1
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit Bot 4c90e1
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit Bot 4c90e1
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Packit Bot 4c90e1
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Packit Bot 4c90e1
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Packit Bot 4c90e1
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * This program is written to show how to use nghttp2 API in C and
Packit Bot 4c90e1
 * intentionally made simple.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
#ifdef HAVE_CONFIG_H
Packit Bot 4c90e1
#  include <config.h>
Packit Bot 4c90e1
#endif /* HAVE_CONFIG_H */
Packit Bot 4c90e1
Packit Bot 4c90e1
#include <inttypes.h>
Packit Bot 4c90e1
#include <stdlib.h>
Packit Bot 4c90e1
#ifdef HAVE_UNISTD_H
Packit Bot 4c90e1
#  include <unistd.h>
Packit Bot 4c90e1
#endif /* HAVE_UNISTD_H */
Packit Bot 4c90e1
#ifdef HAVE_FCNTL_H
Packit Bot 4c90e1
#  include <fcntl.h>
Packit Bot 4c90e1
#endif /* HAVE_FCNTL_H */
Packit Bot 4c90e1
#include <sys/types.h>
Packit Bot 4c90e1
#ifdef HAVE_SYS_SOCKET_H
Packit Bot 4c90e1
#  include <sys/socket.h>
Packit Bot 4c90e1
#endif /* HAVE_SYS_SOCKET_H */
Packit Bot 4c90e1
#ifdef HAVE_NETDB_H
Packit Bot 4c90e1
#  include <netdb.h>
Packit Bot 4c90e1
#endif /* HAVE_NETDB_H */
Packit Bot 4c90e1
#ifdef HAVE_NETINET_IN_H
Packit Bot 4c90e1
#  include <netinet/in.h>
Packit Bot 4c90e1
#endif /* HAVE_NETINET_IN_H */
Packit Bot 4c90e1
#include <netinet/tcp.h>
Packit Bot 4c90e1
#include <poll.h>
Packit Bot 4c90e1
#include <signal.h>
Packit Bot 4c90e1
#include <stdio.h>
Packit Bot 4c90e1
#include <assert.h>
Packit Bot 4c90e1
#include <string.h>
Packit Bot 4c90e1
#include <errno.h>
Packit Bot 4c90e1
Packit Bot 4c90e1
#include <nghttp2/nghttp2.h>
Packit Bot 4c90e1
Packit Bot 4c90e1
#include <openssl/ssl.h>
Packit Bot 4c90e1
#include <openssl/err.h>
Packit Bot 4c90e1
#include <openssl/conf.h>
Packit Bot 4c90e1
Packit Bot 4c90e1
enum { IO_NONE, WANT_READ, WANT_WRITE };
Packit Bot 4c90e1
Packit Bot 4c90e1
#define MAKE_NV(NAME, VALUE)                                                   \
Packit Bot 4c90e1
  {                                                                            \
Packit Bot 4c90e1
    (uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, sizeof(VALUE) - 1,    \
Packit Bot 4c90e1
        NGHTTP2_NV_FLAG_NONE                                                   \
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
#define MAKE_NV_CS(NAME, VALUE)                                                \
Packit Bot 4c90e1
  {                                                                            \
Packit Bot 4c90e1
    (uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, strlen(VALUE),        \
Packit Bot 4c90e1
        NGHTTP2_NV_FLAG_NONE                                                   \
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
struct Connection {
Packit Bot 4c90e1
  SSL *ssl;
Packit Bot 4c90e1
  nghttp2_session *session;
Packit Bot 4c90e1
  /* WANT_READ if SSL/TLS connection needs more input; or WANT_WRITE
Packit Bot 4c90e1
     if it needs more output; or IO_NONE. This is necessary because
Packit Bot 4c90e1
     SSL/TLS re-negotiation is possible at any time. nghttp2 API
Packit Bot 4c90e1
     offers similar functions like nghttp2_session_want_read() and
Packit Bot 4c90e1
     nghttp2_session_want_write() but they do not take into account
Packit Bot 4c90e1
     SSL/TSL connection. */
Packit Bot 4c90e1
  int want_io;
Packit Bot 4c90e1
};
Packit Bot 4c90e1
Packit Bot 4c90e1
struct Request {
Packit Bot 4c90e1
  char *host;
Packit Bot 4c90e1
  /* In this program, path contains query component as well. */
Packit Bot 4c90e1
  char *path;
Packit Bot 4c90e1
  /* This is the concatenation of host and port with ":" in
Packit Bot 4c90e1
     between. */
Packit Bot 4c90e1
  char *hostport;
Packit Bot 4c90e1
  /* Stream ID for this request. */
Packit Bot 4c90e1
  int32_t stream_id;
Packit Bot 4c90e1
  uint16_t port;
Packit Bot 4c90e1
};
Packit Bot 4c90e1
Packit Bot 4c90e1
struct URI {
Packit Bot 4c90e1
  const char *host;
Packit Bot 4c90e1
  /* In this program, path contains query component as well. */
Packit Bot 4c90e1
  const char *path;
Packit Bot 4c90e1
  size_t pathlen;
Packit Bot 4c90e1
  const char *hostport;
Packit Bot 4c90e1
  size_t hostlen;
Packit Bot 4c90e1
  size_t hostportlen;
Packit Bot 4c90e1
  uint16_t port;
Packit Bot 4c90e1
};
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Returns copy of string |s| with the length |len|. The returned
Packit Bot 4c90e1
 * string is NULL-terminated.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static char *strcopy(const char *s, size_t len) {
Packit Bot 4c90e1
  char *dst;
Packit Bot 4c90e1
  dst = malloc(len + 1);
Packit Bot 4c90e1
  memcpy(dst, s, len);
Packit Bot 4c90e1
  dst[len] = '\0';
Packit Bot 4c90e1
  return dst;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Prints error message |msg| and exit.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
NGHTTP2_NORETURN
Packit Bot 4c90e1
static void die(const char *msg) {
Packit Bot 4c90e1
  fprintf(stderr, "FATAL: %s\n", msg);
Packit Bot 4c90e1
  exit(EXIT_FAILURE);
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Prints error containing the function name |func| and message |msg|
Packit Bot 4c90e1
 * and exit.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
NGHTTP2_NORETURN
Packit Bot 4c90e1
static void dief(const char *func, const char *msg) {
Packit Bot 4c90e1
  fprintf(stderr, "FATAL: %s: %s\n", func, msg);
Packit Bot 4c90e1
  exit(EXIT_FAILURE);
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Prints error containing the function name |func| and error code
Packit Bot 4c90e1
 * |error_code| and exit.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
NGHTTP2_NORETURN
Packit Bot 4c90e1
static void diec(const char *func, int error_code) {
Packit Bot 4c90e1
  fprintf(stderr, "FATAL: %s: error_code=%d, msg=%s\n", func, error_code,
Packit Bot 4c90e1
          nghttp2_strerror(error_code));
Packit Bot 4c90e1
  exit(EXIT_FAILURE);
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * The implementation of nghttp2_send_callback type. Here we write
Packit Bot 4c90e1
 * |data| with size |length| to the network and return the number of
Packit Bot 4c90e1
 * bytes actually written. See the documentation of
Packit Bot 4c90e1
 * nghttp2_send_callback for the details.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static ssize_t send_callback(nghttp2_session *session, const uint8_t *data,
Packit Bot 4c90e1
                             size_t length, int flags, void *user_data) {
Packit Bot 4c90e1
  struct Connection *connection;
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  (void)session;
Packit Bot 4c90e1
  (void)flags;
Packit Bot 4c90e1
Packit Bot 4c90e1
  connection = (struct Connection *)user_data;
Packit Bot 4c90e1
  connection->want_io = IO_NONE;
Packit Bot 4c90e1
  ERR_clear_error();
Packit Bot 4c90e1
  rv = SSL_write(connection->ssl, data, (int)length);
Packit Bot 4c90e1
  if (rv <= 0) {
Packit Bot 4c90e1
    int err = SSL_get_error(connection->ssl, rv);
Packit Bot 4c90e1
    if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
Packit Bot 4c90e1
      connection->want_io =
Packit Bot 4c90e1
          (err == SSL_ERROR_WANT_READ ? WANT_READ : WANT_WRITE);
Packit Bot 4c90e1
      rv = NGHTTP2_ERR_WOULDBLOCK;
Packit Bot 4c90e1
    } else {
Packit Bot 4c90e1
      rv = NGHTTP2_ERR_CALLBACK_FAILURE;
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return rv;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * The implementation of nghttp2_recv_callback type. Here we read data
Packit Bot 4c90e1
 * from the network and write them in |buf|. The capacity of |buf| is
Packit Bot 4c90e1
 * |length| bytes. Returns the number of bytes stored in |buf|. See
Packit Bot 4c90e1
 * the documentation of nghttp2_recv_callback for the details.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static ssize_t recv_callback(nghttp2_session *session, uint8_t *buf,
Packit Bot 4c90e1
                             size_t length, int flags, void *user_data) {
Packit Bot 4c90e1
  struct Connection *connection;
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  (void)session;
Packit Bot 4c90e1
  (void)flags;
Packit Bot 4c90e1
Packit Bot 4c90e1
  connection = (struct Connection *)user_data;
Packit Bot 4c90e1
  connection->want_io = IO_NONE;
Packit Bot 4c90e1
  ERR_clear_error();
Packit Bot 4c90e1
  rv = SSL_read(connection->ssl, buf, (int)length);
Packit Bot 4c90e1
  if (rv < 0) {
Packit Bot 4c90e1
    int err = SSL_get_error(connection->ssl, rv);
Packit Bot 4c90e1
    if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
Packit Bot 4c90e1
      connection->want_io =
Packit Bot 4c90e1
          (err == SSL_ERROR_WANT_READ ? WANT_READ : WANT_WRITE);
Packit Bot 4c90e1
      rv = NGHTTP2_ERR_WOULDBLOCK;
Packit Bot 4c90e1
    } else {
Packit Bot 4c90e1
      rv = NGHTTP2_ERR_CALLBACK_FAILURE;
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
  } else if (rv == 0) {
Packit Bot 4c90e1
    rv = NGHTTP2_ERR_EOF;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return rv;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static int on_frame_send_callback(nghttp2_session *session,
Packit Bot 4c90e1
                                  const nghttp2_frame *frame, void *user_data) {
Packit Bot 4c90e1
  size_t i;
Packit Bot 4c90e1
  (void)user_data;
Packit Bot 4c90e1
Packit Bot 4c90e1
  switch (frame->hd.type) {
Packit Bot 4c90e1
  case NGHTTP2_HEADERS:
Packit Bot 4c90e1
    if (nghttp2_session_get_stream_user_data(session, frame->hd.stream_id)) {
Packit Bot 4c90e1
      const nghttp2_nv *nva = frame->headers.nva;
Packit Bot 4c90e1
      printf("[INFO] C ----------------------------> S (HEADERS)\n");
Packit Bot 4c90e1
      for (i = 0; i < frame->headers.nvlen; ++i) {
Packit Bot 4c90e1
        fwrite(nva[i].name, 1, nva[i].namelen, stdout);
Packit Bot 4c90e1
        printf(": ");
Packit Bot 4c90e1
        fwrite(nva[i].value, 1, nva[i].valuelen, stdout);
Packit Bot 4c90e1
        printf("\n");
Packit Bot 4c90e1
      }
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    break;
Packit Bot 4c90e1
  case NGHTTP2_RST_STREAM:
Packit Bot 4c90e1
    printf("[INFO] C ----------------------------> S (RST_STREAM)\n");
Packit Bot 4c90e1
    break;
Packit Bot 4c90e1
  case NGHTTP2_GOAWAY:
Packit Bot 4c90e1
    printf("[INFO] C ----------------------------> S (GOAWAY)\n");
Packit Bot 4c90e1
    break;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return 0;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static int on_frame_recv_callback(nghttp2_session *session,
Packit Bot 4c90e1
                                  const nghttp2_frame *frame, void *user_data) {
Packit Bot 4c90e1
  size_t i;
Packit Bot 4c90e1
  (void)user_data;
Packit Bot 4c90e1
Packit Bot 4c90e1
  switch (frame->hd.type) {
Packit Bot 4c90e1
  case NGHTTP2_HEADERS:
Packit Bot 4c90e1
    if (frame->headers.cat == NGHTTP2_HCAT_RESPONSE) {
Packit Bot 4c90e1
      const nghttp2_nv *nva = frame->headers.nva;
Packit Bot 4c90e1
      struct Request *req;
Packit Bot 4c90e1
      req = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
Packit Bot 4c90e1
      if (req) {
Packit Bot 4c90e1
        printf("[INFO] C <---------------------------- S (HEADERS)\n");
Packit Bot 4c90e1
        for (i = 0; i < frame->headers.nvlen; ++i) {
Packit Bot 4c90e1
          fwrite(nva[i].name, 1, nva[i].namelen, stdout);
Packit Bot 4c90e1
          printf(": ");
Packit Bot 4c90e1
          fwrite(nva[i].value, 1, nva[i].valuelen, stdout);
Packit Bot 4c90e1
          printf("\n");
Packit Bot 4c90e1
        }
Packit Bot 4c90e1
      }
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    break;
Packit Bot 4c90e1
  case NGHTTP2_RST_STREAM:
Packit Bot 4c90e1
    printf("[INFO] C <---------------------------- S (RST_STREAM)\n");
Packit Bot 4c90e1
    break;
Packit Bot 4c90e1
  case NGHTTP2_GOAWAY:
Packit Bot 4c90e1
    printf("[INFO] C <---------------------------- S (GOAWAY)\n");
Packit Bot 4c90e1
    break;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return 0;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * The implementation of nghttp2_on_stream_close_callback type. We use
Packit Bot 4c90e1
 * this function to know the response is fully received. Since we just
Packit Bot 4c90e1
 * fetch 1 resource in this program, after reception of the response,
Packit Bot 4c90e1
 * we submit GOAWAY and close the session.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
Packit Bot 4c90e1
                                    uint32_t error_code, void *user_data) {
Packit Bot 4c90e1
  struct Request *req;
Packit Bot 4c90e1
  (void)error_code;
Packit Bot 4c90e1
  (void)user_data;
Packit Bot 4c90e1
Packit Bot 4c90e1
  req = nghttp2_session_get_stream_user_data(session, stream_id);
Packit Bot 4c90e1
  if (req) {
Packit Bot 4c90e1
    int rv;
Packit Bot 4c90e1
    rv = nghttp2_session_terminate_session(session, NGHTTP2_NO_ERROR);
Packit Bot 4c90e1
Packit Bot 4c90e1
    if (rv != 0) {
Packit Bot 4c90e1
      diec("nghttp2_session_terminate_session", rv);
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return 0;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * The implementation of nghttp2_on_data_chunk_recv_callback type. We
Packit Bot 4c90e1
 * use this function to print the received response body.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
Packit Bot 4c90e1
                                       int32_t stream_id, const uint8_t *data,
Packit Bot 4c90e1
                                       size_t len, void *user_data) {
Packit Bot 4c90e1
  struct Request *req;
Packit Bot 4c90e1
  (void)flags;
Packit Bot 4c90e1
  (void)user_data;
Packit Bot 4c90e1
Packit Bot 4c90e1
  req = nghttp2_session_get_stream_user_data(session, stream_id);
Packit Bot 4c90e1
  if (req) {
Packit Bot 4c90e1
    printf("[INFO] C <---------------------------- S (DATA chunk)\n"
Packit Bot 4c90e1
           "%lu bytes\n",
Packit Bot 4c90e1
           (unsigned long int)len);
Packit Bot 4c90e1
    fwrite(data, 1, len, stdout);
Packit Bot 4c90e1
    printf("\n");
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return 0;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Setup callback functions. nghttp2 API offers many callback
Packit Bot 4c90e1
 * functions, but most of them are optional. The send_callback is
Packit Bot 4c90e1
 * always required. Since we use nghttp2_session_recv(), the
Packit Bot 4c90e1
 * recv_callback is also required.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static void setup_nghttp2_callbacks(nghttp2_session_callbacks *callbacks) {
Packit Bot 4c90e1
  nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
Packit Bot 4c90e1
Packit Bot 4c90e1
  nghttp2_session_callbacks_set_recv_callback(callbacks, recv_callback);
Packit Bot 4c90e1
Packit Bot 4c90e1
  nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
Packit Bot 4c90e1
                                                       on_frame_send_callback);
Packit Bot 4c90e1
Packit Bot 4c90e1
  nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
Packit Bot 4c90e1
                                                       on_frame_recv_callback);
Packit Bot 4c90e1
Packit Bot 4c90e1
  nghttp2_session_callbacks_set_on_stream_close_callback(
Packit Bot 4c90e1
      callbacks, on_stream_close_callback);
Packit Bot 4c90e1
Packit Bot 4c90e1
  nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
Packit Bot 4c90e1
      callbacks, on_data_chunk_recv_callback);
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
#ifndef OPENSSL_NO_NEXTPROTONEG
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Callback function for TLS NPN. Since this program only supports
Packit Bot 4c90e1
 * HTTP/2 protocol, if server does not offer HTTP/2 the nghttp2
Packit Bot 4c90e1
 * library supports, we terminate program.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static int select_next_proto_cb(SSL *ssl, unsigned char **out,
Packit Bot 4c90e1
                                unsigned char *outlen, const unsigned char *in,
Packit Bot 4c90e1
                                unsigned int inlen, void *arg) {
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  (void)ssl;
Packit Bot 4c90e1
  (void)arg;
Packit Bot 4c90e1
Packit Bot 4c90e1
  /* nghttp2_select_next_protocol() selects HTTP/2 protocol the
Packit Bot 4c90e1
     nghttp2 library supports. */
Packit Bot 4c90e1
  rv = nghttp2_select_next_protocol(out, outlen, in, inlen);
Packit Bot 4c90e1
  if (rv <= 0) {
Packit Bot 4c90e1
    die("Server did not advertise HTTP/2 protocol");
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return SSL_TLSEXT_ERR_OK;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
#endif /* !OPENSSL_NO_NEXTPROTONEG */
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Setup SSL/TLS context.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static void init_ssl_ctx(SSL_CTX *ssl_ctx) {
Packit Bot 4c90e1
  /* Disable SSLv2 and enable all workarounds for buggy servers */
Packit Bot 4c90e1
  SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2);
Packit Bot 4c90e1
  SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
Packit Bot 4c90e1
  SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
Packit Bot 4c90e1
  /* Set NPN callback */
Packit Bot 4c90e1
#ifndef OPENSSL_NO_NEXTPROTONEG
Packit Bot 4c90e1
  SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, NULL);
Packit Bot 4c90e1
#endif /* !OPENSSL_NO_NEXTPROTONEG */
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static void ssl_handshake(SSL *ssl, int fd) {
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  if (SSL_set_fd(ssl, fd) == 0) {
Packit Bot 4c90e1
    dief("SSL_set_fd", ERR_error_string(ERR_get_error(), NULL));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  ERR_clear_error();
Packit Bot 4c90e1
  rv = SSL_connect(ssl);
Packit Bot 4c90e1
  if (rv <= 0) {
Packit Bot 4c90e1
    dief("SSL_connect", ERR_error_string(ERR_get_error(), NULL));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Connects to the host |host| and port |port|.  This function returns
Packit Bot 4c90e1
 * the file descriptor of the client socket.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static int connect_to(const char *host, uint16_t port) {
Packit Bot 4c90e1
  struct addrinfo hints;
Packit Bot 4c90e1
  int fd = -1;
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  char service[NI_MAXSERV];
Packit Bot 4c90e1
  struct addrinfo *res, *rp;
Packit Bot 4c90e1
  snprintf(service, sizeof(service), "%u", port);
Packit Bot 4c90e1
  memset(&hints, 0, sizeof(struct addrinfo));
Packit Bot 4c90e1
  hints.ai_family = AF_UNSPEC;
Packit Bot 4c90e1
  hints.ai_socktype = SOCK_STREAM;
Packit Bot 4c90e1
  rv = getaddrinfo(host, service, &hints, &res;;
Packit Bot 4c90e1
  if (rv != 0) {
Packit Bot 4c90e1
    dief("getaddrinfo", gai_strerror(rv));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  for (rp = res; rp; rp = rp->ai_next) {
Packit Bot 4c90e1
    fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
Packit Bot 4c90e1
    if (fd == -1) {
Packit Bot 4c90e1
      continue;
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    while ((rv = connect(fd, rp->ai_addr, rp->ai_addrlen)) == -1 &&
Packit Bot 4c90e1
           errno == EINTR)
Packit Bot 4c90e1
      ;
Packit Bot 4c90e1
    if (rv == 0) {
Packit Bot 4c90e1
      break;
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    close(fd);
Packit Bot 4c90e1
    fd = -1;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  freeaddrinfo(res);
Packit Bot 4c90e1
  return fd;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static void make_non_block(int fd) {
Packit Bot 4c90e1
  int flags, rv;
Packit Bot 4c90e1
  while ((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR)
Packit Bot 4c90e1
    ;
Packit Bot 4c90e1
  if (flags == -1) {
Packit Bot 4c90e1
    dief("fcntl", strerror(errno));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  while ((rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR)
Packit Bot 4c90e1
    ;
Packit Bot 4c90e1
  if (rv == -1) {
Packit Bot 4c90e1
    dief("fcntl", strerror(errno));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static void set_tcp_nodelay(int fd) {
Packit Bot 4c90e1
  int val = 1;
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val));
Packit Bot 4c90e1
  if (rv == -1) {
Packit Bot 4c90e1
    dief("setsockopt", strerror(errno));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Update |pollfd| based on the state of |connection|.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static void ctl_poll(struct pollfd *pollfd, struct Connection *connection) {
Packit Bot 4c90e1
  pollfd->events = 0;
Packit Bot 4c90e1
  if (nghttp2_session_want_read(connection->session) ||
Packit Bot 4c90e1
      connection->want_io == WANT_READ) {
Packit Bot 4c90e1
    pollfd->events |= POLLIN;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  if (nghttp2_session_want_write(connection->session) ||
Packit Bot 4c90e1
      connection->want_io == WANT_WRITE) {
Packit Bot 4c90e1
    pollfd->events |= POLLOUT;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Submits the request |req| to the connection |connection|.  This
Packit Bot 4c90e1
 * function does not send packets; just append the request to the
Packit Bot 4c90e1
 * internal queue in |connection->session|.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static void submit_request(struct Connection *connection, struct Request *req) {
Packit Bot 4c90e1
  int32_t stream_id;
Packit Bot 4c90e1
  /* Make sure that the last item is NULL */
Packit Bot 4c90e1
  const nghttp2_nv nva[] = {MAKE_NV(":method", "GET"),
Packit Bot 4c90e1
                            MAKE_NV_CS(":path", req->path),
Packit Bot 4c90e1
                            MAKE_NV(":scheme", "https"),
Packit Bot 4c90e1
                            MAKE_NV_CS(":authority", req->hostport),
Packit Bot 4c90e1
                            MAKE_NV("accept", "*/*"),
Packit Bot 4c90e1
                            MAKE_NV("user-agent", "nghttp2/" NGHTTP2_VERSION)};
Packit Bot 4c90e1
Packit Bot 4c90e1
  stream_id = nghttp2_submit_request(connection->session, NULL, nva,
Packit Bot 4c90e1
                                     sizeof(nva) / sizeof(nva[0]), NULL, req);
Packit Bot 4c90e1
Packit Bot 4c90e1
  if (stream_id < 0) {
Packit Bot 4c90e1
    diec("nghttp2_submit_request", stream_id);
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
  req->stream_id = stream_id;
Packit Bot 4c90e1
  printf("[INFO] Stream ID = %d\n", stream_id);
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Performs the network I/O.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static void exec_io(struct Connection *connection) {
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  rv = nghttp2_session_recv(connection->session);
Packit Bot 4c90e1
  if (rv != 0) {
Packit Bot 4c90e1
    diec("nghttp2_session_recv", rv);
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  rv = nghttp2_session_send(connection->session);
Packit Bot 4c90e1
  if (rv != 0) {
Packit Bot 4c90e1
    diec("nghttp2_session_send", rv);
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static void request_init(struct Request *req, const struct URI *uri) {
Packit Bot 4c90e1
  req->host = strcopy(uri->host, uri->hostlen);
Packit Bot 4c90e1
  req->port = uri->port;
Packit Bot 4c90e1
  req->path = strcopy(uri->path, uri->pathlen);
Packit Bot 4c90e1
  req->hostport = strcopy(uri->hostport, uri->hostportlen);
Packit Bot 4c90e1
  req->stream_id = -1;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static void request_free(struct Request *req) {
Packit Bot 4c90e1
  free(req->host);
Packit Bot 4c90e1
  free(req->path);
Packit Bot 4c90e1
  free(req->hostport);
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
/*
Packit Bot 4c90e1
 * Fetches the resource denoted by |uri|.
Packit Bot 4c90e1
 */
Packit Bot 4c90e1
static void fetch_uri(const struct URI *uri) {
Packit Bot 4c90e1
  nghttp2_session_callbacks *callbacks;
Packit Bot 4c90e1
  int fd;
Packit Bot 4c90e1
  SSL_CTX *ssl_ctx;
Packit Bot 4c90e1
  SSL *ssl;
Packit Bot 4c90e1
  struct Request req;
Packit Bot 4c90e1
  struct Connection connection;
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
  nfds_t npollfds = 1;
Packit Bot 4c90e1
  struct pollfd pollfds[1];
Packit Bot 4c90e1
Packit Bot 4c90e1
  request_init(&req, uri);
Packit Bot 4c90e1
Packit Bot 4c90e1
  /* Establish connection and setup SSL */
Packit Bot 4c90e1
  fd = connect_to(req.host, req.port);
Packit Bot 4c90e1
  if (fd == -1) {
Packit Bot 4c90e1
    die("Could not open file descriptor");
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  ssl_ctx = SSL_CTX_new(SSLv23_client_method());
Packit Bot 4c90e1
  if (ssl_ctx == NULL) {
Packit Bot 4c90e1
    dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  init_ssl_ctx(ssl_ctx);
Packit Bot 4c90e1
  ssl = SSL_new(ssl_ctx);
Packit Bot 4c90e1
  if (ssl == NULL) {
Packit Bot 4c90e1
    dief("SSL_new", ERR_error_string(ERR_get_error(), NULL));
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  /* To simplify the program, we perform SSL/TLS handshake in blocking
Packit Bot 4c90e1
     I/O. */
Packit Bot 4c90e1
  ssl_handshake(ssl, fd);
Packit Bot 4c90e1
Packit Bot 4c90e1
  connection.ssl = ssl;
Packit Bot 4c90e1
  connection.want_io = IO_NONE;
Packit Bot 4c90e1
Packit Bot 4c90e1
  /* Here make file descriptor non-block */
Packit Bot 4c90e1
  make_non_block(fd);
Packit Bot 4c90e1
  set_tcp_nodelay(fd);
Packit Bot 4c90e1
Packit Bot 4c90e1
  printf("[INFO] SSL/TLS handshake completed\n");
Packit Bot 4c90e1
Packit Bot 4c90e1
  rv = nghttp2_session_callbacks_new(&callbacks);
Packit Bot 4c90e1
Packit Bot 4c90e1
  if (rv != 0) {
Packit Bot 4c90e1
    diec("nghttp2_session_callbacks_new", rv);
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
  setup_nghttp2_callbacks(callbacks);
Packit Bot 4c90e1
Packit Bot 4c90e1
  rv = nghttp2_session_client_new(&connection.session, callbacks, &connection);
Packit Bot 4c90e1
Packit Bot 4c90e1
  nghttp2_session_callbacks_del(callbacks);
Packit Bot 4c90e1
Packit Bot 4c90e1
  if (rv != 0) {
Packit Bot 4c90e1
    diec("nghttp2_session_client_new", rv);
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
  rv = nghttp2_submit_settings(connection.session, NGHTTP2_FLAG_NONE, NULL, 0);
Packit Bot 4c90e1
Packit Bot 4c90e1
  if (rv != 0) {
Packit Bot 4c90e1
    diec("nghttp2_submit_settings", rv);
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
  /* Submit the HTTP request to the outbound queue. */
Packit Bot 4c90e1
  submit_request(&connection, &req;;
Packit Bot 4c90e1
Packit Bot 4c90e1
  pollfds[0].fd = fd;
Packit Bot 4c90e1
  ctl_poll(pollfds, &connection);
Packit Bot 4c90e1
Packit Bot 4c90e1
  /* Event loop */
Packit Bot 4c90e1
  while (nghttp2_session_want_read(connection.session) ||
Packit Bot 4c90e1
         nghttp2_session_want_write(connection.session)) {
Packit Bot 4c90e1
    int nfds = poll(pollfds, npollfds, -1);
Packit Bot 4c90e1
    if (nfds == -1) {
Packit Bot 4c90e1
      dief("poll", strerror(errno));
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    if (pollfds[0].revents & (POLLIN | POLLOUT)) {
Packit Bot 4c90e1
      exec_io(&connection);
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    if ((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) {
Packit Bot 4c90e1
      die("Connection error");
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    ctl_poll(pollfds, &connection);
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
  /* Resource cleanup */
Packit Bot 4c90e1
  nghttp2_session_del(connection.session);
Packit Bot 4c90e1
  SSL_shutdown(ssl);
Packit Bot 4c90e1
  SSL_free(ssl);
Packit Bot 4c90e1
  SSL_CTX_free(ssl_ctx);
Packit Bot 4c90e1
  shutdown(fd, SHUT_WR);
Packit Bot 4c90e1
  close(fd);
Packit Bot 4c90e1
  request_free(&req;;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
static int parse_uri(struct URI *res, const char *uri) {
Packit Bot 4c90e1
  /* We only interested in https */
Packit Bot 4c90e1
  size_t len, i, offset;
Packit Bot 4c90e1
  int ipv6addr = 0;
Packit Bot 4c90e1
  memset(res, 0, sizeof(struct URI));
Packit Bot 4c90e1
  len = strlen(uri);
Packit Bot 4c90e1
  if (len < 9 || memcmp("https://", uri, 8) != 0) {
Packit Bot 4c90e1
    return -1;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  offset = 8;
Packit Bot 4c90e1
  res->host = res->hostport = &uri[offset];
Packit Bot 4c90e1
  res->hostlen = 0;
Packit Bot 4c90e1
  if (uri[offset] == '[') {
Packit Bot 4c90e1
    /* IPv6 literal address */
Packit Bot 4c90e1
    ++offset;
Packit Bot 4c90e1
    ++res->host;
Packit Bot 4c90e1
    ipv6addr = 1;
Packit Bot 4c90e1
    for (i = offset; i < len; ++i) {
Packit Bot 4c90e1
      if (uri[i] == ']') {
Packit Bot 4c90e1
        res->hostlen = i - offset;
Packit Bot 4c90e1
        offset = i + 1;
Packit Bot 4c90e1
        break;
Packit Bot 4c90e1
      }
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
  } else {
Packit Bot 4c90e1
    const char delims[] = ":/?#";
Packit Bot 4c90e1
    for (i = offset; i < len; ++i) {
Packit Bot 4c90e1
      if (strchr(delims, uri[i]) != NULL) {
Packit Bot 4c90e1
        break;
Packit Bot 4c90e1
      }
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
    res->hostlen = i - offset;
Packit Bot 4c90e1
    offset = i;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  if (res->hostlen == 0) {
Packit Bot 4c90e1
    return -1;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  /* Assuming https */
Packit Bot 4c90e1
  res->port = 443;
Packit Bot 4c90e1
  if (offset < len) {
Packit Bot 4c90e1
    if (uri[offset] == ':') {
Packit Bot 4c90e1
      /* port */
Packit Bot 4c90e1
      const char delims[] = "/?#";
Packit Bot 4c90e1
      int port = 0;
Packit Bot 4c90e1
      ++offset;
Packit Bot 4c90e1
      for (i = offset; i < len; ++i) {
Packit Bot 4c90e1
        if (strchr(delims, uri[i]) != NULL) {
Packit Bot 4c90e1
          break;
Packit Bot 4c90e1
        }
Packit Bot 4c90e1
        if ('0' <= uri[i] && uri[i] <= '9') {
Packit Bot 4c90e1
          port *= 10;
Packit Bot 4c90e1
          port += uri[i] - '0';
Packit Bot 4c90e1
          if (port > 65535) {
Packit Bot 4c90e1
            return -1;
Packit Bot 4c90e1
          }
Packit Bot 4c90e1
        } else {
Packit Bot 4c90e1
          return -1;
Packit Bot 4c90e1
        }
Packit Bot 4c90e1
      }
Packit Bot 4c90e1
      if (port == 0) {
Packit Bot 4c90e1
        return -1;
Packit Bot 4c90e1
      }
Packit Bot 4c90e1
      offset = i;
Packit Bot 4c90e1
      res->port = (uint16_t)port;
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  res->hostportlen = (size_t)(uri + offset + ipv6addr - res->host);
Packit Bot 4c90e1
  for (i = offset; i < len; ++i) {
Packit Bot 4c90e1
    if (uri[i] == '#') {
Packit Bot 4c90e1
      break;
Packit Bot 4c90e1
    }
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  if (i - offset == 0) {
Packit Bot 4c90e1
    res->path = "/";
Packit Bot 4c90e1
    res->pathlen = 1;
Packit Bot 4c90e1
  } else {
Packit Bot 4c90e1
    res->path = &uri[offset];
Packit Bot 4c90e1
    res->pathlen = i - offset;
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  return 0;
Packit Bot 4c90e1
}
Packit Bot 4c90e1
Packit Bot 4c90e1
int main(int argc, char **argv) {
Packit Bot 4c90e1
  struct URI uri;
Packit Bot 4c90e1
  struct sigaction act;
Packit Bot 4c90e1
  int rv;
Packit Bot 4c90e1
Packit Bot 4c90e1
  if (argc < 2) {
Packit Bot 4c90e1
    die("Specify a https URI");
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
Packit Bot 4c90e1
  memset(&act, 0, sizeof(struct sigaction));
Packit Bot 4c90e1
  act.sa_handler = SIG_IGN;
Packit Bot 4c90e1
  sigaction(SIGPIPE, &act, 0);
Packit Bot 4c90e1
Packit Bot 4c90e1
  SSL_load_error_strings();
Packit Bot 4c90e1
  SSL_library_init();
Packit Bot 4c90e1
Packit Bot 4c90e1
  rv = parse_uri(&uri, argv[1]);
Packit Bot 4c90e1
  if (rv != 0) {
Packit Bot 4c90e1
    die("parse_uri failed");
Packit Bot 4c90e1
  }
Packit Bot 4c90e1
  fetch_uri(&uri);
Packit Bot 4c90e1
  return EXIT_SUCCESS;
Packit Bot 4c90e1
}