2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2013 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * This program is written to show how to use nghttp2 API in C and
27 * intentionally made simple.
31 #endif /* !HAVE_CONFIG_H */
37 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
47 #include <nghttp2/nghttp2.h>
49 #include <openssl/ssl.h>
50 #include <openssl/err.h>
51 #include <openssl/conf.h>
53 enum { IO_NONE, WANT_READ, WANT_WRITE };
55 #define MAKE_NV(NAME, VALUE) \
57 (uint8_t *) NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, sizeof(VALUE) - 1, \
58 NGHTTP2_NV_FLAG_NONE \
61 #define MAKE_NV_CS(NAME, VALUE) \
63 (uint8_t *) NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, strlen(VALUE), \
64 NGHTTP2_NV_FLAG_NONE \
69 nghttp2_session *session;
70 /* WANT_READ if SSL/TLS connection needs more input; or WANT_WRITE
71 if it needs more output; or IO_NONE. This is necessary because
72 SSL/TLS re-negotiation is possible at any time. nghttp2 API
73 offers similar functions like nghttp2_session_want_read() and
74 nghttp2_session_want_write() but they do not take into account
75 SSL/TSL connection. */
81 /* In this program, path contains query component as well. */
83 /* This is the concatenation of host and port with ":" in
86 /* Stream ID for this request. */
93 /* In this program, path contains query component as well. */
103 * Returns copy of string |s| with the length |len|. The returned
104 * string is NULL-terminated.
106 static char *strcopy(const char *s, size_t len) {
108 dst = malloc(len + 1);
115 * Prints error message |msg| and exit.
117 static void die(const char *msg) {
118 fprintf(stderr, "FATAL: %s\n", msg);
123 * Prints error containing the function name |func| and message |msg|
126 static void dief(const char *func, const char *msg) {
127 fprintf(stderr, "FATAL: %s: %s\n", func, msg);
132 * Prints error containing the function name |func| and error code
133 * |error_code| and exit.
135 static void diec(const char *func, int error_code) {
136 fprintf(stderr, "FATAL: %s: error_code=%d, msg=%s\n", func, error_code,
137 nghttp2_strerror(error_code));
142 * The implementation of nghttp2_send_callback type. Here we write
143 * |data| with size |length| to the network and return the number of
144 * bytes actually written. See the documentation of
145 * nghttp2_send_callback for the details.
147 static ssize_t send_callback(nghttp2_session *session _U_, const uint8_t *data,
148 size_t length, int flags _U_, void *user_data) {
149 struct Connection *connection;
151 connection = (struct Connection *)user_data;
152 connection->want_io = IO_NONE;
154 rv = SSL_write(connection->ssl, data, (int)length);
156 int err = SSL_get_error(connection->ssl, rv);
157 if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
158 connection->want_io =
159 (err == SSL_ERROR_WANT_READ ? WANT_READ : WANT_WRITE);
160 rv = NGHTTP2_ERR_WOULDBLOCK;
162 rv = NGHTTP2_ERR_CALLBACK_FAILURE;
169 * The implementation of nghttp2_recv_callback type. Here we read data
170 * from the network and write them in |buf|. The capacity of |buf| is
171 * |length| bytes. Returns the number of bytes stored in |buf|. See
172 * the documentation of nghttp2_recv_callback for the details.
174 static ssize_t recv_callback(nghttp2_session *session _U_, uint8_t *buf,
175 size_t length, int flags _U_, void *user_data) {
176 struct Connection *connection;
178 connection = (struct Connection *)user_data;
179 connection->want_io = IO_NONE;
181 rv = SSL_read(connection->ssl, buf, (int)length);
183 int err = SSL_get_error(connection->ssl, rv);
184 if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
185 connection->want_io =
186 (err == SSL_ERROR_WANT_READ ? WANT_READ : WANT_WRITE);
187 rv = NGHTTP2_ERR_WOULDBLOCK;
189 rv = NGHTTP2_ERR_CALLBACK_FAILURE;
191 } else if (rv == 0) {
192 rv = NGHTTP2_ERR_EOF;
197 static int on_frame_send_callback(nghttp2_session *session,
198 const nghttp2_frame *frame,
199 void *user_data _U_) {
201 switch (frame->hd.type) {
202 case NGHTTP2_HEADERS:
203 if (nghttp2_session_get_stream_user_data(session, frame->hd.stream_id)) {
204 const nghttp2_nv *nva = frame->headers.nva;
205 printf("[INFO] C ----------------------------> S (HEADERS)\n");
206 for (i = 0; i < frame->headers.nvlen; ++i) {
207 fwrite(nva[i].name, nva[i].namelen, 1, stdout);
209 fwrite(nva[i].value, nva[i].valuelen, 1, stdout);
214 case NGHTTP2_RST_STREAM:
215 printf("[INFO] C ----------------------------> S (RST_STREAM)\n");
218 printf("[INFO] C ----------------------------> S (GOAWAY)\n");
224 static int on_frame_recv_callback(nghttp2_session *session,
225 const nghttp2_frame *frame,
226 void *user_data _U_) {
228 switch (frame->hd.type) {
229 case NGHTTP2_HEADERS:
230 if (frame->headers.cat == NGHTTP2_HCAT_RESPONSE) {
231 const nghttp2_nv *nva = frame->headers.nva;
233 req = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
235 printf("[INFO] C <---------------------------- S (HEADERS)\n");
236 for (i = 0; i < frame->headers.nvlen; ++i) {
237 fwrite(nva[i].name, nva[i].namelen, 1, stdout);
239 fwrite(nva[i].value, nva[i].valuelen, 1, stdout);
245 case NGHTTP2_RST_STREAM:
246 printf("[INFO] C <---------------------------- S (RST_STREAM)\n");
249 printf("[INFO] C <---------------------------- S (GOAWAY)\n");
256 * The implementation of nghttp2_on_stream_close_callback type. We use
257 * this function to know the response is fully received. Since we just
258 * fetch 1 resource in this program, after reception of the response,
259 * we submit GOAWAY and close the session.
261 static int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
262 uint32_t error_code _U_,
263 void *user_data _U_) {
265 req = nghttp2_session_get_stream_user_data(session, stream_id);
268 rv = nghttp2_session_terminate_session(session, NGHTTP2_NO_ERROR);
271 diec("nghttp2_session_terminate_session", rv);
277 #define MAX_OUTLEN 4096
280 * The implementation of nghttp2_on_data_chunk_recv_callback type. We
281 * use this function to print the received response body.
283 static int on_data_chunk_recv_callback(nghttp2_session *session,
284 uint8_t flags _U_, int32_t stream_id,
285 const uint8_t *data, size_t len,
286 void *user_data _U_) {
288 req = nghttp2_session_get_stream_user_data(session, stream_id);
290 printf("[INFO] C <---------------------------- S (DATA chunk)\n"
292 (unsigned long int)len);
293 fwrite(data, 1, len, stdout);
300 * Setup callback functions. nghttp2 API offers many callback
301 * functions, but most of them are optional. The send_callback is
302 * always required. Since we use nghttp2_session_recv(), the
303 * recv_callback is also required.
305 static void setup_nghttp2_callbacks(nghttp2_session_callbacks *callbacks) {
306 nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
308 nghttp2_session_callbacks_set_recv_callback(callbacks, recv_callback);
310 nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
311 on_frame_send_callback);
313 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
314 on_frame_recv_callback);
316 nghttp2_session_callbacks_set_on_stream_close_callback(
317 callbacks, on_stream_close_callback);
319 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
320 callbacks, on_data_chunk_recv_callback);
324 * Callback function for TLS NPN. Since this program only supports
325 * HTTP/2 protocol, if server does not offer HTTP/2 the nghttp2
326 * library supports, we terminate program.
328 static int select_next_proto_cb(SSL *ssl _U_, unsigned char **out,
329 unsigned char *outlen, const unsigned char *in,
330 unsigned int inlen, void *arg _U_) {
332 /* nghttp2_select_next_protocol() selects HTTP/2 protocol the
333 nghttp2 library supports. */
334 rv = nghttp2_select_next_protocol(out, outlen, in, inlen);
336 die("Server did not advertise HTTP/2 protocol");
338 return SSL_TLSEXT_ERR_OK;
342 * Setup SSL/TLS context.
344 static void init_ssl_ctx(SSL_CTX *ssl_ctx) {
345 /* Disable SSLv2 and enable all workarounds for buggy servers */
346 SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2);
347 SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
348 SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
349 /* Set NPN callback */
350 SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, NULL);
353 static void ssl_handshake(SSL *ssl, int fd) {
355 if (SSL_set_fd(ssl, fd) == 0) {
356 dief("SSL_set_fd", ERR_error_string(ERR_get_error(), NULL));
359 rv = SSL_connect(ssl);
361 dief("SSL_connect", ERR_error_string(ERR_get_error(), NULL));
366 * Connects to the host |host| and port |port|. This function returns
367 * the file descriptor of the client socket.
369 static int connect_to(const char *host, uint16_t port) {
370 struct addrinfo hints;
373 char service[NI_MAXSERV];
374 struct addrinfo *res, *rp;
375 snprintf(service, sizeof(service), "%u", port);
376 memset(&hints, 0, sizeof(struct addrinfo));
377 hints.ai_family = AF_UNSPEC;
378 hints.ai_socktype = SOCK_STREAM;
379 rv = getaddrinfo(host, service, &hints, &res);
381 dief("getaddrinfo", gai_strerror(rv));
383 for (rp = res; rp; rp = rp->ai_next) {
384 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
388 while ((rv = connect(fd, rp->ai_addr, rp->ai_addrlen)) == -1 &&
401 static void make_non_block(int fd) {
403 while ((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR)
406 dief("fcntl", strerror(errno));
408 while ((rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR)
411 dief("fcntl", strerror(errno));
415 static void set_tcp_nodelay(int fd) {
418 rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val));
420 dief("setsockopt", strerror(errno));
425 * Update |pollfd| based on the state of |connection|.
427 static void ctl_poll(struct pollfd *pollfd, struct Connection *connection) {
429 if (nghttp2_session_want_read(connection->session) ||
430 connection->want_io == WANT_READ) {
431 pollfd->events |= POLLIN;
433 if (nghttp2_session_want_write(connection->session) ||
434 connection->want_io == WANT_WRITE) {
435 pollfd->events |= POLLOUT;
440 * Submits the request |req| to the connection |connection|. This
441 * function does not send packets; just append the request to the
442 * internal queue in |connection->session|.
444 static void submit_request(struct Connection *connection, struct Request *req) {
446 /* Make sure that the last item is NULL */
447 const nghttp2_nv nva[] = {MAKE_NV(":method", "GET"),
448 MAKE_NV_CS(":path", req->path),
449 MAKE_NV(":scheme", "https"),
450 MAKE_NV_CS(":authority", req->hostport),
451 MAKE_NV("accept", "*/*"),
452 MAKE_NV("user-agent", "nghttp2/" NGHTTP2_VERSION)};
454 stream_id = nghttp2_submit_request(connection->session, NULL, nva,
455 sizeof(nva) / sizeof(nva[0]), NULL, req);
458 diec("nghttp2_submit_request", stream_id);
461 req->stream_id = stream_id;
462 printf("[INFO] Stream ID = %d\n", stream_id);
466 * Performs the network I/O.
468 static void exec_io(struct Connection *connection) {
470 rv = nghttp2_session_recv(connection->session);
472 diec("nghttp2_session_recv", rv);
474 rv = nghttp2_session_send(connection->session);
476 diec("nghttp2_session_send", rv);
480 static void request_init(struct Request *req, const struct URI *uri) {
481 req->host = strcopy(uri->host, uri->hostlen);
482 req->port = uri->port;
483 req->path = strcopy(uri->path, uri->pathlen);
484 req->hostport = strcopy(uri->hostport, uri->hostportlen);
488 static void request_free(struct Request *req) {
495 * Fetches the resource denoted by |uri|.
497 static void fetch_uri(const struct URI *uri) {
498 nghttp2_session_callbacks *callbacks;
503 struct Connection connection;
506 struct pollfd pollfds[1];
508 request_init(&req, uri);
510 /* Establish connection and setup SSL */
511 fd = connect_to(req.host, req.port);
513 die("Could not open file descriptor");
515 ssl_ctx = SSL_CTX_new(SSLv23_client_method());
516 if (ssl_ctx == NULL) {
517 dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL));
519 init_ssl_ctx(ssl_ctx);
520 ssl = SSL_new(ssl_ctx);
522 dief("SSL_new", ERR_error_string(ERR_get_error(), NULL));
524 /* To simplify the program, we perform SSL/TLS handshake in blocking
526 ssl_handshake(ssl, fd);
528 connection.ssl = ssl;
529 connection.want_io = IO_NONE;
531 /* Send connection header in blocking I/O mode */
532 rv = SSL_write(ssl, NGHTTP2_CLIENT_CONNECTION_PREFACE,
533 NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN);
535 dief("SSL_write failed: could not send connection preface",
536 ERR_error_string(ERR_get_error(), NULL));
539 /* Here make file descriptor non-block */
543 printf("[INFO] SSL/TLS handshake completed\n");
545 rv = nghttp2_session_callbacks_new(&callbacks);
548 diec("nghttp2_session_callbacks_new", rv);
551 setup_nghttp2_callbacks(callbacks);
553 rv = nghttp2_session_client_new(&connection.session, callbacks, &connection);
555 nghttp2_session_callbacks_del(callbacks);
558 diec("nghttp2_session_client_new", rv);
561 nghttp2_submit_settings(connection.session, NGHTTP2_FLAG_NONE, NULL, 0);
563 /* Submit the HTTP request to the outbound queue. */
564 submit_request(&connection, &req);
567 ctl_poll(pollfds, &connection);
570 while (nghttp2_session_want_read(connection.session) ||
571 nghttp2_session_want_write(connection.session)) {
572 int nfds = poll(pollfds, npollfds, -1);
574 dief("poll", strerror(errno));
576 if (pollfds[0].revents & (POLLIN | POLLOUT)) {
577 exec_io(&connection);
579 if ((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) {
580 die("Connection error");
582 ctl_poll(pollfds, &connection);
585 /* Resource cleanup */
586 nghttp2_session_del(connection.session);
589 SSL_CTX_free(ssl_ctx);
590 shutdown(fd, SHUT_WR);
595 static int parse_uri(struct URI *res, const char *uri) {
596 /* We only interested in https */
597 size_t len, i, offset;
599 memset(res, 0, sizeof(struct URI));
601 if (len < 9 || memcmp("https://", uri, 8) != 0) {
605 res->host = res->hostport = &uri[offset];
607 if (uri[offset] == '[') {
608 /* IPv6 literal address */
612 for (i = offset; i < len; ++i) {
614 res->hostlen = i - offset;
620 const char delims[] = ":/?#";
621 for (i = offset; i < len; ++i) {
622 if (strchr(delims, uri[i]) != NULL) {
626 res->hostlen = i - offset;
629 if (res->hostlen == 0) {
635 if (uri[offset] == ':') {
637 const char delims[] = "/?#";
640 for (i = offset; i < len; ++i) {
641 if (strchr(delims, uri[i]) != NULL) {
644 if ('0' <= uri[i] && uri[i] <= '9') {
646 port += uri[i] - '0';
661 res->hostportlen = uri + offset + ipv6addr - res->host;
662 for (i = offset; i < len; ++i) {
667 if (i - offset == 0) {
671 res->path = &uri[offset];
672 res->pathlen = i - offset;
677 int main(int argc, char **argv) {
679 struct sigaction act;
683 die("Specify a https URI");
686 memset(&act, 0, sizeof(struct sigaction));
687 act.sa_handler = SIG_IGN;
688 sigaction(SIGPIPE, &act, 0);
690 OPENSSL_config(NULL);
691 OpenSSL_add_all_algorithms();
692 SSL_load_error_strings();
695 rv = parse_uri(&uri, argv[1]);
697 die("parse_uri failed");