2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2015 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,
22 #include "private-libwebsockets.h"
25 * -04 of the protocol (actually the 80th version) has a radically different
26 * handshake. The 04 spec gives the following idea
28 * The handshake from the client looks as follows:
31 * Host: server.example.com
34 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
35 * Sec-WebSocket-Origin: http://example.com
36 * Sec-WebSocket-Protocol: chat, superchat
37 * Sec-WebSocket-Version: 4
39 * The handshake from the server looks as follows:
41 * HTTP/1.1 101 Switching Protocols
44 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
45 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
46 * Sec-WebSocket-Protocol: chat
50 #define min(a, b) ((a) < (b) ? (a) : (b))
54 * We have to take care about parsing because the headers may be split
55 * into multiple fragments. They may contain unknown headers with arbitrary
56 * argument lengths. So, we parse using a single-character at a time state
57 * machine that is completely independent of packet size.
61 lws_read(struct lws *wsi, unsigned char *buf, size_t len)
63 unsigned char *last_char;
69 case WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE:
70 case WSI_STATE_HTTP2_ESTABLISHED_PRE_SETTINGS:
71 case WSI_STATE_HTTP2_ESTABLISHED:
75 * we were accepting input but now we stopped doing so
77 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
78 lws_rxflow_cache(wsi, buf, n, len);
83 /* account for what we're using in rxflow buffer */
84 if (wsi->rxflow_buffer)
86 if (lws_http2_parser(wsi, buf[n++]))
93 wsi->hdr_parsing_completed = 0;
95 case WSI_STATE_HTTP_ISSUING_FILE:
96 wsi->state = WSI_STATE_HTTP_HEADERS;
97 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
98 wsi->u.hdr.lextable_pos = 0;
100 case WSI_STATE_HTTP_HEADERS:
101 lwsl_parser("issuing %d bytes to parser\n", (int)len);
103 if (lws_handshake_client(wsi, &buf, len))
107 if (lws_handshake_server(wsi, &buf, len))
108 /* Handshake indicates this session is done. */
111 /* It's possible that we've exhausted our data already, but
112 * lws_handshake_server doesn't update len for us. Figure out how
113 * much was read, so that we can proceed appropriately: */
114 len -= (buf - last_char);
116 if (!wsi->hdr_parsing_completed)
117 /* More header content on the way */
120 switch (wsi->state) {
122 case WSI_STATE_HTTP_HEADERS:
124 case WSI_STATE_HTTP_ISSUING_FILE:
126 case WSI_STATE_HTTP_BODY:
127 wsi->u.http.content_remain =
128 wsi->u.http.content_length;
129 if (wsi->u.http.content_remain)
132 /* there is no POST content */
133 goto postbody_completion;
139 case WSI_STATE_HTTP_BODY:
141 while (len && wsi->u.http.content_remain) {
142 /* Copy as much as possible, up to the limit of:
143 * what we have in the read buffer (len)
144 * remaining portion of the POST body (content_remain)
146 body_chunk_len = min(wsi->u.http.content_remain,len);
147 wsi->u.http.content_remain -= body_chunk_len;
148 len -= body_chunk_len;
150 n = wsi->protocol->callback(
151 lws_get_ctx(wsi), wsi,
152 LWS_CALLBACK_HTTP_BODY, wsi->user_space,
153 buf, body_chunk_len);
157 buf += body_chunk_len;
159 if (wsi->u.http.content_remain) {
160 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
164 /* he sent all the content in time */
166 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
167 n = wsi->protocol->callback(
168 lws_get_ctx(wsi), wsi,
169 LWS_CALLBACK_HTTP_BODY_COMPLETION,
170 wsi->user_space, NULL, 0);
178 case WSI_STATE_ESTABLISHED:
179 case WSI_STATE_AWAITING_CLOSE_ACK:
180 if (lws_handshake_client(wsi, &buf, len))
183 case LWS_CONNMODE_WS_SERVING:
185 if (lws_interpret_incoming_packet(wsi, buf, len) < 0) {
186 lwsl_info("interpret_incoming_packet has bailed\n");
193 lwsl_err("%s: Unhandled state\n", __func__);
198 /* Nothing more to do for now */
199 lwsl_debug("%s: read_ok\n", __func__);
204 lwsl_debug("%s: http_complete\n", __func__);
206 #ifndef LWS_NO_SERVER
207 /* Did the client want to keep the HTTP connection going? */
208 if (lws_http_transaction_completed(wsi))
211 /* If we have more data, loop back around: */
218 lwsl_debug("closing connection at lws_read bail:\n");
219 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);