1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
26 #define _MPRINTF_REPLACE
27 #include <curl/mprintf.h>
29 #include <nghttp2/nghttp2.h>
34 #include "curl_base64.h"
35 #include "curl_memory.h"
37 /* include memdebug.h last */
40 #if (NGHTTP2_VERSION_NUM < 0x000300)
41 #error too old nghttp2 version, upgrade!
45 * HTTP2 handler interface. This isn't added to the general list of protocols
46 * but will be used at run-time when the protocol is dynamically switched from
49 const struct Curl_handler Curl_handler_http2 = {
51 ZERO_NULL, /* setup_connection */
52 ZERO_NULL, /* do_it */
53 ZERO_NULL , /* done */
54 ZERO_NULL, /* do_more */
55 ZERO_NULL, /* connect_it */
56 ZERO_NULL, /* connecting */
57 ZERO_NULL, /* doing */
58 ZERO_NULL, /* proto_getsock */
59 ZERO_NULL, /* doing_getsock */
60 ZERO_NULL, /* domore_getsock */
61 ZERO_NULL, /* perform_getsock */
62 ZERO_NULL, /* disconnect */
63 ZERO_NULL, /* readwrite */
64 PORT_HTTP, /* defport */
66 PROTOPT_NONE /* flags */
71 * Store nghttp2 version info in this buffer, Prefix with a space. Return
72 * total length written.
74 int Curl_http2_ver(char *p, size_t len)
76 nghttp2_info *h2 = nghttp2_version(0);
77 return snprintf(p, len, " nghttp2/%s", h2->version_str);
81 * The implementation of nghttp2_send_callback type. Here we write |data| with
82 * size |length| to the network and return the number of bytes actually
83 * written. See the documentation of nghttp2_send_callback for the details.
85 static ssize_t send_callback(nghttp2_session *h2,
86 const uint8_t *data, size_t length, int flags,
89 struct connectdata *conn = (struct connectdata *)userp;
92 Curl_write(conn, conn->sock[FIRSTSOCKET], data, length, &written);
97 failf(conn->data, "Failed sending HTTP2 data");
98 return NGHTTP2_ERR_CALLBACK_FAILURE;
101 return NGHTTP2_ERR_WOULDBLOCK;
107 * The implementation of nghttp2_recv_callback type. Here we read data from
108 * the network and write them in |buf|. The capacity of |buf| is |length|
109 * bytes. Returns the number of bytes stored in |buf|. See the documentation
110 * of nghttp2_recv_callback for the details.
112 static ssize_t recv_callback(nghttp2_session *h2,
113 uint8_t *buf, size_t length, int flags,
116 struct connectdata *conn = (struct connectdata *)userp;
120 infof(conn->data, "recv_callback() was called with length %d\n", length);
122 rc = Curl_read_plain(conn->sock[FIRSTSOCKET], (char *)buf, length,
128 failf(conn->data, "Failed receiving HTTP2 data");
129 return NGHTTP2_ERR_CALLBACK_FAILURE;
132 return NGHTTP2_ERR_WOULDBLOCK;
137 static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
140 struct connectdata *conn = (struct connectdata *)userp;
143 infof(conn->data, "on_frame_recv() was called with header %x\n",
148 static int on_invalid_frame_recv(nghttp2_session *session,
149 const nghttp2_frame *frame,
150 nghttp2_error_code error_code, void *userp)
152 struct connectdata *conn = (struct connectdata *)userp;
155 infof(conn->data, "on_invalid_frame_recv() was called, error_code = %d\n",
160 static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
162 const uint8_t *data, size_t len, void *userp)
164 struct connectdata *conn = (struct connectdata *)userp;
169 infof(conn->data, "on_data_chunk_recv() was called with len = %u\n", len);
173 static int before_frame_send(nghttp2_session *session,
174 const nghttp2_frame *frame,
177 struct connectdata *conn = (struct connectdata *)userp;
180 infof(conn->data, "before_frame_send() was called\n");
183 static int on_frame_send(nghttp2_session *session,
184 const nghttp2_frame *frame,
187 struct connectdata *conn = (struct connectdata *)userp;
190 infof(conn->data, "on_frame_send() was called\n");
193 static int on_frame_not_send(nghttp2_session *session,
194 const nghttp2_frame *frame,
195 int lib_error_code, void *userp)
197 struct connectdata *conn = (struct connectdata *)userp;
200 infof(conn->data, "on_frame_not_send() was called, lib_error_code = %d\n",
204 static int on_stream_close(nghttp2_session *session, int32_t stream_id,
205 nghttp2_error_code error_code, void *userp)
207 struct connectdata *conn = (struct connectdata *)userp;
210 infof(conn->data, "on_stream_close() was called, error_code = %d\n",
215 static int on_unknown_frame_recv(nghttp2_session *session,
216 const uint8_t *head, size_t headlen,
217 const uint8_t *payload, size_t payloadlen,
220 struct connectdata *conn = (struct connectdata *)userp;
226 infof(conn->data, "on_unknown_frame_recv() was called\n");
229 static int on_begin_headers(nghttp2_session *session,
230 const nghttp2_frame *frame, void *userp)
232 struct connectdata *conn = (struct connectdata *)userp;
235 infof(conn->data, "on_begin_headers() was called\n");
239 /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
240 static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
241 const uint8_t *name, size_t namelen,
242 const uint8_t *value, size_t valuelen,
245 struct connectdata *conn = (struct connectdata *)userp;
249 if(namelen + valuelen < 200) {
251 memcpy(buffer, name, namelen);
253 memcpy(&buffer[namelen+1], value, valuelen);
254 buffer[namelen + valuelen + 1]=0;
255 infof(conn->data, "Got '%s'\n", buffer);
256 /* TODO: the headers need to be passed to the http parser */
259 infof(conn->data, "Got header with no name or too long\n",
260 namelen, name, valuelen, value);
263 return 0; /* 0 is successful */
267 * This is all callbacks nghttp2 calls
269 static const nghttp2_session_callbacks callbacks = {
270 send_callback, /* nghttp2_send_callback */
271 recv_callback, /* nghttp2_recv_callback */
272 on_frame_recv, /* nghttp2_on_frame_recv_callback */
273 on_invalid_frame_recv, /* nghttp2_on_invalid_frame_recv_callback */
274 on_data_chunk_recv, /* nghttp2_on_data_chunk_recv_callback */
275 before_frame_send, /* nghttp2_before_frame_send_callback */
276 on_frame_send, /* nghttp2_on_frame_send_callback */
277 on_frame_not_send, /* nghttp2_on_frame_not_send_callback */
278 on_stream_close, /* nghttp2_on_stream_close_callback */
279 on_unknown_frame_recv, /* nghttp2_on_unknown_frame_recv_callback */
280 on_begin_headers, /* nghttp2_on_begin_headers_callback */
281 on_header /* nghttp2_on_header_callback */
285 * The HTTP2 settings we send in the Upgrade request
287 static nghttp2_settings_entry settings[] = {
288 { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
289 { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
293 * Initialize nghttp2 for a Curl connection
295 CURLcode Curl_http2_init(struct connectdata *conn) {
296 if(!conn->proto.httpc.h2) {
297 /* The nghttp2 session is not yet setup, do it */
298 int rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
301 failf(conn->data, "Couldn't initialize nghttp2!");
302 return CURLE_OUT_OF_MEMORY; /* most likely at least */
309 * Send a request using http2
311 CURLcode Curl_http2_send_request(struct connectdata *conn)
318 * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
320 CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req,
321 struct connectdata *conn)
323 uint8_t binsettings[80];
328 struct SingleRequest *k = &conn->data->req;
330 Curl_http2_init(conn);
332 /* As long as we have a fixed set of settings, we don't have to dynamically
333 * figure out the base64 strings since it'll always be the same. However,
334 * the settings will likely not be fixed every time in the future.
337 /* this returns number of bytes it wrote */
338 binlen = nghttp2_pack_settings_payload(binsettings,
341 sizeof(settings)/sizeof(settings[0]));
343 failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
344 return CURLE_FAILED_INIT;
347 result = Curl_base64_encode(conn->data, (const char *)binsettings, binlen,
352 result = Curl_add_bufferf(req,
353 "Connection: Upgrade, HTTP2-Settings\r\n"
355 "HTTP2-Settings: %s\r\n",
356 NGHTTP2_PROTO_VERSION_ID, base64);
359 k->upgr101 = UPGR101_REQUESTED;
365 * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
366 * a regular CURLcode value.
368 static ssize_t http2_recv(struct connectdata *conn, int sockindex,
369 char *mem, size_t len, CURLcode *err)
372 (void)sockindex; /* we always do HTTP2 on sockindex 0 */
374 conn->proto.httpc.mem = mem;
375 conn->proto.httpc.size = len;
377 rc = nghttp2_session_recv(conn->proto.httpc.h2);
380 failf(conn->data, "nghttp2_session_recv() returned %d\n",
382 *err = CURLE_RECV_ERROR;
387 /* return number of received (decrypted) bytes */
388 static ssize_t http2_send(struct connectdata *conn, int sockindex,
389 const void *mem, size_t len, CURLcode *err)
391 /* TODO: proper implementation */
400 void Curl_http2_switched(struct connectdata *conn)
402 /* we are switched! */
403 conn->handler = &Curl_handler_http2;
404 conn->recv[FIRSTSOCKET] = http2_recv;
405 conn->send[FIRSTSOCKET] = http2_send;
406 infof(conn->data, "We have switched to HTTP2\n");