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.
59 * Returns <0 for error or length of chars consumed from buf (up to len)
63 lws_read(struct lws *wsi, unsigned char *buf, lws_filepos_t len)
65 unsigned char *last_char, *oldbuf = buf;
66 lws_filepos_t body_chunk_len;
69 lwsl_debug("%s: incoming len %d state %d\n", __func__, (int)len, wsi->state);
73 case LWSS_HTTP2_AWAIT_CLIENT_PREFACE:
74 case LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS:
75 case LWSS_HTTP2_ESTABLISHED:
79 * we were accepting input but now we stopped doing so
81 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
82 lws_rxflow_cache(wsi, buf, n, len);
87 /* account for what we're using in rxflow buffer */
88 if (wsi->rxflow_buffer)
90 if (lws_http2_parser(wsi, buf[n++])) {
91 lwsl_debug("%s: http2_parser bailed\n", __func__);
98 case LWSS_HTTP_ISSUING_FILE:
101 case LWSS_CLIENT_HTTP_ESTABLISHED:
105 wsi->hdr_parsing_completed = 0;
108 case LWSS_HTTP_HEADERS:
109 if (!wsi->u.hdr.ah) {
110 lwsl_err("%s: LWSS_HTTP_HEADERS: NULL ah\n", __func__);
113 lwsl_parser("issuing %d bytes to parser\n", (int)len);
115 if (lws_handshake_client(wsi, &buf, (size_t)len))
119 if (lws_handshake_server(wsi, &buf, (size_t)len))
120 /* Handshake indicates this session is done. */
123 /* we might have transitioned to RAW */
124 if (wsi->mode == LWSCM_RAW)
125 /* we gave the read buffer to RAW handler already */
129 * It's possible that we've exhausted our data already, or
130 * rx flow control has stopped us dealing with this early,
131 * but lws_handshake_server doesn't update len for us.
132 * Figure out how much was read, so that we can proceed
135 len -= (buf - last_char);
136 lwsl_debug("%s: thinks we have used %ld\n", __func__, (long)len);
138 if (!wsi->hdr_parsing_completed)
139 /* More header content on the way */
142 switch (wsi->state) {
144 case LWSS_HTTP_HEADERS:
146 case LWSS_HTTP_ISSUING_FILE:
149 wsi->u.http.content_remain =
150 wsi->u.http.content_length;
151 if (wsi->u.http.content_remain)
154 /* there is no POST content */
155 goto postbody_completion;
163 while (len && wsi->u.http.content_remain) {
164 /* Copy as much as possible, up to the limit of:
165 * what we have in the read buffer (len)
166 * remaining portion of the POST body (content_remain)
168 body_chunk_len = min(wsi->u.http.content_remain,len);
169 wsi->u.http.content_remain -= body_chunk_len;
170 len -= body_chunk_len;
173 struct lws_cgi_args args;
176 args.stdwsi = &wsi->cgi->stdwsi[0];
178 args.len = body_chunk_len;
180 /* returns how much used */
181 n = user_callback_handle_rxflow(
182 wsi->protocol->callback,
183 wsi, LWS_CALLBACK_CGI_STDIN_DATA,
190 n = wsi->protocol->callback(wsi,
191 LWS_CALLBACK_HTTP_BODY, wsi->user_space,
192 buf, (size_t)body_chunk_len);
195 n = (size_t)body_chunk_len;
201 if (wsi->u.http.content_remain) {
202 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
203 wsi->context->timeout_secs);
206 /* he sent all the content in time */
209 /* if we're running a cgi, we can't let him off the hook just because he sent his POST data */
211 lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, wsi->context->timeout_secs);
214 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
219 n = wsi->protocol->callback(wsi,
220 LWS_CALLBACK_HTTP_BODY_COMPLETION,
221 wsi->user_space, NULL, 0);
230 case LWSS_ESTABLISHED:
231 case LWSS_AWAITING_CLOSE_ACK:
233 if (lws_handshake_client(wsi, &buf, (size_t)len))
236 case LWSCM_WS_SERVING:
238 if (lws_interpret_incoming_packet(wsi, &buf, (size_t)len) < 0) {
239 lwsl_info("interpret_incoming_packet has bailed\n");
246 lwsl_err("%s: Unhandled state %d\n", __func__, wsi->state);
251 /* Nothing more to do for now */
252 lwsl_info("%s: read_ok, used %ld\n", __func__, (long)(buf - oldbuf));
257 //lwsl_notice("closing connection at lws_read bail:\n");
258 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);