1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
26 #define _MPRINTF_REPLACE
27 #include <curl/mprintf.h>
29 #include <nghttp2/nghttp2.h>
34 #include "curl_base64.h"
35 #include "curl_memory.h"
39 /* include memdebug.h last */
42 #if (NGHTTP2_VERSION_NUM < 0x000300)
43 #error too old nghttp2 version, upgrade!
46 static int http2_perform_getsock(const struct connectdata *conn,
47 curl_socket_t *sock, /* points to
53 const struct http_conn *httpc = &conn->proto.httpc;
54 int bitmap = GETSOCK_BLANK;
57 /* TODO We should check underlying socket state if it is SSL socket
58 because of renegotiation. */
59 sock[0] = conn->sock[FIRSTSOCKET];
61 if(nghttp2_session_want_read(httpc->h2))
62 bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
64 if(nghttp2_session_want_write(httpc->h2))
65 bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
70 static int http2_getsock(struct connectdata *conn,
71 curl_socket_t *sock, /* points to numsocks
75 return http2_perform_getsock(conn, sock, numsocks);
78 static CURLcode http2_disconnect(struct connectdata *conn,
81 struct http_conn *httpc = &conn->proto.httpc;
82 (void)dead_connection;
84 infof(conn->data, "HTTP/2 DISCONNECT starts now\n");
86 nghttp2_session_del(httpc->h2);
88 Curl_safefree(httpc->header_recvbuf->buffer);
89 Curl_safefree(httpc->header_recvbuf);
91 Curl_safefree(httpc->inbuf);
93 infof(conn->data, "HTTP/2 DISCONNECT done\n");
99 * HTTP2 handler interface. This isn't added to the general list of protocols
100 * but will be used at run-time when the protocol is dynamically switched from
103 const struct Curl_handler Curl_handler_http2 = {
104 "HTTP2", /* scheme */
105 ZERO_NULL, /* setup_connection */
106 ZERO_NULL, /* do_it */
107 ZERO_NULL , /* done */
108 ZERO_NULL, /* do_more */
109 ZERO_NULL, /* connect_it */
110 ZERO_NULL, /* connecting */
111 ZERO_NULL, /* doing */
112 http2_getsock, /* proto_getsock */
113 http2_getsock, /* doing_getsock */
114 ZERO_NULL, /* domore_getsock */
115 http2_perform_getsock, /* perform_getsock */
116 http2_disconnect, /* disconnect */
117 ZERO_NULL, /* readwrite */
118 PORT_HTTP, /* defport */
119 CURLPROTO_HTTP, /* protocol */
120 PROTOPT_NONE /* flags */
123 const struct Curl_handler Curl_handler_http2_ssl = {
124 "HTTP2", /* scheme */
125 ZERO_NULL, /* setup_connection */
126 ZERO_NULL, /* do_it */
127 ZERO_NULL , /* done */
128 ZERO_NULL, /* do_more */
129 ZERO_NULL, /* connect_it */
130 ZERO_NULL, /* connecting */
131 ZERO_NULL, /* doing */
132 http2_getsock, /* proto_getsock */
133 http2_getsock, /* doing_getsock */
134 ZERO_NULL, /* domore_getsock */
135 http2_perform_getsock, /* perform_getsock */
136 http2_disconnect, /* disconnect */
137 ZERO_NULL, /* readwrite */
138 PORT_HTTP, /* defport */
139 CURLPROTO_HTTP | CURLPROTO_HTTPS, /* protocol */
140 PROTOPT_SSL /* flags */
144 * Store nghttp2 version info in this buffer, Prefix with a space. Return
145 * total length written.
147 int Curl_http2_ver(char *p, size_t len)
149 nghttp2_info *h2 = nghttp2_version(0);
150 return snprintf(p, len, " nghttp2/%s", h2->version_str);
154 * The implementation of nghttp2_send_callback type. Here we write |data| with
155 * size |length| to the network and return the number of bytes actually
156 * written. See the documentation of nghttp2_send_callback for the details.
158 static ssize_t send_callback(nghttp2_session *h2,
159 const uint8_t *data, size_t length, int flags,
162 struct connectdata *conn = (struct connectdata *)userp;
163 struct http_conn *httpc = &conn->proto.httpc;
170 written = ((Curl_send*)httpc->send_underlying)(conn, FIRSTSOCKET,
173 if(rc == CURLE_AGAIN) {
174 return NGHTTP2_ERR_WOULDBLOCK;
178 failf(conn->data, "Failed sending HTTP2 data");
179 return NGHTTP2_ERR_CALLBACK_FAILURE;
183 return NGHTTP2_ERR_WOULDBLOCK;
188 static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
191 struct connectdata *conn = (struct connectdata *)userp;
192 struct http_conn *c = &conn->proto.httpc;
196 infof(conn->data, "on_frame_recv() was called with header %x\n",
198 switch(frame->hd.type) {
199 case NGHTTP2_HEADERS:
200 if(frame->headers.cat != NGHTTP2_HCAT_RESPONSE)
202 c->bodystarted = TRUE;
203 Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
204 c->nread_header_recvbuf = c->len < c->header_recvbuf->size_used ?
205 c->len : c->header_recvbuf->size_used;
207 memcpy(c->mem, c->header_recvbuf->buffer, c->nread_header_recvbuf);
209 c->mem += c->nread_header_recvbuf;
210 c->len -= c->nread_header_recvbuf;
212 case NGHTTP2_PUSH_PROMISE:
213 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
214 frame->hd.stream_id, NGHTTP2_CANCEL);
215 if(nghttp2_is_fatal(rv)) {
223 static int on_invalid_frame_recv(nghttp2_session *session,
224 const nghttp2_frame *frame,
225 nghttp2_error_code error_code, void *userp)
227 struct connectdata *conn = (struct connectdata *)userp;
230 infof(conn->data, "on_invalid_frame_recv() was called, error_code = %d\n",
235 static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
237 const uint8_t *data, size_t len, void *userp)
239 struct connectdata *conn = (struct connectdata *)userp;
240 struct http_conn *c = &conn->proto.httpc;
245 infof(conn->data, "on_data_chunk_recv() "
246 "len = %u, stream = %x\n", len, stream_id);
248 if(stream_id != c->stream_id) {
252 nread = c->len < len ? c->len : len;
253 memcpy(c->mem, data, nread);
258 infof(conn->data, "%zu data written\n", nread);
261 c->data = data + nread;
262 c->datalen = len - nread;
263 return NGHTTP2_ERR_PAUSE;
268 static int before_frame_send(nghttp2_session *session,
269 const nghttp2_frame *frame,
272 struct connectdata *conn = (struct connectdata *)userp;
273 struct http_conn *c = &conn->proto.httpc;
276 infof(conn->data, "before_frame_send() was called\n");
277 if(frame->hd.type == NGHTTP2_HEADERS &&
278 frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
279 /* Get stream ID of our request */
280 c->stream_id = frame->hd.stream_id;
284 static int on_frame_send(nghttp2_session *session,
285 const nghttp2_frame *frame,
288 struct connectdata *conn = (struct connectdata *)userp;
291 infof(conn->data, "on_frame_send() was called\n");
294 static int on_frame_not_send(nghttp2_session *session,
295 const nghttp2_frame *frame,
296 int lib_error_code, void *userp)
298 struct connectdata *conn = (struct connectdata *)userp;
301 infof(conn->data, "on_frame_not_send() was called, lib_error_code = %d\n",
305 static int on_stream_close(nghttp2_session *session, int32_t stream_id,
306 nghttp2_error_code error_code, void *userp)
308 struct connectdata *conn = (struct connectdata *)userp;
309 struct http_conn *c = &conn->proto.httpc;
312 infof(conn->data, "on_stream_close() was called, error_code = %d\n",
315 if(stream_id != c->stream_id) {
324 static int on_unknown_frame_recv(nghttp2_session *session,
325 const uint8_t *head, size_t headlen,
326 const uint8_t *payload, size_t payloadlen,
329 struct connectdata *conn = (struct connectdata *)userp;
335 infof(conn->data, "on_unknown_frame_recv() was called\n");
338 static int on_begin_headers(nghttp2_session *session,
339 const nghttp2_frame *frame, void *userp)
341 struct connectdata *conn = (struct connectdata *)userp;
344 infof(conn->data, "on_begin_headers() was called\n");
348 static const char STATUS[] = ":status";
350 /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
351 static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
352 const uint8_t *name, size_t namelen,
353 const uint8_t *value, size_t valuelen,
356 struct connectdata *conn = (struct connectdata *)userp;
357 struct http_conn *c = &conn->proto.httpc;
361 if(frame->hd.stream_id != c->stream_id) {
365 if(namelen == sizeof(":status") - 1 &&
366 memcmp(STATUS, name, namelen) == 0) {
367 snprintf(c->header_recvbuf->buffer, 13, "HTTP/2.0 %s", value);
368 c->header_recvbuf->buffer[12] = '\r';
372 /* convert to a HTTP1-style header */
373 infof(conn->data, "got header\n");
374 Curl_add_buffer(c->header_recvbuf, name, namelen);
375 Curl_add_buffer(c->header_recvbuf, ":", 1);
376 Curl_add_buffer(c->header_recvbuf, value, valuelen);
377 Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
380 return 0; /* 0 is successful */
384 * This is all callbacks nghttp2 calls
386 static const nghttp2_session_callbacks callbacks = {
387 send_callback, /* nghttp2_send_callback */
388 NULL, /* nghttp2_recv_callback */
389 on_frame_recv, /* nghttp2_on_frame_recv_callback */
390 on_invalid_frame_recv, /* nghttp2_on_invalid_frame_recv_callback */
391 on_data_chunk_recv, /* nghttp2_on_data_chunk_recv_callback */
392 before_frame_send, /* nghttp2_before_frame_send_callback */
393 on_frame_send, /* nghttp2_on_frame_send_callback */
394 on_frame_not_send, /* nghttp2_on_frame_not_send_callback */
395 on_stream_close, /* nghttp2_on_stream_close_callback */
396 on_unknown_frame_recv, /* nghttp2_on_unknown_frame_recv_callback */
397 on_begin_headers, /* nghttp2_on_begin_headers_callback */
398 on_header /* nghttp2_on_header_callback */
399 #if NGHTTP2_VERSION_NUM >= 0x000400
400 , NULL /* nghttp2_select_padding_callback */
404 static ssize_t data_source_read_callback(nghttp2_session *session,
406 uint8_t *buf, size_t length,
408 nghttp2_data_source *source,
411 struct connectdata *conn = (struct connectdata *)userp;
412 struct http_conn *c = &conn->proto.httpc;
419 nread = c->upload_len < length ? c->upload_len : length;
421 memcpy(buf, c->upload_mem, nread);
422 c->upload_mem += nread;
423 c->upload_len -= nread;
424 c->upload_left -= nread;
427 if(c->upload_left == 0)
430 return NGHTTP2_ERR_DEFERRED;
436 * The HTTP2 settings we send in the Upgrade request
438 static nghttp2_settings_entry settings[] = {
439 { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
440 { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
443 #define H2_BUFSIZE 4096
446 * Initialize nghttp2 for a Curl connection
448 CURLcode Curl_http2_init(struct connectdata *conn)
450 if(!conn->proto.httpc.h2) {
452 conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
453 if(conn->proto.httpc.inbuf == NULL)
454 return CURLE_OUT_OF_MEMORY;
456 /* The nghttp2 session is not yet setup, do it */
457 rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
460 failf(conn->data, "Couldn't initialize nghttp2!");
461 return CURLE_OUT_OF_MEMORY; /* most likely at least */
468 * Send a request using http2
470 CURLcode Curl_http2_send_request(struct connectdata *conn)
477 * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
479 CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req,
480 struct connectdata *conn)
486 struct SingleRequest *k = &conn->data->req;
487 uint8_t *binsettings = conn->proto.httpc.binsettings;
489 Curl_http2_init(conn);
491 /* As long as we have a fixed set of settings, we don't have to dynamically
492 * figure out the base64 strings since it'll always be the same. However,
493 * the settings will likely not be fixed every time in the future.
496 /* this returns number of bytes it wrote */
497 binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
499 sizeof(settings)/sizeof(settings[0]));
501 failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
502 return CURLE_FAILED_INIT;
504 conn->proto.httpc.binlen = binlen;
506 result = Curl_base64_encode(conn->data, (const char *)binsettings, binlen,
511 result = Curl_add_bufferf(req,
512 "Connection: Upgrade, HTTP2-Settings\r\n"
514 "HTTP2-Settings: %s\r\n",
515 NGHTTP2_PROTO_VERSION_ID, base64);
516 Curl_safefree(base64);
518 k->upgr101 = UPGR101_REQUESTED;
524 * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
525 * a regular CURLcode value.
527 static ssize_t http2_recv(struct connectdata *conn, int sockindex,
528 char *mem, size_t len, CURLcode *err)
533 struct http_conn *httpc = &conn->proto.httpc;
535 (void)sockindex; /* we always do HTTP2 on sockindex 0 */
541 /* Nullify here because we call nghttp2_session_send() and they
542 might refer to the old buffer. */
543 httpc->upload_mem = NULL;
544 httpc->upload_len = 0;
546 if(httpc->bodystarted &&
547 httpc->nread_header_recvbuf < httpc->header_recvbuf->size_used) {
549 httpc->header_recvbuf->size_used - httpc->nread_header_recvbuf;
550 size_t ncopy = len < left ? len : left;
551 memcpy(mem, httpc->header_recvbuf->buffer + httpc->nread_header_recvbuf,
553 httpc->nread_header_recvbuf += ncopy;
558 nread = len < httpc->datalen ? len : httpc->datalen;
559 memcpy(mem, httpc->data, nread);
561 httpc->data += nread;
562 httpc->datalen -= nread;
564 infof(conn->data, "%zu data written\n", nread);
565 if(httpc->datalen == 0) {
572 conn->proto.httpc.mem = mem;
573 conn->proto.httpc.len = len;
575 infof(conn->data, "http2_recv: %d bytes buffer\n",
576 conn->proto.httpc.len);
579 nread = ((Curl_recv*)httpc->recv_underlying)(conn, FIRSTSOCKET,
580 httpc->inbuf, H2_BUFSIZE, &rc);
582 if(rc == CURLE_AGAIN) {
588 failf(conn->data, "Failed receiving HTTP2 data");
593 infof(conn->data, "nread=%zd\n", nread);
594 rv = nghttp2_session_mem_recv(httpc->h2,
595 (const uint8_t *)httpc->inbuf, nread);
597 if(nghttp2_is_fatal((int)rv)) {
598 failf(conn->data, "nghttp2_session_mem_recv() returned %d:%s\n",
599 rv, nghttp2_strerror((int)rv));
600 *err = CURLE_RECV_ERROR;
603 infof(conn->data, "nghttp2_session_mem_recv() returns %zd\n", rv);
604 /* Always send pending frames in nghttp2 session, because
605 nghttp2_session_mem_recv() may queue new frame */
606 rv = nghttp2_session_send(httpc->h2);
608 *err = CURLE_SEND_ERROR;
611 if(len != httpc->len) {
612 return len - conn->proto.httpc.len;
614 /* If stream is closed, return 0 to signal the http routine to close
623 #define MAKE_NV(k, v) \
624 { (uint8_t*)k, (uint8_t*)v, sizeof(k) - 1, sizeof(v) - 1 }
626 #define MAKE_NV2(k, v, vlen) \
627 { (uint8_t*)k, (uint8_t*)v, sizeof(k) - 1, vlen }
629 /* return number of received (decrypted) bytes */
630 static ssize_t http2_send(struct connectdata *conn, int sockindex,
631 const void *mem, size_t len, CURLcode *err)
634 * BIG TODO: Currently, we send request in this function, but this
635 * function is also used to send request body. It would be nice to
636 * add dedicated function for request.
639 struct http_conn *httpc = &conn->proto.httpc;
643 char *hdbuf = (char*)mem;
645 nghttp2_data_provider data_prd;
648 infof(conn->data, "http2_send len=%zu\n", len);
650 if(httpc->stream_id != -1) {
651 /* If stream_id != -1, we have dispatched request HEADERS, and now
652 are going to send or sending request body in DATA frame */
653 httpc->upload_mem = mem;
654 httpc->upload_len = len;
655 nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
656 rv = nghttp2_session_send(httpc->h2);
657 if(nghttp2_is_fatal(rv)) {
658 *err = CURLE_SEND_ERROR;
661 return len - httpc->upload_len;
664 /* Calculate number of headers contained in [mem, mem + len) */
665 /* Here, we assume the curl http code generate *correct* HTTP header
668 for(i = 0; i < len; ++i) {
669 if(hdbuf[i] == 0x0a) {
673 /* We counted additional 2 \n in the first and last line. We need 3
674 new headers: :method, :path and :scheme. Therefore we need one
677 nva = malloc(sizeof(nghttp2_nv) * nheader);
679 *err = CURLE_OUT_OF_MEMORY;
682 /* Extract :method, :path from request line */
683 end = strchr(hdbuf, ' ');
684 nva[0].name = (unsigned char *)":method";
685 nva[0].namelen = (uint16_t)strlen((char *)nva[0].name);
686 nva[0].value = (unsigned char *)hdbuf;
687 nva[0].valuelen = (uint16_t)(end - hdbuf);
691 end = strchr(hdbuf, ' ');
692 nva[1].name = (unsigned char *)":path";
693 nva[1].namelen = (uint16_t)strlen((char *)nva[1].name);
694 nva[1].value = (unsigned char *)hdbuf;
695 nva[1].valuelen = (uint16_t)(end - hdbuf);
697 nva[2].name = (unsigned char *)":scheme";
698 nva[2].namelen = (uint16_t)strlen((char *)nva[2].name);
699 if(conn->handler->flags & PROTOPT_SSL)
700 nva[2].value = (unsigned char *)"https";
702 nva[2].value = (unsigned char *)"http";
703 nva[2].valuelen = (uint16_t)strlen((char *)nva[2].value);
705 hdbuf = strchr(hdbuf, 0x0a);
708 for(i = 3; i < nheader; ++i) {
709 end = strchr(hdbuf, ':');
711 if(end - hdbuf == 4 && Curl_raw_nequal("host", hdbuf, 4)) {
712 nva[i].name = (unsigned char *)":authority";
713 nva[i].namelen = (uint16_t)strlen((char *)nva[i].name);
716 nva[i].name = (unsigned char *)hdbuf;
717 nva[i].namelen = (uint16_t)(end - hdbuf);
720 for(; *hdbuf == ' '; ++hdbuf);
721 end = strchr(hdbuf, 0x0d);
723 nva[i].value = (unsigned char *)hdbuf;
724 nva[i].valuelen = (uint16_t)(end - hdbuf);
727 /* Inspect Content-Length header field and retrieve the request
728 entity length so that we can set END_STREAM to the last DATA
730 if(nva[i].namelen == 14 &&
731 Curl_raw_nequal("content-length", (char*)nva[i].name, 14)) {
733 for(j = 0; j < nva[i].valuelen; ++j) {
734 httpc->upload_left *= 10;
735 httpc->upload_left += nva[i].value[j] - '0';
737 infof(conn->data, "request content-length=%zu\n", httpc->upload_left);
741 switch(conn->data->set.httpreq) {
743 case HTTPREQ_POST_FORM:
745 data_prd.read_callback = data_source_read_callback;
746 data_prd.source.ptr = NULL;
747 rv = nghttp2_submit_request(httpc->h2, 0, nva, nheader, &data_prd, NULL);
750 rv = nghttp2_submit_request(httpc->h2, 0, nva, nheader, NULL, NULL);
756 *err = CURLE_SEND_ERROR;
760 rv = nghttp2_session_send(httpc->h2);
763 *err = CURLE_SEND_ERROR;
767 if(httpc->stream_id != -1) {
768 /* If whole HEADERS frame was sent off to the underlying socket,
769 the nghttp2 library calls data_source_read_callback. But only
770 it found that no data available, so it deferred the DATA
771 transmission. Which means that nghttp2_session_want_write()
772 returns 0 on http2_perform_getsock(), which results that no
773 writable socket check is performed. To workaround this, we
774 issue nghttp2_session_resume_data() here to bring back DATA
775 transmission from deferred state. */
776 nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
782 int Curl_http2_switched(struct connectdata *conn)
786 struct http_conn *httpc = &conn->proto.httpc;
787 /* we are switched! */
788 /* Don't know this is needed here at this moment. Original
789 handler->flags is still useful. */
790 if(conn->handler->flags & PROTOPT_SSL)
791 conn->handler = &Curl_handler_http2_ssl;
793 conn->handler = &Curl_handler_http2;
795 httpc->recv_underlying = (recving)conn->recv[FIRSTSOCKET];
796 httpc->send_underlying = (sending)conn->send[FIRSTSOCKET];
797 conn->recv[FIRSTSOCKET] = http2_recv;
798 conn->send[FIRSTSOCKET] = http2_send;
799 infof(conn->data, "We have switched to HTTP2\n");
800 httpc->bodystarted = FALSE;
801 httpc->closed = FALSE;
802 httpc->header_recvbuf = Curl_add_buffer_init();
803 httpc->nread_header_recvbuf = 0;
806 httpc->upload_left = 0;
807 httpc->upload_mem = NULL;
808 httpc->upload_len = 0;
810 conn->httpversion = 20;
812 /* Put place holder for status line */
813 Curl_add_buffer(httpc->header_recvbuf, "HTTP/2.0 200\r\n", 14);
815 /* TODO: May get CURLE_AGAIN */
816 rv = (int) ((Curl_send*)httpc->send_underlying)
818 NGHTTP2_CLIENT_CONNECTION_HEADER,
819 NGHTTP2_CLIENT_CONNECTION_HEADER_LEN,
822 if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
823 /* stream 1 is opened implicitly on upgrade */
824 httpc->stream_id = 1;
825 /* queue SETTINGS frame (again) */
826 rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
827 httpc->binlen, NULL);
829 failf(conn->data, "nghttp2_session_upgrade() failed: %s(%d)",
830 nghttp2_strerror(rv), rv);
835 /* stream ID is unknown at this point */
836 httpc->stream_id = -1;
837 rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE, NULL, 0);
839 failf(conn->data, "nghttp2_submit_settings() failed: %s(%d)",
840 nghttp2_strerror(rv), rv);