2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
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.
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.
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,
23 #include "private-libwebsockets.h"
25 const struct http2_settings lws_http2_default_settings = { {
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,
36 void lws_http2_init(struct http2_settings *settings)
38 memcpy(settings, lws_http2_default_settings.setting, sizeof(*settings));
42 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid)
45 if (wsi->u.http2.my_stream_id == sid)
48 wsi = wsi->u.http2.next_child_wsi;
55 lws_create_server_child_wsi(struct lws_vhost *vhost, struct lws *parent_wsi,
58 struct lws *wsi = lws_create_new_server_wsi(vhost);
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])
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;
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++;
78 wsi->u.http2.my_priority = 16;
79 wsi->u.http2.tx_credit = 65535;
81 wsi->state = LWSS_HTTP2_ESTABLISHED;
82 wsi->mode = parent_wsi->mode;
84 wsi->protocol = &vhost->protocols[0];
85 if (lws_ensure_user_space(wsi))
88 lwsl_info("%s: %p new child %p, sid %d, user_space=%p\n", __func__,
89 parent_wsi, wsi, sid, wsi->user_space);
94 vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
101 int lws_remove_server_child_wsi(struct lws_context *context, struct lws *wsi)
103 struct lws **w = &wsi->u.http2.parent_wsi;
106 *w = wsi->u.http2.next_child_wsi;
107 (wsi->u.http2.parent_wsi)->u.http2.child_count--;
111 w = &((*w)->u.http2.next_child_wsi);
114 lwsl_err("%s: can't find %p\n", __func__, wsi);
119 lws_http2_interpret_settings_payload(struct http2_settings *settings,
120 unsigned char *buf, int len)
127 if (len < LWS_HTTP2_SETTINGS_LENGTH)
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);
137 len -= LWS_HTTP2_SETTINGS_LENGTH;
138 buf += LWS_HTTP2_SETTINGS_LENGTH;
147 struct lws *lws_http2_get_network_wsi(struct lws *wsi)
149 while (wsi->u.http2.parent_wsi)
150 wsi = wsi->u.http2.parent_wsi;
155 int lws_http2_frame_write(struct lws *wsi, int type, int flags,
156 unsigned int sid, unsigned int len, unsigned char *buf)
158 struct lws *wsi_eff = lws_http2_get_network_wsi(wsi);
159 unsigned char *p = &buf[-LWS_HTTP2_FRAME_HEADER_LENGTH];
172 lwsl_info("%s: %p (eff %p). type %d, flags 0x%x, sid=%d, len=%d\n",
173 __func__, wsi, wsi_eff, type, flags, sid, len,
174 wsi->u.http2.tx_credit);
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", len,
180 wsi->u.http2.tx_credit);
181 wsi->u.http2.tx_credit -= len;
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;
192 static void lws_http2_settings_write(struct lws *wsi, int n, unsigned char *buf)
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];
202 static const char * https_client_preface =
203 "PRI * HTTP/2.0\x0d\x0a\x0d\x0aSM\x0d\x0a\x0d\x0a";
206 lws_http2_parser(struct lws *wsi, unsigned char c)
211 switch (wsi->state) {
212 case LWSS_HTTP2_AWAIT_CLIENT_PREFACE:
213 if (https_client_preface[wsi->u.http2.count++] != c)
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;
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...
228 lws_set_protocol_write_pending(wsi,
229 LWS_PPS_HTTP2_MY_SETTINGS);
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))
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__);
257 if (lws_hpack_interpret(wsi->u.http2.stream_wsi, c)) {
258 lwsl_notice("%s: lws_hpack_interpret failed\n", __func__);
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);
270 wsi->u.http2.GOING_AWAY = 1;
272 case LWS_HTTP2_FRAME_TYPE_DATA:
274 case LWS_HTTP2_FRAME_TYPE_PRIORITY:
276 case LWS_HTTP2_FRAME_TYPE_RST_STREAM:
278 case LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE:
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)
285 wsi->u.http2.ping_payload[wsi->u.http2.count - 1] = c;
288 case LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE:
289 wsi->u.http2.hpack_e_dep <<= 8;
290 wsi->u.http2.hpack_e_dep |= c;
293 if (wsi->u.http2.count != wsi->u.http2.length)
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;
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);
313 lwsl_info(" action result %d\n", n);
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);
321 case LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE:
322 wsi->u.http2.hpack_e_dep &= ~(1 << 31);
323 if ((long long)swsi->u.http2.tx_credit + (unsigned long long)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);
335 switch (wsi->u.http2.frame_state++) {
337 wsi->u.http2.length = c;
341 wsi->u.http2.length <<= 8;
342 wsi->u.http2.length |= c;
345 wsi->u.http2.type = c;
348 wsi->u.http2.flags = c;
354 wsi->u.http2.stream_id <<= 8;
355 wsi->u.http2.stream_id |= c;
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;
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);
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)
373 if (wsi->u.http2.flags & LWS_HTTP2_FLAG_SETTINGS_ACK) { // ack
375 /* non-ACK coming in means we must ACK it */
376 lws_set_protocol_write_pending(wsi, LWS_PPS_HTTP2_ACK_SETTINGS);
378 case LWS_HTTP2_FRAME_TYPE_PING:
379 if (wsi->u.http2.stream_id)
381 if (wsi->u.http2.length != 8)
384 case LWS_HTTP2_FRAME_TYPE_CONTINUATION:
385 if (wsi->u.http2.END_HEADERS)
387 goto update_end_headers;
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)
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;
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);
403 /* no END_HEADERS means CONTINUATION must come */
404 wsi->u.http2.END_HEADERS = !!(wsi->u.http2.flags & LWS_HTTP2_FLAG_END_HEADERS);
406 swsi = wsi->u.http2.stream_wsi;
411 /* prepare the hpack parser at the right start */
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;
417 if (swsi->u.http2.flags & LWS_HTTP2_FLAG_PADDED)
418 swsi->u.http2.hpack = HPKS_OPT_PADDING;
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;
424 swsi->u.http2.hpack = HPKS_TYPE;
425 lwsl_info("initial hpack state %d\n", swsi->u.http2.hpack);
427 case LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE:
428 if (wsi->u.http2.length != 4)
432 if (wsi->u.http2.length == 0)
433 wsi->u.http2.frame_state = 0;
442 int lws_http2_do_pps_send(struct lws_context *context, struct lws *wsi)
444 unsigned char settings[LWS_PRE + 6 * LWS_HTTP2_SETTINGS__COUNT];
448 lwsl_debug("%s: %p: %d\n", __func__, wsi, 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);
458 n = lws_http2_frame_write(wsi, LWS_HTTP2_FRAME_TYPE_SETTINGS,
459 0, LWS_HTTP2_STREAM_ID_MASTER, m,
462 lwsl_info("send %d %d\n", n, m);
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,
472 lwsl_err("ack tells %d\n", n);
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;
479 wsi->u.http.fd = LWS_INVALID_FILE;
481 if (lws_is_ssl(lws_http2_get_network_wsi(wsi))) {
482 lwsl_info("skipping nonexistant ssl upgrade headers\n");
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
491 lwsl_info("%s: setting up sid 1\n", __func__);
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;
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);
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,
516 lwsl_info("send %d %d\n", n, m);
527 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n)
530 wsi = wsi->u.http2.next_child_wsi;