/*
* libwebsockets - small server side websockets and web server implementation
*
- * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
+ * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* Sec-WebSocket-Protocol: chat
*/
+#ifndef min
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
/*
* We have to take care about parsing because the headers may be split
* into multiple fragments. They may contain unknown headers with arbitrary
* argument lengths. So, we parse using a single-character at a time state
* machine that is completely independent of packet size.
+ *
+ * Returns <0 for error or length of chars consumed from buf (up to len)
*/
LWS_VISIBLE int
-libwebsocket_read(struct libwebsocket_context *context,
- struct libwebsocket *wsi, unsigned char *buf, size_t len)
+lws_read(struct lws *wsi, unsigned char *buf, lws_filepos_t len)
{
+ unsigned char *last_char, *oldbuf = buf;
+ lws_filepos_t body_chunk_len;
size_t n;
-#ifndef LWS_NO_SERVER
- struct allocated_headers *ah;
- char *uri_ptr = NULL;
- int uri_len = 0;
- char content_length_str[32];
-#endif
-
- switch (wsi->state) {
-
- case WSI_STATE_HTTP_BODY:
-#ifndef LWS_NO_SERVER
-http_postbody:
-#endif
- while (len--) {
-
- if (wsi->u.http.content_length_seen >= wsi->u.http.content_length)
- break;
-
- wsi->u.http.post_buffer[wsi->u.http.body_index++] = *buf++;
- wsi->u.http.content_length_seen++;
- n = wsi->protocol->rx_buffer_size;
- if (!n)
- n = LWS_MAX_SOCKET_IO_BUF;
- if (wsi->u.http.body_index != n &&
- wsi->u.http.content_length_seen != wsi->u.http.content_length)
- continue;
+ lwsl_debug("%s: incoming len %d state %d\n", __func__, (int)len, wsi->state);
- if (wsi->protocol->callback) {
- n = wsi->protocol->callback(
- wsi->protocol->owning_server, wsi,
- LWS_CALLBACK_HTTP_BODY,
- wsi->user_space, wsi->u.http.post_buffer,
- wsi->u.http.body_index);
- wsi->u.http.body_index = 0;
- if (n)
- goto bail;
- }
+ switch (wsi->state) {
+#ifdef LWS_USE_HTTP2
+ case LWSS_HTTP2_AWAIT_CLIENT_PREFACE:
+ case LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS:
+ case LWSS_HTTP2_ESTABLISHED:
+ n = 0;
+ while (n < len) {
+ /*
+ * we were accepting input but now we stopped doing so
+ */
+ if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
+ lws_rxflow_cache(wsi, buf, n, len);
- if (wsi->u.http.content_length_seen == wsi->u.http.content_length) {
- /* he sent the content in time */
- libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
- n = wsi->protocol->callback(
- wsi->protocol->owning_server, wsi,
- LWS_CALLBACK_HTTP_BODY_COMPLETION,
- wsi->user_space, NULL, 0);
- wsi->u.http.body_index = 0;
- if (n)
- goto bail;
+ return 1;
}
- }
-
- /*
- * we need to spill here so everything is seen in the case
- * there is no content-length
- */
- if (wsi->u.http.body_index && wsi->protocol->callback) {
- n = wsi->protocol->callback(
- wsi->protocol->owning_server, wsi,
- LWS_CALLBACK_HTTP_BODY,
- wsi->user_space, wsi->u.http.post_buffer,
- wsi->u.http.body_index);
- wsi->u.http.body_index = 0;
- if (n)
+ /* account for what we're using in rxflow buffer */
+ if (wsi->rxflow_buffer)
+ wsi->rxflow_pos++;
+ if (lws_http2_parser(wsi, buf[n++])) {
+ lwsl_debug("%s: http2_parser bailed\n", __func__);
goto bail;
+ }
}
break;
-
- case WSI_STATE_HTTP_ISSUING_FILE:
- case WSI_STATE_HTTP:
- wsi->state = WSI_STATE_HTTP_HEADERS;
- wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
- wsi->u.hdr.lextable_pos = 0;
- /* fallthru */
- case WSI_STATE_HTTP_HEADERS:
-
- lwsl_parser("issuing %d bytes to parser\n", (int)len);
-
-#ifndef LWS_NO_CLIENT
- switch (wsi->mode) {
- case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
- case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
- case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
- case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
- case LWS_CONNMODE_WS_CLIENT:
- for (n = 0; n < len; n++)
- if (libwebsocket_client_rx_sm(wsi, *buf++)) {
- lwsl_info("client_rx_sm failed\n");
- goto bail;
- }
- return 0;
- default:
- break;
- }
#endif
-#ifndef LWS_NO_SERVER
- /* LWS_CONNMODE_WS_SERVING */
-
- while (len--) {
- if (libwebsocket_parse(wsi, *buf++)) {
- lwsl_info("libwebsocket_parse failed\n");
- goto bail_nuke_ah;
- }
-
- if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
- continue;
-
- lwsl_parser("libwebsocket_parse sees parsing complete\n");
-
- wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT;
- libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
-
- /* is this websocket protocol or normal http 1.0? */
-
- if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
- !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
-
- /* it's not websocket.... shall we accept it as http? */
-
- if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) &&
- !lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
- lwsl_warn("Missing URI in HTTP request\n");
- goto bail_nuke_ah;
- }
-
- if (lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) &&
- lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
- lwsl_warn("GET and POST methods?\n");
- goto bail_nuke_ah;
- }
-
- if (libwebsocket_ensure_user_space(wsi))
- goto bail_nuke_ah;
-
- if (lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI)) {
- uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI);
- uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
- lwsl_info("HTTP GET request for '%s'\n",
- lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI));
-
- }
- if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
- lwsl_info("HTTP POST request for '%s'\n",
- lws_hdr_simple_ptr(wsi, WSI_TOKEN_POST_URI));
- uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_POST_URI);
- uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI);
- }
-
- /*
- * Hm we still need the headers so the
- * callback can look at leaders like the URI, but we
- * need to transition to http union state.... hold a
- * copy of u.hdr.ah and deallocate afterwards
- */
- ah = wsi->u.hdr.ah;
-
- /* union transition */
- memset(&wsi->u, 0, sizeof(wsi->u));
- wsi->mode = LWS_CONNMODE_HTTP_SERVING_ACCEPTED;
- wsi->state = WSI_STATE_HTTP;
- wsi->u.http.fd = LWS_INVALID_FILE;
-
- /* expose it at the same offset as u.hdr */
- wsi->u.http.ah = ah;
-
- /* HTTP header had a content length? */
-
- wsi->u.http.content_length = 0;
- if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
- wsi->u.http.content_length = 100 * 1024 * 1024;
-
- if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
- lws_hdr_copy(wsi, content_length_str,
- sizeof(content_length_str) - 1,
- WSI_TOKEN_HTTP_CONTENT_LENGTH);
- wsi->u.http.content_length = atoi(content_length_str);
- }
-
- if (wsi->u.http.content_length > 0) {
- wsi->u.http.body_index = 0;
- n = wsi->protocol->rx_buffer_size;
- if (!n)
- n = LWS_MAX_SOCKET_IO_BUF;
- wsi->u.http.post_buffer = malloc(n);
- if (!wsi->u.http.post_buffer) {
- lwsl_err("Unable to allocate post buffer\n");
- n = -1;
- goto cleanup;
- }
- }
-
- n = 0;
- if (wsi->protocol->callback)
- n = wsi->protocol->callback(context, wsi,
- LWS_CALLBACK_FILTER_HTTP_CONNECTION,
- wsi->user_space, uri_ptr, uri_len);
-
- if (!n) {
- /*
- * if there is content supposed to be coming,
- * put a timeout on it having arrived
- */
- libwebsocket_set_timeout(wsi,
- PENDING_TIMEOUT_HTTP_CONTENT,
- AWAITING_TIMEOUT);
-
- if (wsi->protocol->callback)
- n = wsi->protocol->callback(context, wsi,
- LWS_CALLBACK_HTTP,
- wsi->user_space, uri_ptr, uri_len);
- }
-
-cleanup:
- /* now drop the header info we kept a pointer to */
- if (ah)
- free(ah);
- /* not possible to continue to use past here */
- wsi->u.http.ah = NULL;
-
- if (n) {
- lwsl_info("LWS_CALLBACK_HTTP closing\n");
- goto bail; /* struct ah ptr already nuked */
- }
-
- /*
- * (if callback didn't start sending a file)
- * deal with anything else as body, whether
- * there was a content-length or not
- */
-
- if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE)
- wsi->state = WSI_STATE_HTTP_BODY;
- goto http_postbody;
- }
- if (!wsi->protocol)
- lwsl_err("NULL protocol at libwebsocket_read\n");
+ case LWSS_HTTP_ISSUING_FILE:
+ return 0;
- /*
- * It's websocket
- *
- * Make sure user side is happy about protocol
- */
-
- while (wsi->protocol->callback) {
+ case LWSS_CLIENT_HTTP_ESTABLISHED:
+ break;
- if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
- if (wsi->protocol->name == NULL)
- break;
- } else
- if (wsi->protocol->name && strcmp(
- lws_hdr_simple_ptr(wsi,
- WSI_TOKEN_PROTOCOL),
- wsi->protocol->name) == 0)
- break;
+ case LWSS_HTTP:
+ wsi->hdr_parsing_completed = 0;
+ /* fallthru */
- wsi->protocol++;
- }
+ case LWSS_HTTP_HEADERS:
+ if (!wsi->u.hdr.ah) {
+ lwsl_err("%s: LWSS_HTTP_HEADERS: NULL ah\n", __func__);
+ assert(0);
+ }
+ lwsl_parser("issuing %d bytes to parser\n", (int)len);
- /* we didn't find a protocol he wanted? */
+ if (lws_handshake_client(wsi, &buf, (size_t)len))
+ goto bail;
- if (wsi->protocol->callback == NULL) {
- if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) ==
- NULL) {
- lwsl_info("no protocol -> prot 0 handler\n");
- wsi->protocol = &context->protocols[0];
- } else {
- lwsl_err("Req protocol %s not supported\n",
- lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL));
- goto bail_nuke_ah;
- }
- }
+ last_char = buf;
+ if (lws_handshake_server(wsi, &buf, (size_t)len))
+ /* Handshake indicates this session is done. */
+ goto bail;
- /* allocate wsi->user storage */
- if (libwebsocket_ensure_user_space(wsi))
- goto bail_nuke_ah;
+ /* we might have transitioned to RAW */
+ if (wsi->mode == LWSCM_RAW)
+ /* we gave the read buffer to RAW handler already */
+ goto read_ok;
+
+ /*
+ * It's possible that we've exhausted our data already, or
+ * rx flow control has stopped us dealing with this early,
+ * but lws_handshake_server doesn't update len for us.
+ * Figure out how much was read, so that we can proceed
+ * appropriately:
+ */
+ len -= (buf - last_char);
+ lwsl_debug("%s: thinks we have used %ld\n", __func__, (long)len);
+
+ if (!wsi->hdr_parsing_completed)
+ /* More header content on the way */
+ goto read_ok;
+
+ switch (wsi->state) {
+ case LWSS_HTTP:
+ case LWSS_HTTP_HEADERS:
+ goto read_ok;
+ case LWSS_HTTP_ISSUING_FILE:
+ goto read_ok;
+ case LWSS_HTTP_BODY:
+ wsi->u.http.content_remain =
+ wsi->u.http.content_length;
+ if (wsi->u.http.content_remain)
+ goto http_postbody;
+
+ /* there is no POST content */
+ goto postbody_completion;
+ default:
+ break;
+ }
+ break;
- /*
- * Give the user code a chance to study the request and
- * have the opportunity to deny it
+ case LWSS_HTTP_BODY:
+http_postbody:
+ while (len && wsi->u.http.content_remain) {
+ /* Copy as much as possible, up to the limit of:
+ * what we have in the read buffer (len)
+ * remaining portion of the POST body (content_remain)
*/
-
- if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
- LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
+ body_chunk_len = min(wsi->u.http.content_remain,len);
+ wsi->u.http.content_remain -= body_chunk_len;
+ len -= body_chunk_len;
+#ifdef LWS_WITH_CGI
+ if (wsi->cgi) {
+ struct lws_cgi_args args;
+
+ args.ch = LWS_STDIN;
+ args.stdwsi = &wsi->cgi->stdwsi[0];
+ args.data = buf;
+ args.len = body_chunk_len;
+
+ /* returns how much used */
+ n = user_callback_handle_rxflow(
+ wsi->protocol->callback,
+ wsi, LWS_CALLBACK_CGI_STDIN_DATA,
wsi->user_space,
- lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
- lwsl_warn("User code denied connection\n");
- goto bail_nuke_ah;
+ (void *)&args, 0);
+ if ((int)n < 0)
+ goto bail;
+ } else {
+#endif
+ n = wsi->protocol->callback(wsi,
+ LWS_CALLBACK_HTTP_BODY, wsi->user_space,
+ buf, (size_t)body_chunk_len);
+ if (n)
+ goto bail;
+ n = (size_t)body_chunk_len;
+#ifdef LWS_WITH_CGI
}
+#endif
+ buf += n;
-
- /*
- * Perform the handshake according to the protocol version the
- * client announced
- */
-
- switch (wsi->ietf_spec_revision) {
- case 13:
- lwsl_parser("lws_parse calling handshake_04\n");
- if (handshake_0405(context, wsi)) {
- lwsl_info("hs0405 has failed the connection\n");
- goto bail_nuke_ah;
- }
+ if (wsi->u.http.content_remain) {
+ lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
+ wsi->context->timeout_secs);
break;
-
- default:
- lwsl_warn("Unknown client spec version %d\n",
- wsi->ietf_spec_revision);
- goto bail_nuke_ah;
}
-
- /* drop the header info -- no bail_nuke_ah after this */
-
- if (wsi->u.hdr.ah)
- free(wsi->u.hdr.ah);
-
- wsi->mode = LWS_CONNMODE_WS_SERVING;
-
- /* union transition */
- memset(&wsi->u, 0, sizeof(wsi->u));
- wsi->u.ws.rxflow_change_to = LWS_RXFLOW_ALLOW;
-
- /*
- * create the frame buffer for this connection according to the
- * size mentioned in the protocol definition. If 0 there, use
- * a big default for compatibility
- */
-
- n = wsi->protocol->rx_buffer_size;
- if (!n)
- n = LWS_MAX_SOCKET_IO_BUF;
- n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
- wsi->u.ws.rx_user_buffer = malloc(n);
- if (!wsi->u.ws.rx_user_buffer) {
- lwsl_err("Out of Mem allocating rx buffer %d\n", n);
- goto bail;
- }
- lwsl_info("Allocating RX buffer %d\n", n);
-
- if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n)) {
- lwsl_warn("Failed to set SNDBUF to %d", n);
- goto bail;
- }
-
- lwsl_parser("accepted v%02d connection\n",
- wsi->ietf_spec_revision);
- } /* while all chars are handled */
- break;
+ /* he sent all the content in time */
+postbody_completion:
+#ifdef LWS_WITH_CGI
+ /* if we're running a cgi, we can't let him off the hook just because he sent his POST data */
+ if (wsi->cgi)
+ lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, wsi->context->timeout_secs);
+ else
#endif
-
- case WSI_STATE_AWAITING_CLOSE_ACK:
- case WSI_STATE_ESTABLISHED:
-#ifndef LWS_NO_CLIENT
- switch (wsi->mode) {
- case LWS_CONNMODE_WS_CLIENT:
- for (n = 0; n < len; n++)
- if (libwebsocket_client_rx_sm(
- wsi, *buf++) < 0) {
- lwsl_info("client rx has bailed\n");
+ lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
+#ifdef LWS_WITH_CGI
+ if (!wsi->cgi)
+#endif
+ {
+ n = wsi->protocol->callback(wsi,
+ LWS_CALLBACK_HTTP_BODY_COMPLETION,
+ wsi->user_space, NULL, 0);
+ if (n)
goto bail;
- }
+ }
- return 0;
- default:
break;
}
-#endif
-#ifndef LWS_NO_SERVER
- /* LWS_CONNMODE_WS_SERVING */
+ break;
- if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
- lwsl_info("interpret_incoming_packet has bailed\n");
+ case LWSS_ESTABLISHED:
+ case LWSS_AWAITING_CLOSE_ACK:
+ case LWSS_WAITING_TO_SEND_CLOSE_NOTIFICATION:
+ case LWSS_SHUTDOWN:
+ if (lws_handshake_client(wsi, &buf, (size_t)len))
goto bail;
+ switch (wsi->mode) {
+ case LWSCM_WS_SERVING:
+
+ if (lws_interpret_incoming_packet(wsi, &buf, (size_t)len) < 0) {
+ lwsl_info("interpret_incoming_packet has bailed\n");
+ goto bail;
+ }
+ break;
}
-#endif
break;
default:
- lwsl_err("libwebsocket_read: Unhandled state\n");
+ lwsl_err("%s: Unhandled state %d\n", __func__, wsi->state);
break;
}
- return 0;
+read_ok:
+ /* Nothing more to do for now */
+ lwsl_info("%s: read_ok, used %ld\n", __func__, (long)(buf - oldbuf));
-#ifndef LWS_NO_SERVER
-bail_nuke_ah:
- /* drop the header info */
- if (wsi->u.hdr.ah)
- free(wsi->u.hdr.ah);
-#endif
+ return buf - oldbuf;
bail:
- lwsl_info("closing connection at libwebsocket_read bail:\n");
-
- libwebsocket_close_and_free_session(context, wsi,
- LWS_CLOSE_STATUS_NOSTATUS);
+ //lwsl_notice("closing connection at lws_read bail:\n");
+ lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return -1;
}