http2: adjusted to newer nghttp2_session_callbacks struct
[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 /*
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_plain(conn->sock[FIRSTSOCKET], (char *)buf, length,
115                                 &nread);
116   (void)h2;
117   (void)flags;
118
119   if(rc) {
120     failf(conn->data, "Failed receiving HTTP2 data");
121     return NGHTTP2_ERR_CALLBACK_FAILURE;
122   }
123   if(!nread)
124     return NGHTTP2_ERR_WOULDBLOCK;
125
126   return nread;
127 }
128
129 /*
130  * This is all callbacks nghttp2 calls
131  */
132 static const nghttp2_session_callbacks callbacks = {
133   send_callback,
134   recv_callback,
135   NULL,
136   NULL,
137   NULL,
138   NULL,
139   NULL,
140   NULL,
141   NULL,
142   NULL,
143   NULL,
144   NULL
145 };
146
147 /*
148  * The HTTP2 settings we send in the Upgrade request
149  */
150 static nghttp2_settings_entry settings[] = {
151   { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
152   { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
153 };
154
155 /*
156  * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
157  */
158 CURLcode Curl_http2_request(Curl_send_buffer *req,
159                             struct connectdata *conn)
160 {
161   uint8_t binsettings[80];
162   CURLcode result;
163   ssize_t binlen;
164   char *base64;
165   size_t blen;
166   struct SingleRequest *k = &conn->data->req;
167
168   if(!conn->proto.httpc.h2) {
169     /* The nghttp2 session is not yet setup, do it */
170     int rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
171                                         &callbacks, conn);
172     if(rc) {
173       failf(conn->data, "Couldn't initialize nghttp2!");
174       return CURLE_OUT_OF_MEMORY; /* most likely at least */
175     }
176   }
177
178   /* As long as we have a fixed set of settings, we don't have to dynamically
179    * figure out the base64 strings since it'll always be the same. However,
180    * the settings will likely not be fixed every time in the future.
181    */
182
183   /* this returns number of bytes it wrote */
184   binlen = nghttp2_pack_settings_payload(binsettings,
185                                          sizeof(binsettings),
186                                          settings,
187                                          sizeof(settings)/sizeof(settings[0]));
188   if(!binlen) {
189     failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
190     return CURLE_FAILED_INIT;
191   }
192
193   result = Curl_base64_encode(conn->data, (const char *)binsettings, binlen,
194                               &base64, &blen);
195   if(result)
196     return result;
197
198   result = Curl_add_bufferf(req,
199                             "Connection: Upgrade, HTTP2-Settings\r\n"
200                             "Upgrade: %s\r\n"
201                             "HTTP2-Settings: %s\r\n",
202                             NGHTTP2_PROTO_VERSION_ID, base64);
203   free(base64);
204
205   k->upgr101 = UPGR101_REQUESTED;
206
207   return result;
208 }
209
210 /*
211  * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
212  * a regular CURLcode value.
213  */
214 static ssize_t http2_recv(struct connectdata *conn, int sockindex,
215                           char *mem, size_t len, CURLcode *err)
216 {
217   int rc;
218   (void)sockindex; /* we always do HTTP2 on sockindex 0 */
219
220   conn->proto.httpc.mem = mem;
221   conn->proto.httpc.size = len;
222
223   rc = nghttp2_session_recv(conn->proto.httpc.h2);
224
225   if(rc < 0) {
226     *err = CURLE_RECV_ERROR;
227   }
228   return 0;
229 }
230
231 /* return number of received (decrypted) bytes */
232 static ssize_t http2_send(struct connectdata *conn, int sockindex,
233                           const void *mem, size_t len, CURLcode *err)
234 {
235   /* TODO: proper implementation */
236   (void)conn;
237   (void)sockindex;
238   (void)mem;
239   (void)len;
240   (void)err;
241   return 0;
242 }
243
244 void Curl_http2_switched(struct connectdata *conn)
245 {
246   /* we are switched! */
247   conn->handler = &Curl_handler_http2;
248   conn->recv[FIRSTSOCKET] = http2_recv;
249   conn->send[FIRSTSOCKET] = http2_send;
250   infof(conn->data, "We have switched to HTTP2\n");
251 }
252
253 #endif