Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 13 | * |
| 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. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | |
| 23 | #include "curl_setup.h" |
| 24 | |
| 25 | #ifdef USE_NGHTTP2 |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 26 | #include <nghttp2/nghttp2.h> |
| 27 | #include "urldata.h" |
| 28 | #include "http2.h" |
| 29 | #include "http.h" |
| 30 | #include "sendf.h" |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 31 | #include "select.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 32 | #include "curl_base64.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 33 | #include "strcase.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 34 | #include "multiif.h" |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 35 | #include "url.h" |
| 36 | #include "connect.h" |
| 37 | #include "strtoofft.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 38 | #include "strdup.h" |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 39 | /* The last 3 #include files should be in this order */ |
| 40 | #include "curl_printf.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 41 | #include "curl_memory.h" |
| 42 | #include "memdebug.h" |
| 43 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 44 | #define H2_BUFSIZE 32768 |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 45 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
| 46 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 47 | #if (NGHTTP2_VERSION_NUM < 0x010000) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 48 | #error too old nghttp2 version, upgrade! |
| 49 | #endif |
| 50 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 51 | #if (NGHTTP2_VERSION_NUM > 0x010800) |
| 52 | #define NGHTTP2_HAS_HTTP2_STRERROR 1 |
| 53 | #endif |
| 54 | |
| 55 | #if (NGHTTP2_VERSION_NUM >= 0x010900) |
| 56 | /* nghttp2_session_callbacks_set_error_callback is present in nghttp2 1.9.0 or |
| 57 | later */ |
| 58 | #define NGHTTP2_HAS_ERROR_CALLBACK 1 |
| 59 | #else |
| 60 | #define nghttp2_session_callbacks_set_error_callback(x,y) |
| 61 | #endif |
| 62 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 63 | #if (NGHTTP2_VERSION_NUM >= 0x010c00) |
| 64 | #define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1 |
| 65 | #endif |
| 66 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 67 | #define HTTP2_HUGE_WINDOW_SIZE (1 << 30) |
| 68 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 69 | #ifdef DEBUG_HTTP2 |
| 70 | #define H2BUGF(x) x |
| 71 | #else |
| 72 | #define H2BUGF(x) do { } WHILE_FALSE |
| 73 | #endif |
| 74 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 75 | |
| 76 | static ssize_t http2_recv(struct connectdata *conn, int sockindex, |
| 77 | char *mem, size_t len, CURLcode *err); |
| 78 | static bool http2_connisdead(struct connectdata *conn); |
| 79 | static int h2_session_send(struct Curl_easy *data, |
| 80 | nghttp2_session *h2); |
| 81 | static int h2_process_pending_input(struct connectdata *conn, |
| 82 | struct http_conn *httpc, |
| 83 | CURLcode *err); |
| 84 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Curl_http2_init_state() is called when the easy handle is created and |
| 87 | * allows for HTTP/2 specific init of state. |
| 88 | */ |
| 89 | void Curl_http2_init_state(struct UrlState *state) |
| 90 | { |
| 91 | state->stream_weight = NGHTTP2_DEFAULT_WEIGHT; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Curl_http2_init_userset() is called when the easy handle is created and |
| 96 | * allows for HTTP/2 specific user-set fields. |
| 97 | */ |
| 98 | void Curl_http2_init_userset(struct UserDefined *set) |
| 99 | { |
| 100 | set->stream_weight = NGHTTP2_DEFAULT_WEIGHT; |
| 101 | } |
| 102 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 103 | static int http2_perform_getsock(const struct connectdata *conn, |
| 104 | curl_socket_t *sock, /* points to |
| 105 | numsocks |
| 106 | number of |
| 107 | sockets */ |
| 108 | int numsocks) |
| 109 | { |
| 110 | const struct http_conn *c = &conn->proto.httpc; |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 111 | struct SingleRequest *k = &conn->data->req; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 112 | int bitmap = GETSOCK_BLANK; |
| 113 | (void)numsocks; |
| 114 | |
| 115 | /* TODO We should check underlying socket state if it is SSL socket |
| 116 | because of renegotiation. */ |
| 117 | sock[0] = conn->sock[FIRSTSOCKET]; |
| 118 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 119 | /* in a HTTP/2 connection we can basically always get a frame so we should |
| 120 | always be ready for one */ |
| 121 | bitmap |= GETSOCK_READSOCK(FIRSTSOCKET); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 122 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 123 | /* we're still uploading or the HTTP/2 layer wants to send data */ |
| 124 | if(((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND) || |
| 125 | nghttp2_session_want_write(c->h2)) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 126 | bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET); |
| 127 | |
| 128 | return bitmap; |
| 129 | } |
| 130 | |
| 131 | static int http2_getsock(struct connectdata *conn, |
| 132 | curl_socket_t *sock, /* points to numsocks |
| 133 | number of sockets */ |
| 134 | int numsocks) |
| 135 | { |
| 136 | return http2_perform_getsock(conn, sock, numsocks); |
| 137 | } |
| 138 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 139 | /* |
| 140 | * http2_stream_free() free HTTP2 stream related data |
| 141 | */ |
| 142 | static void http2_stream_free(struct HTTP *http) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 143 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 144 | if(http) { |
| 145 | Curl_add_buffer_free(http->header_recvbuf); |
| 146 | http->header_recvbuf = NULL; /* clear the pointer */ |
| 147 | Curl_add_buffer_free(http->trailer_recvbuf); |
| 148 | http->trailer_recvbuf = NULL; /* clear the pointer */ |
| 149 | for(; http->push_headers_used > 0; --http->push_headers_used) { |
| 150 | free(http->push_headers[http->push_headers_used - 1]); |
| 151 | } |
| 152 | free(http->push_headers); |
| 153 | http->push_headers = NULL; |
| 154 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static CURLcode http2_disconnect(struct connectdata *conn, |
| 158 | bool dead_connection) |
| 159 | { |
| 160 | struct http_conn *c = &conn->proto.httpc; |
| 161 | (void)dead_connection; |
| 162 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 163 | H2BUGF(infof(conn->data, "HTTP/2 DISCONNECT starts now\n")); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 164 | |
| 165 | nghttp2_session_del(c->h2); |
| 166 | Curl_safefree(c->inbuf); |
| 167 | http2_stream_free(conn->data->req.protop); |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 168 | conn->data->state.drain = 0; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 169 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 170 | H2BUGF(infof(conn->data, "HTTP/2 DISCONNECT done\n")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 171 | |
| 172 | return CURLE_OK; |
| 173 | } |
| 174 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 175 | /* |
| 176 | * The server may send us data at any point (e.g. PING frames). Therefore, |
| 177 | * we cannot assume that an HTTP/2 socket is dead just because it is readable. |
| 178 | * |
| 179 | * Instead, if it is readable, run Curl_connalive() to peek at the socket |
| 180 | * and distinguish between closed and data. |
| 181 | */ |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 182 | static bool http2_connisdead(struct connectdata *conn) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 183 | { |
| 184 | int sval; |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 185 | bool dead = TRUE; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 186 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 187 | if(conn->bits.close) |
| 188 | return TRUE; |
| 189 | |
| 190 | sval = SOCKET_READABLE(conn->sock[FIRSTSOCKET], 0); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 191 | if(sval == 0) { |
| 192 | /* timeout */ |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 193 | dead = FALSE; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 194 | } |
| 195 | else if(sval & CURL_CSELECT_ERR) { |
| 196 | /* socket is in an error state */ |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 197 | dead = TRUE; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 198 | } |
| 199 | else if(sval & CURL_CSELECT_IN) { |
| 200 | /* readable with no error. could still be closed */ |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 201 | dead = !Curl_connalive(conn); |
| 202 | if(!dead) { |
| 203 | /* This happens before we've sent off a request and the connection is |
| 204 | not in use by any other thransfer, there shouldn't be any data here, |
| 205 | only "protocol frames" */ |
| 206 | CURLcode result; |
| 207 | struct http_conn *httpc = &conn->proto.httpc; |
| 208 | ssize_t nread = -1; |
| 209 | if(httpc->recv_underlying) |
| 210 | /* if called "too early", this pointer isn't setup yet! */ |
| 211 | nread = ((Curl_recv *)httpc->recv_underlying)( |
| 212 | conn, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result); |
| 213 | if(nread != -1) { |
| 214 | infof(conn->data, |
| 215 | "%d bytes stray data read before trying h2 connection\n", |
| 216 | (int)nread); |
| 217 | httpc->nread_inbuf = 0; |
| 218 | httpc->inbuflen = nread; |
| 219 | (void)h2_process_pending_input(conn, httpc, &result); |
| 220 | } |
| 221 | else |
| 222 | /* the read failed so let's say this is dead anyway */ |
| 223 | dead = TRUE; |
| 224 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 227 | return dead; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 230 | static unsigned int http2_conncheck(struct connectdata *check, |
| 231 | unsigned int checks_to_perform) |
| 232 | { |
| 233 | unsigned int ret_val = CONNRESULT_NONE; |
| 234 | |
| 235 | if(checks_to_perform & CONNCHECK_ISDEAD) { |
| 236 | if(http2_connisdead(check)) |
| 237 | ret_val |= CONNRESULT_DEAD; |
| 238 | } |
| 239 | |
| 240 | return ret_val; |
| 241 | } |
| 242 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 243 | /* called from Curl_http_setup_conn */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 244 | void Curl_http2_setup_req(struct Curl_easy *data) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 245 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 246 | struct HTTP *http = data->req.protop; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 247 | |
| 248 | http->nread_header_recvbuf = 0; |
| 249 | http->bodystarted = FALSE; |
| 250 | http->status_code = -1; |
| 251 | http->pausedata = NULL; |
| 252 | http->pauselen = 0; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 253 | http->closed = FALSE; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 254 | http->close_handled = FALSE; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 255 | http->mem = data->state.buffer; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 256 | http->len = data->set.buffer_size; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 257 | http->memlen = 0; |
| 258 | } |
| 259 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 260 | /* called from Curl_http_setup_conn */ |
| 261 | void Curl_http2_setup_conn(struct connectdata *conn) |
| 262 | { |
| 263 | conn->proto.httpc.settings.max_concurrent_streams = |
| 264 | DEFAULT_MAX_CONCURRENT_STREAMS; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 265 | conn->proto.httpc.error_code = NGHTTP2_NO_ERROR; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 268 | /* |
| 269 | * HTTP2 handler interface. This isn't added to the general list of protocols |
| 270 | * but will be used at run-time when the protocol is dynamically switched from |
| 271 | * HTTP to HTTP2. |
| 272 | */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 273 | static const struct Curl_handler Curl_handler_http2 = { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 274 | "HTTP", /* scheme */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 275 | ZERO_NULL, /* setup_connection */ |
| 276 | Curl_http, /* do_it */ |
| 277 | Curl_http_done, /* done */ |
| 278 | ZERO_NULL, /* do_more */ |
| 279 | ZERO_NULL, /* connect_it */ |
| 280 | ZERO_NULL, /* connecting */ |
| 281 | ZERO_NULL, /* doing */ |
| 282 | http2_getsock, /* proto_getsock */ |
| 283 | http2_getsock, /* doing_getsock */ |
| 284 | ZERO_NULL, /* domore_getsock */ |
| 285 | http2_perform_getsock, /* perform_getsock */ |
| 286 | http2_disconnect, /* disconnect */ |
| 287 | ZERO_NULL, /* readwrite */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 288 | http2_conncheck, /* connection_check */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 289 | PORT_HTTP, /* defport */ |
| 290 | CURLPROTO_HTTP, /* protocol */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 291 | PROTOPT_STREAM /* flags */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 292 | }; |
| 293 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 294 | static const struct Curl_handler Curl_handler_http2_ssl = { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 295 | "HTTPS", /* scheme */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 296 | ZERO_NULL, /* setup_connection */ |
| 297 | Curl_http, /* do_it */ |
| 298 | Curl_http_done, /* done */ |
| 299 | ZERO_NULL, /* do_more */ |
| 300 | ZERO_NULL, /* connect_it */ |
| 301 | ZERO_NULL, /* connecting */ |
| 302 | ZERO_NULL, /* doing */ |
| 303 | http2_getsock, /* proto_getsock */ |
| 304 | http2_getsock, /* doing_getsock */ |
| 305 | ZERO_NULL, /* domore_getsock */ |
| 306 | http2_perform_getsock, /* perform_getsock */ |
| 307 | http2_disconnect, /* disconnect */ |
| 308 | ZERO_NULL, /* readwrite */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 309 | http2_conncheck, /* connection_check */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 310 | PORT_HTTP, /* defport */ |
| 311 | CURLPROTO_HTTPS, /* protocol */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 312 | PROTOPT_SSL | PROTOPT_STREAM /* flags */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | /* |
| 316 | * Store nghttp2 version info in this buffer, Prefix with a space. Return |
| 317 | * total length written. |
| 318 | */ |
| 319 | int Curl_http2_ver(char *p, size_t len) |
| 320 | { |
| 321 | nghttp2_info *h2 = nghttp2_version(0); |
| 322 | return snprintf(p, len, " nghttp2/%s", h2->version_str); |
| 323 | } |
| 324 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 325 | /* HTTP/2 error code to name based on the Error Code Registry. |
| 326 | https://tools.ietf.org/html/rfc7540#page-77 |
| 327 | nghttp2_error_code enums are identical. |
| 328 | */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 329 | const char *Curl_http2_strerror(uint32_t err) |
| 330 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 331 | #ifndef NGHTTP2_HAS_HTTP2_STRERROR |
| 332 | const char *str[] = { |
| 333 | "NO_ERROR", /* 0x0 */ |
| 334 | "PROTOCOL_ERROR", /* 0x1 */ |
| 335 | "INTERNAL_ERROR", /* 0x2 */ |
| 336 | "FLOW_CONTROL_ERROR", /* 0x3 */ |
| 337 | "SETTINGS_TIMEOUT", /* 0x4 */ |
| 338 | "STREAM_CLOSED", /* 0x5 */ |
| 339 | "FRAME_SIZE_ERROR", /* 0x6 */ |
| 340 | "REFUSED_STREAM", /* 0x7 */ |
| 341 | "CANCEL", /* 0x8 */ |
| 342 | "COMPRESSION_ERROR", /* 0x9 */ |
| 343 | "CONNECT_ERROR", /* 0xA */ |
| 344 | "ENHANCE_YOUR_CALM", /* 0xB */ |
| 345 | "INADEQUATE_SECURITY", /* 0xC */ |
| 346 | "HTTP_1_1_REQUIRED" /* 0xD */ |
| 347 | }; |
| 348 | return (err < sizeof str / sizeof str[0]) ? str[err] : "unknown"; |
| 349 | #else |
| 350 | return nghttp2_http2_strerror(err); |
| 351 | #endif |
| 352 | } |
| 353 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 354 | /* |
| 355 | * The implementation of nghttp2_send_callback type. Here we write |data| with |
| 356 | * size |length| to the network and return the number of bytes actually |
| 357 | * written. See the documentation of nghttp2_send_callback for the details. |
| 358 | */ |
| 359 | static ssize_t send_callback(nghttp2_session *h2, |
| 360 | const uint8_t *data, size_t length, int flags, |
| 361 | void *userp) |
| 362 | { |
| 363 | struct connectdata *conn = (struct connectdata *)userp; |
| 364 | struct http_conn *c = &conn->proto.httpc; |
| 365 | ssize_t written; |
| 366 | CURLcode result = CURLE_OK; |
| 367 | |
| 368 | (void)h2; |
| 369 | (void)flags; |
| 370 | |
| 371 | written = ((Curl_send*)c->send_underlying)(conn, FIRSTSOCKET, |
| 372 | data, length, &result); |
| 373 | |
| 374 | if(result == CURLE_AGAIN) { |
| 375 | return NGHTTP2_ERR_WOULDBLOCK; |
| 376 | } |
| 377 | |
| 378 | if(written == -1) { |
| 379 | failf(conn->data, "Failed sending HTTP2 data"); |
| 380 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 381 | } |
| 382 | |
| 383 | if(!written) |
| 384 | return NGHTTP2_ERR_WOULDBLOCK; |
| 385 | |
| 386 | return written; |
| 387 | } |
| 388 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 389 | |
| 390 | /* We pass a pointer to this struct in the push callback, but the contents of |
| 391 | the struct are hidden from the user. */ |
| 392 | struct curl_pushheaders { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 393 | struct Curl_easy *data; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 394 | const nghttp2_push_promise *frame; |
| 395 | }; |
| 396 | |
| 397 | /* |
| 398 | * push header access function. Only to be used from within the push callback |
| 399 | */ |
| 400 | char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num) |
| 401 | { |
| 402 | /* Verify that we got a good easy handle in the push header struct, mostly to |
| 403 | detect rubbish input fast(er). */ |
| 404 | if(!h || !GOOD_EASY_HANDLE(h->data)) |
| 405 | return NULL; |
| 406 | else { |
| 407 | struct HTTP *stream = h->data->req.protop; |
| 408 | if(num < stream->push_headers_used) |
| 409 | return stream->push_headers[num]; |
| 410 | } |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * push header access function. Only to be used from within the push callback |
| 416 | */ |
| 417 | char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header) |
| 418 | { |
| 419 | /* Verify that we got a good easy handle in the push header struct, |
| 420 | mostly to detect rubbish input fast(er). Also empty header name |
| 421 | is just a rubbish too. We have to allow ":" at the beginning of |
| 422 | the header, but header == ":" must be rejected. If we have ':' in |
| 423 | the middle of header, it could be matched in middle of the value, |
| 424 | this is because we do prefix match.*/ |
| 425 | if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] || |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 426 | !strcmp(header, ":") || strchr(header + 1, ':')) |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 427 | return NULL; |
| 428 | else { |
| 429 | struct HTTP *stream = h->data->req.protop; |
| 430 | size_t len = strlen(header); |
| 431 | size_t i; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 432 | for(i = 0; i<stream->push_headers_used; i++) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 433 | if(!strncmp(header, stream->push_headers[i], len)) { |
| 434 | /* sub-match, make sure that it is followed by a colon */ |
| 435 | if(stream->push_headers[i][len] != ':') |
| 436 | continue; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 437 | return &stream->push_headers[i][len + 1]; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
| 441 | return NULL; |
| 442 | } |
| 443 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 444 | static struct Curl_easy *duphandle(struct Curl_easy *data) |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 445 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 446 | struct Curl_easy *second = curl_easy_duphandle(data); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 447 | if(second) { |
| 448 | /* setup the request struct */ |
| 449 | struct HTTP *http = calloc(1, sizeof(struct HTTP)); |
| 450 | if(!http) { |
| 451 | (void)Curl_close(second); |
| 452 | second = NULL; |
| 453 | } |
| 454 | else { |
| 455 | second->req.protop = http; |
| 456 | http->header_recvbuf = Curl_add_buffer_init(); |
| 457 | if(!http->header_recvbuf) { |
| 458 | free(http); |
| 459 | (void)Curl_close(second); |
| 460 | second = NULL; |
| 461 | } |
| 462 | else { |
| 463 | Curl_http2_setup_req(second); |
| 464 | second->state.stream_weight = data->state.stream_weight; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | return second; |
| 469 | } |
| 470 | |
| 471 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 472 | static int push_promise(struct Curl_easy *data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 473 | struct connectdata *conn, |
| 474 | const nghttp2_push_promise *frame) |
| 475 | { |
| 476 | int rv; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 477 | H2BUGF(infof(data, "PUSH_PROMISE received, stream %u!\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 478 | frame->promised_stream_id)); |
| 479 | if(data->multi->push_cb) { |
| 480 | struct HTTP *stream; |
| 481 | struct HTTP *newstream; |
| 482 | struct curl_pushheaders heads; |
| 483 | CURLMcode rc; |
| 484 | struct http_conn *httpc; |
| 485 | size_t i; |
| 486 | /* clone the parent */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 487 | struct Curl_easy *newhandle = duphandle(data); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 488 | if(!newhandle) { |
| 489 | infof(data, "failed to duplicate handle\n"); |
| 490 | rv = 1; /* FAIL HARD */ |
| 491 | goto fail; |
| 492 | } |
| 493 | |
| 494 | heads.data = data; |
| 495 | heads.frame = frame; |
| 496 | /* ask the application */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 497 | H2BUGF(infof(data, "Got PUSH_PROMISE, ask application!\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 498 | |
| 499 | stream = data->req.protop; |
| 500 | if(!stream) { |
| 501 | failf(data, "Internal NULL stream!\n"); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 502 | (void)Curl_close(newhandle); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 503 | rv = 1; |
| 504 | goto fail; |
| 505 | } |
| 506 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 507 | Curl_set_in_callback(data, true); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 508 | rv = data->multi->push_cb(data, newhandle, |
| 509 | stream->push_headers_used, &heads, |
| 510 | data->multi->push_userp); |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 511 | Curl_set_in_callback(data, false); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 512 | |
| 513 | /* free the headers again */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 514 | for(i = 0; i<stream->push_headers_used; i++) |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 515 | free(stream->push_headers[i]); |
| 516 | free(stream->push_headers); |
| 517 | stream->push_headers = NULL; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 518 | stream->push_headers_used = 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 519 | |
| 520 | if(rv) { |
| 521 | /* denied, kill off the new handle again */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 522 | http2_stream_free(newhandle->req.protop); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 523 | (void)Curl_close(newhandle); |
| 524 | goto fail; |
| 525 | } |
| 526 | |
| 527 | newstream = newhandle->req.protop; |
| 528 | newstream->stream_id = frame->promised_stream_id; |
| 529 | newhandle->req.maxdownload = -1; |
| 530 | newhandle->req.size = -1; |
| 531 | |
| 532 | /* approved, add to the multi handle and immediately switch to PERFORM |
| 533 | state with the given connection !*/ |
| 534 | rc = Curl_multi_add_perform(data->multi, newhandle, conn); |
| 535 | if(rc) { |
| 536 | infof(data, "failed to add handle to multi\n"); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 537 | http2_stream_free(newhandle->req.protop); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 538 | Curl_close(newhandle); |
| 539 | rv = 1; |
| 540 | goto fail; |
| 541 | } |
| 542 | |
| 543 | httpc = &conn->proto.httpc; |
| 544 | nghttp2_session_set_stream_user_data(httpc->h2, |
| 545 | frame->promised_stream_id, newhandle); |
| 546 | } |
| 547 | else { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 548 | H2BUGF(infof(data, "Got PUSH_PROMISE, ignore it!\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 549 | rv = 1; |
| 550 | } |
| 551 | fail: |
| 552 | return rv; |
| 553 | } |
| 554 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 555 | static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, |
| 556 | void *userp) |
| 557 | { |
| 558 | struct connectdata *conn = (struct connectdata *)userp; |
| 559 | struct http_conn *httpc = &conn->proto.httpc; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 560 | struct Curl_easy *data_s = NULL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 561 | struct HTTP *stream = NULL; |
| 562 | int rv; |
| 563 | size_t left, ncopy; |
| 564 | int32_t stream_id = frame->hd.stream_id; |
| 565 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 566 | if(!stream_id) { |
| 567 | /* stream ID zero is for connection-oriented stuff */ |
| 568 | if(frame->hd.type == NGHTTP2_SETTINGS) { |
| 569 | uint32_t max_conn = httpc->settings.max_concurrent_streams; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 570 | H2BUGF(infof(conn->data, "Got SETTINGS\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 571 | httpc->settings.max_concurrent_streams = |
| 572 | nghttp2_session_get_remote_settings( |
| 573 | session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS); |
| 574 | httpc->settings.enable_push = |
| 575 | nghttp2_session_get_remote_settings( |
| 576 | session, NGHTTP2_SETTINGS_ENABLE_PUSH); |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 577 | H2BUGF(infof(conn->data, "MAX_CONCURRENT_STREAMS == %d\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 578 | httpc->settings.max_concurrent_streams)); |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 579 | H2BUGF(infof(conn->data, "ENABLE_PUSH == %s\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 580 | httpc->settings.enable_push?"TRUE":"false")); |
| 581 | if(max_conn != httpc->settings.max_concurrent_streams) { |
| 582 | /* only signal change if the value actually changed */ |
| 583 | infof(conn->data, |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 584 | "Connection state changed (MAX_CONCURRENT_STREAMS == %d)!\n", |
| 585 | httpc->settings.max_concurrent_streams); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 586 | Curl_multi_connchanged(conn->data->multi); |
| 587 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 588 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 589 | return 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 590 | } |
| 591 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 592 | if(!data_s) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 593 | H2BUGF(infof(conn->data, |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 594 | "No Curl_easy associated with stream: %x\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 595 | stream_id)); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | stream = data_s->req.protop; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 600 | if(!stream) { |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 601 | H2BUGF(infof(data_s, "No proto pointer for stream: %x\n", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 602 | stream_id)); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 603 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 604 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 605 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 606 | H2BUGF(infof(data_s, "on_frame_recv() header %x stream %x\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 607 | frame->hd.type, stream_id)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 608 | |
| 609 | switch(frame->hd.type) { |
| 610 | case NGHTTP2_DATA: |
| 611 | /* If body started on this stream, then receiving DATA is illegal. */ |
| 612 | if(!stream->bodystarted) { |
| 613 | rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, |
| 614 | stream_id, NGHTTP2_PROTOCOL_ERROR); |
| 615 | |
| 616 | if(nghttp2_is_fatal(rv)) { |
| 617 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 618 | } |
| 619 | } |
| 620 | break; |
| 621 | case NGHTTP2_HEADERS: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 622 | if(stream->bodystarted) { |
| 623 | /* Only valid HEADERS after body started is trailer HEADERS. We |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 624 | buffer them in on_header callback. */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 625 | break; |
| 626 | } |
| 627 | |
| 628 | /* nghttp2 guarantees that :status is received, and we store it to |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 629 | stream->status_code. Fuzzing has proven this can still be reached |
| 630 | without status code having been set. */ |
| 631 | if(stream->status_code == -1) |
| 632 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 633 | |
| 634 | /* Only final status code signals the end of header */ |
| 635 | if(stream->status_code / 100 != 1) { |
| 636 | stream->bodystarted = TRUE; |
| 637 | stream->status_code = -1; |
| 638 | } |
| 639 | |
| 640 | Curl_add_buffer(stream->header_recvbuf, "\r\n", 2); |
| 641 | |
| 642 | left = stream->header_recvbuf->size_used - stream->nread_header_recvbuf; |
| 643 | ncopy = MIN(stream->len, left); |
| 644 | |
| 645 | memcpy(&stream->mem[stream->memlen], |
| 646 | stream->header_recvbuf->buffer + stream->nread_header_recvbuf, |
| 647 | ncopy); |
| 648 | stream->nread_header_recvbuf += ncopy; |
| 649 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 650 | H2BUGF(infof(data_s, "Store %zu bytes headers from stream %u at %p\n", |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 651 | ncopy, stream_id, stream->mem)); |
| 652 | |
| 653 | stream->len -= ncopy; |
| 654 | stream->memlen += ncopy; |
| 655 | |
| 656 | data_s->state.drain++; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 657 | httpc->drain_total++; |
| 658 | { |
| 659 | /* get the pointer from userp again since it was re-assigned above */ |
| 660 | struct connectdata *conn_s = (struct connectdata *)userp; |
| 661 | |
| 662 | /* if we receive data for another handle, wake that up */ |
| 663 | if(conn_s->data != data_s) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 664 | Curl_expire(data_s, 0, EXPIRE_RUN_NOW); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 665 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 666 | break; |
| 667 | case NGHTTP2_PUSH_PROMISE: |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 668 | rv = push_promise(data_s, conn, &frame->push_promise); |
| 669 | if(rv) { /* deny! */ |
| 670 | rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, |
| 671 | frame->push_promise.promised_stream_id, |
| 672 | NGHTTP2_CANCEL); |
| 673 | if(nghttp2_is_fatal(rv)) { |
| 674 | return rv; |
| 675 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 676 | } |
| 677 | break; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 678 | default: |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 679 | H2BUGF(infof(data_s, "Got frame type %x for stream %u!\n", |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 680 | frame->hd.type, stream_id)); |
| 681 | break; |
| 682 | } |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int on_invalid_frame_recv(nghttp2_session *session, |
| 687 | const nghttp2_frame *frame, |
| 688 | int lib_error_code, void *userp) |
| 689 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 690 | struct Curl_easy *data_s = NULL; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 691 | (void)userp; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 692 | #if !defined(DEBUG_HTTP2) || defined(CURL_DISABLE_VERBOSE_STRINGS) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 693 | (void)lib_error_code; |
| 694 | #endif |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 695 | |
| 696 | data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); |
| 697 | if(data_s) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 698 | H2BUGF(infof(data_s, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 699 | "on_invalid_frame_recv() was called, error=%d:%s\n", |
| 700 | lib_error_code, nghttp2_strerror(lib_error_code))); |
| 701 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags, |
| 706 | int32_t stream_id, |
| 707 | const uint8_t *data, size_t len, void *userp) |
| 708 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 709 | struct HTTP *stream; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 710 | struct Curl_easy *data_s; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 711 | size_t nread; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 712 | struct connectdata *conn = (struct connectdata *)userp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 713 | (void)session; |
| 714 | (void)flags; |
| 715 | (void)data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 716 | |
| 717 | DEBUGASSERT(stream_id); /* should never be a zero stream ID here */ |
| 718 | |
| 719 | /* get the stream from the hash based on Stream ID */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 720 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
| 721 | if(!data_s) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 722 | /* Receiving a Stream ID not in the hash should not happen, this is an |
| 723 | internal error more than anything else! */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 724 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 725 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 726 | stream = data_s->req.protop; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 727 | if(!stream) |
| 728 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 729 | |
| 730 | nread = MIN(stream->len, len); |
| 731 | memcpy(&stream->mem[stream->memlen], data, nread); |
| 732 | |
| 733 | stream->len -= nread; |
| 734 | stream->memlen += nread; |
| 735 | |
| 736 | data_s->state.drain++; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 737 | conn->proto.httpc.drain_total++; |
| 738 | |
| 739 | /* if we receive data for another handle, wake that up */ |
| 740 | if(conn->data != data_s) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 741 | Curl_expire(data_s, 0, EXPIRE_RUN_NOW); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 742 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 743 | H2BUGF(infof(data_s, "%zu data received for stream %u " |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 744 | "(%zu left in buffer %p, total %zu)\n", |
| 745 | nread, stream_id, |
| 746 | stream->len, stream->mem, |
| 747 | stream->memlen)); |
| 748 | |
| 749 | if(nread < len) { |
| 750 | stream->pausedata = data + nread; |
| 751 | stream->pauselen = len - nread; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 752 | H2BUGF(infof(data_s, "NGHTTP2_ERR_PAUSE - %zu bytes out of buffer" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 753 | ", stream %u\n", |
| 754 | len - nread, stream_id)); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 755 | data_s->easy_conn->proto.httpc.pause_stream_id = stream_id; |
| 756 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 757 | return NGHTTP2_ERR_PAUSE; |
| 758 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 759 | |
| 760 | /* pause execution of nghttp2 if we received data for another handle |
| 761 | in order to process them first. */ |
| 762 | if(conn->data != data_s) { |
| 763 | data_s->easy_conn->proto.httpc.pause_stream_id = stream_id; |
| 764 | |
| 765 | return NGHTTP2_ERR_PAUSE; |
| 766 | } |
| 767 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static int before_frame_send(nghttp2_session *session, |
| 772 | const nghttp2_frame *frame, |
| 773 | void *userp) |
| 774 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 775 | struct Curl_easy *data_s; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 776 | (void)userp; |
| 777 | |
| 778 | data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); |
| 779 | if(data_s) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 780 | H2BUGF(infof(data_s, "before_frame_send() was called\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
| 785 | static int on_frame_send(nghttp2_session *session, |
| 786 | const nghttp2_frame *frame, |
| 787 | void *userp) |
| 788 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 789 | struct Curl_easy *data_s; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 790 | (void)userp; |
| 791 | |
| 792 | data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); |
| 793 | if(data_s) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 794 | H2BUGF(infof(data_s, "on_frame_send() was called, length = %zd\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 795 | frame->hd.length)); |
| 796 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | static int on_frame_not_send(nghttp2_session *session, |
| 800 | const nghttp2_frame *frame, |
| 801 | int lib_error_code, void *userp) |
| 802 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 803 | struct Curl_easy *data_s; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 804 | (void)userp; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 805 | #if !defined(DEBUG_HTTP2) || defined(CURL_DISABLE_VERBOSE_STRINGS) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 806 | (void)lib_error_code; |
| 807 | #endif |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 808 | |
| 809 | data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); |
| 810 | if(data_s) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 811 | H2BUGF(infof(data_s, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 812 | "on_frame_not_send() was called, lib_error_code = %d\n", |
| 813 | lib_error_code)); |
| 814 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | static int on_stream_close(nghttp2_session *session, int32_t stream_id, |
| 818 | uint32_t error_code, void *userp) |
| 819 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 820 | struct Curl_easy *data_s; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 821 | struct HTTP *stream; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 822 | struct connectdata *conn = (struct connectdata *)userp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 823 | (void)session; |
| 824 | (void)stream_id; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 825 | |
| 826 | if(stream_id) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 827 | struct http_conn *httpc; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 828 | /* get the stream from the hash based on Stream ID, stream ID zero is for |
| 829 | connection-oriented stuff */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 830 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 831 | if(!data_s) { |
| 832 | /* We could get stream ID not in the hash. For example, if we |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 833 | decided to reject stream (e.g., PUSH_PROMISE). */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 834 | return 0; |
| 835 | } |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 836 | H2BUGF(infof(data_s, "on_stream_close(), %s (err %d), stream %u\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 837 | Curl_http2_strerror(error_code), error_code, stream_id)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 838 | stream = data_s->req.protop; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 839 | if(!stream) |
| 840 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 841 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 842 | stream->closed = TRUE; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 843 | data_s->state.drain++; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 844 | httpc = &conn->proto.httpc; |
| 845 | httpc->drain_total++; |
| 846 | httpc->error_code = error_code; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 847 | |
| 848 | /* remove the entry from the hash as the stream is now gone */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 849 | nghttp2_session_set_stream_user_data(session, stream_id, 0); |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 850 | H2BUGF(infof(data_s, "Removed stream %u hash!\n", stream_id)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 851 | } |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static int on_begin_headers(nghttp2_session *session, |
| 856 | const nghttp2_frame *frame, void *userp) |
| 857 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 858 | struct HTTP *stream; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 859 | struct Curl_easy *data_s = NULL; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 860 | (void)userp; |
| 861 | |
| 862 | data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); |
| 863 | if(!data_s) { |
| 864 | return 0; |
| 865 | } |
| 866 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 867 | H2BUGF(infof(data_s, "on_begin_headers() was called\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 868 | |
| 869 | if(frame->hd.type != NGHTTP2_HEADERS) { |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | stream = data_s->req.protop; |
| 874 | if(!stream || !stream->bodystarted) { |
| 875 | return 0; |
| 876 | } |
| 877 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 878 | if(!stream->trailer_recvbuf) { |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 879 | stream->trailer_recvbuf = Curl_add_buffer_init(); |
| 880 | if(!stream->trailer_recvbuf) { |
| 881 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 882 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 883 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* Decode HTTP status code. Returns -1 if no valid status code was |
| 888 | decoded. */ |
| 889 | static int decode_status_code(const uint8_t *value, size_t len) |
| 890 | { |
| 891 | int i; |
| 892 | int res; |
| 893 | |
| 894 | if(len != 3) { |
| 895 | return -1; |
| 896 | } |
| 897 | |
| 898 | res = 0; |
| 899 | |
| 900 | for(i = 0; i < 3; ++i) { |
| 901 | char c = value[i]; |
| 902 | |
| 903 | if(c < '0' || c > '9') { |
| 904 | return -1; |
| 905 | } |
| 906 | |
| 907 | res *= 10; |
| 908 | res += c - '0'; |
| 909 | } |
| 910 | |
| 911 | return res; |
| 912 | } |
| 913 | |
| 914 | /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */ |
| 915 | static int on_header(nghttp2_session *session, const nghttp2_frame *frame, |
| 916 | const uint8_t *name, size_t namelen, |
| 917 | const uint8_t *value, size_t valuelen, |
| 918 | uint8_t flags, |
| 919 | void *userp) |
| 920 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 921 | struct HTTP *stream; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 922 | struct Curl_easy *data_s; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 923 | int32_t stream_id = frame->hd.stream_id; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 924 | struct connectdata *conn = (struct connectdata *)userp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 925 | (void)flags; |
| 926 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 927 | DEBUGASSERT(stream_id); /* should never be a zero stream ID here */ |
| 928 | |
| 929 | /* get the stream from the hash based on Stream ID */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 930 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
| 931 | if(!data_s) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 932 | /* Receiving a Stream ID not in the hash should not happen, this is an |
| 933 | internal error more than anything else! */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 934 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 935 | |
| 936 | stream = data_s->req.protop; |
| 937 | if(!stream) { |
| 938 | failf(data_s, "Internal NULL stream! 5\n"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 939 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 940 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 941 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 942 | /* Store received PUSH_PROMISE headers to be used when the subsequent |
| 943 | PUSH_PROMISE callback comes */ |
| 944 | if(frame->hd.type == NGHTTP2_PUSH_PROMISE) { |
| 945 | char *h; |
| 946 | |
| 947 | if(!stream->push_headers) { |
| 948 | stream->push_headers_alloc = 10; |
| 949 | stream->push_headers = malloc(stream->push_headers_alloc * |
| 950 | sizeof(char *)); |
| 951 | stream->push_headers_used = 0; |
| 952 | } |
| 953 | else if(stream->push_headers_used == |
| 954 | stream->push_headers_alloc) { |
| 955 | char **headp; |
| 956 | stream->push_headers_alloc *= 2; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 957 | headp = Curl_saferealloc(stream->push_headers, |
| 958 | stream->push_headers_alloc * sizeof(char *)); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 959 | if(!headp) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 960 | stream->push_headers = NULL; |
| 961 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 962 | } |
| 963 | stream->push_headers = headp; |
| 964 | } |
| 965 | h = aprintf("%s:%s", name, value); |
| 966 | if(h) |
| 967 | stream->push_headers[stream->push_headers_used++] = h; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 968 | return 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | if(stream->bodystarted) { |
| 972 | /* This is trailer fields. */ |
Elliott Hughes | 0128fe4 | 2018-02-27 14:57:55 -0800 | [diff] [blame] | 973 | /* 4 is for ": " and "\r\n". */ |
| 974 | uint32_t n = (uint32_t)(namelen + valuelen + 4); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 975 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 976 | H2BUGF(infof(data_s, "h2 trailer: %.*s: %.*s\n", namelen, name, valuelen, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 977 | value)); |
| 978 | |
| 979 | Curl_add_buffer(stream->trailer_recvbuf, &n, sizeof(n)); |
| 980 | Curl_add_buffer(stream->trailer_recvbuf, name, namelen); |
| 981 | Curl_add_buffer(stream->trailer_recvbuf, ": ", 2); |
| 982 | Curl_add_buffer(stream->trailer_recvbuf, value, valuelen); |
| 983 | Curl_add_buffer(stream->trailer_recvbuf, "\r\n\0", 3); |
| 984 | |
| 985 | return 0; |
| 986 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 987 | |
| 988 | if(namelen == sizeof(":status") - 1 && |
| 989 | memcmp(":status", name, namelen) == 0) { |
| 990 | /* nghttp2 guarantees :status is received first and only once, and |
| 991 | value is 3 digits status code, and decode_status_code always |
| 992 | succeeds. */ |
| 993 | stream->status_code = decode_status_code(value, valuelen); |
| 994 | DEBUGASSERT(stream->status_code != -1); |
| 995 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 996 | Curl_add_buffer(stream->header_recvbuf, "HTTP/2 ", 7); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 997 | Curl_add_buffer(stream->header_recvbuf, value, valuelen); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 998 | /* the space character after the status code is mandatory */ |
| 999 | Curl_add_buffer(stream->header_recvbuf, " \r\n", 3); |
| 1000 | /* if we receive data for another handle, wake that up */ |
| 1001 | if(conn->data != data_s) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1002 | Curl_expire(data_s, 0, EXPIRE_RUN_NOW); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1003 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1004 | H2BUGF(infof(data_s, "h2 status: HTTP/2 %03d (easy %p)\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1005 | stream->status_code, data_s)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /* nghttp2 guarantees that namelen > 0, and :status was already |
| 1010 | received, and this is not pseudo-header field . */ |
| 1011 | /* convert to a HTTP1-style header */ |
| 1012 | Curl_add_buffer(stream->header_recvbuf, name, namelen); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1013 | Curl_add_buffer(stream->header_recvbuf, ": ", 2); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1014 | Curl_add_buffer(stream->header_recvbuf, value, valuelen); |
| 1015 | Curl_add_buffer(stream->header_recvbuf, "\r\n", 2); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1016 | /* if we receive data for another handle, wake that up */ |
| 1017 | if(conn->data != data_s) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1018 | Curl_expire(data_s, 0, EXPIRE_RUN_NOW); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1019 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1020 | H2BUGF(infof(data_s, "h2 header: %.*s: %.*s\n", namelen, name, valuelen, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1021 | value)); |
| 1022 | |
| 1023 | return 0; /* 0 is successful */ |
| 1024 | } |
| 1025 | |
| 1026 | static ssize_t data_source_read_callback(nghttp2_session *session, |
| 1027 | int32_t stream_id, |
| 1028 | uint8_t *buf, size_t length, |
| 1029 | uint32_t *data_flags, |
| 1030 | nghttp2_data_source *source, |
| 1031 | void *userp) |
| 1032 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1033 | struct Curl_easy *data_s; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1034 | struct HTTP *stream = NULL; |
| 1035 | size_t nread; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1036 | (void)source; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1037 | (void)userp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1038 | |
| 1039 | if(stream_id) { |
| 1040 | /* get the stream from the hash based on Stream ID, stream ID zero is for |
| 1041 | connection-oriented stuff */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1042 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
| 1043 | if(!data_s) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1044 | /* Receiving a Stream ID not in the hash should not happen, this is an |
| 1045 | internal error more than anything else! */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1046 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1047 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1048 | stream = data_s->req.protop; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1049 | if(!stream) |
| 1050 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1051 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1052 | else |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1053 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1054 | |
| 1055 | nread = MIN(stream->upload_len, length); |
| 1056 | if(nread > 0) { |
| 1057 | memcpy(buf, stream->upload_mem, nread); |
| 1058 | stream->upload_mem += nread; |
| 1059 | stream->upload_len -= nread; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1060 | if(data_s->state.infilesize != -1) |
| 1061 | stream->upload_left -= nread; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | if(stream->upload_left == 0) |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1065 | *data_flags = NGHTTP2_DATA_FLAG_EOF; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1066 | else if(nread == 0) |
| 1067 | return NGHTTP2_ERR_DEFERRED; |
| 1068 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1069 | H2BUGF(infof(data_s, "data_source_read_callback: " |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1070 | "returns %zu bytes stream %u\n", |
| 1071 | nread, stream_id)); |
| 1072 | |
| 1073 | return nread; |
| 1074 | } |
| 1075 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1076 | #ifdef NGHTTP2_HAS_ERROR_CALLBACK |
| 1077 | static int error_callback(nghttp2_session *session, |
| 1078 | const char *msg, |
| 1079 | size_t len, |
| 1080 | void *userp) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1081 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1082 | struct connectdata *conn = (struct connectdata *)userp; |
| 1083 | (void)session; |
| 1084 | infof(conn->data, "http2 error: %.*s\n", len, msg); |
| 1085 | return 0; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1086 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1087 | #endif |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1088 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1089 | static void populate_settings(struct connectdata *conn, |
| 1090 | struct http_conn *httpc) |
| 1091 | { |
| 1092 | nghttp2_settings_entry *iv = httpc->local_settings; |
| 1093 | |
| 1094 | iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; |
| 1095 | iv[0].value = 100; |
| 1096 | |
| 1097 | iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; |
| 1098 | iv[1].value = HTTP2_HUGE_WINDOW_SIZE; |
| 1099 | |
| 1100 | iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH; |
| 1101 | iv[2].value = conn->data->multi->push_cb != NULL; |
| 1102 | |
| 1103 | httpc->local_settings_num = 3; |
| 1104 | } |
| 1105 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1106 | void Curl_http2_done(struct connectdata *conn, bool premature) |
| 1107 | { |
| 1108 | struct Curl_easy *data = conn->data; |
| 1109 | struct HTTP *http = data->req.protop; |
| 1110 | struct http_conn *httpc = &conn->proto.httpc; |
| 1111 | |
| 1112 | if(http->header_recvbuf) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1113 | Curl_add_buffer_free(http->header_recvbuf); |
| 1114 | http->header_recvbuf = NULL; /* clear the pointer */ |
| 1115 | Curl_add_buffer_free(http->trailer_recvbuf); |
| 1116 | http->trailer_recvbuf = NULL; /* clear the pointer */ |
| 1117 | if(http->push_headers) { |
| 1118 | /* if they weren't used and then freed before */ |
| 1119 | for(; http->push_headers_used > 0; --http->push_headers_used) { |
| 1120 | free(http->push_headers[http->push_headers_used - 1]); |
| 1121 | } |
| 1122 | free(http->push_headers); |
| 1123 | http->push_headers = NULL; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if(premature) { |
| 1128 | /* RST_STREAM */ |
| 1129 | nghttp2_submit_rst_stream(httpc->h2, NGHTTP2_FLAG_NONE, http->stream_id, |
| 1130 | NGHTTP2_STREAM_CLOSED); |
| 1131 | if(http->stream_id == httpc->pause_stream_id) { |
| 1132 | infof(data, "stopped the pause stream!\n"); |
| 1133 | httpc->pause_stream_id = 0; |
| 1134 | } |
| 1135 | } |
| 1136 | if(http->stream_id) { |
| 1137 | nghttp2_session_set_stream_user_data(httpc->h2, http->stream_id, 0); |
| 1138 | http->stream_id = 0; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1142 | /* |
| 1143 | * Initialize nghttp2 for a Curl connection |
| 1144 | */ |
| 1145 | CURLcode Curl_http2_init(struct connectdata *conn) |
| 1146 | { |
| 1147 | if(!conn->proto.httpc.h2) { |
| 1148 | int rc; |
| 1149 | nghttp2_session_callbacks *callbacks; |
| 1150 | |
| 1151 | conn->proto.httpc.inbuf = malloc(H2_BUFSIZE); |
| 1152 | if(conn->proto.httpc.inbuf == NULL) |
| 1153 | return CURLE_OUT_OF_MEMORY; |
| 1154 | |
| 1155 | rc = nghttp2_session_callbacks_new(&callbacks); |
| 1156 | |
| 1157 | if(rc) { |
| 1158 | failf(conn->data, "Couldn't initialize nghttp2 callbacks!"); |
| 1159 | return CURLE_OUT_OF_MEMORY; /* most likely at least */ |
| 1160 | } |
| 1161 | |
| 1162 | /* nghttp2_send_callback */ |
| 1163 | nghttp2_session_callbacks_set_send_callback(callbacks, send_callback); |
| 1164 | /* nghttp2_on_frame_recv_callback */ |
| 1165 | nghttp2_session_callbacks_set_on_frame_recv_callback |
| 1166 | (callbacks, on_frame_recv); |
| 1167 | /* nghttp2_on_invalid_frame_recv_callback */ |
| 1168 | nghttp2_session_callbacks_set_on_invalid_frame_recv_callback |
| 1169 | (callbacks, on_invalid_frame_recv); |
| 1170 | /* nghttp2_on_data_chunk_recv_callback */ |
| 1171 | nghttp2_session_callbacks_set_on_data_chunk_recv_callback |
| 1172 | (callbacks, on_data_chunk_recv); |
| 1173 | /* nghttp2_before_frame_send_callback */ |
| 1174 | nghttp2_session_callbacks_set_before_frame_send_callback |
| 1175 | (callbacks, before_frame_send); |
| 1176 | /* nghttp2_on_frame_send_callback */ |
| 1177 | nghttp2_session_callbacks_set_on_frame_send_callback |
| 1178 | (callbacks, on_frame_send); |
| 1179 | /* nghttp2_on_frame_not_send_callback */ |
| 1180 | nghttp2_session_callbacks_set_on_frame_not_send_callback |
| 1181 | (callbacks, on_frame_not_send); |
| 1182 | /* nghttp2_on_stream_close_callback */ |
| 1183 | nghttp2_session_callbacks_set_on_stream_close_callback |
| 1184 | (callbacks, on_stream_close); |
| 1185 | /* nghttp2_on_begin_headers_callback */ |
| 1186 | nghttp2_session_callbacks_set_on_begin_headers_callback |
| 1187 | (callbacks, on_begin_headers); |
| 1188 | /* nghttp2_on_header_callback */ |
| 1189 | nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header); |
| 1190 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1191 | nghttp2_session_callbacks_set_error_callback(callbacks, error_callback); |
| 1192 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1193 | /* The nghttp2 session is not yet setup, do it */ |
| 1194 | rc = nghttp2_session_client_new(&conn->proto.httpc.h2, callbacks, conn); |
| 1195 | |
| 1196 | nghttp2_session_callbacks_del(callbacks); |
| 1197 | |
| 1198 | if(rc) { |
| 1199 | failf(conn->data, "Couldn't initialize nghttp2!"); |
| 1200 | return CURLE_OUT_OF_MEMORY; /* most likely at least */ |
| 1201 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return CURLE_OK; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1207 | * Append headers to ask for a HTTP1.1 to HTTP2 upgrade. |
| 1208 | */ |
| 1209 | CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req, |
| 1210 | struct connectdata *conn) |
| 1211 | { |
| 1212 | CURLcode result; |
| 1213 | ssize_t binlen; |
| 1214 | char *base64; |
| 1215 | size_t blen; |
| 1216 | struct SingleRequest *k = &conn->data->req; |
| 1217 | uint8_t *binsettings = conn->proto.httpc.binsettings; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1218 | struct http_conn *httpc = &conn->proto.httpc; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1219 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1220 | populate_settings(conn, httpc); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1221 | |
| 1222 | /* this returns number of bytes it wrote */ |
| 1223 | binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN, |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1224 | httpc->local_settings, |
| 1225 | httpc->local_settings_num); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1226 | if(!binlen) { |
| 1227 | failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload"); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1228 | Curl_add_buffer_free(req); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1229 | return CURLE_FAILED_INIT; |
| 1230 | } |
| 1231 | conn->proto.httpc.binlen = binlen; |
| 1232 | |
| 1233 | result = Curl_base64url_encode(conn->data, (const char *)binsettings, binlen, |
| 1234 | &base64, &blen); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1235 | if(result) { |
| 1236 | Curl_add_buffer_free(req); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1237 | return result; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1238 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1239 | |
| 1240 | result = Curl_add_bufferf(req, |
| 1241 | "Connection: Upgrade, HTTP2-Settings\r\n" |
| 1242 | "Upgrade: %s\r\n" |
| 1243 | "HTTP2-Settings: %s\r\n", |
| 1244 | NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64); |
| 1245 | free(base64); |
| 1246 | |
| 1247 | k->upgr101 = UPGR101_REQUESTED; |
| 1248 | |
| 1249 | return result; |
| 1250 | } |
| 1251 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1252 | /* |
| 1253 | * Returns nonzero if current HTTP/2 session should be closed. |
| 1254 | */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1255 | static int should_close_session(struct http_conn *httpc) |
| 1256 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1257 | return httpc->drain_total == 0 && !nghttp2_session_want_read(httpc->h2) && |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1258 | !nghttp2_session_want_write(httpc->h2); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1261 | /* |
| 1262 | * h2_process_pending_input() processes pending input left in |
| 1263 | * httpc->inbuf. Then, call h2_session_send() to send pending data. |
| 1264 | * This function returns 0 if it succeeds, or -1 and error code will |
| 1265 | * be assigned to *err. |
| 1266 | */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1267 | static int h2_process_pending_input(struct connectdata *conn, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1268 | struct http_conn *httpc, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1269 | CURLcode *err) |
| 1270 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1271 | ssize_t nread; |
| 1272 | char *inbuf; |
| 1273 | ssize_t rv; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1274 | struct Curl_easy *data = conn->data; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1275 | |
| 1276 | nread = httpc->inbuflen - httpc->nread_inbuf; |
| 1277 | inbuf = httpc->inbuf + httpc->nread_inbuf; |
| 1278 | |
| 1279 | rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread); |
| 1280 | if(rv < 0) { |
| 1281 | failf(data, |
| 1282 | "h2_process_pending_input: nghttp2_session_mem_recv() returned " |
| 1283 | "%d:%s\n", rv, nghttp2_strerror((int)rv)); |
| 1284 | *err = CURLE_RECV_ERROR; |
| 1285 | return -1; |
| 1286 | } |
| 1287 | |
| 1288 | if(nread == rv) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1289 | H2BUGF(infof(data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1290 | "h2_process_pending_input: All data in connection buffer " |
| 1291 | "processed\n")); |
| 1292 | httpc->inbuflen = 0; |
| 1293 | httpc->nread_inbuf = 0; |
| 1294 | } |
| 1295 | else { |
| 1296 | httpc->nread_inbuf += rv; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1297 | H2BUGF(infof(data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1298 | "h2_process_pending_input: %zu bytes left in connection " |
| 1299 | "buffer\n", |
| 1300 | httpc->inbuflen - httpc->nread_inbuf)); |
| 1301 | } |
| 1302 | |
| 1303 | rv = h2_session_send(data, httpc->h2); |
| 1304 | if(rv != 0) { |
| 1305 | *err = CURLE_SEND_ERROR; |
| 1306 | return -1; |
| 1307 | } |
| 1308 | |
| 1309 | if(should_close_session(httpc)) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1310 | H2BUGF(infof(data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1311 | "h2_process_pending_input: nothing to do in this session\n")); |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1312 | if(httpc->error_code) |
| 1313 | *err = CURLE_HTTP2; |
| 1314 | else { |
| 1315 | /* not an error per se, but should still close the connection */ |
| 1316 | connclose(conn, "GOAWAY received"); |
| 1317 | *err = CURLE_OK; |
| 1318 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1319 | return -1; |
| 1320 | } |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1325 | /* |
| 1326 | * Called from transfer.c:done_sending when we stop uploading. |
| 1327 | */ |
| 1328 | CURLcode Curl_http2_done_sending(struct connectdata *conn) |
| 1329 | { |
| 1330 | CURLcode result = CURLE_OK; |
| 1331 | |
| 1332 | if((conn->handler == &Curl_handler_http2_ssl) || |
| 1333 | (conn->handler == &Curl_handler_http2)) { |
| 1334 | /* make sure this is only attempted for HTTP/2 transfers */ |
| 1335 | |
| 1336 | struct HTTP *stream = conn->data->req.protop; |
| 1337 | |
| 1338 | if(stream->upload_left) { |
| 1339 | /* If the stream still thinks there's data left to upload. */ |
| 1340 | struct http_conn *httpc = &conn->proto.httpc; |
| 1341 | nghttp2_session *h2 = httpc->h2; |
| 1342 | |
| 1343 | stream->upload_left = 0; /* DONE! */ |
| 1344 | |
| 1345 | /* resume sending here to trigger the callback to get called again so |
| 1346 | that it can signal EOF to nghttp2 */ |
| 1347 | (void)nghttp2_session_resume_data(h2, stream->stream_id); |
| 1348 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1349 | (void)h2_process_pending_input(conn, httpc, &result); |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | return result; |
| 1353 | } |
| 1354 | |
| 1355 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1356 | static ssize_t http2_handle_stream_close(struct connectdata *conn, |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1357 | struct Curl_easy *data, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1358 | struct HTTP *stream, CURLcode *err) |
| 1359 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1360 | char *trailer_pos, *trailer_end; |
| 1361 | CURLcode result; |
| 1362 | struct http_conn *httpc = &conn->proto.httpc; |
| 1363 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1364 | if(httpc->pause_stream_id == stream->stream_id) { |
| 1365 | httpc->pause_stream_id = 0; |
| 1366 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1367 | |
| 1368 | DEBUGASSERT(httpc->drain_total >= data->state.drain); |
| 1369 | httpc->drain_total -= data->state.drain; |
| 1370 | data->state.drain = 0; |
| 1371 | |
| 1372 | if(httpc->pause_stream_id == 0) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1373 | if(h2_process_pending_input(conn, httpc, err) != 0) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1374 | return -1; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | DEBUGASSERT(data->state.drain == 0); |
| 1379 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1380 | /* Reset to FALSE to prevent infinite loop in readwrite_data function. */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1381 | stream->closed = FALSE; |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 1382 | if(httpc->error_code == NGHTTP2_REFUSED_STREAM) { |
| 1383 | H2BUGF(infof(data, "REFUSED_STREAM (%d), try again on a new connection!\n", |
| 1384 | stream->stream_id)); |
| 1385 | connclose(conn, "REFUSED_STREAM"); /* don't use this anymore */ |
| 1386 | data->state.refused_stream = TRUE; |
| 1387 | *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */ |
| 1388 | return -1; |
| 1389 | } |
| 1390 | else if(httpc->error_code != NGHTTP2_NO_ERROR) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1391 | failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %d)", |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1392 | stream->stream_id, Curl_http2_strerror(httpc->error_code), |
| 1393 | httpc->error_code); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1394 | *err = CURLE_HTTP2_STREAM; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1395 | return -1; |
| 1396 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1397 | |
| 1398 | if(!stream->bodystarted) { |
| 1399 | failf(data, "HTTP/2 stream %u was closed cleanly, but before getting " |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 1400 | " all response header fields, treated as error", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1401 | stream->stream_id); |
| 1402 | *err = CURLE_HTTP2_STREAM; |
| 1403 | return -1; |
| 1404 | } |
| 1405 | |
| 1406 | if(stream->trailer_recvbuf && stream->trailer_recvbuf->buffer) { |
| 1407 | trailer_pos = stream->trailer_recvbuf->buffer; |
| 1408 | trailer_end = trailer_pos + stream->trailer_recvbuf->size_used; |
| 1409 | |
| 1410 | for(; trailer_pos < trailer_end;) { |
| 1411 | uint32_t n; |
| 1412 | memcpy(&n, trailer_pos, sizeof(n)); |
| 1413 | trailer_pos += sizeof(n); |
| 1414 | |
| 1415 | result = Curl_client_write(conn, CLIENTWRITE_HEADER, trailer_pos, n); |
| 1416 | if(result) { |
| 1417 | *err = result; |
| 1418 | return -1; |
| 1419 | } |
| 1420 | |
| 1421 | trailer_pos += n + 1; |
| 1422 | } |
| 1423 | } |
| 1424 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1425 | stream->close_handled = TRUE; |
| 1426 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1427 | H2BUGF(infof(data, "http2_recv returns 0, http2_handle_stream_close\n")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1432 | * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight |
| 1433 | * and dependency to the peer. It also stores the updated values in the state |
| 1434 | * struct. |
| 1435 | */ |
| 1436 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1437 | static void h2_pri_spec(struct Curl_easy *data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1438 | nghttp2_priority_spec *pri_spec) |
| 1439 | { |
| 1440 | struct HTTP *depstream = (data->set.stream_depends_on? |
| 1441 | data->set.stream_depends_on->req.protop:NULL); |
| 1442 | int32_t depstream_id = depstream? depstream->stream_id:0; |
| 1443 | nghttp2_priority_spec_init(pri_spec, depstream_id, data->set.stream_weight, |
| 1444 | data->set.stream_depends_e); |
| 1445 | data->state.stream_weight = data->set.stream_weight; |
| 1446 | data->state.stream_depends_e = data->set.stream_depends_e; |
| 1447 | data->state.stream_depends_on = data->set.stream_depends_on; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * h2_session_send() checks if there's been an update in the priority / |
| 1452 | * dependency settings and if so it submits a PRIORITY frame with the updated |
| 1453 | * info. |
| 1454 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1455 | static int h2_session_send(struct Curl_easy *data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1456 | nghttp2_session *h2) |
| 1457 | { |
| 1458 | struct HTTP *stream = data->req.protop; |
| 1459 | if((data->set.stream_weight != data->state.stream_weight) || |
| 1460 | (data->set.stream_depends_e != data->state.stream_depends_e) || |
| 1461 | (data->set.stream_depends_on != data->state.stream_depends_on) ) { |
| 1462 | /* send new weight and/or dependency */ |
| 1463 | nghttp2_priority_spec pri_spec; |
| 1464 | int rv; |
| 1465 | |
| 1466 | h2_pri_spec(data, &pri_spec); |
| 1467 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1468 | H2BUGF(infof(data, "Queuing PRIORITY on stream %u (easy %p)\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1469 | stream->stream_id, data)); |
| 1470 | rv = nghttp2_submit_priority(h2, NGHTTP2_FLAG_NONE, stream->stream_id, |
| 1471 | &pri_spec); |
| 1472 | if(rv) |
| 1473 | return rv; |
| 1474 | } |
| 1475 | |
| 1476 | return nghttp2_session_send(h2); |
| 1477 | } |
| 1478 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1479 | static ssize_t http2_recv(struct connectdata *conn, int sockindex, |
| 1480 | char *mem, size_t len, CURLcode *err) |
| 1481 | { |
| 1482 | CURLcode result = CURLE_OK; |
| 1483 | ssize_t rv; |
| 1484 | ssize_t nread; |
| 1485 | struct http_conn *httpc = &conn->proto.httpc; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1486 | struct Curl_easy *data = conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1487 | struct HTTP *stream = data->req.protop; |
| 1488 | |
| 1489 | (void)sockindex; /* we always do HTTP2 on sockindex 0 */ |
| 1490 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1491 | if(should_close_session(httpc)) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1492 | H2BUGF(infof(data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1493 | "http2_recv: nothing to do in this session\n")); |
| 1494 | *err = CURLE_HTTP2; |
| 1495 | return -1; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | /* Nullify here because we call nghttp2_session_send() and they |
| 1499 | might refer to the old buffer. */ |
| 1500 | stream->upload_mem = NULL; |
| 1501 | stream->upload_len = 0; |
| 1502 | |
| 1503 | /* |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1504 | * At this point 'stream' is just in the Curl_easy the connection |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1505 | * identifies as its owner at this time. |
| 1506 | */ |
| 1507 | |
| 1508 | if(stream->bodystarted && |
| 1509 | stream->nread_header_recvbuf < stream->header_recvbuf->size_used) { |
| 1510 | /* If there is body data pending for this stream to return, do that */ |
| 1511 | size_t left = |
| 1512 | stream->header_recvbuf->size_used - stream->nread_header_recvbuf; |
| 1513 | size_t ncopy = MIN(len, left); |
| 1514 | memcpy(mem, stream->header_recvbuf->buffer + stream->nread_header_recvbuf, |
| 1515 | ncopy); |
| 1516 | stream->nread_header_recvbuf += ncopy; |
| 1517 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1518 | H2BUGF(infof(data, "http2_recv: Got %d bytes from header_recvbuf\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1519 | (int)ncopy)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1520 | return ncopy; |
| 1521 | } |
| 1522 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1523 | H2BUGF(infof(data, "http2_recv: easy %p (stream %u)\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1524 | data, stream->stream_id)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1525 | |
| 1526 | if((data->state.drain) && stream->memlen) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1527 | H2BUGF(infof(data, "http2_recv: DRAIN %zu bytes stream %u!! (%p => %p)\n", |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1528 | stream->memlen, stream->stream_id, |
| 1529 | stream->mem, mem)); |
| 1530 | if(mem != stream->mem) { |
| 1531 | /* if we didn't get the same buffer this time, we must move the data to |
| 1532 | the beginning */ |
| 1533 | memmove(mem, stream->mem, stream->memlen); |
| 1534 | stream->len = len - stream->memlen; |
| 1535 | stream->mem = mem; |
| 1536 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1537 | if(httpc->pause_stream_id == stream->stream_id && !stream->pausedata) { |
| 1538 | /* We have paused nghttp2, but we have no pause data (see |
| 1539 | on_data_chunk_recv). */ |
| 1540 | httpc->pause_stream_id = 0; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1541 | if(h2_process_pending_input(conn, httpc, &result) != 0) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1542 | *err = result; |
| 1543 | return -1; |
| 1544 | } |
| 1545 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1546 | } |
| 1547 | else if(stream->pausedata) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1548 | DEBUGASSERT(httpc->pause_stream_id == stream->stream_id); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1549 | nread = MIN(len, stream->pauselen); |
| 1550 | memcpy(mem, stream->pausedata, nread); |
| 1551 | |
| 1552 | stream->pausedata += nread; |
| 1553 | stream->pauselen -= nread; |
| 1554 | |
| 1555 | infof(data, "%zu data bytes written\n", nread); |
| 1556 | if(stream->pauselen == 0) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1557 | H2BUGF(infof(data, "Unpaused by stream %u\n", stream->stream_id)); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1558 | DEBUGASSERT(httpc->pause_stream_id == stream->stream_id); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1559 | httpc->pause_stream_id = 0; |
| 1560 | |
| 1561 | stream->pausedata = NULL; |
| 1562 | stream->pauselen = 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1563 | |
| 1564 | /* When NGHTTP2_ERR_PAUSE is returned from |
| 1565 | data_source_read_callback, we might not process DATA frame |
| 1566 | fully. Calling nghttp2_session_mem_recv() again will |
| 1567 | continue to process DATA frame, but if there is no incoming |
| 1568 | frames, then we have to call it again with 0-length data. |
| 1569 | Without this, on_stream_close callback will not be called, |
| 1570 | and stream could be hanged. */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1571 | if(h2_process_pending_input(conn, httpc, &result) != 0) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1572 | *err = result; |
| 1573 | return -1; |
| 1574 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1575 | } |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1576 | H2BUGF(infof(data, "http2_recv: returns unpaused %zd bytes on stream %u\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1577 | nread, stream->stream_id)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1578 | return nread; |
| 1579 | } |
| 1580 | else if(httpc->pause_stream_id) { |
| 1581 | /* If a stream paused nghttp2_session_mem_recv previously, and has |
| 1582 | not processed all data, it still refers to the buffer in |
| 1583 | nghttp2_session. If we call nghttp2_session_mem_recv(), we may |
| 1584 | overwrite that buffer. To avoid that situation, just return |
| 1585 | here with CURLE_AGAIN. This could be busy loop since data in |
| 1586 | socket is not read. But it seems that usually streams are |
| 1587 | notified with its drain property, and socket is read again |
| 1588 | quickly. */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1589 | H2BUGF(infof(data, "stream %x is paused, pause id: %x\n", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1590 | stream->stream_id, httpc->pause_stream_id)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1591 | *err = CURLE_AGAIN; |
| 1592 | return -1; |
| 1593 | } |
| 1594 | else { |
| 1595 | char *inbuf; |
| 1596 | /* remember where to store incoming data for this stream and how big the |
| 1597 | buffer is */ |
| 1598 | stream->mem = mem; |
| 1599 | stream->len = len; |
| 1600 | stream->memlen = 0; |
| 1601 | |
| 1602 | if(httpc->inbuflen == 0) { |
| 1603 | nread = ((Curl_recv *)httpc->recv_underlying)( |
| 1604 | conn, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result); |
| 1605 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1606 | if(nread == -1) { |
| 1607 | if(result != CURLE_AGAIN) |
| 1608 | failf(data, "Failed receiving HTTP2 data"); |
| 1609 | else if(stream->closed) |
| 1610 | /* received when the stream was already closed! */ |
| 1611 | return http2_handle_stream_close(conn, data, stream, err); |
| 1612 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1613 | *err = result; |
| 1614 | return -1; |
| 1615 | } |
| 1616 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1617 | if(nread == 0) { |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 1618 | H2BUGF(infof(data, "end of stream\n")); |
| 1619 | *err = CURLE_OK; |
| 1620 | return 0; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1623 | H2BUGF(infof(data, "nread=%zd\n", nread)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1624 | |
| 1625 | httpc->inbuflen = nread; |
| 1626 | inbuf = httpc->inbuf; |
| 1627 | } |
| 1628 | else { |
| 1629 | nread = httpc->inbuflen - httpc->nread_inbuf; |
| 1630 | inbuf = httpc->inbuf + httpc->nread_inbuf; |
| 1631 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1632 | H2BUGF(infof(data, "Use data left in connection buffer, nread=%zd\n", |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1633 | nread)); |
| 1634 | } |
| 1635 | rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread); |
| 1636 | |
| 1637 | if(nghttp2_is_fatal((int)rv)) { |
| 1638 | failf(data, "nghttp2_session_mem_recv() returned %d:%s\n", |
| 1639 | rv, nghttp2_strerror((int)rv)); |
| 1640 | *err = CURLE_RECV_ERROR; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1641 | return -1; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1642 | } |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1643 | H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", rv)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1644 | if(nread == rv) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1645 | H2BUGF(infof(data, "All data in connection buffer processed\n")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1646 | httpc->inbuflen = 0; |
| 1647 | httpc->nread_inbuf = 0; |
| 1648 | } |
| 1649 | else { |
| 1650 | httpc->nread_inbuf += rv; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1651 | H2BUGF(infof(data, "%zu bytes left in connection buffer\n", |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1652 | httpc->inbuflen - httpc->nread_inbuf)); |
| 1653 | } |
| 1654 | /* Always send pending frames in nghttp2 session, because |
| 1655 | nghttp2_session_mem_recv() may queue new frame */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1656 | rv = h2_session_send(data, httpc->h2); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1657 | if(rv != 0) { |
| 1658 | *err = CURLE_SEND_ERROR; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1659 | return -1; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1660 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1661 | |
| 1662 | if(should_close_session(httpc)) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1663 | H2BUGF(infof(data, "http2_recv: nothing to do in this session\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1664 | *err = CURLE_HTTP2; |
| 1665 | return -1; |
| 1666 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1667 | } |
| 1668 | if(stream->memlen) { |
| 1669 | ssize_t retlen = stream->memlen; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1670 | H2BUGF(infof(data, "http2_recv: returns %zd for stream %u\n", |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1671 | retlen, stream->stream_id)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1672 | stream->memlen = 0; |
| 1673 | |
| 1674 | if(httpc->pause_stream_id == stream->stream_id) { |
| 1675 | /* data for this stream is returned now, but this stream caused a pause |
| 1676 | already so we need it called again asap */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1677 | H2BUGF(infof(data, "Data returned for PAUSED stream %u\n", |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1678 | stream->stream_id)); |
| 1679 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1680 | else if(!stream->closed) { |
| 1681 | DEBUGASSERT(httpc->drain_total >= data->state.drain); |
| 1682 | httpc->drain_total -= data->state.drain; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1683 | data->state.drain = 0; /* this stream is hereby drained */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1684 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1685 | |
| 1686 | return retlen; |
| 1687 | } |
| 1688 | /* If stream is closed, return 0 to signal the http routine to close |
| 1689 | the connection */ |
| 1690 | if(stream->closed) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1691 | return http2_handle_stream_close(conn, data, stream, err); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1692 | } |
| 1693 | *err = CURLE_AGAIN; |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1694 | H2BUGF(infof(data, "http2_recv returns AGAIN for stream %u\n", |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1695 | stream->stream_id)); |
| 1696 | return -1; |
| 1697 | } |
| 1698 | |
| 1699 | /* Index where :authority header field will appear in request header |
| 1700 | field list. */ |
| 1701 | #define AUTHORITY_DST_IDX 3 |
| 1702 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1703 | #define HEADER_OVERFLOW(x) \ |
| 1704 | (x.namelen > (uint16_t)-1 || x.valuelen > (uint16_t)-1 - x.namelen) |
| 1705 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1706 | /* |
| 1707 | * Check header memory for the token "trailers". |
| 1708 | * Parse the tokens as separated by comma and surrounded by whitespace. |
| 1709 | * Returns TRUE if found or FALSE if not. |
| 1710 | */ |
| 1711 | static bool contains_trailers(const char *p, size_t len) |
| 1712 | { |
| 1713 | const char *end = p + len; |
| 1714 | for(;;) { |
| 1715 | for(; p != end && (*p == ' ' || *p == '\t'); ++p) |
| 1716 | ; |
| 1717 | if(p == end || (size_t)(end - p) < sizeof("trailers") - 1) |
| 1718 | return FALSE; |
| 1719 | if(strncasecompare("trailers", p, sizeof("trailers") - 1)) { |
| 1720 | p += sizeof("trailers") - 1; |
| 1721 | for(; p != end && (*p == ' ' || *p == '\t'); ++p) |
| 1722 | ; |
| 1723 | if(p == end || *p == ',') |
| 1724 | return TRUE; |
| 1725 | } |
| 1726 | /* skip to next token */ |
| 1727 | for(; p != end && *p != ','; ++p) |
| 1728 | ; |
| 1729 | if(p == end) |
| 1730 | return FALSE; |
| 1731 | ++p; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | typedef enum { |
| 1736 | /* Send header to server */ |
| 1737 | HEADERINST_FORWARD, |
| 1738 | /* Don't send header to server */ |
| 1739 | HEADERINST_IGNORE, |
| 1740 | /* Discard header, and replace it with "te: trailers" */ |
| 1741 | HEADERINST_TE_TRAILERS |
| 1742 | } header_instruction; |
| 1743 | |
| 1744 | /* Decides how to treat given header field. */ |
| 1745 | static header_instruction inspect_header(const char *name, size_t namelen, |
| 1746 | const char *value, size_t valuelen) { |
| 1747 | switch(namelen) { |
| 1748 | case 2: |
| 1749 | if(!strncasecompare("te", name, namelen)) |
| 1750 | return HEADERINST_FORWARD; |
| 1751 | |
| 1752 | return contains_trailers(value, valuelen) ? |
| 1753 | HEADERINST_TE_TRAILERS : HEADERINST_IGNORE; |
| 1754 | case 7: |
| 1755 | return strncasecompare("upgrade", name, namelen) ? |
| 1756 | HEADERINST_IGNORE : HEADERINST_FORWARD; |
| 1757 | case 10: |
| 1758 | return (strncasecompare("connection", name, namelen) || |
| 1759 | strncasecompare("keep-alive", name, namelen)) ? |
| 1760 | HEADERINST_IGNORE : HEADERINST_FORWARD; |
| 1761 | case 16: |
| 1762 | return strncasecompare("proxy-connection", name, namelen) ? |
| 1763 | HEADERINST_IGNORE : HEADERINST_FORWARD; |
| 1764 | case 17: |
| 1765 | return strncasecompare("transfer-encoding", name, namelen) ? |
| 1766 | HEADERINST_IGNORE : HEADERINST_FORWARD; |
| 1767 | default: |
| 1768 | return HEADERINST_FORWARD; |
| 1769 | } |
| 1770 | } |
| 1771 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1772 | static ssize_t http2_send(struct connectdata *conn, int sockindex, |
| 1773 | const void *mem, size_t len, CURLcode *err) |
| 1774 | { |
| 1775 | /* |
| 1776 | * BIG TODO: Currently, we send request in this function, but this |
| 1777 | * function is also used to send request body. It would be nice to |
| 1778 | * add dedicated function for request. |
| 1779 | */ |
| 1780 | int rv; |
| 1781 | struct http_conn *httpc = &conn->proto.httpc; |
| 1782 | struct HTTP *stream = conn->data->req.protop; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1783 | nghttp2_nv *nva = NULL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1784 | size_t nheader; |
| 1785 | size_t i; |
| 1786 | size_t authority_idx; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1787 | char *hdbuf = (char *)mem; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1788 | char *end, *line_end; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1789 | nghttp2_data_provider data_prd; |
| 1790 | int32_t stream_id; |
| 1791 | nghttp2_session *h2 = httpc->h2; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1792 | nghttp2_priority_spec pri_spec; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1793 | |
| 1794 | (void)sockindex; |
| 1795 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1796 | H2BUGF(infof(conn->data, "http2_send len=%zu\n", len)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1797 | |
| 1798 | if(stream->stream_id != -1) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1799 | if(stream->close_handled) { |
| 1800 | infof(conn->data, "stream %d closed\n", stream->stream_id); |
| 1801 | *err = CURLE_HTTP2_STREAM; |
| 1802 | return -1; |
| 1803 | } |
| 1804 | else if(stream->closed) { |
| 1805 | return http2_handle_stream_close(conn, conn->data, stream, err); |
| 1806 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1807 | /* If stream_id != -1, we have dispatched request HEADERS, and now |
| 1808 | are going to send or sending request body in DATA frame */ |
| 1809 | stream->upload_mem = mem; |
| 1810 | stream->upload_len = len; |
| 1811 | nghttp2_session_resume_data(h2, stream->stream_id); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1812 | rv = h2_session_send(conn->data, h2); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1813 | if(nghttp2_is_fatal(rv)) { |
| 1814 | *err = CURLE_SEND_ERROR; |
| 1815 | return -1; |
| 1816 | } |
| 1817 | len -= stream->upload_len; |
| 1818 | |
| 1819 | /* Nullify here because we call nghttp2_session_send() and they |
| 1820 | might refer to the old buffer. */ |
| 1821 | stream->upload_mem = NULL; |
| 1822 | stream->upload_len = 0; |
| 1823 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1824 | if(should_close_session(httpc)) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1825 | H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1826 | *err = CURLE_HTTP2; |
| 1827 | return -1; |
| 1828 | } |
| 1829 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1830 | if(stream->upload_left) { |
| 1831 | /* we are sure that we have more data to send here. Calling the |
| 1832 | following API will make nghttp2_session_want_write() return |
| 1833 | nonzero if remote window allows it, which then libcurl checks |
| 1834 | socket is writable or not. See http2_perform_getsock(). */ |
| 1835 | nghttp2_session_resume_data(h2, stream->stream_id); |
| 1836 | } |
| 1837 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1838 | H2BUGF(infof(conn->data, "http2_send returns %zu for stream %u\n", len, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1839 | stream->stream_id)); |
| 1840 | return len; |
| 1841 | } |
| 1842 | |
| 1843 | /* Calculate number of headers contained in [mem, mem + len) */ |
| 1844 | /* Here, we assume the curl http code generate *correct* HTTP header |
| 1845 | field block */ |
| 1846 | nheader = 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1847 | for(i = 1; i < len; ++i) { |
| 1848 | if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1849 | ++nheader; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1850 | ++i; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1851 | } |
| 1852 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1853 | if(nheader < 2) |
| 1854 | goto fail; |
| 1855 | |
| 1856 | /* We counted additional 2 \r\n in the first and last line. We need 3 |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1857 | new headers: :method, :path and :scheme. Therefore we need one |
| 1858 | more space. */ |
| 1859 | nheader += 1; |
| 1860 | nva = malloc(sizeof(nghttp2_nv) * nheader); |
| 1861 | if(nva == NULL) { |
| 1862 | *err = CURLE_OUT_OF_MEMORY; |
| 1863 | return -1; |
| 1864 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1865 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 1866 | /* Extract :method, :path from request line |
| 1867 | We do line endings with CRLF so checking for CR is enough */ |
| 1868 | line_end = memchr(hdbuf, '\r', len); |
| 1869 | if(!line_end) |
| 1870 | goto fail; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1871 | |
| 1872 | /* Method does not contain spaces */ |
| 1873 | end = memchr(hdbuf, ' ', line_end - hdbuf); |
| 1874 | if(!end || end == hdbuf) |
| 1875 | goto fail; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1876 | nva[0].name = (unsigned char *)":method"; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1877 | nva[0].namelen = strlen((char *)nva[0].name); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1878 | nva[0].value = (unsigned char *)hdbuf; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1879 | nva[0].valuelen = (size_t)(end - hdbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1880 | nva[0].flags = NGHTTP2_NV_FLAG_NONE; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1881 | if(HEADER_OVERFLOW(nva[0])) { |
| 1882 | failf(conn->data, "Failed sending HTTP request: Header overflow"); |
| 1883 | goto fail; |
| 1884 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1885 | |
| 1886 | hdbuf = end + 1; |
| 1887 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1888 | /* Path may contain spaces so scan backwards */ |
| 1889 | end = NULL; |
| 1890 | for(i = (size_t)(line_end - hdbuf); i; --i) { |
| 1891 | if(hdbuf[i - 1] == ' ') { |
| 1892 | end = &hdbuf[i - 1]; |
| 1893 | break; |
| 1894 | } |
| 1895 | } |
| 1896 | if(!end || end == hdbuf) |
| 1897 | goto fail; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1898 | nva[1].name = (unsigned char *)":path"; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1899 | nva[1].namelen = strlen((char *)nva[1].name); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1900 | nva[1].value = (unsigned char *)hdbuf; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1901 | nva[1].valuelen = (size_t)(end - hdbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1902 | nva[1].flags = NGHTTP2_NV_FLAG_NONE; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1903 | if(HEADER_OVERFLOW(nva[1])) { |
| 1904 | failf(conn->data, "Failed sending HTTP request: Header overflow"); |
| 1905 | goto fail; |
| 1906 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1907 | |
| 1908 | nva[2].name = (unsigned char *)":scheme"; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1909 | nva[2].namelen = strlen((char *)nva[2].name); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1910 | if(conn->handler->flags & PROTOPT_SSL) |
| 1911 | nva[2].value = (unsigned char *)"https"; |
| 1912 | else |
| 1913 | nva[2].value = (unsigned char *)"http"; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1914 | nva[2].valuelen = strlen((char *)nva[2].value); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1915 | nva[2].flags = NGHTTP2_NV_FLAG_NONE; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1916 | if(HEADER_OVERFLOW(nva[2])) { |
| 1917 | failf(conn->data, "Failed sending HTTP request: Header overflow"); |
| 1918 | goto fail; |
| 1919 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1920 | |
| 1921 | authority_idx = 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1922 | i = 3; |
| 1923 | while(i < nheader) { |
| 1924 | size_t hlen; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1925 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1926 | hdbuf = line_end + 2; |
| 1927 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 1928 | /* check for next CR, but only within the piece of data left in the given |
| 1929 | buffer */ |
| 1930 | line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem)); |
| 1931 | if(!line_end || (line_end == hdbuf)) |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1932 | goto fail; |
| 1933 | |
| 1934 | /* header continuation lines are not supported */ |
| 1935 | if(*hdbuf == ' ' || *hdbuf == '\t') |
| 1936 | goto fail; |
| 1937 | |
| 1938 | for(end = hdbuf; end < line_end && *end != ':'; ++end) |
| 1939 | ; |
| 1940 | if(end == hdbuf || end == line_end) |
| 1941 | goto fail; |
| 1942 | hlen = end - hdbuf; |
| 1943 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1944 | if(hlen == 4 && strncasecompare("host", hdbuf, 4)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1945 | authority_idx = i; |
| 1946 | nva[i].name = (unsigned char *)":authority"; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1947 | nva[i].namelen = strlen((char *)nva[i].name); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1948 | } |
| 1949 | else { |
| 1950 | nva[i].name = (unsigned char *)hdbuf; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1951 | nva[i].namelen = (size_t)(end - hdbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1952 | } |
| 1953 | hdbuf = end + 1; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1954 | while(*hdbuf == ' ' || *hdbuf == '\t') |
| 1955 | ++hdbuf; |
| 1956 | end = line_end; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1957 | |
| 1958 | switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf, |
| 1959 | end - hdbuf)) { |
| 1960 | case HEADERINST_IGNORE: |
| 1961 | /* skip header fields prohibited by HTTP/2 specification. */ |
| 1962 | --nheader; |
| 1963 | continue; |
| 1964 | case HEADERINST_TE_TRAILERS: |
| 1965 | nva[i].value = (uint8_t*)"trailers"; |
| 1966 | nva[i].valuelen = sizeof("trailers") - 1; |
| 1967 | break; |
| 1968 | default: |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1969 | nva[i].value = (unsigned char *)hdbuf; |
| 1970 | nva[i].valuelen = (size_t)(end - hdbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1971 | } |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1972 | |
| 1973 | nva[i].flags = NGHTTP2_NV_FLAG_NONE; |
| 1974 | if(HEADER_OVERFLOW(nva[i])) { |
| 1975 | failf(conn->data, "Failed sending HTTP request: Header overflow"); |
| 1976 | goto fail; |
| 1977 | } |
| 1978 | ++i; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /* :authority must come before non-pseudo header fields */ |
| 1982 | if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) { |
| 1983 | nghttp2_nv authority = nva[authority_idx]; |
| 1984 | for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) { |
| 1985 | nva[i] = nva[i - 1]; |
| 1986 | } |
| 1987 | nva[i] = authority; |
| 1988 | } |
| 1989 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1990 | /* Warn stream may be rejected if cumulative length of headers is too large. |
| 1991 | It appears nghttp2 will not send a header frame larger than 64KB. */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1992 | #define MAX_ACC 60000 /* <64KB to account for some overhead */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1993 | { |
| 1994 | size_t acc = 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1995 | |
| 1996 | for(i = 0; i < nheader; ++i) { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1997 | acc += nva[i].namelen + nva[i].valuelen; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1998 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 1999 | H2BUGF(infof(conn->data, "h2 header: %.*s:%.*s\n", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 2000 | nva[i].namelen, nva[i].name, |
| 2001 | nva[i].valuelen, nva[i].value)); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 2004 | if(acc > MAX_ACC) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2005 | infof(conn->data, "http2_send: Warning: The cumulative length of all " |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 2006 | "headers exceeds %zu bytes and that could cause the " |
| 2007 | "stream to be rejected.\n", MAX_ACC); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | h2_pri_spec(conn->data, &pri_spec); |
| 2012 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2013 | switch(conn->data->set.httpreq) { |
| 2014 | case HTTPREQ_POST: |
| 2015 | case HTTPREQ_POST_FORM: |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 2016 | case HTTPREQ_POST_MIME: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2017 | case HTTPREQ_PUT: |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 2018 | if(conn->data->state.infilesize != -1) |
| 2019 | stream->upload_left = conn->data->state.infilesize; |
| 2020 | else |
| 2021 | /* data sending without specifying the data amount up front */ |
| 2022 | stream->upload_left = -1; /* unknown, but not zero */ |
| 2023 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2024 | data_prd.read_callback = data_source_read_callback; |
| 2025 | data_prd.source.ptr = NULL; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2026 | stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader, |
| 2027 | &data_prd, conn->data); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2028 | break; |
| 2029 | default: |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2030 | stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader, |
| 2031 | NULL, conn->data); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2034 | Curl_safefree(nva); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2035 | |
| 2036 | if(stream_id < 0) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 2037 | H2BUGF(infof(conn->data, "http2_send() send error\n")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2038 | *err = CURLE_SEND_ERROR; |
| 2039 | return -1; |
| 2040 | } |
| 2041 | |
| 2042 | infof(conn->data, "Using Stream ID: %x (easy handle %p)\n", |
| 2043 | stream_id, conn->data); |
| 2044 | stream->stream_id = stream_id; |
| 2045 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2046 | /* this does not call h2_session_send() since there can not have been any |
| 2047 | * priority upodate since the nghttp2_submit_request() call above */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2048 | rv = nghttp2_session_send(h2); |
| 2049 | |
| 2050 | if(rv != 0) { |
| 2051 | *err = CURLE_SEND_ERROR; |
| 2052 | return -1; |
| 2053 | } |
| 2054 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2055 | if(should_close_session(httpc)) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 2056 | H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n")); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2057 | *err = CURLE_HTTP2; |
| 2058 | return -1; |
| 2059 | } |
| 2060 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2061 | if(stream->stream_id != -1) { |
| 2062 | /* If whole HEADERS frame was sent off to the underlying socket, |
| 2063 | the nghttp2 library calls data_source_read_callback. But only |
| 2064 | it found that no data available, so it deferred the DATA |
| 2065 | transmission. Which means that nghttp2_session_want_write() |
| 2066 | returns 0 on http2_perform_getsock(), which results that no |
| 2067 | writable socket check is performed. To workaround this, we |
| 2068 | issue nghttp2_session_resume_data() here to bring back DATA |
| 2069 | transmission from deferred state. */ |
| 2070 | nghttp2_session_resume_data(h2, stream->stream_id); |
| 2071 | } |
| 2072 | |
| 2073 | return len; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2074 | |
| 2075 | fail: |
| 2076 | free(nva); |
| 2077 | *err = CURLE_SEND_ERROR; |
| 2078 | return -1; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | CURLcode Curl_http2_setup(struct connectdata *conn) |
| 2082 | { |
| 2083 | CURLcode result; |
| 2084 | struct http_conn *httpc = &conn->proto.httpc; |
| 2085 | struct HTTP *stream = conn->data->req.protop; |
| 2086 | |
| 2087 | stream->stream_id = -1; |
| 2088 | |
| 2089 | if(!stream->header_recvbuf) |
| 2090 | stream->header_recvbuf = Curl_add_buffer_init(); |
| 2091 | |
| 2092 | if((conn->handler == &Curl_handler_http2_ssl) || |
| 2093 | (conn->handler == &Curl_handler_http2)) |
| 2094 | return CURLE_OK; /* already done */ |
| 2095 | |
| 2096 | if(conn->handler->flags & PROTOPT_SSL) |
| 2097 | conn->handler = &Curl_handler_http2_ssl; |
| 2098 | else |
| 2099 | conn->handler = &Curl_handler_http2; |
| 2100 | |
| 2101 | result = Curl_http2_init(conn); |
| 2102 | if(result) |
| 2103 | return result; |
| 2104 | |
| 2105 | infof(conn->data, "Using HTTP2, server supports multi-use\n"); |
| 2106 | stream->upload_left = 0; |
| 2107 | stream->upload_mem = NULL; |
| 2108 | stream->upload_len = 0; |
| 2109 | |
| 2110 | httpc->inbuflen = 0; |
| 2111 | httpc->nread_inbuf = 0; |
| 2112 | |
| 2113 | httpc->pause_stream_id = 0; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2114 | httpc->drain_total = 0; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2115 | |
| 2116 | conn->bits.multiplex = TRUE; /* at least potentially multiplexed */ |
| 2117 | conn->httpversion = 20; |
| 2118 | conn->bundle->multiuse = BUNDLE_MULTIPLEX; |
| 2119 | |
| 2120 | infof(conn->data, "Connection state changed (HTTP/2 confirmed)\n"); |
| 2121 | Curl_multi_connchanged(conn->data->multi); |
| 2122 | |
| 2123 | return CURLE_OK; |
| 2124 | } |
| 2125 | |
| 2126 | CURLcode Curl_http2_switched(struct connectdata *conn, |
| 2127 | const char *mem, size_t nread) |
| 2128 | { |
| 2129 | CURLcode result; |
| 2130 | struct http_conn *httpc = &conn->proto.httpc; |
| 2131 | int rv; |
| 2132 | ssize_t nproc; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 2133 | struct Curl_easy *data = conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2134 | struct HTTP *stream = conn->data->req.protop; |
| 2135 | |
| 2136 | result = Curl_http2_setup(conn); |
| 2137 | if(result) |
| 2138 | return result; |
| 2139 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 2140 | httpc->recv_underlying = conn->recv[FIRSTSOCKET]; |
| 2141 | httpc->send_underlying = conn->send[FIRSTSOCKET]; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2142 | conn->recv[FIRSTSOCKET] = http2_recv; |
| 2143 | conn->send[FIRSTSOCKET] = http2_send; |
| 2144 | |
| 2145 | if(conn->data->req.upgr101 == UPGR101_RECEIVED) { |
| 2146 | /* stream 1 is opened implicitly on upgrade */ |
| 2147 | stream->stream_id = 1; |
| 2148 | /* queue SETTINGS frame (again) */ |
| 2149 | rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings, |
| 2150 | httpc->binlen, NULL); |
| 2151 | if(rv != 0) { |
| 2152 | failf(data, "nghttp2_session_upgrade() failed: %s(%d)", |
| 2153 | nghttp2_strerror(rv), rv); |
| 2154 | return CURLE_HTTP2; |
| 2155 | } |
| 2156 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2157 | nghttp2_session_set_stream_user_data(httpc->h2, |
| 2158 | stream->stream_id, |
| 2159 | conn->data); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2160 | } |
| 2161 | else { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 2162 | populate_settings(conn, httpc); |
| 2163 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2164 | /* stream ID is unknown at this point */ |
| 2165 | stream->stream_id = -1; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 2166 | rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE, |
| 2167 | httpc->local_settings, |
| 2168 | httpc->local_settings_num); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2169 | if(rv != 0) { |
| 2170 | failf(data, "nghttp2_submit_settings() failed: %s(%d)", |
| 2171 | nghttp2_strerror(rv), rv); |
| 2172 | return CURLE_HTTP2; |
| 2173 | } |
| 2174 | } |
| 2175 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 2176 | #ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 2177 | rv = nghttp2_session_set_local_window_size(httpc->h2, NGHTTP2_FLAG_NONE, 0, |
| 2178 | HTTP2_HUGE_WINDOW_SIZE); |
| 2179 | if(rv != 0) { |
| 2180 | failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)", |
| 2181 | nghttp2_strerror(rv), rv); |
| 2182 | return CURLE_HTTP2; |
| 2183 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 2184 | #endif |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 2185 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2186 | /* we are going to copy mem to httpc->inbuf. This is required since |
| 2187 | mem is part of buffer pointed by stream->mem, and callbacks |
| 2188 | called by nghttp2_session_mem_recv() will write stream specific |
| 2189 | data into stream->mem, overwriting data already there. */ |
| 2190 | if(H2_BUFSIZE < nread) { |
| 2191 | failf(data, "connection buffer size is too small to store data following " |
| 2192 | "HTTP Upgrade response header: buflen=%zu, datalen=%zu", |
| 2193 | H2_BUFSIZE, nread); |
| 2194 | return CURLE_HTTP2; |
| 2195 | } |
| 2196 | |
| 2197 | infof(conn->data, "Copying HTTP/2 data in stream buffer to connection buffer" |
| 2198 | " after upgrade: len=%zu\n", |
| 2199 | nread); |
| 2200 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 2201 | if(nread) |
| 2202 | memcpy(httpc->inbuf, mem, nread); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2203 | httpc->inbuflen = nread; |
| 2204 | |
| 2205 | nproc = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)httpc->inbuf, |
| 2206 | httpc->inbuflen); |
| 2207 | |
| 2208 | if(nghttp2_is_fatal((int)nproc)) { |
| 2209 | failf(data, "nghttp2_session_mem_recv() failed: %s(%d)", |
| 2210 | nghttp2_strerror((int)nproc), (int)nproc); |
| 2211 | return CURLE_HTTP2; |
| 2212 | } |
| 2213 | |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 2214 | H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", nproc)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2215 | |
| 2216 | if((ssize_t)nread == nproc) { |
| 2217 | httpc->inbuflen = 0; |
| 2218 | httpc->nread_inbuf = 0; |
| 2219 | } |
| 2220 | else { |
| 2221 | httpc->nread_inbuf += nproc; |
| 2222 | } |
| 2223 | |
| 2224 | /* Try to send some frames since we may read SETTINGS already. */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2225 | rv = h2_session_send(data, httpc->h2); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2226 | |
| 2227 | if(rv != 0) { |
| 2228 | failf(data, "nghttp2_session_send() failed: %s(%d)", |
| 2229 | nghttp2_strerror(rv), rv); |
| 2230 | return CURLE_HTTP2; |
| 2231 | } |
| 2232 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2233 | if(should_close_session(httpc)) { |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 2234 | H2BUGF(infof(data, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2235 | "nghttp2_session_send(): nothing to do in this session\n")); |
| 2236 | return CURLE_HTTP2; |
| 2237 | } |
| 2238 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 2239 | return CURLE_OK; |
| 2240 | } |
| 2241 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 2242 | CURLcode Curl_http2_add_child(struct Curl_easy *parent, |
| 2243 | struct Curl_easy *child, |
| 2244 | bool exclusive) |
| 2245 | { |
| 2246 | if(parent) { |
| 2247 | struct Curl_http2_dep **tail; |
| 2248 | struct Curl_http2_dep *dep = calloc(1, sizeof(struct Curl_http2_dep)); |
| 2249 | if(!dep) |
| 2250 | return CURLE_OUT_OF_MEMORY; |
| 2251 | dep->data = child; |
| 2252 | |
| 2253 | if(parent->set.stream_dependents && exclusive) { |
| 2254 | struct Curl_http2_dep *node = parent->set.stream_dependents; |
| 2255 | while(node) { |
| 2256 | node->data->set.stream_depends_on = child; |
| 2257 | node = node->next; |
| 2258 | } |
| 2259 | |
| 2260 | tail = &child->set.stream_dependents; |
| 2261 | while(*tail) |
| 2262 | tail = &(*tail)->next; |
| 2263 | |
| 2264 | DEBUGASSERT(!*tail); |
| 2265 | *tail = parent->set.stream_dependents; |
| 2266 | parent->set.stream_dependents = 0; |
| 2267 | } |
| 2268 | |
| 2269 | tail = &parent->set.stream_dependents; |
| 2270 | while(*tail) { |
| 2271 | (*tail)->data->set.stream_depends_e = FALSE; |
| 2272 | tail = &(*tail)->next; |
| 2273 | } |
| 2274 | |
| 2275 | DEBUGASSERT(!*tail); |
| 2276 | *tail = dep; |
| 2277 | } |
| 2278 | |
| 2279 | child->set.stream_depends_on = parent; |
| 2280 | child->set.stream_depends_e = exclusive; |
| 2281 | return CURLE_OK; |
| 2282 | } |
| 2283 | |
| 2284 | void Curl_http2_remove_child(struct Curl_easy *parent, struct Curl_easy *child) |
| 2285 | { |
| 2286 | struct Curl_http2_dep *last = 0; |
| 2287 | struct Curl_http2_dep *data = parent->set.stream_dependents; |
| 2288 | DEBUGASSERT(child->set.stream_depends_on == parent); |
| 2289 | |
| 2290 | while(data && data->data != child) { |
| 2291 | last = data; |
| 2292 | data = data->next; |
| 2293 | } |
| 2294 | |
| 2295 | DEBUGASSERT(data); |
| 2296 | |
| 2297 | if(data) { |
| 2298 | if(last) { |
| 2299 | last->next = data->next; |
| 2300 | } |
| 2301 | else { |
| 2302 | parent->set.stream_dependents = data->next; |
| 2303 | } |
| 2304 | free(data); |
| 2305 | } |
| 2306 | |
| 2307 | child->set.stream_depends_on = 0; |
| 2308 | child->set.stream_depends_e = FALSE; |
| 2309 | } |
| 2310 | |
| 2311 | void Curl_http2_cleanup_dependencies(struct Curl_easy *data) |
| 2312 | { |
| 2313 | while(data->set.stream_dependents) { |
| 2314 | struct Curl_easy *tmp = data->set.stream_dependents->data; |
| 2315 | Curl_http2_remove_child(data, tmp); |
| 2316 | if(data->set.stream_depends_on) |
| 2317 | Curl_http2_add_child(data->set.stream_depends_on, tmp, FALSE); |
| 2318 | } |
| 2319 | |
| 2320 | if(data->set.stream_depends_on) |
| 2321 | Curl_http2_remove_child(data->set.stream_depends_on, data); |
| 2322 | } |
| 2323 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 2324 | #else /* !USE_NGHTTP2 */ |
| 2325 | |
| 2326 | /* Satisfy external references even if http2 is not compiled in. */ |
| 2327 | |
| 2328 | #define CURL_DISABLE_TYPECHECK |
| 2329 | #include <curl/curl.h> |
| 2330 | |
| 2331 | char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num) |
| 2332 | { |
| 2333 | (void) h; |
| 2334 | (void) num; |
| 2335 | return NULL; |
| 2336 | } |
| 2337 | |
| 2338 | char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header) |
| 2339 | { |
| 2340 | (void) h; |
| 2341 | (void) header; |
| 2342 | return NULL; |
| 2343 | } |
| 2344 | |
| 2345 | #endif /* USE_NGHTTP2 */ |