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 < 0x000600)
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 Curl_http, /* 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 Curl_http, /* 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_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;
165 CURLcode result = CURLE_OK;
170 written = ((Curl_send*)httpc->send_underlying)(conn, FIRSTSOCKET,
171 data, length, &result);
173 if(result == 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;
198 infof(conn->data, "on_frame_recv() was called with header %x\n",
200 switch(frame->hd.type) {
202 /* If body started, then receiving DATA is illegal. */
203 if(!c->bodystarted) {
204 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
206 NGHTTP2_PROTOCOL_ERROR);
208 if(nghttp2_is_fatal(rv)) {
209 return NGHTTP2_ERR_CALLBACK_FAILURE;
213 case NGHTTP2_HEADERS:
214 if(frame->headers.cat == NGHTTP2_HCAT_REQUEST)
218 /* Only valid HEADERS after body started is trailer header,
219 which is not fully supported in this code. If HEADERS is not
220 trailer, then it is a PROTOCOL_ERROR. */
221 if((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
222 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
224 NGHTTP2_PROTOCOL_ERROR);
226 if(nghttp2_is_fatal(rv)) {
227 return NGHTTP2_ERR_CALLBACK_FAILURE;
233 if(c->status_code == -1) {
234 /* No :status header field means PROTOCOL_ERROR. */
235 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
237 NGHTTP2_PROTOCOL_ERROR);
239 if(nghttp2_is_fatal(rv)) {
240 return NGHTTP2_ERR_CALLBACK_FAILURE;
246 /* Only final status code signals the end of header */
247 if(c->status_code / 100 != 1) {
248 c->bodystarted = TRUE;
253 Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
255 left = c->header_recvbuf->size_used - c->nread_header_recvbuf;
256 ncopy = c->len < left ? c->len : left;
258 memcpy(c->mem, c->header_recvbuf->buffer + c->nread_header_recvbuf, ncopy);
259 c->nread_header_recvbuf += ncopy;
264 case NGHTTP2_PUSH_PROMISE:
265 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
266 frame->push_promise.promised_stream_id,
268 if(nghttp2_is_fatal(rv)) {
276 static int on_invalid_frame_recv(nghttp2_session *session,
277 const nghttp2_frame *frame,
278 uint32_t error_code, void *userp)
280 struct connectdata *conn = (struct connectdata *)userp;
283 infof(conn->data, "on_invalid_frame_recv() was called, error_code = %d\n",
288 static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
290 const uint8_t *data, size_t len, void *userp)
292 struct connectdata *conn = (struct connectdata *)userp;
293 struct http_conn *c = &conn->proto.httpc;
298 infof(conn->data, "on_data_chunk_recv() "
299 "len = %u, stream = %x\n", len, stream_id);
301 if(stream_id != c->stream_id) {
305 nread = c->len < len ? c->len : len;
306 memcpy(c->mem, data, nread);
311 infof(conn->data, "%zu data written\n", nread);
314 c->data = data + nread;
315 c->datalen = len - nread;
316 return NGHTTP2_ERR_PAUSE;
321 static int before_frame_send(nghttp2_session *session,
322 const nghttp2_frame *frame,
325 struct connectdata *conn = (struct connectdata *)userp;
328 infof(conn->data, "before_frame_send() was called\n");
331 static int on_frame_send(nghttp2_session *session,
332 const nghttp2_frame *frame,
335 struct connectdata *conn = (struct connectdata *)userp;
338 infof(conn->data, "on_frame_send() was called\n");
341 static int on_frame_not_send(nghttp2_session *session,
342 const nghttp2_frame *frame,
343 int lib_error_code, void *userp)
345 struct connectdata *conn = (struct connectdata *)userp;
348 infof(conn->data, "on_frame_not_send() was called, lib_error_code = %d\n",
352 static int on_stream_close(nghttp2_session *session, int32_t stream_id,
353 uint32_t error_code, void *userp)
355 struct connectdata *conn = (struct connectdata *)userp;
356 struct http_conn *c = &conn->proto.httpc;
359 infof(conn->data, "on_stream_close() was called, error_code = %d\n",
362 if(stream_id != c->stream_id) {
371 static int on_begin_headers(nghttp2_session *session,
372 const nghttp2_frame *frame, void *userp)
374 struct connectdata *conn = (struct connectdata *)userp;
377 infof(conn->data, "on_begin_headers() was called\n");
381 /* Decode HTTP status code. Returns -1 if no valid status code was
383 static int decode_status_code(const uint8_t *value, size_t len)
394 for(i = 0; i < 3; ++i) {
397 if(c < '0' || c > '9') {
408 static const char STATUS[] = ":status";
410 /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
411 static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
412 const uint8_t *name, size_t namelen,
413 const uint8_t *value, size_t valuelen,
417 struct connectdata *conn = (struct connectdata *)userp;
418 struct http_conn *c = &conn->proto.httpc;
427 if(frame->hd.stream_id != c->stream_id) {
432 /* Ignore trailer or HEADERS not mapped to HTTP semantics. The
433 consequence is handled in on_frame_recv(). */
437 goodname = nghttp2_check_header_name(name, namelen);
438 goodheader = nghttp2_check_header_value(value, valuelen);
440 if(!goodname || !goodheader) {
442 infof(conn->data, "Detected bad incoming header %s%s, reset stream!\n",
444 goodheader?"":"value");
446 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
448 NGHTTP2_PROTOCOL_ERROR);
450 if(nghttp2_is_fatal(rv)) {
451 return NGHTTP2_ERR_CALLBACK_FAILURE;
454 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
457 if(namelen == sizeof(":status") - 1 &&
458 memcmp(STATUS, name, namelen) == 0) {
460 /* :status must appear exactly once. */
461 if(c->status_code != -1 ||
462 (c->status_code = decode_status_code(value, valuelen)) == -1) {
464 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
466 NGHTTP2_PROTOCOL_ERROR);
467 if(nghttp2_is_fatal(rv)) {
468 return NGHTTP2_ERR_CALLBACK_FAILURE;
471 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
474 Curl_add_buffer(c->header_recvbuf, "HTTP/2.0 ", 9);
475 Curl_add_buffer(c->header_recvbuf, value, valuelen);
476 Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
481 /* Here we are sure that namelen > 0 because of
482 nghttp2_check_header_name(). Pseudo header other than :status
484 if(c->status_code == -1 || name[0] == ':') {
485 rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
487 NGHTTP2_PROTOCOL_ERROR);
488 if(nghttp2_is_fatal(rv)) {
489 return NGHTTP2_ERR_CALLBACK_FAILURE;
492 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
495 /* convert to a HTTP1-style header */
496 Curl_add_buffer(c->header_recvbuf, name, namelen);
497 Curl_add_buffer(c->header_recvbuf, ":", 1);
498 Curl_add_buffer(c->header_recvbuf, value, valuelen);
499 Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
501 infof(conn->data, "got http2 header: %.*s: %.*s\n",
502 namelen, name, valuelen, value);
505 return 0; /* 0 is successful */
508 static ssize_t data_source_read_callback(nghttp2_session *session,
510 uint8_t *buf, size_t length,
511 uint32_t *data_flags,
512 nghttp2_data_source *source,
515 struct connectdata *conn = (struct connectdata *)userp;
516 struct http_conn *c = &conn->proto.httpc;
522 nread = c->upload_len < length ? c->upload_len : length;
524 memcpy(buf, c->upload_mem, nread);
525 c->upload_mem += nread;
526 c->upload_len -= nread;
527 c->upload_left -= nread;
530 if(c->upload_left == 0)
533 return NGHTTP2_ERR_DEFERRED;
539 * The HTTP2 settings we send in the Upgrade request
541 static nghttp2_settings_entry settings[] = {
542 { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
543 { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
546 #define H2_BUFSIZE 4096
549 * Initialize nghttp2 for a Curl connection
551 CURLcode Curl_http2_init(struct connectdata *conn)
553 if(!conn->proto.httpc.h2) {
555 nghttp2_session_callbacks *callbacks;
557 conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
558 if(conn->proto.httpc.inbuf == NULL)
559 return CURLE_OUT_OF_MEMORY;
561 rc = nghttp2_session_callbacks_new(&callbacks);
564 failf(conn->data, "Couldn't initialize nghttp2 callbacks!");
565 return CURLE_OUT_OF_MEMORY; /* most likely at least */
568 /* nghttp2_send_callback */
569 nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
570 /* nghttp2_on_frame_recv_callback */
571 nghttp2_session_callbacks_set_on_frame_recv_callback
572 (callbacks, on_frame_recv);
573 /* nghttp2_on_invalid_frame_recv_callback */
574 nghttp2_session_callbacks_set_on_invalid_frame_recv_callback
575 (callbacks, on_invalid_frame_recv);
576 /* nghttp2_on_data_chunk_recv_callback */
577 nghttp2_session_callbacks_set_on_data_chunk_recv_callback
578 (callbacks, on_data_chunk_recv);
579 /* nghttp2_before_frame_send_callback */
580 nghttp2_session_callbacks_set_before_frame_send_callback
581 (callbacks, before_frame_send);
582 /* nghttp2_on_frame_send_callback */
583 nghttp2_session_callbacks_set_on_frame_send_callback
584 (callbacks, on_frame_send);
585 /* nghttp2_on_frame_not_send_callback */
586 nghttp2_session_callbacks_set_on_frame_not_send_callback
587 (callbacks, on_frame_not_send);
588 /* nghttp2_on_stream_close_callback */
589 nghttp2_session_callbacks_set_on_stream_close_callback
590 (callbacks, on_stream_close);
591 /* nghttp2_on_begin_headers_callback */
592 nghttp2_session_callbacks_set_on_begin_headers_callback
593 (callbacks, on_begin_headers);
594 /* nghttp2_on_header_callback */
595 nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header);
597 /* The nghttp2 session is not yet setup, do it */
598 rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
601 nghttp2_session_callbacks_del(callbacks);
604 failf(conn->data, "Couldn't initialize nghttp2!");
605 return CURLE_OUT_OF_MEMORY; /* most likely at least */
612 * Send a request using http2
614 CURLcode Curl_http2_send_request(struct connectdata *conn)
621 * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
623 CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req,
624 struct connectdata *conn)
630 struct SingleRequest *k = &conn->data->req;
631 uint8_t *binsettings = conn->proto.httpc.binsettings;
633 result = Curl_http2_init(conn);
637 result = Curl_http2_setup(conn);
641 /* As long as we have a fixed set of settings, we don't have to dynamically
642 * figure out the base64 strings since it'll always be the same. However,
643 * the settings will likely not be fixed every time in the future.
646 /* this returns number of bytes it wrote */
647 binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
649 sizeof(settings)/sizeof(settings[0]));
651 failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
652 return CURLE_FAILED_INIT;
654 conn->proto.httpc.binlen = binlen;
656 result = Curl_base64url_encode(conn->data, (const char *)binsettings, binlen,
661 result = Curl_add_bufferf(req,
662 "Connection: Upgrade, HTTP2-Settings\r\n"
664 "HTTP2-Settings: %s\r\n",
665 NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
666 Curl_safefree(base64);
668 k->upgr101 = UPGR101_REQUESTED;
674 * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
675 * a regular CURLcode value.
677 static ssize_t http2_recv(struct connectdata *conn, int sockindex,
678 char *mem, size_t len, CURLcode *err)
680 CURLcode result = CURLE_OK;
683 struct http_conn *httpc = &conn->proto.httpc;
685 (void)sockindex; /* we always do HTTP2 on sockindex 0 */
688 /* Reset to FALSE to prevent infinite loop in readwrite_data
690 httpc->closed = FALSE;
694 /* Nullify here because we call nghttp2_session_send() and they
695 might refer to the old buffer. */
696 httpc->upload_mem = NULL;
697 httpc->upload_len = 0;
699 if(httpc->bodystarted &&
700 httpc->nread_header_recvbuf < httpc->header_recvbuf->size_used) {
702 httpc->header_recvbuf->size_used - httpc->nread_header_recvbuf;
703 size_t ncopy = len < left ? len : left;
704 memcpy(mem, httpc->header_recvbuf->buffer + httpc->nread_header_recvbuf,
706 httpc->nread_header_recvbuf += ncopy;
711 nread = len < httpc->datalen ? len : httpc->datalen;
712 memcpy(mem, httpc->data, nread);
714 httpc->data += nread;
715 httpc->datalen -= nread;
717 infof(conn->data, "%zu data written\n", nread);
718 if(httpc->datalen == 0) {
725 conn->proto.httpc.mem = mem;
726 conn->proto.httpc.len = len;
728 infof(conn->data, "http2_recv: %d bytes buffer\n",
729 conn->proto.httpc.len);
731 nread = ((Curl_recv*)httpc->recv_underlying)(conn, FIRSTSOCKET,
732 httpc->inbuf, H2_BUFSIZE,
734 if(result == CURLE_AGAIN) {
740 failf(conn->data, "Failed receiving HTTP2 data");
745 infof(conn->data, "nread=%zd\n", nread);
748 failf(conn->data, "EOF");
752 rv = nghttp2_session_mem_recv(httpc->h2,
753 (const uint8_t *)httpc->inbuf, nread);
755 if(nghttp2_is_fatal((int)rv)) {
756 failf(conn->data, "nghttp2_session_mem_recv() returned %d:%s\n",
757 rv, nghttp2_strerror((int)rv));
758 *err = CURLE_RECV_ERROR;
761 infof(conn->data, "nghttp2_session_mem_recv() returns %zd\n", rv);
762 /* Always send pending frames in nghttp2 session, because
763 nghttp2_session_mem_recv() may queue new frame */
764 rv = nghttp2_session_send(httpc->h2);
766 *err = CURLE_SEND_ERROR;
769 if(len != httpc->len) {
770 return len - conn->proto.httpc.len;
772 /* If stream is closed, return 0 to signal the http routine to close
775 /* Reset to FALSE to prevent infinite loop in readwrite_data
777 httpc->closed = FALSE;
784 /* Index where :authority header field will appear in request header
786 #define AUTHORITY_DST_IDX 3
788 /* return number of received (decrypted) bytes */
789 static ssize_t http2_send(struct connectdata *conn, int sockindex,
790 const void *mem, size_t len, CURLcode *err)
793 * BIG TODO: Currently, we send request in this function, but this
794 * function is also used to send request body. It would be nice to
795 * add dedicated function for request.
798 struct http_conn *httpc = &conn->proto.httpc;
802 size_t authority_idx;
803 char *hdbuf = (char*)mem;
805 nghttp2_data_provider data_prd;
810 infof(conn->data, "http2_send len=%zu\n", len);
812 if(httpc->stream_id != -1) {
813 /* If stream_id != -1, we have dispatched request HEADERS, and now
814 are going to send or sending request body in DATA frame */
815 httpc->upload_mem = mem;
816 httpc->upload_len = len;
817 nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
818 rv = nghttp2_session_send(httpc->h2);
819 if(nghttp2_is_fatal(rv)) {
820 *err = CURLE_SEND_ERROR;
823 return len - httpc->upload_len;
826 /* Calculate number of headers contained in [mem, mem + len) */
827 /* Here, we assume the curl http code generate *correct* HTTP header
830 for(i = 0; i < len; ++i) {
831 if(hdbuf[i] == 0x0a) {
835 /* We counted additional 2 \n in the first and last line. We need 3
836 new headers: :method, :path and :scheme. Therefore we need one
839 nva = malloc(sizeof(nghttp2_nv) * nheader);
841 *err = CURLE_OUT_OF_MEMORY;
844 /* Extract :method, :path from request line */
845 end = strchr(hdbuf, ' ');
846 nva[0].name = (unsigned char *)":method";
847 nva[0].namelen = (uint16_t)strlen((char *)nva[0].name);
848 nva[0].value = (unsigned char *)hdbuf;
849 nva[0].valuelen = (uint16_t)(end - hdbuf);
850 nva[0].flags = NGHTTP2_NV_FLAG_NONE;
854 end = strchr(hdbuf, ' ');
855 nva[1].name = (unsigned char *)":path";
856 nva[1].namelen = (uint16_t)strlen((char *)nva[1].name);
857 nva[1].value = (unsigned char *)hdbuf;
858 nva[1].valuelen = (uint16_t)(end - hdbuf);
859 nva[1].flags = NGHTTP2_NV_FLAG_NONE;
861 nva[2].name = (unsigned char *)":scheme";
862 nva[2].namelen = (uint16_t)strlen((char *)nva[2].name);
863 if(conn->handler->flags & PROTOPT_SSL)
864 nva[2].value = (unsigned char *)"https";
866 nva[2].value = (unsigned char *)"http";
867 nva[2].valuelen = (uint16_t)strlen((char *)nva[2].value);
868 nva[2].flags = NGHTTP2_NV_FLAG_NONE;
870 hdbuf = strchr(hdbuf, 0x0a);
875 for(i = 3; i < nheader; ++i) {
876 end = strchr(hdbuf, ':');
878 if(end - hdbuf == 4 && Curl_raw_nequal("host", hdbuf, 4)) {
880 nva[i].name = (unsigned char *)":authority";
881 nva[i].namelen = (uint16_t)strlen((char *)nva[i].name);
884 nva[i].name = (unsigned char *)hdbuf;
885 nva[i].namelen = (uint16_t)(end - hdbuf);
888 for(; *hdbuf == ' '; ++hdbuf);
889 end = strchr(hdbuf, 0x0d);
891 nva[i].value = (unsigned char *)hdbuf;
892 nva[i].valuelen = (uint16_t)(end - hdbuf);
893 nva[i].flags = NGHTTP2_NV_FLAG_NONE;
896 /* Inspect Content-Length header field and retrieve the request
897 entity length so that we can set END_STREAM to the last DATA
899 if(nva[i].namelen == 14 &&
900 Curl_raw_nequal("content-length", (char*)nva[i].name, 14)) {
902 for(j = 0; j < nva[i].valuelen; ++j) {
903 httpc->upload_left *= 10;
904 httpc->upload_left += nva[i].value[j] - '0';
906 infof(conn->data, "request content-length=%zu\n", httpc->upload_left);
910 /* :authority must come before non-pseudo header fields */
911 if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
912 nghttp2_nv authority = nva[authority_idx];
913 for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
919 switch(conn->data->set.httpreq) {
921 case HTTPREQ_POST_FORM:
923 data_prd.read_callback = data_source_read_callback;
924 data_prd.source.ptr = NULL;
925 stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
929 stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
936 *err = CURLE_SEND_ERROR;
940 httpc->stream_id = stream_id;
942 rv = nghttp2_session_send(httpc->h2);
945 *err = CURLE_SEND_ERROR;
949 if(httpc->stream_id != -1) {
950 /* If whole HEADERS frame was sent off to the underlying socket,
951 the nghttp2 library calls data_source_read_callback. But only
952 it found that no data available, so it deferred the DATA
953 transmission. Which means that nghttp2_session_want_write()
954 returns 0 on http2_perform_getsock(), which results that no
955 writable socket check is performed. To workaround this, we
956 issue nghttp2_session_resume_data() here to bring back DATA
957 transmission from deferred state. */
958 nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
964 CURLcode Curl_http2_setup(struct connectdata *conn)
966 struct http_conn *httpc = &conn->proto.httpc;
967 if(conn->handler->flags & PROTOPT_SSL)
968 conn->handler = &Curl_handler_http2_ssl;
970 conn->handler = &Curl_handler_http2;
972 infof(conn->data, "Using HTTP2\n");
973 httpc->bodystarted = FALSE;
974 httpc->closed = FALSE;
975 httpc->header_recvbuf = Curl_add_buffer_init();
976 httpc->nread_header_recvbuf = 0;
979 httpc->upload_left = 0;
980 httpc->upload_mem = NULL;
981 httpc->upload_len = 0;
982 httpc->stream_id = -1;
983 httpc->status_code = -1;
985 conn->httpversion = 20;
990 CURLcode Curl_http2_switched(struct connectdata *conn,
991 const char *mem, size_t nread)
994 struct http_conn *httpc = &conn->proto.httpc;
996 struct SessionHandle *data = conn->data;
998 httpc->recv_underlying = (recving)conn->recv[FIRSTSOCKET];
999 httpc->send_underlying = (sending)conn->send[FIRSTSOCKET];
1000 conn->recv[FIRSTSOCKET] = http2_recv;
1001 conn->send[FIRSTSOCKET] = http2_send;
1003 rv = (int) ((Curl_send*)httpc->send_underlying)
1005 NGHTTP2_CLIENT_CONNECTION_PREFACE,
1006 NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN,
1009 /* TODO: This may get CURLE_AGAIN */
1013 failf(data, "Only sent partial HTTP2 packet");
1014 return CURLE_SEND_ERROR;
1017 if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
1018 /* stream 1 is opened implicitly on upgrade */
1019 httpc->stream_id = 1;
1020 /* queue SETTINGS frame (again) */
1021 rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
1022 httpc->binlen, NULL);
1024 failf(data, "nghttp2_session_upgrade() failed: %s(%d)",
1025 nghttp2_strerror(rv), rv);
1030 /* stream ID is unknown at this point */
1031 httpc->stream_id = -1;
1032 rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE, NULL, 0);
1034 failf(data, "nghttp2_submit_settings() failed: %s(%d)",
1035 nghttp2_strerror(rv), rv);
1040 rv = (int)nghttp2_session_mem_recv(httpc->h2, (const uint8_t*)mem, nread);
1042 if(rv != (int)nread) {
1043 failf(data, "nghttp2_session_mem_recv() failed: %s(%d)",
1044 nghttp2_strerror(rv), rv);