nghttp2/examples/client.c

728 lines
20 KiB
C
Raw Permalink Normal View History

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