HTTP2: reject nghttp2 versions before 0.3.0
[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
37 /* include memdebug.h last */
38 #include "memdebug.h"
39
40 #if (NGHTTP2_VERSION_NUM < 0x000300)
41 #error too old nghttp2 version, upgrade!
42 #endif
43
44 /*
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
47  * HTTP to HTTP2.
48  */
49 const struct Curl_handler Curl_handler_http2 = {
50   "HTTP2",                              /* scheme */
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 */
65   0,                                    /* protocol */
66   PROTOPT_NONE                          /* flags */
67 };
68
69
70 /*
71  * Store nghttp2 version info in this buffer, Prefix with a space.  Return
72  * total length written.
73  */
74 int Curl_http2_ver(char *p, size_t len)
75 {
76   nghttp2_info *h2 = nghttp2_version(0);
77   return snprintf(p, len, " nghttp2/%s", h2->version_str);
78 }
79
80 /*
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.
84  */
85 static ssize_t send_callback(nghttp2_session *h2,
86                              const uint8_t *data, size_t length, int flags,
87                              void *userp)
88 {
89   struct connectdata *conn = (struct connectdata *)userp;
90   ssize_t written;
91   CURLcode rc =
92     Curl_write(conn, conn->sock[0], data, length, &written);
93   (void)h2;
94   (void)flags;
95
96   if(rc) {
97     failf(conn->data, "Failed sending HTTP2 data");
98     return NGHTTP2_ERR_CALLBACK_FAILURE;
99   }
100   else if(!written)
101     return NGHTTP2_ERR_WOULDBLOCK;
102
103   return written;
104 }
105
106 /*
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.
111  */
112 static ssize_t recv_callback(nghttp2_session *h2,
113                              uint8_t *buf, size_t length, int flags,
114                              void *userp)
115 {
116   struct connectdata *conn = (struct connectdata *)userp;
117   ssize_t nread;
118   CURLcode rc = Curl_read_plain(conn->sock[FIRSTSOCKET], (char *)buf, length,
119                                 &nread);
120   (void)h2;
121   (void)flags;
122
123   if(rc) {
124     failf(conn->data, "Failed receiving HTTP2 data");
125     return NGHTTP2_ERR_CALLBACK_FAILURE;
126   }
127   if(!nread)
128     return NGHTTP2_ERR_WOULDBLOCK;
129
130   return nread;
131 }
132
133 /*
134  * This is all callbacks nghttp2 calls
135  */
136 static const nghttp2_session_callbacks callbacks = {
137   send_callback,
138   recv_callback,
139   NULL,
140   NULL,
141   NULL,
142   NULL,
143   NULL,
144   NULL,
145   NULL,
146   NULL,
147   NULL,
148   NULL
149 };
150
151 /*
152  * The HTTP2 settings we send in the Upgrade request
153  */
154 static nghttp2_settings_entry settings[] = {
155   { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
156   { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
157 };
158
159 /*
160  * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
161  */
162 CURLcode Curl_http2_request(Curl_send_buffer *req,
163                             struct connectdata *conn)
164 {
165   uint8_t binsettings[80];
166   CURLcode result;
167   ssize_t binlen;
168   char *base64;
169   size_t blen;
170   struct SingleRequest *k = &conn->data->req;
171
172   if(!conn->proto.httpc.h2) {
173     /* The nghttp2 session is not yet setup, do it */
174     int rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
175                                         &callbacks, conn);
176     if(rc) {
177       failf(conn->data, "Couldn't initialize nghttp2!");
178       return CURLE_OUT_OF_MEMORY; /* most likely at least */
179     }
180   }
181
182   /* As long as we have a fixed set of settings, we don't have to dynamically
183    * figure out the base64 strings since it'll always be the same. However,
184    * the settings will likely not be fixed every time in the future.
185    */
186
187   /* this returns number of bytes it wrote */
188   binlen = nghttp2_pack_settings_payload(binsettings,
189                                          sizeof(binsettings),
190                                          settings,
191                                          sizeof(settings)/sizeof(settings[0]));
192   if(!binlen) {
193     failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
194     return CURLE_FAILED_INIT;
195   }
196
197   result = Curl_base64_encode(conn->data, (const char *)binsettings, binlen,
198                               &base64, &blen);
199   if(result)
200     return result;
201
202   result = Curl_add_bufferf(req,
203                             "Connection: Upgrade, HTTP2-Settings\r\n"
204                             "Upgrade: %s\r\n"
205                             "HTTP2-Settings: %s\r\n",
206                             NGHTTP2_PROTO_VERSION_ID, base64);
207   free(base64);
208
209   k->upgr101 = UPGR101_REQUESTED;
210
211   return result;
212 }
213
214 /*
215  * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
216  * a regular CURLcode value.
217  */
218 static ssize_t http2_recv(struct connectdata *conn, int sockindex,
219                           char *mem, size_t len, CURLcode *err)
220 {
221   int rc;
222   (void)sockindex; /* we always do HTTP2 on sockindex 0 */
223
224   conn->proto.httpc.mem = mem;
225   conn->proto.httpc.size = len;
226
227   rc = nghttp2_session_recv(conn->proto.httpc.h2);
228
229   if(rc < 0) {
230     *err = CURLE_RECV_ERROR;
231   }
232   return 0;
233 }
234
235 /* return number of received (decrypted) bytes */
236 static ssize_t http2_send(struct connectdata *conn, int sockindex,
237                           const void *mem, size_t len, CURLcode *err)
238 {
239   /* TODO: proper implementation */
240   (void)conn;
241   (void)sockindex;
242   (void)mem;
243   (void)len;
244   (void)err;
245   return 0;
246 }
247
248 void Curl_http2_switched(struct connectdata *conn)
249 {
250   /* we are switched! */
251   conn->handler = &Curl_handler_http2;
252   conn->recv[FIRSTSOCKET] = http2_recv;
253   conn->send[FIRSTSOCKET] = http2_send;
254   infof(conn->data, "We have switched to HTTP2\n");
255 }
256
257 #endif