2014-03-30 12:09:21 +02:00
|
|
|
Tutorial: HTTP/2 client
|
2013-12-24 16:30:05 +01:00
|
|
|
=========================
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
In this tutorial, we are going to write a very primitive HTTP/2
|
2013-12-24 16:35:15 +01:00
|
|
|
client. The complete source code, `libevent-client.c`_, is attached at
|
2015-08-04 00:36:00 +02:00
|
|
|
the end of this page. It also resides in the examples directory in
|
|
|
|
the archive or repository.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
This simple client takes a single HTTPS URI and retrieves the resource
|
|
|
|
at the URI. The synopsis is::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
|
|
|
$ libevent-client HTTPS_URI
|
|
|
|
|
|
|
|
We use libevent in this tutorial to handle networking I/O. Please
|
2013-12-28 14:09:17 +01:00
|
|
|
note that nghttp2 itself does not depend on libevent.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
The client starts with some libevent and OpenSSL setup in the
|
|
|
|
``main()`` and ``run()`` functions. This setup isn't specific to
|
|
|
|
nghttp2, but one thing you should look at is setup of the NPN
|
|
|
|
callback. The NPN callback is used by the client to select the next
|
|
|
|
application protocol over TLS. In this tutorial, we use the
|
|
|
|
`nghttp2_select_next_protocol()` helper function to select the HTTP/2
|
2013-12-24 16:30:05 +01:00
|
|
|
protocol the library supports::
|
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static int select_next_proto_cb(SSL *ssl _U_, unsigned char **out,
|
|
|
|
unsigned char *outlen, const unsigned char *in,
|
|
|
|
unsigned int inlen, void *arg _U_) {
|
|
|
|
if (nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
errx(1, "Server did not advertise " NGHTTP2_PROTO_VERSION_ID);
|
|
|
|
}
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:03:04 +02:00
|
|
|
If you are following TLS related RFC, you know that NPN is not the
|
|
|
|
standardized way to negotiate HTTP/2. NPN itself is not event
|
|
|
|
published as RFC. The standard way to negotiate HTTP/2 is ALPN,
|
|
|
|
Application-Layer Protocol Negotiation Extension, defined in `RFC 7301
|
|
|
|
<https://tools.ietf.org/html/rfc7301>`_. The one caveat of ALPN is
|
|
|
|
that OpenSSL >= 1.0.2 is required. We use macro to enable/disable
|
|
|
|
ALPN support depending on OpenSSL version. OpenSSL's ALPN
|
|
|
|
implementation does not require callback function like the above. But
|
|
|
|
we have to instruct OpenSSL SSL_CTX to use ALPN, which we'll talk
|
|
|
|
about soon.
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
The callback is added to the SSL_CTX object using
|
|
|
|
``SSL_CTX_set_next_proto_select_cb()``::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static SSL_CTX *create_ssl_ctx(void) {
|
2013-12-24 16:30:05 +01:00
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
2014-11-30 13:10:59 +01:00
|
|
|
if (!ssl_ctx) {
|
2013-12-24 16:30:05 +01:00
|
|
|
errx(1, "Could not create SSL/TLS context: %s",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
}
|
|
|
|
SSL_CTX_set_options(ssl_ctx,
|
2014-11-30 13:10:59 +01:00
|
|
|
SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
|
|
|
|
SSL_OP_NO_COMPRESSION |
|
|
|
|
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
|
2013-12-24 16:30:05 +01:00
|
|
|
SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, NULL);
|
2016-06-19 16:03:04 +02:00
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
SSL_CTX_set_alpn_protos(ssl_ctx, (const unsigned char *)"\x02h2", 3);
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
|
2013-12-24 16:30:05 +01:00
|
|
|
return ssl_ctx;
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:03:04 +02:00
|
|
|
Here we see ``SSL_CTX_get_alpn_protos()`` function call. We instructs
|
|
|
|
OpenSSL to notify the server that we support h2, ALPN identifier for
|
|
|
|
HTTP/2.
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
The example client defines a couple of structs:
|
|
|
|
|
|
|
|
We define and use a ``http2_session_data`` structure to store data
|
|
|
|
related to the HTTP/2 session::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
nghttp2_session *session;
|
|
|
|
struct evdns_base *dnsbase;
|
|
|
|
struct bufferevent *bev;
|
|
|
|
http2_stream_data *stream_data;
|
|
|
|
} http2_session_data;
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
Since this program only handles one URI, it uses only one stream. We
|
|
|
|
store the single stream's data in a ``http2_stream_data`` structure
|
|
|
|
and the ``stream_data`` points to it. The ``http2_stream_data``
|
|
|
|
structure is defined as follows::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
|
|
|
typedef struct {
|
2015-05-12 03:49:42 +02:00
|
|
|
/* The NULL-terminated URI string to retrieve. */
|
2013-12-24 16:30:05 +01:00
|
|
|
const char *uri;
|
|
|
|
/* Parsed result of the |uri| */
|
|
|
|
struct http_parser_url *u;
|
2015-05-12 03:49:42 +02:00
|
|
|
/* The authority portion of the |uri|, not NULL-terminated */
|
2013-12-24 16:30:05 +01:00
|
|
|
char *authority;
|
|
|
|
/* The path portion of the |uri|, including query, not
|
|
|
|
NULL-terminated */
|
|
|
|
char *path;
|
|
|
|
/* The length of the |authority| */
|
|
|
|
size_t authoritylen;
|
|
|
|
/* The length of the |path| */
|
|
|
|
size_t pathlen;
|
|
|
|
/* The stream ID of this stream */
|
|
|
|
int32_t stream_id;
|
|
|
|
} http2_stream_data;
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
We create and initialize these structures in
|
2013-12-24 16:30:05 +01:00
|
|
|
``create_http2_session_data()`` and ``create_http2_stream_data()``
|
|
|
|
respectively.
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
``initiate_connection()`` is called to start the connection to the
|
|
|
|
remote server. It's defined as::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static void initiate_connection(struct event_base *evbase, SSL_CTX *ssl_ctx,
|
2013-12-24 16:30:05 +01:00
|
|
|
const char *host, uint16_t port,
|
2014-11-30 13:10:59 +01:00
|
|
|
http2_session_data *session_data) {
|
2013-12-24 16:30:05 +01:00
|
|
|
int rv;
|
|
|
|
struct bufferevent *bev;
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
ssl = create_ssl(ssl_ctx);
|
2014-11-30 13:10:59 +01:00
|
|
|
bev = bufferevent_openssl_socket_new(
|
|
|
|
evbase, -1, ssl, BUFFEREVENT_SSL_CONNECTING,
|
|
|
|
BEV_OPT_DEFER_CALLBACKS | BEV_OPT_CLOSE_ON_FREE);
|
2013-12-24 16:30:05 +01:00
|
|
|
bufferevent_setcb(bev, readcb, writecb, eventcb, session_data);
|
|
|
|
rv = bufferevent_socket_connect_hostname(bev, session_data->dnsbase,
|
|
|
|
AF_UNSPEC, host, port);
|
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
if (rv != 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
errx(1, "Could not connect to the remote host %s", host);
|
|
|
|
}
|
|
|
|
session_data->bev = bev;
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
``initiate_connection()`` creates a bufferevent for the connection and
|
|
|
|
sets up three callbacks: ``readcb``, ``writecb``, and ``eventcb``.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
The ``eventcb()`` is invoked by the libevent event loop when an event
|
|
|
|
(e.g. connection has been established, timeout, etc.) occurs on the
|
2013-12-28 14:09:17 +01:00
|
|
|
underlying network socket::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static void eventcb(struct bufferevent *bev, short events, void *ptr) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)ptr;
|
|
|
|
if (events & BEV_EVENT_CONNECTED) {
|
2013-12-24 16:30:05 +01:00
|
|
|
int fd = bufferevent_getfd(bev);
|
|
|
|
int val = 1;
|
2016-06-19 16:03:04 +02:00
|
|
|
const unsigned char *alpn = NULL;
|
|
|
|
unsigned int alpnlen = 0;
|
|
|
|
SSL *ssl;
|
|
|
|
|
2013-12-24 16:30:05 +01:00
|
|
|
fprintf(stderr, "Connected\n");
|
2016-06-19 16:03:04 +02:00
|
|
|
|
|
|
|
ssl = bufferevent_openssl_get_ssl(session_data->bev);
|
|
|
|
|
|
|
|
SSL_get0_next_proto_negotiated(ssl, &alpn, &alpnlen);
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
if (alpn == NULL) {
|
|
|
|
SSL_get0_alpn_selected(ssl, &alpn, &alpnlen);
|
|
|
|
}
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
|
|
|
|
if (alpn == NULL || alpnlen != 2 || memcmp("h2", alpn, 2) != 0) {
|
|
|
|
fprintf(stderr, "h2 is not negotiated\n");
|
|
|
|
delete_http2_session_data(session_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-24 16:30:05 +01:00
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
|
|
|
|
initialize_nghttp2_session(session_data);
|
|
|
|
send_client_connection_header(session_data);
|
|
|
|
submit_request(session_data);
|
2014-11-30 13:10:59 +01:00
|
|
|
if (session_send(session_data) != 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
delete_http2_session_data(session_data);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-11-30 13:10:59 +01:00
|
|
|
if (events & BEV_EVENT_EOF) {
|
2013-12-24 16:30:05 +01:00
|
|
|
warnx("Disconnected from the remote host");
|
2014-11-30 13:10:59 +01:00
|
|
|
} else if (events & BEV_EVENT_ERROR) {
|
2013-12-24 16:30:05 +01:00
|
|
|
warnx("Network error");
|
2014-11-30 13:10:59 +01:00
|
|
|
} else if (events & BEV_EVENT_TIMEOUT) {
|
2013-12-24 16:30:05 +01:00
|
|
|
warnx("Timeout");
|
|
|
|
}
|
|
|
|
delete_http2_session_data(session_data);
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:03:04 +02:00
|
|
|
Here we validate that HTTP/2 is negotiated, and if not, drop
|
|
|
|
connection.
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
For ``BEV_EVENT_EOF``, ``BEV_EVENT_ERROR``, and ``BEV_EVENT_TIMEOUT``
|
|
|
|
events, we just simply tear down the connection.
|
|
|
|
|
|
|
|
The ``BEV_EVENT_CONNECTED`` event is invoked when the SSL/TLS
|
|
|
|
handshake has completed successfully. After this we're ready to begin
|
|
|
|
communicating via HTTP/2.
|
|
|
|
|
|
|
|
The ``initialize_nghttp2_session()`` function initializes the nghttp2
|
|
|
|
session object and several callbacks::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static void initialize_nghttp2_session(http2_session_data *session_data) {
|
2014-08-23 10:55:17 +02:00
|
|
|
nghttp2_session_callbacks *callbacks;
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_new(&callbacks);
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
|
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
|
|
|
|
on_frame_recv_callback);
|
2014-08-23 10:55:17 +02:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
|
|
|
|
callbacks, on_data_chunk_recv_callback);
|
2014-08-23 10:55:17 +02:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
nghttp2_session_callbacks_set_on_stream_close_callback(
|
|
|
|
callbacks, on_stream_close_callback);
|
2014-08-23 10:55:17 +02:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
nghttp2_session_callbacks_set_on_header_callback(callbacks,
|
|
|
|
on_header_callback);
|
2014-08-23 10:55:17 +02:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
nghttp2_session_callbacks_set_on_begin_headers_callback(
|
|
|
|
callbacks, on_begin_headers_callback);
|
2014-08-23 10:55:17 +02:00
|
|
|
|
|
|
|
nghttp2_session_client_new(&session_data->session, callbacks, session_data);
|
|
|
|
|
|
|
|
nghttp2_session_callbacks_del(callbacks);
|
2013-12-24 16:30:05 +01:00
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
Since we are creating a client, we use `nghttp2_session_client_new()`
|
|
|
|
to initialize the nghttp2 session object. The callbacks setup are
|
|
|
|
explained later.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
The `delete_http2_session_data()` function destroys ``session_data``
|
|
|
|
and frees its bufferevent, so the underlying connection is closed. It
|
|
|
|
also calls `nghttp2_session_del()` to delete the nghttp2 session
|
|
|
|
object.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
A HTTP/2 connection begins by sending the client connection preface,
|
|
|
|
which is a 24 byte magic byte string (:macro:`NGHTTP2_CLIENT_MAGIC`),
|
|
|
|
followed by a SETTINGS frame. The 24 byte magic string is sent
|
|
|
|
automatically by nghttp2. We send the SETTINGS frame in
|
2013-12-24 16:30:05 +01:00
|
|
|
``send_client_connection_header()``::
|
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static void send_client_connection_header(http2_session_data *session_data) {
|
2013-12-24 16:30:05 +01:00
|
|
|
nghttp2_settings_entry iv[1] = {
|
2014-11-30 13:10:59 +01:00
|
|
|
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100}};
|
2013-12-24 16:30:05 +01:00
|
|
|
int rv;
|
|
|
|
|
2015-04-05 15:35:40 +02:00
|
|
|
/* client 24 bytes magic string will be sent by nghttp2 library */
|
2014-11-30 13:10:59 +01:00
|
|
|
rv = nghttp2_submit_settings(session_data->session, NGHTTP2_FLAG_NONE, iv,
|
|
|
|
ARRLEN(iv));
|
|
|
|
if (rv != 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
errx(1, "Could not submit SETTINGS: %s", nghttp2_strerror(rv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
Here we specify SETTINGS_MAX_CONCURRENT_STREAMS as 100. This is not
|
|
|
|
needed for this tiny example program, it just demonstrates use of the
|
|
|
|
SETTINGS frame. To queue the SETTINGS frame for transmission, we call
|
|
|
|
`nghttp2_submit_settings()`. Note that `nghttp2_submit_settings()`
|
|
|
|
only queues the frame for transmission, and doesn't actually send it.
|
|
|
|
All ``nghttp2_submit_*()`` family functions have this property. To
|
|
|
|
actually send the frame, `nghttp2_session_send()` has to be called,
|
|
|
|
which is described (and called) later.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
After the transmission of the client connection header, we enqueue the
|
|
|
|
HTTP request in the ``submit_request()`` function::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static void submit_request(http2_session_data *session_data) {
|
2014-05-07 17:11:36 +02:00
|
|
|
int32_t stream_id;
|
2013-12-24 16:30:05 +01:00
|
|
|
http2_stream_data *stream_data = session_data->stream_data;
|
|
|
|
const char *uri = stream_data->uri;
|
|
|
|
const struct http_parser_url *u = stream_data->u;
|
|
|
|
nghttp2_nv hdrs[] = {
|
2014-11-30 13:10:59 +01:00
|
|
|
MAKE_NV2(":method", "GET"),
|
|
|
|
MAKE_NV(":scheme", &uri[u->field_data[UF_SCHEMA].off],
|
|
|
|
u->field_data[UF_SCHEMA].len),
|
|
|
|
MAKE_NV(":authority", stream_data->authority, stream_data->authoritylen),
|
|
|
|
MAKE_NV(":path", stream_data->path, stream_data->pathlen)};
|
2013-12-24 16:30:05 +01:00
|
|
|
fprintf(stderr, "Request headers:\n");
|
|
|
|
print_headers(stderr, hdrs, ARRLEN(hdrs));
|
2014-11-30 13:10:59 +01:00
|
|
|
stream_id = nghttp2_submit_request(session_data->session, NULL, hdrs,
|
|
|
|
ARRLEN(hdrs), NULL, stream_data);
|
|
|
|
if (stream_id < 0) {
|
2014-05-07 17:11:36 +02:00
|
|
|
errx(1, "Could not submit HTTP request: %s", nghttp2_strerror(stream_id));
|
2013-12-24 16:30:05 +01:00
|
|
|
}
|
2014-05-07 17:11:36 +02:00
|
|
|
|
|
|
|
stream_data->stream_id = stream_id;
|
2013-12-24 16:30:05 +01:00
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
We build the HTTP request header fields in ``hdrs``, which is an array
|
|
|
|
of :type:`nghttp2_nv`. There are four header fields to be sent:
|
|
|
|
``:method``, ``:scheme``, ``:authority``, and ``:path``. To queue the
|
|
|
|
HTTP request, we call `nghttp2_submit_request()`. The ``stream_data``
|
|
|
|
is passed via the *stream_user_data* parameter, which is helpfully
|
|
|
|
later passed back to callback functions.
|
|
|
|
|
2014-05-07 17:11:36 +02:00
|
|
|
`nghttp2_submit_request()` returns the newly assigned stream ID for
|
2015-08-04 00:36:00 +02:00
|
|
|
the request.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
|
|
|
The next bufferevent callback is ``readcb()``, which is invoked when
|
2015-08-04 00:36:00 +02:00
|
|
|
data is available to read from the bufferevent input buffer::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static void readcb(struct bufferevent *bev, void *ptr) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)ptr;
|
2014-06-14 15:47:56 +02:00
|
|
|
ssize_t readlen;
|
2013-12-24 16:30:05 +01:00
|
|
|
struct evbuffer *input = bufferevent_get_input(bev);
|
|
|
|
size_t datalen = evbuffer_get_length(input);
|
|
|
|
unsigned char *data = evbuffer_pullup(input, -1);
|
2014-06-14 15:47:56 +02:00
|
|
|
|
|
|
|
readlen = nghttp2_session_mem_recv(session_data->session, data, datalen);
|
2014-11-30 13:10:59 +01:00
|
|
|
if (readlen < 0) {
|
2014-06-14 15:47:56 +02:00
|
|
|
warnx("Fatal error: %s", nghttp2_strerror((int)readlen));
|
2013-12-24 16:30:05 +01:00
|
|
|
delete_http2_session_data(session_data);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-23 15:57:53 +02:00
|
|
|
if (evbuffer_drain(input, (size_t)readlen) != 0) {
|
2014-06-18 06:14:07 +02:00
|
|
|
warnx("Fatal error: evbuffer_drain failed");
|
|
|
|
delete_http2_session_data(session_data);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-30 13:10:59 +01:00
|
|
|
if (session_send(session_data) != 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
delete_http2_session_data(session_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
In this function we feed all unprocessed, received data to the nghttp2
|
|
|
|
session object using the `nghttp2_session_mem_recv()` function.
|
2013-12-24 16:30:05 +01:00
|
|
|
`nghttp2_session_mem_recv()` processes the received data and may
|
2015-08-04 00:36:00 +02:00
|
|
|
invoke nghttp2 callbacks and queue frames for transmission. Since
|
|
|
|
there may be pending frames for transmission, we call immediately
|
|
|
|
``session_send()`` to send them. ``session_send()`` is defined as
|
|
|
|
follows::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static int session_send(http2_session_data *session_data) {
|
2013-12-24 16:30:05 +01:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = nghttp2_session_send(session_data->session);
|
2014-11-30 13:10:59 +01:00
|
|
|
if (rv != 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
warnx("Fatal error: %s", nghttp2_strerror(rv));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
The `nghttp2_session_send()` function serializes pending frames into
|
|
|
|
wire format and calls the ``send_callback()`` function to send them.
|
|
|
|
``send_callback()`` has type :type:`nghttp2_send_callback` and is
|
|
|
|
defined as::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static ssize_t send_callback(nghttp2_session *session _U_, const uint8_t *data,
|
|
|
|
size_t length, int flags _U_, void *user_data) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)user_data;
|
2013-12-24 16:30:05 +01:00
|
|
|
struct bufferevent *bev = session_data->bev;
|
|
|
|
bufferevent_write(bev, data, length);
|
2015-09-23 15:57:53 +02:00
|
|
|
return (ssize_t)length;
|
2013-12-24 16:30:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Since we use bufferevent to abstract network I/O, we just write the
|
|
|
|
data to the bufferevent object. Note that `nghttp2_session_send()`
|
|
|
|
continues to write all frames queued so far. If we were writing the
|
2015-08-04 00:36:00 +02:00
|
|
|
data to the non-blocking socket directly using the ``write()`` system
|
|
|
|
call, we'd soon receive an ``EAGAIN`` or ``EWOULDBLOCK`` error, since
|
|
|
|
sockets have a limited send buffer. If that happens, it's possible to
|
|
|
|
return :macro:`NGHTTP2_ERR_WOULDBLOCK` to signal the nghttp2 library
|
|
|
|
to stop sending further data. When writing to a bufferevent, you
|
|
|
|
should regulate the amount of data written, to avoid possible huge
|
|
|
|
memory consumption. In this example client however we don't implement
|
|
|
|
a limit. To see how to regulate the amount of buffered data, see the
|
|
|
|
``send_callback()`` in the server tutorial.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
|
|
|
The third bufferevent callback is ``writecb()``, which is invoked when
|
2015-08-04 00:36:00 +02:00
|
|
|
all data written in the bufferevent output buffer has been sent::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static void writecb(struct bufferevent *bev _U_, void *ptr) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)ptr;
|
|
|
|
if (nghttp2_session_want_read(session_data->session) == 0 &&
|
|
|
|
nghttp2_session_want_write(session_data->session) == 0 &&
|
|
|
|
evbuffer_get_length(bufferevent_get_output(session_data->bev)) == 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
delete_http2_session_data(session_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
As described earlier, we just write off all data in `send_callback()`,
|
2015-08-04 00:36:00 +02:00
|
|
|
so there is no data to write in this function. All we have to do is
|
|
|
|
check if the connection should be dropped or not. The nghttp2 session
|
|
|
|
object keeps track of reception and transmission of GOAWAY frames and
|
|
|
|
other error conditions. Using this information, the nghttp2 session
|
|
|
|
object can state whether the connection should be dropped or not.
|
|
|
|
More specifically, when both `nghttp2_session_want_read()` and
|
|
|
|
`nghttp2_session_want_write()` return 0, the connection is no-longer
|
|
|
|
required and can be closed. Since we're using bufferevent and its
|
|
|
|
deferred callback option, the bufferevent output buffer may still
|
|
|
|
contain pending data when the ``writecb()`` is called. To handle this
|
|
|
|
situation, we also check whether the output buffer is empty or not. If
|
|
|
|
all of these conditions are met, then we drop the connection.
|
|
|
|
|
|
|
|
Now let's look at the remaining nghttp2 callbacks setup in the
|
2013-12-24 16:30:05 +01:00
|
|
|
``initialize_nghttp2_setup()`` function.
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
A server responds to the request by first sending a HEADERS frame.
|
|
|
|
The HEADERS frame consists of response header name/value pairs, and
|
|
|
|
the ``on_header_callback()`` is called for each name/value pair::
|
2014-01-17 16:06:24 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static int on_header_callback(nghttp2_session *session _U_,
|
|
|
|
const nghttp2_frame *frame, const uint8_t *name,
|
|
|
|
size_t namelen, const uint8_t *value,
|
|
|
|
size_t valuelen, uint8_t flags _U_,
|
|
|
|
void *user_data) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)user_data;
|
|
|
|
switch (frame->hd.type) {
|
2014-01-17 16:06:24 +01:00
|
|
|
case NGHTTP2_HEADERS:
|
2014-11-30 13:10:59 +01:00
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_RESPONSE &&
|
|
|
|
session_data->stream_data->stream_id == frame->hd.stream_id) {
|
2014-01-17 16:06:24 +01:00
|
|
|
/* Print response headers for the initiated request. */
|
|
|
|
print_header(stderr, name, namelen, value, valuelen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
In this tutorial, we just print the name/value pairs on stdout.
|
2014-01-17 16:06:24 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
After the HEADERS frame has been fully received (and thus all response
|
|
|
|
header name/value pairs have been received), the
|
|
|
|
``on_frame_recv_callback()`` function is called::
|
2014-01-17 16:06:24 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static int on_frame_recv_callback(nghttp2_session *session _U_,
|
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)user_data;
|
|
|
|
switch (frame->hd.type) {
|
2014-01-17 16:06:24 +01:00
|
|
|
case NGHTTP2_HEADERS:
|
2014-11-30 13:10:59 +01:00
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_RESPONSE &&
|
|
|
|
session_data->stream_data->stream_id == frame->hd.stream_id) {
|
2014-01-29 13:49:50 +01:00
|
|
|
fprintf(stderr, "All headers received\n");
|
2014-01-17 16:06:24 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
``on_frame_recv_callback()`` is called for other frame types too.
|
|
|
|
|
|
|
|
In this tutorial, we are just interested in the HTTP response HEADERS
|
|
|
|
frame. We check the frame type and its category (it should be
|
|
|
|
:macro:`NGHTTP2_HCAT_RESPONSE` for HTTP response HEADERS). We also
|
|
|
|
check its stream ID.
|
2014-01-17 16:06:24 +01:00
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
Next, zero or more DATA frames can be received. The
|
|
|
|
``on_data_chunk_recv_callback()`` function is invoked when a chunk of
|
|
|
|
data is received from the remote peer::
|
2013-12-24 16:30:05 +01:00
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static int on_data_chunk_recv_callback(nghttp2_session *session _U_,
|
|
|
|
uint8_t flags _U_, int32_t stream_id,
|
2013-12-24 16:30:05 +01:00
|
|
|
const uint8_t *data, size_t len,
|
2014-11-30 13:10:59 +01:00
|
|
|
void *user_data) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)user_data;
|
|
|
|
if (session_data->stream_data->stream_id == stream_id) {
|
2013-12-24 16:30:05 +01:00
|
|
|
fwrite(data, len, 1, stdout);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:36:00 +02:00
|
|
|
In our case, a chunk of data is HTTP response body. After checking the
|
|
|
|
stream ID, we just write the received data to stdout. Note the output
|
|
|
|
in the terminal may be corrupted if the response body contains some
|
|
|
|
binary data.
|
2013-12-24 16:30:05 +01:00
|
|
|
|
|
|
|
The ``on_stream_close_callback()`` function is invoked when the stream
|
|
|
|
is about to close::
|
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
static int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
|
2013-12-24 16:30:05 +01:00
|
|
|
nghttp2_error_code error_code,
|
2014-11-30 13:10:59 +01:00
|
|
|
void *user_data) {
|
|
|
|
http2_session_data *session_data = (http2_session_data *)user_data;
|
2013-12-24 16:30:05 +01:00
|
|
|
int rv;
|
|
|
|
|
2014-11-30 13:10:59 +01:00
|
|
|
if (session_data->stream_data->stream_id == stream_id) {
|
|
|
|
fprintf(stderr, "Stream %d closed with error_code=%d\n", stream_id,
|
|
|
|
error_code);
|
2013-12-25 16:23:43 +01:00
|
|
|
rv = nghttp2_session_terminate_session(session, NGHTTP2_NO_ERROR);
|
2014-11-30 13:10:59 +01:00
|
|
|
if (rv != 0) {
|
2013-12-24 16:30:05 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
If the stream ID matches the one we initiated, it means that its
|
2015-08-04 00:36:00 +02:00
|
|
|
stream is going to be closed. Since we have finished receiving
|
|
|
|
resource we wanted (or the stream was reset by RST_STREAM from the
|
2013-12-25 16:23:43 +01:00
|
|
|
remote peer), we call `nghttp2_session_terminate_session()` to
|
2015-08-04 00:36:00 +02:00
|
|
|
commence closure of the HTTP/2 session gracefully. If you have
|
2013-12-25 16:23:43 +01:00
|
|
|
some data associated for the stream to be closed, you may delete it
|
|
|
|
here.
|