url-parser: only use if_nametoindex if detected by configure
[platform/upstream/curl.git] / lib / http2.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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
12  * are also available at http://curl.haxx.se/docs/copyright.html.
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
26 #define _MPRINTF_REPLACE
27 #include <curl/mprintf.h>
28
29 #include <nghttp2/nghttp2.h>
30 #include "urldata.h"
31 #include "http2.h"
32 #include "http.h"
33 #include "sendf.h"
34 #include "curl_base64.h"
35 #include "curl_memory.h"
36 #include "rawstr.h"
37 #include "multiif.h"
38
39 /* include memdebug.h last */
40 #include "memdebug.h"
41
42 #if (NGHTTP2_VERSION_NUM < 0x000300)
43 #error too old nghttp2 version, upgrade!
44 #endif
45
46 static int http2_perform_getsock(const struct connectdata *conn,
47                                  curl_socket_t *sock, /* points to
48                                                          numsocks
49                                                          number of
50                                                          sockets */
51                                  int numsocks)
52 {
53   const struct http_conn *httpc = &conn->proto.httpc;
54   int bitmap = GETSOCK_BLANK;
55   (void)numsocks;
56
57   /* TODO We should check underlying socket state if it is SSL socket
58      because of renegotiation. */
59   sock[0] = conn->sock[FIRSTSOCKET];
60
61   if(nghttp2_session_want_read(httpc->h2))
62     bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
63
64   if(nghttp2_session_want_write(httpc->h2))
65     bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
66
67   return bitmap;
68 }
69
70 static int http2_getsock(struct connectdata *conn,
71                          curl_socket_t *sock, /* points to numsocks
72                                                  number of sockets */
73                          int numsocks)
74 {
75   return http2_perform_getsock(conn, sock, numsocks);
76 }
77
78 static CURLcode http2_disconnect(struct connectdata *conn,
79                                  bool dead_connection)
80 {
81   struct http_conn *httpc = &conn->proto.httpc;
82   (void)dead_connection;
83
84   infof(conn->data, "HTTP/2 DISCONNECT starts now\n");
85
86   nghttp2_session_del(httpc->h2);
87
88   Curl_safefree(httpc->header_recvbuf->buffer);
89   Curl_safefree(httpc->header_recvbuf);
90
91   Curl_safefree(httpc->inbuf);
92
93   infof(conn->data, "HTTP/2 DISCONNECT done\n");
94
95   return CURLE_OK;
96 }
97
98 /*
99  * HTTP2 handler interface. This isn't added to the general list of protocols
100  * but will be used at run-time when the protocol is dynamically switched from
101  * HTTP to HTTP2.
102  */
103 const struct Curl_handler Curl_handler_http2 = {
104   "HTTP2",                              /* scheme */
105   ZERO_NULL,                            /* setup_connection */
106   Curl_http,                            /* do_it */
107   ZERO_NULL,                            /* done */
108   ZERO_NULL,                            /* do_more */
109   ZERO_NULL,                            /* connect_it */
110   ZERO_NULL,                            /* connecting */
111   ZERO_NULL,                            /* doing */
112   http2_getsock,                        /* proto_getsock */
113   http2_getsock,                        /* doing_getsock */
114   ZERO_NULL,                            /* domore_getsock */
115   http2_perform_getsock,                /* perform_getsock */
116   http2_disconnect,                     /* disconnect */
117   ZERO_NULL,                            /* readwrite */
118   PORT_HTTP,                            /* defport */
119   CURLPROTO_HTTP,                       /* protocol */
120   PROTOPT_NONE                          /* flags */
121 };
122
123 const struct Curl_handler Curl_handler_http2_ssl = {
124   "HTTP2",                              /* scheme */
125   ZERO_NULL,                            /* setup_connection */
126   Curl_http,                            /* do_it */
127   ZERO_NULL,                            /* done */
128   ZERO_NULL,                            /* do_more */
129   ZERO_NULL,                            /* connect_it */
130   ZERO_NULL,                            /* connecting */
131   ZERO_NULL,                            /* doing */
132   http2_getsock,                        /* proto_getsock */
133   http2_getsock,                        /* doing_getsock */
134   ZERO_NULL,                            /* domore_getsock */
135   http2_perform_getsock,                /* perform_getsock */
136   http2_disconnect,                     /* disconnect */
137   ZERO_NULL,                            /* readwrite */
138   PORT_HTTP,                            /* defport */
139   CURLPROTO_HTTPS,                      /* protocol */
140   PROTOPT_SSL                           /* flags */
141 };
142
143 /*
144  * Store nghttp2 version info in this buffer, Prefix with a space.  Return
145  * total length written.
146  */
147 int Curl_http2_ver(char *p, size_t len)
148 {
149   nghttp2_info *h2 = nghttp2_version(0);
150   return snprintf(p, len, " nghttp2/%s", h2->version_str);
151 }
152
153 /*
154  * The implementation of nghttp2_send_callback type. Here we write |data| with
155  * size |length| to the network and return the number of bytes actually
156  * written. See the documentation of nghttp2_send_callback for the details.
157  */
158 static ssize_t send_callback(nghttp2_session *h2,
159                              const uint8_t *data, size_t length, int flags,
160                              void *userp)
161 {
162   struct connectdata *conn = (struct connectdata *)userp;
163   struct http_conn *httpc = &conn->proto.httpc;
164   ssize_t written;
165   CURLcode rc;
166   (void)h2;
167   (void)flags;
168
169   rc = 0;
170   written = ((Curl_send*)httpc->send_underlying)(conn, FIRSTSOCKET,
171                                                  data, length, &rc);
172
173   if(rc == CURLE_AGAIN) {
174     return NGHTTP2_ERR_WOULDBLOCK;
175   }
176
177   if(written == -1) {
178     failf(conn->data, "Failed sending HTTP2 data");
179     return NGHTTP2_ERR_CALLBACK_FAILURE;
180   }
181
182   if(!written)
183     return NGHTTP2_ERR_WOULDBLOCK;
184
185   return written;
186 }
187
188 static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
189                          void *userp)
190 {
191   struct connectdata *conn = (struct connectdata *)userp;
192   struct http_conn *c = &conn->proto.httpc;
193   int rv;
194   (void)session;
195   (void)frame;
196   infof(conn->data, "on_frame_recv() was called with header %x\n",
197         frame->hd.type);
198   switch(frame->hd.type) {
199   case NGHTTP2_HEADERS:
200     if(frame->headers.cat != NGHTTP2_HCAT_RESPONSE)
201       break;
202     c->bodystarted = TRUE;
203     Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
204     c->nread_header_recvbuf = c->len < c->header_recvbuf->size_used ?
205       c->len : c->header_recvbuf->size_used;
206
207     memcpy(c->mem, c->header_recvbuf->buffer, c->nread_header_recvbuf);
208
209     c->mem += c->nread_header_recvbuf;
210     c->len -= c->nread_header_recvbuf;
211     break;
212   case NGHTTP2_PUSH_PROMISE:
213     rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
214                                    frame->hd.stream_id, NGHTTP2_CANCEL);
215     if(nghttp2_is_fatal(rv)) {
216       return rv;
217     }
218     break;
219   }
220   return 0;
221 }
222
223 static int on_invalid_frame_recv(nghttp2_session *session,
224                                  const nghttp2_frame *frame,
225                                  nghttp2_error_code error_code, void *userp)
226 {
227   struct connectdata *conn = (struct connectdata *)userp;
228   (void)session;
229   (void)frame;
230   infof(conn->data, "on_invalid_frame_recv() was called, error_code = %d\n",
231         error_code);
232   return 0;
233 }
234
235 static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
236                               int32_t stream_id,
237                               const uint8_t *data, size_t len, void *userp)
238 {
239   struct connectdata *conn = (struct connectdata *)userp;
240   struct http_conn *c = &conn->proto.httpc;
241   size_t nread;
242   (void)session;
243   (void)flags;
244   (void)data;
245   infof(conn->data, "on_data_chunk_recv() "
246         "len = %u, stream = %x\n", len, stream_id);
247
248   if(stream_id != c->stream_id) {
249     return 0;
250   }
251
252   nread = c->len < len ? c->len : len;
253   memcpy(c->mem, data, nread);
254
255   c->mem += nread;
256   c->len -= nread;
257
258   infof(conn->data, "%zu data written\n", nread);
259
260   if(nread < len) {
261     c->data = data + nread;
262     c->datalen = len - nread;
263     return NGHTTP2_ERR_PAUSE;
264   }
265   return 0;
266 }
267
268 static int before_frame_send(nghttp2_session *session,
269                              const nghttp2_frame *frame,
270                              void *userp)
271 {
272   struct connectdata *conn = (struct connectdata *)userp;
273   (void)session;
274   (void)frame;
275   infof(conn->data, "before_frame_send() was called\n");
276   return 0;
277 }
278 static int on_frame_send(nghttp2_session *session,
279                          const nghttp2_frame *frame,
280                          void *userp)
281 {
282   struct connectdata *conn = (struct connectdata *)userp;
283   (void)session;
284   (void)frame;
285   infof(conn->data, "on_frame_send() was called\n");
286   return 0;
287 }
288 static int on_frame_not_send(nghttp2_session *session,
289                              const nghttp2_frame *frame,
290                              int lib_error_code, void *userp)
291 {
292   struct connectdata *conn = (struct connectdata *)userp;
293   (void)session;
294   (void)frame;
295   infof(conn->data, "on_frame_not_send() was called, lib_error_code = %d\n",
296         lib_error_code);
297   return 0;
298 }
299 static int on_stream_close(nghttp2_session *session, int32_t stream_id,
300                            nghttp2_error_code error_code, void *userp)
301 {
302   struct connectdata *conn = (struct connectdata *)userp;
303   struct http_conn *c = &conn->proto.httpc;
304   (void)session;
305   (void)stream_id;
306   infof(conn->data, "on_stream_close() was called, error_code = %d\n",
307         error_code);
308
309   if(stream_id != c->stream_id) {
310     return 0;
311   }
312
313   c->closed = TRUE;
314
315   return 0;
316 }
317
318 static int on_unknown_frame_recv(nghttp2_session *session,
319                                  const uint8_t *head, size_t headlen,
320                                  const uint8_t *payload, size_t payloadlen,
321                                  void *userp)
322 {
323   struct connectdata *conn = (struct connectdata *)userp;
324   (void)session;
325   (void)head;
326   (void)headlen;
327   (void)payload;
328   (void)payloadlen;
329   infof(conn->data, "on_unknown_frame_recv() was called\n");
330   return 0;
331 }
332 static int on_begin_headers(nghttp2_session *session,
333                             const nghttp2_frame *frame, void *userp)
334 {
335   struct connectdata *conn = (struct connectdata *)userp;
336   (void)session;
337   (void)frame;
338   infof(conn->data, "on_begin_headers() was called\n");
339   return 0;
340 }
341
342 static const char STATUS[] = ":status";
343
344 /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
345 static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
346                      const uint8_t *name, size_t namelen,
347                      const uint8_t *value, size_t valuelen,
348                      uint8_t flags,
349                      void *userp)
350 {
351   struct connectdata *conn = (struct connectdata *)userp;
352   struct http_conn *c = &conn->proto.httpc;
353   (void)session;
354   (void)frame;
355   (void)flags;
356
357   if(frame->hd.stream_id != c->stream_id) {
358     return 0;
359   }
360
361   if(namelen == sizeof(":status") - 1 &&
362      memcmp(STATUS, name, namelen) == 0) {
363     snprintf(c->header_recvbuf->buffer, 13, "HTTP/2.0 %s", value);
364     c->header_recvbuf->buffer[12] = '\r';
365     return 0;
366   }
367   else {
368     /* convert to a HTTP1-style header */
369     infof(conn->data, "got header\n");
370     Curl_add_buffer(c->header_recvbuf, name, namelen);
371     Curl_add_buffer(c->header_recvbuf, ":", 1);
372     Curl_add_buffer(c->header_recvbuf, value, valuelen);
373     Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
374   }
375
376   return 0; /* 0 is successful */
377 }
378
379 /*
380  * This is all callbacks nghttp2 calls
381  */
382 static const nghttp2_session_callbacks callbacks = {
383   send_callback,         /* nghttp2_send_callback */
384   NULL,                  /* nghttp2_recv_callback */
385   on_frame_recv,         /* nghttp2_on_frame_recv_callback */
386   on_invalid_frame_recv, /* nghttp2_on_invalid_frame_recv_callback */
387   on_data_chunk_recv,    /* nghttp2_on_data_chunk_recv_callback */
388   before_frame_send,     /* nghttp2_before_frame_send_callback */
389   on_frame_send,         /* nghttp2_on_frame_send_callback */
390   on_frame_not_send,     /* nghttp2_on_frame_not_send_callback */
391   on_stream_close,       /* nghttp2_on_stream_close_callback */
392   on_unknown_frame_recv, /* nghttp2_on_unknown_frame_recv_callback */
393   on_begin_headers,      /* nghttp2_on_begin_headers_callback */
394   on_header              /* nghttp2_on_header_callback */
395 #if NGHTTP2_VERSION_NUM >= 0x000400
396   , NULL                 /* nghttp2_select_padding_callback */
397 #endif
398 };
399
400 static ssize_t data_source_read_callback(nghttp2_session *session,
401                                          int32_t stream_id,
402                                          uint8_t *buf, size_t length,
403                                          uint32_t *data_flags,
404                                          nghttp2_data_source *source,
405                                          void *userp)
406 {
407   struct connectdata *conn = (struct connectdata *)userp;
408   struct http_conn *c = &conn->proto.httpc;
409   size_t nread;
410   (void)session;
411   (void)stream_id;
412   (void)source;
413
414   nread = c->upload_len < length ? c->upload_len : length;
415   if(nread > 0) {
416     memcpy(buf, c->upload_mem, nread);
417     c->upload_mem += nread;
418     c->upload_len -= nread;
419     c->upload_left -= nread;
420   }
421
422   if(c->upload_left == 0)
423     *data_flags = 1;
424   else if(nread == 0)
425     return NGHTTP2_ERR_DEFERRED;
426
427   return nread;
428 }
429
430 /*
431  * The HTTP2 settings we send in the Upgrade request
432  */
433 static nghttp2_settings_entry settings[] = {
434   { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
435   { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
436 };
437
438 #define H2_BUFSIZE 4096
439
440 /*
441  * Initialize nghttp2 for a Curl connection
442  */
443 CURLcode Curl_http2_init(struct connectdata *conn)
444 {
445   if(!conn->proto.httpc.h2) {
446     int rc;
447     conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
448     if(conn->proto.httpc.inbuf == NULL)
449       return CURLE_OUT_OF_MEMORY;
450
451     /* The nghttp2 session is not yet setup, do it */
452     rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
453                                     &callbacks, conn);
454     if(rc) {
455       failf(conn->data, "Couldn't initialize nghttp2!");
456       return CURLE_OUT_OF_MEMORY; /* most likely at least */
457     }
458   }
459   return CURLE_OK;
460 }
461
462 /*
463  * Send a request using http2
464  */
465 CURLcode Curl_http2_send_request(struct connectdata *conn)
466 {
467   (void)conn;
468   return CURLE_OK;
469 }
470
471 /*
472  * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
473  */
474 CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req,
475                                     struct connectdata *conn)
476 {
477   CURLcode result;
478   ssize_t binlen;
479   char *base64;
480   size_t blen;
481   struct SingleRequest *k = &conn->data->req;
482   uint8_t *binsettings = conn->proto.httpc.binsettings;
483
484   Curl_http2_init(conn);
485
486   /* As long as we have a fixed set of settings, we don't have to dynamically
487    * figure out the base64 strings since it'll always be the same. However,
488    * the settings will likely not be fixed every time in the future.
489    */
490
491   /* this returns number of bytes it wrote */
492   binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
493                                          settings,
494                                          sizeof(settings)/sizeof(settings[0]));
495   if(!binlen) {
496     failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
497     return CURLE_FAILED_INIT;
498   }
499   conn->proto.httpc.binlen = binlen;
500
501   result = Curl_base64_encode(conn->data, (const char *)binsettings, binlen,
502                               &base64, &blen);
503   if(result)
504     return result;
505
506   result = Curl_add_bufferf(req,
507                             "Connection: Upgrade, HTTP2-Settings\r\n"
508                             "Upgrade: %s\r\n"
509                             "HTTP2-Settings: %s\r\n",
510                             NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
511   Curl_safefree(base64);
512
513   k->upgr101 = UPGR101_REQUESTED;
514
515   return result;
516 }
517
518 /*
519  * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
520  * a regular CURLcode value.
521  */
522 static ssize_t http2_recv(struct connectdata *conn, int sockindex,
523                           char *mem, size_t len, CURLcode *err)
524 {
525   CURLcode rc;
526   ssize_t rv;
527   ssize_t nread;
528   struct http_conn *httpc = &conn->proto.httpc;
529
530   (void)sockindex; /* we always do HTTP2 on sockindex 0 */
531
532   if(httpc->closed) {
533     return 0;
534   }
535
536   /* Nullify here because we call nghttp2_session_send() and they
537      might refer to the old buffer. */
538   httpc->upload_mem = NULL;
539   httpc->upload_len = 0;
540
541   if(httpc->bodystarted &&
542      httpc->nread_header_recvbuf < httpc->header_recvbuf->size_used) {
543     size_t left =
544       httpc->header_recvbuf->size_used - httpc->nread_header_recvbuf;
545     size_t ncopy = len < left ? len : left;
546     memcpy(mem, httpc->header_recvbuf->buffer + httpc->nread_header_recvbuf,
547            ncopy);
548     httpc->nread_header_recvbuf += ncopy;
549     return ncopy;
550   }
551
552   if(httpc->data) {
553     nread = len < httpc->datalen ? len : httpc->datalen;
554     memcpy(mem, httpc->data, nread);
555
556     httpc->data += nread;
557     httpc->datalen -= nread;
558
559     infof(conn->data, "%zu data written\n", nread);
560     if(httpc->datalen == 0) {
561       httpc->data = NULL;
562       httpc->datalen = 0;
563     }
564     return nread;
565   }
566
567   conn->proto.httpc.mem = mem;
568   conn->proto.httpc.len = len;
569
570   infof(conn->data, "http2_recv: %d bytes buffer\n",
571         conn->proto.httpc.len);
572
573   rc = 0;
574   nread = ((Curl_recv*)httpc->recv_underlying)(conn, FIRSTSOCKET,
575                                                httpc->inbuf, H2_BUFSIZE, &rc);
576
577   if(rc == CURLE_AGAIN) {
578     *err = rc;
579     return -1;
580   }
581
582   if(nread == -1) {
583     failf(conn->data, "Failed receiving HTTP2 data");
584     *err = rc;
585     return 0;
586   }
587
588   infof(conn->data, "nread=%zd\n", nread);
589   rv = nghttp2_session_mem_recv(httpc->h2,
590                                 (const uint8_t *)httpc->inbuf, nread);
591
592   if(nghttp2_is_fatal((int)rv)) {
593     failf(conn->data, "nghttp2_session_mem_recv() returned %d:%s\n",
594           rv, nghttp2_strerror((int)rv));
595     *err = CURLE_RECV_ERROR;
596     return 0;
597   }
598   infof(conn->data, "nghttp2_session_mem_recv() returns %zd\n", rv);
599   /* Always send pending frames in nghttp2 session, because
600      nghttp2_session_mem_recv() may queue new frame */
601   rv = nghttp2_session_send(httpc->h2);
602   if(rv != 0) {
603     *err = CURLE_SEND_ERROR;
604     return 0;
605   }
606   if(len != httpc->len) {
607     return len - conn->proto.httpc.len;
608   }
609   /* If stream is closed, return 0 to signal the http routine to close
610      the connection */
611   if(httpc->closed) {
612     return 0;
613   }
614   *err = CURLE_AGAIN;
615   return -1;
616 }
617
618 /* return number of received (decrypted) bytes */
619 static ssize_t http2_send(struct connectdata *conn, int sockindex,
620                           const void *mem, size_t len, CURLcode *err)
621 {
622   /*
623    * BIG TODO: Currently, we send request in this function, but this
624    * function is also used to send request body. It would be nice to
625    * add dedicated function for request.
626    */
627   int rv;
628   struct http_conn *httpc = &conn->proto.httpc;
629   nghttp2_nv *nva;
630   size_t nheader;
631   size_t i;
632   char *hdbuf = (char*)mem;
633   char *end;
634   nghttp2_data_provider data_prd;
635   int32_t stream_id;
636
637   (void)sockindex;
638
639   infof(conn->data, "http2_send len=%zu\n", len);
640
641   if(httpc->stream_id != -1) {
642     /* If stream_id != -1, we have dispatched request HEADERS, and now
643        are going to send or sending request body in DATA frame */
644     httpc->upload_mem = mem;
645     httpc->upload_len = len;
646     nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
647     rv = nghttp2_session_send(httpc->h2);
648     if(nghttp2_is_fatal(rv)) {
649       *err = CURLE_SEND_ERROR;
650       return -1;
651     }
652     return len - httpc->upload_len;
653   }
654
655   /* Calculate number of headers contained in [mem, mem + len) */
656   /* Here, we assume the curl http code generate *correct* HTTP header
657      field block */
658   nheader = 0;
659   for(i = 0; i < len; ++i) {
660     if(hdbuf[i] == 0x0a) {
661       ++nheader;
662     }
663   }
664   /* We counted additional 2 \n in the first and last line. We need 3
665      new headers: :method, :path and :scheme. Therefore we need one
666      more space. */
667   nheader += 1;
668   nva = malloc(sizeof(nghttp2_nv) * nheader);
669   if(nva == NULL) {
670     *err = CURLE_OUT_OF_MEMORY;
671     return -1;
672   }
673   /* Extract :method, :path from request line */
674   end = strchr(hdbuf, ' ');
675   nva[0].name = (unsigned char *)":method";
676   nva[0].namelen = (uint16_t)strlen((char *)nva[0].name);
677   nva[0].value = (unsigned char *)hdbuf;
678   nva[0].valuelen = (uint16_t)(end - hdbuf);
679   nva[0].flags = NGHTTP2_NV_FLAG_NONE;
680
681   hdbuf = end + 1;
682
683   end = strchr(hdbuf, ' ');
684   nva[1].name = (unsigned char *)":path";
685   nva[1].namelen = (uint16_t)strlen((char *)nva[1].name);
686   nva[1].value = (unsigned char *)hdbuf;
687   nva[1].valuelen = (uint16_t)(end - hdbuf);
688   nva[1].flags = NGHTTP2_NV_FLAG_NONE;
689
690   nva[2].name = (unsigned char *)":scheme";
691   nva[2].namelen = (uint16_t)strlen((char *)nva[2].name);
692   if(conn->handler->flags & PROTOPT_SSL)
693     nva[2].value = (unsigned char *)"https";
694   else
695     nva[2].value = (unsigned char *)"http";
696   nva[2].valuelen = (uint16_t)strlen((char *)nva[2].value);
697   nva[2].flags = NGHTTP2_NV_FLAG_NONE;
698
699   hdbuf = strchr(hdbuf, 0x0a);
700   ++hdbuf;
701
702   for(i = 3; i < nheader; ++i) {
703     end = strchr(hdbuf, ':');
704     assert(end);
705     if(end - hdbuf == 4 && Curl_raw_nequal("host", hdbuf, 4)) {
706       nva[i].name = (unsigned char *)":authority";
707       nva[i].namelen = (uint16_t)strlen((char *)nva[i].name);
708     }
709     else {
710       nva[i].name = (unsigned char *)hdbuf;
711       nva[i].namelen = (uint16_t)(end - hdbuf);
712     }
713     hdbuf = end + 1;
714     for(; *hdbuf == ' '; ++hdbuf);
715     end = strchr(hdbuf, 0x0d);
716     assert(end);
717     nva[i].value = (unsigned char *)hdbuf;
718     nva[i].valuelen = (uint16_t)(end - hdbuf);
719     nva[i].flags = NGHTTP2_NV_FLAG_NONE;
720
721     hdbuf = end + 2;
722     /* Inspect Content-Length header field and retrieve the request
723        entity length so that we can set END_STREAM to the last DATA
724        frame. */
725     if(nva[i].namelen == 14 &&
726        Curl_raw_nequal("content-length", (char*)nva[i].name, 14)) {
727       size_t j;
728       for(j = 0; j < nva[i].valuelen; ++j) {
729         httpc->upload_left *= 10;
730         httpc->upload_left += nva[i].value[j] - '0';
731       }
732       infof(conn->data, "request content-length=%zu\n", httpc->upload_left);
733     }
734   }
735
736   switch(conn->data->set.httpreq) {
737   case HTTPREQ_POST:
738   case HTTPREQ_POST_FORM:
739   case HTTPREQ_PUT:
740     data_prd.read_callback = data_source_read_callback;
741     data_prd.source.ptr = NULL;
742     stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
743                                        &data_prd, NULL);
744     break;
745   default:
746     stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
747                                        NULL, NULL);
748   }
749
750   Curl_safefree(nva);
751
752   if(stream_id < 0) {
753     *err = CURLE_SEND_ERROR;
754     return -1;
755   }
756
757   httpc->stream_id = stream_id;
758
759   rv = nghttp2_session_send(httpc->h2);
760
761   if(rv != 0) {
762     *err = CURLE_SEND_ERROR;
763     return -1;
764   }
765
766   if(httpc->stream_id != -1) {
767     /* If whole HEADERS frame was sent off to the underlying socket,
768        the nghttp2 library calls data_source_read_callback. But only
769        it found that no data available, so it deferred the DATA
770        transmission. Which means that nghttp2_session_want_write()
771        returns 0 on http2_perform_getsock(), which results that no
772        writable socket check is performed. To workaround this, we
773        issue nghttp2_session_resume_data() here to bring back DATA
774        transmission from deferred state. */
775     nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
776   }
777
778   return len;
779 }
780
781 void Curl_http2_setup(struct connectdata *conn)
782 {
783   struct http_conn *httpc = &conn->proto.httpc;
784   if(conn->handler->flags & PROTOPT_SSL)
785     conn->handler = &Curl_handler_http2_ssl;
786   else
787     conn->handler = &Curl_handler_http2;
788
789   infof(conn->data, "Using HTTP2\n");
790   httpc->bodystarted = FALSE;
791   httpc->closed = FALSE;
792   httpc->header_recvbuf = Curl_add_buffer_init();
793   httpc->nread_header_recvbuf = 0;
794   httpc->data = NULL;
795   httpc->datalen = 0;
796   httpc->upload_left = 0;
797   httpc->upload_mem = NULL;
798   httpc->upload_len = 0;
799   httpc->stream_id = -1;
800
801   conn->httpversion = 20;
802
803   /* Put place holder for status line */
804   Curl_add_buffer(httpc->header_recvbuf, "HTTP/2.0 200\r\n", 14);
805 }
806
807 int Curl_http2_switched(struct connectdata *conn)
808 {
809   /* TODO: May get CURLE_AGAIN */
810   CURLcode rc;
811   struct http_conn *httpc = &conn->proto.httpc;
812   int rv;
813
814   httpc->recv_underlying = (recving)conn->recv[FIRSTSOCKET];
815   httpc->send_underlying = (sending)conn->send[FIRSTSOCKET];
816   conn->recv[FIRSTSOCKET] = http2_recv;
817   conn->send[FIRSTSOCKET] = http2_send;
818
819   rv = (int) ((Curl_send*)httpc->send_underlying)
820     (conn, FIRSTSOCKET,
821      NGHTTP2_CLIENT_CONNECTION_PREFACE,
822      NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN,
823      &rc);
824   assert(rv == 24);
825   if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
826     /* stream 1 is opened implicitly on upgrade */
827     httpc->stream_id = 1;
828     /* queue SETTINGS frame (again) */
829     rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
830                                  httpc->binlen, NULL);
831     if(rv != 0) {
832       failf(conn->data, "nghttp2_session_upgrade() failed: %s(%d)",
833             nghttp2_strerror(rv), rv);
834       return -1;
835     }
836   }
837   else {
838     /* stream ID is unknown at this point */
839     httpc->stream_id = -1;
840     rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE, NULL, 0);
841     if(rv != 0) {
842       failf(conn->data, "nghttp2_submit_settings() failed: %s(%d)",
843             nghttp2_strerror(rv), rv);
844       return -1;
845     }
846   }
847   return 0;
848 }
849
850 #endif