lws_intptr_t
[platform/upstream/libwebsockets.git] / lib / http2.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22
23 #include "private-libwebsockets.h"
24
25 const struct http2_settings lws_http2_default_settings = { {
26         0,
27         /* LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE */             4096,
28         /* LWS_HTTP2_SETTINGS__ENABLE_PUSH */                      1,
29         /* LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS */         100,
30         /* LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE */          65535,
31         /* LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE */               16384,
32         /* LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE */            ~0,
33 }};
34
35
36 void lws_http2_init(struct http2_settings *settings)
37 {
38         memcpy(settings, lws_http2_default_settings.setting, sizeof(*settings));
39 }
40
41 struct lws *
42 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid)
43 {
44         do {
45                 if (wsi->u.http2.my_stream_id == sid)
46                         return wsi;
47
48                 wsi = wsi->u.http2.next_child_wsi;
49         } while (wsi);
50
51         return NULL;
52 }
53
54 struct lws *
55 lws_create_server_child_wsi(struct lws_vhost *vhost, struct lws *parent_wsi,
56                             unsigned int sid)
57 {
58         struct lws *wsi = lws_create_new_server_wsi(vhost);
59
60         if (!wsi)
61                 return NULL;
62
63         /* no more children allowed by parent */
64         if (parent_wsi->u.http2.child_count + 1 ==
65             parent_wsi->u.http2.peer_settings.setting[
66                         LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS])
67                 goto bail;
68         lws_http2_init(&wsi->u.http2.peer_settings);
69         lws_http2_init(&wsi->u.http2.my_settings);
70         wsi->u.http2.stream_id = sid;
71         wsi->u.http2.my_stream_id = sid;
72
73         wsi->u.http2.parent_wsi = parent_wsi;
74         wsi->u.http2.next_child_wsi = parent_wsi->u.http2.next_child_wsi;
75         parent_wsi->u.http2.next_child_wsi = wsi;
76         parent_wsi->u.http2.child_count++;
77
78         wsi->u.http2.my_priority = 16;
79         wsi->u.http2.tx_credit = 65535;
80
81         wsi->state = LWSS_HTTP2_ESTABLISHED;
82         wsi->mode = parent_wsi->mode;
83
84         wsi->protocol = &vhost->protocols[0];
85         if (lws_ensure_user_space(wsi))
86                 goto bail;
87
88         lwsl_info("%s: %p new child %p, sid %d, user_space=%p\n", __func__,
89                   parent_wsi, wsi, sid, wsi->user_space);
90
91         return wsi;
92
93 bail:
94         vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
95                                NULL, NULL, 0);
96         lws_free(wsi);
97
98         return NULL;
99 }
100
101 int lws_remove_server_child_wsi(struct lws_context *context, struct lws *wsi)
102 {
103         struct lws **w = &wsi->u.http2.parent_wsi;
104         do {
105                 if (*w == wsi) {
106                         *w = wsi->u.http2.next_child_wsi;
107                         (wsi->u.http2.parent_wsi)->u.http2.child_count--;
108                         return 0;
109                 }
110
111                 w = &((*w)->u.http2.next_child_wsi);
112         } while (*w);
113
114         lwsl_err("%s: can't find %p\n", __func__, wsi);
115         return 1;
116 }
117
118 int
119 lws_http2_interpret_settings_payload(struct http2_settings *settings,
120                                      unsigned char *buf, int len)
121 {
122         unsigned int a, b;
123
124         if (!len)
125                 return 0;
126
127         if (len < LWS_HTTP2_SETTINGS_LENGTH)
128                 return 1;
129
130         while (len >= LWS_HTTP2_SETTINGS_LENGTH) {
131                 a = (buf[0] << 8) | buf[1];
132                 if (a < LWS_HTTP2_SETTINGS__COUNT) {
133                         b = buf[2] << 24 | buf[3] << 16 | buf[4] << 8 | buf[5];
134                         settings->setting[a] = b;
135                         lwsl_info("http2 settings %d <- 0x%x\n", a, b);
136                 }
137                 len -= LWS_HTTP2_SETTINGS_LENGTH;
138                 buf += LWS_HTTP2_SETTINGS_LENGTH;
139         }
140
141         if (len)
142                 return 1;
143
144         return 0;
145 }
146
147 struct lws *lws_http2_get_network_wsi(struct lws *wsi)
148 {
149         while (wsi->u.http2.parent_wsi)
150                 wsi = wsi->u.http2.parent_wsi;
151
152         return wsi;
153 }
154
155 int lws_http2_frame_write(struct lws *wsi, int type, int flags,
156                           unsigned int sid, unsigned int len, unsigned char *buf)
157 {
158         struct lws *wsi_eff = lws_http2_get_network_wsi(wsi);
159         unsigned char *p = &buf[-LWS_HTTP2_FRAME_HEADER_LENGTH];
160         int n;
161
162         *p++ = len >> 16;
163         *p++ = len >> 8;
164         *p++ = len;
165         *p++ = type;
166         *p++ = flags;
167         *p++ = sid >> 24;
168         *p++ = sid >> 16;
169         *p++ = sid >> 8;
170         *p++ = sid;
171
172         lwsl_info("%s: %p (eff %p). type %d, flags 0x%x, sid=%d, len=%d, tx_credit=%d\n",
173                   __func__, wsi, wsi_eff, type, flags, sid, len,
174                   wsi->u.http2.tx_credit);
175
176         if (type == LWS_HTTP2_FRAME_TYPE_DATA) {
177                 if (wsi->u.http2.tx_credit < len)
178                         lwsl_err("%s: %p: sending payload len %d"
179                                  " but tx_credit only %d!\n", __func__, wsi, len,
180                                  wsi->u.http2.tx_credit);
181                 wsi->u.http2.tx_credit -= len;
182         }
183
184         n = lws_issue_raw(wsi_eff, &buf[-LWS_HTTP2_FRAME_HEADER_LENGTH],
185                           len + LWS_HTTP2_FRAME_HEADER_LENGTH);
186         if (n >= LWS_HTTP2_FRAME_HEADER_LENGTH)
187                 return n - LWS_HTTP2_FRAME_HEADER_LENGTH;
188
189         return n;
190 }
191
192 static void lws_http2_settings_write(struct lws *wsi, int n, unsigned char *buf)
193 {
194         *buf++ = n >> 8;
195         *buf++ = n;
196         *buf++ = wsi->u.http2.my_settings.setting[n] >> 24;
197         *buf++ = wsi->u.http2.my_settings.setting[n] >> 16;
198         *buf++ = wsi->u.http2.my_settings.setting[n] >> 8;
199         *buf = wsi->u.http2.my_settings.setting[n];
200 }
201
202 static const char * https_client_preface =
203         "PRI * HTTP/2.0\x0d\x0a\x0d\x0aSM\x0d\x0a\x0d\x0a";
204
205 int
206 lws_http2_parser(struct lws *wsi, unsigned char c)
207 {
208         struct lws *swsi;
209         int n;
210
211         switch (wsi->state) {
212         case LWSS_HTTP2_AWAIT_CLIENT_PREFACE:
213                 if (https_client_preface[wsi->u.http2.count++] != c)
214                         return 1;
215
216                 if (!https_client_preface[wsi->u.http2.count]) {
217                         lwsl_info("http2: %p: established\n", wsi);
218                         wsi->state = LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS;
219                         wsi->u.http2.count = 0;
220                         wsi->u.http2.tx_credit = 65535;
221
222                         /*
223                          * we must send a settings frame -- empty one is OK...
224                          * that must be the first thing sent by server
225                          * and the peer must send a SETTINGS with ACK flag...
226                          */
227
228                         lws_set_protocol_write_pending(wsi,
229                                                        LWS_PPS_HTTP2_MY_SETTINGS);
230                 }
231                 break;
232
233         case LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS:
234         case LWSS_HTTP2_ESTABLISHED:
235                 if (wsi->u.http2.frame_state == LWS_HTTP2_FRAME_HEADER_LENGTH) { // payload
236                         wsi->u.http2.count++;
237                         wsi->u.http2.stream_wsi->u.http2.count = wsi->u.http2.count;
238                         /* applies to wsi->u.http2.stream_wsi which may be wsi*/
239                         switch(wsi->u.http2.type) {
240                         case LWS_HTTP2_FRAME_TYPE_SETTINGS:
241                                 wsi->u.http2.stream_wsi->u.http2.one_setting[wsi->u.http2.count % LWS_HTTP2_SETTINGS_LENGTH] = c;
242                                 if (wsi->u.http2.count % LWS_HTTP2_SETTINGS_LENGTH == LWS_HTTP2_SETTINGS_LENGTH - 1)
243                                         if (lws_http2_interpret_settings_payload(
244                                              &wsi->u.http2.stream_wsi->u.http2.peer_settings,
245                                              wsi->u.http2.one_setting,
246                                              LWS_HTTP2_SETTINGS_LENGTH))
247                                                 return 1;
248                                 break;
249                         case LWS_HTTP2_FRAME_TYPE_CONTINUATION:
250                         case LWS_HTTP2_FRAME_TYPE_HEADERS:
251                                 lwsl_info(" %02X\n", c);
252                                 if (!wsi->u.http2.stream_wsi->u.hdr.ah)
253                                         if (lws_header_table_attach(wsi->u.http2.stream_wsi, 0)) {
254                                                 lwsl_err("%s: Failed to get ah\n", __func__);
255                                                 return 1;
256                                         }
257                                 if (lws_hpack_interpret(wsi->u.http2.stream_wsi, c)) {
258                                         lwsl_notice("%s: lws_hpack_interpret failed\n", __func__);
259                                         return 1;
260                                 }
261                                 break;
262                         case LWS_HTTP2_FRAME_TYPE_GOAWAY:
263                                 if (wsi->u.http2.count >= 5 && wsi->u.http2.count <= 8) {
264                                         wsi->u.http2.hpack_e_dep <<= 8;
265                                         wsi->u.http2.hpack_e_dep |= c;
266                                         if (wsi->u.http2.count == 8) {
267                                                 lwsl_info("goaway err 0x%x\n", wsi->u.http2.hpack_e_dep);
268                                         }
269                                 }
270                                 wsi->u.http2.GOING_AWAY = 1;
271                                 break;
272                         case LWS_HTTP2_FRAME_TYPE_DATA:
273                                 break;
274                         case LWS_HTTP2_FRAME_TYPE_PRIORITY:
275                                 break;
276                         case LWS_HTTP2_FRAME_TYPE_RST_STREAM:
277                                 break;
278                         case LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE:
279                                 break;
280                         case LWS_HTTP2_FRAME_TYPE_PING:
281                                 if (wsi->u.http2.flags & LWS_HTTP2_FLAG_SETTINGS_ACK) { // ack
282                                 } else { /* they're sending us a ping request */
283                                         if (wsi->u.http2.count > 8)
284                                                 return 1;
285                                         wsi->u.http2.ping_payload[wsi->u.http2.count - 1] = c;
286                                 }
287                                 break;
288                         case LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE:
289                                 wsi->u.http2.hpack_e_dep <<= 8;
290                                 wsi->u.http2.hpack_e_dep |= c;
291                                 break;
292                         }
293                         if (wsi->u.http2.count != wsi->u.http2.length)
294                                 break;
295
296                         /* end of frame */
297
298                         wsi->u.http2.frame_state = 0;
299                         wsi->u.http2.count = 0;
300                         swsi = wsi->u.http2.stream_wsi;
301                         /* set our initial window size */
302                         if (!wsi->u.http2.initialized) {
303                                 wsi->u.http2.tx_credit = wsi->u.http2.peer_settings.setting[LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE];
304                                 lwsl_info("initial tx credit on master conn %p: %d\n", wsi, wsi->u.http2.tx_credit);
305                                 wsi->u.http2.initialized = 1;
306                         }
307                         switch (wsi->u.http2.type) {
308                         case LWS_HTTP2_FRAME_TYPE_HEADERS:
309                                 /* service the http request itself */
310                                 lwsl_info("servicing initial http request, wsi=%p, stream wsi=%p\n", wsi, wsi->u.http2.stream_wsi);
311                                 n = lws_http_action(swsi);
312                                 (void)n;
313                                 lwsl_info("  action result %d\n", n);
314                                 break;
315                         case LWS_HTTP2_FRAME_TYPE_PING:
316                                 if (wsi->u.http2.flags & LWS_HTTP2_FLAG_SETTINGS_ACK) { // ack
317                                 } else { /* they're sending us a ping request */
318                                         lws_set_protocol_write_pending(wsi, LWS_PPS_HTTP2_PONG);
319                                 }
320                                 break;
321                         case LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE:
322                                 wsi->u.http2.hpack_e_dep &= ~(1 << 31);
323                                 if ((lws_intptr_t)swsi->u.http2.tx_credit + (lws_intptr_t)wsi->u.http2.hpack_e_dep > (~(1 << 31)))
324                                         return 1; /* actually need to close swsi not the whole show */
325                                 swsi->u.http2.tx_credit += wsi->u.http2.hpack_e_dep;
326                                 if (swsi->u.http2.waiting_tx_credit && swsi->u.http2.tx_credit > 0) {
327                                         lwsl_info("%s: %p: waiting_tx_credit -> wait on writeable\n", __func__, wsi);
328                                         swsi->u.http2.waiting_tx_credit = 0;
329                                         lws_callback_on_writable(swsi);
330                                 }
331                                 break;
332                         }
333                         break;
334                 }
335                 switch (wsi->u.http2.frame_state++) {
336                 case 0:
337                         wsi->u.http2.length = c;
338                         break;
339                 case 1:
340                 case 2:
341                         wsi->u.http2.length <<= 8;
342                         wsi->u.http2.length |= c;
343                         break;
344                 case 3:
345                         wsi->u.http2.type = c;
346                         break;
347                 case 4:
348                         wsi->u.http2.flags = c;
349                         break;
350                 case 5:
351                 case 6:
352                 case 7:
353                 case 8:
354                         wsi->u.http2.stream_id <<= 8;
355                         wsi->u.http2.stream_id |= c;
356                         break;
357                 }
358                 if (wsi->u.http2.frame_state == LWS_HTTP2_FRAME_HEADER_LENGTH) { /* frame header complete */
359                         lwsl_info("frame: type 0x%x, flags 0x%x, sid 0x%x, len 0x%x\n",
360                                   wsi->u.http2.type, wsi->u.http2.flags, wsi->u.http2.stream_id, wsi->u.http2.length);
361                         wsi->u.http2.count = 0;
362
363                         wsi->u.http2.stream_wsi = wsi;
364                         if (wsi->u.http2.stream_id)
365                                 wsi->u.http2.stream_wsi = lws_http2_wsi_from_id(wsi, wsi->u.http2.stream_id);
366
367                         switch (wsi->u.http2.type) {
368                         case LWS_HTTP2_FRAME_TYPE_SETTINGS:
369                                 /* nonzero sid on settings is illegal */
370                                 if (wsi->u.http2.stream_id)
371                                         return 1;
372
373                                 if (wsi->u.http2.flags & LWS_HTTP2_FLAG_SETTINGS_ACK) { // ack
374                                 } else
375                                         /* non-ACK coming in means we must ACK it */
376                                         lws_set_protocol_write_pending(wsi, LWS_PPS_HTTP2_ACK_SETTINGS);
377                                 break;
378                         case LWS_HTTP2_FRAME_TYPE_PING:
379                                 if (wsi->u.http2.stream_id)
380                                         return 1;
381                                 if (wsi->u.http2.length != 8)
382                                         return 1;
383                                 break;
384                         case LWS_HTTP2_FRAME_TYPE_CONTINUATION:
385                                 if (wsi->u.http2.END_HEADERS)
386                                         return 1;
387                                 goto update_end_headers;
388
389                         case LWS_HTTP2_FRAME_TYPE_HEADERS:
390                                 lwsl_info("LWS_HTTP2_FRAME_TYPE_HEADERS: stream_id = %d\n", wsi->u.http2.stream_id);
391                                 if (!wsi->u.http2.stream_id)
392                                         return 1;
393                                 if (!wsi->u.http2.stream_wsi) {
394                                         wsi->u.http2.stream_wsi =
395                                                 lws_create_server_child_wsi(wsi->vhost, wsi, wsi->u.http2.stream_id);
396                                         wsi->u.http2.stream_wsi->http2_substream = 1;
397                                 }
398
399                                 /* END_STREAM means after servicing this, close the stream */
400                                 wsi->u.http2.END_STREAM = !!(wsi->u.http2.flags & LWS_HTTP2_FLAG_END_STREAM);
401                                 lwsl_info("%s: headers END_STREAM = %d\n",__func__, wsi->u.http2.END_STREAM);
402 update_end_headers:
403                                 /* no END_HEADERS means CONTINUATION must come */
404                                 wsi->u.http2.END_HEADERS = !!(wsi->u.http2.flags & LWS_HTTP2_FLAG_END_HEADERS);
405
406                                 swsi = wsi->u.http2.stream_wsi;
407                                 if (!swsi)
408                                         return 1;
409
410
411                                 /* prepare the hpack parser at the right start */
412
413                                 swsi->u.http2.flags = wsi->u.http2.flags;
414                                 swsi->u.http2.length = wsi->u.http2.length;
415                                 swsi->u.http2.END_STREAM = wsi->u.http2.END_STREAM;
416
417                                 if (swsi->u.http2.flags & LWS_HTTP2_FLAG_PADDED)
418                                         swsi->u.http2.hpack = HPKS_OPT_PADDING;
419                                 else
420                                         if (swsi->u.http2.flags & LWS_HTTP2_FLAG_PRIORITY) {
421                                                 swsi->u.http2.hpack = HKPS_OPT_E_DEPENDENCY;
422                                                 swsi->u.http2.hpack_m = 4;
423                                         } else
424                                                 swsi->u.http2.hpack = HPKS_TYPE;
425                                 lwsl_info("initial hpack state %d\n", swsi->u.http2.hpack);
426                                 break;
427                         case LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE:
428                                 if (wsi->u.http2.length != 4)
429                                         return 1;
430                                 break;
431                         }
432                         if (wsi->u.http2.length == 0)
433                                 wsi->u.http2.frame_state = 0;
434
435                 }
436                 break;
437         }
438
439         return 0;
440 }
441
442 int lws_http2_do_pps_send(struct lws_context *context, struct lws *wsi)
443 {
444         unsigned char settings[LWS_PRE + 6 * LWS_HTTP2_SETTINGS__COUNT];
445         struct lws *swsi;
446         int n, m = 0;
447
448         lwsl_debug("%s: %p: %d\n", __func__, wsi, wsi->pps);
449
450         switch (wsi->pps) {
451         case LWS_PPS_HTTP2_MY_SETTINGS:
452                 for (n = 1; n < LWS_HTTP2_SETTINGS__COUNT; n++)
453                         if (wsi->u.http2.my_settings.setting[n] != lws_http2_default_settings.setting[n]) {
454                                 lws_http2_settings_write(wsi, n,
455                                                          &settings[LWS_PRE + m]);
456                                 m += sizeof(wsi->u.http2.one_setting);
457                         }
458                 n = lws_http2_frame_write(wsi, LWS_HTTP2_FRAME_TYPE_SETTINGS,
459                                           0, LWS_HTTP2_STREAM_ID_MASTER, m,
460                                           &settings[LWS_PRE]);
461                 if (n != m) {
462                         lwsl_info("send %d %d\n", n, m);
463                         return 1;
464                 }
465                 break;
466         case LWS_PPS_HTTP2_ACK_SETTINGS:
467                 /* send ack ... always empty */
468                 n = lws_http2_frame_write(wsi, LWS_HTTP2_FRAME_TYPE_SETTINGS,
469                         1, LWS_HTTP2_STREAM_ID_MASTER, 0,
470                         &settings[LWS_PRE]);
471                 if (n) {
472                         lwsl_err("ack tells %d\n", n);
473                         return 1;
474                 }
475                 /* this is the end of the preface dance then? */
476                 if (wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS) {
477                         wsi->state = LWSS_HTTP2_ESTABLISHED;
478
479                         wsi->u.http.fop_fd = NULL;
480
481                         if (lws_is_ssl(lws_http2_get_network_wsi(wsi))) {
482                                 lwsl_info("skipping nonexistent ssl upgrade headers\n");
483                                 break;
484                         }
485
486                         /*
487                          * we need to treat the headers from this upgrade
488                          * as the first job.  These need to get
489                          * shifted to stream ID 1
490                          */
491                         lwsl_info("%s: setting up sid 1\n", __func__);
492
493                         swsi = wsi->u.http2.stream_wsi =
494                                         lws_create_server_child_wsi(wsi->vhost, wsi, 1);
495                         /* pass on the initial headers to SID 1 */
496                         swsi->u.http.ah = wsi->u.http.ah;
497                         wsi->u.http.ah = NULL;
498
499                         lwsl_info("%s: inherited headers %p\n", __func__, swsi->u.http.ah);
500                         swsi->u.http2.tx_credit = wsi->u.http2.peer_settings.setting[LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE];
501                         lwsl_info("initial tx credit on conn %p: %d\n", swsi, swsi->u.http2.tx_credit);
502                         swsi->u.http2.initialized = 1;
503                         /* demanded by HTTP2 */
504                         swsi->u.http2.END_STREAM = 1;
505                         lwsl_info("servicing initial http request\n");
506                         return lws_http_action(swsi);
507                 }
508                 break;
509         case LWS_PPS_HTTP2_PONG:
510                 memcpy(&settings[LWS_PRE], wsi->u.http2.ping_payload, 8);
511                 n = lws_http2_frame_write(wsi, LWS_HTTP2_FRAME_TYPE_PING,
512                                           LWS_HTTP2_FLAG_SETTINGS_ACK,
513                                           LWS_HTTP2_STREAM_ID_MASTER, 8,
514                                           &settings[LWS_PRE]);
515                 if (n != 8) {
516                         lwsl_info("send %d %d\n", n, m);
517                         return 1;
518                 }
519                 break;
520         default:
521                 break;
522         }
523
524         return 0;
525 }
526
527 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n)
528 {
529         do {
530                 wsi = wsi->u.http2.next_child_wsi;
531                 if (!wsi)
532                         return NULL;
533         } while (n--);
534
535         return wsi;
536 }