http2: handle 101 responses and switch to HTTP2
[platform/upstream/curl.git] / lib / http2.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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
37 /* include memdebug.h last */
38 #include "memdebug.h"
39
40 /*
41  * HTTP2 handler interface. This isn't added to the general list of protocols
42  * but will be used at run-time when the protocol is dynamically switched from
43  * HTTP to HTTP2.
44  */
45 const struct Curl_handler Curl_handler_http2 = {
46   "HTTP2",                              /* scheme */
47   ZERO_NULL,                            /* setup_connection */
48   ZERO_NULL,                            /* do_it */
49   ZERO_NULL     ,                       /* done */
50   ZERO_NULL,                            /* do_more */
51   ZERO_NULL,                            /* connect_it */
52   ZERO_NULL,                            /* connecting */
53   ZERO_NULL,                            /* doing */
54   ZERO_NULL,                            /* proto_getsock */
55   ZERO_NULL,                            /* doing_getsock */
56   ZERO_NULL,                            /* domore_getsock */
57   ZERO_NULL,                            /* perform_getsock */
58   ZERO_NULL,                            /* disconnect */
59   ZERO_NULL,                            /* readwrite */
60   PORT_HTTP,                            /* defport */
61   0,                                    /* protocol */
62   PROTOPT_NONE                          /* flags */
63 };
64
65
66 /*
67  * Store nghttp2 version info in this buffer, Prefix with a space.  Return
68  * total length written.
69  */
70 int Curl_http2_ver(char *p, size_t len)
71 {
72   nghttp2_info *h2 = nghttp2_version(0);
73   return snprintf(p, len, " nghttp2/%s", h2->version_str);
74 }
75
76 /*
77  * The implementation of nghttp2_send_callback type. Here we write |data| with
78  * size |length| to the network and return the number of bytes actually
79  * written. See the documentation of nghttp2_send_callback for the details.
80  */
81 static ssize_t send_callback(nghttp2_session *h2,
82                              const uint8_t *data, size_t length, int flags,
83                              void *userp)
84 {
85   struct connectdata *conn = (struct connectdata *)userp;
86   ssize_t written;
87   CURLcode rc =
88     Curl_write(conn, conn->sock[0], data, length, &written);
89   (void)h2;
90   (void)flags;
91
92   if(rc) {
93     failf(conn->data, "Failed sending HTTP2 data");
94     return NGHTTP2_ERR_CALLBACK_FAILURE;
95   }
96   else if(!written)
97     return NGHTTP2_ERR_WOULDBLOCK;
98
99   return written;
100 }
101
102 /*
103  * The implementation of nghttp2_recv_callback type. Here we read data from
104  * the network and write them in |buf|. The capacity of |buf| is |length|
105  * bytes. Returns the number of bytes stored in |buf|. See the documentation
106  * of nghttp2_recv_callback for the details.
107  */
108 static ssize_t recv_callback(nghttp2_session *h2,
109                              uint8_t *buf, size_t length, int flags,
110                              void *userp)
111 {
112   struct connectdata *conn = (struct connectdata *)userp;
113   ssize_t nread;
114   CURLcode rc = Curl_read(conn, conn->sock[0], (char *)buf, length, &nread);
115   (void)h2;
116   (void)flags;
117
118   if(rc) {
119     failf(conn->data, "Failed recving HTTP2 data");
120     return NGHTTP2_ERR_CALLBACK_FAILURE;
121   }
122   if(!nread)
123     return NGHTTP2_ERR_WOULDBLOCK;
124
125   return nread;
126 }
127
128 /*
129  * This is all callbacks nghttp2 calls
130  */
131 static const nghttp2_session_callbacks callbacks = {
132   send_callback,
133   recv_callback,
134   NULL,
135   NULL,
136   NULL,
137   NULL,
138   NULL,
139   NULL,
140   NULL,
141   NULL,
142   NULL,
143   NULL,
144   NULL,
145   NULL
146 };
147
148 /*
149  * The HTTP2 settings we send in the Upgrade request
150  */
151 static nghttp2_settings_entry settings[] = {
152   { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
153   { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
154 };
155
156 /*
157  * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
158  */
159 CURLcode Curl_http2_request(Curl_send_buffer *req,
160                             struct connectdata *conn)
161 {
162   uint8_t binsettings[80];
163   CURLcode result;
164   ssize_t binlen;
165   char *base64;
166   size_t blen;
167   struct SingleRequest *k = &conn->data->req;
168
169   if(!conn->proto.httpc.h2) {
170     /* The nghttp2 session is not yet setup, do it */
171     int rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
172                                         &callbacks, &conn);
173     if(rc) {
174       failf(conn->data, "Couldn't initialize nghttp2!");
175       return CURLE_OUT_OF_MEMORY; /* most likely at least */
176     }
177   }
178
179   /* As long as we have a fixed set of settings, we don't have to dynamically
180    * figure out the base64 strings since it'll always be the same. However,
181    * the settings will likely not be fixed every time in the future.
182    */
183
184   /* this returns number of bytes it wrote */
185   binlen = nghttp2_pack_settings_payload(binsettings,
186                                          sizeof(binsettings),
187                                          settings,
188                                          sizeof(settings)/sizeof(settings[0]));
189   if(!binlen) {
190     failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
191     return CURLE_FAILED_INIT;
192   }
193
194   result = Curl_base64_encode(conn->data, (const char *)binsettings, binlen,
195                               &base64, &blen);
196   if(result)
197     return result;
198
199   result = Curl_add_bufferf(req,
200                             "Connection: Upgrade, HTTP2-Settings\r\n"
201                             "Upgrade: %s\r\n"
202                             "HTTP2-Settings: %s\r\n",
203                             NGHTTP2_PROTO_VERSION_ID, base64);
204   free(base64);
205
206   k->upgr101 = UPGR101_REQUESTED;
207
208   return result;
209 }
210
211 void Curl_http2_switched(struct connectdata *conn)
212 {
213   /* we are switched! */
214   conn->handler = &Curl_handler_http2;
215   infof(conn->data, "We have switched to HTTP2\n");
216 }
217
218 #endif