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,
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 * We have to take care about parsing because the headers may be split
51 * into multiple fragments. They may contain unknown headers with arbitrary
52 * argument lengths. So, we parse using a single-character at a time state
53 * machine that is completely independent of packet size.
57 libwebsocket_read(struct libwebsocket_context *context,
58 struct libwebsocket *wsi, unsigned char *buf, size_t len)
61 struct allocated_headers *ah;
66 case WSI_STATE_HTTP_ISSUING_FILE:
68 wsi->state = WSI_STATE_HTTP_HEADERS;
69 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
70 wsi->u.hdr.lextable_pos = 0;
72 case WSI_STATE_HTTP_HEADERS:
74 lwsl_parser("issuing %d bytes to parser\n", (int)len);
78 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
79 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
80 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
81 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
82 case LWS_CONNMODE_WS_CLIENT:
83 for (n = 0; n < len; n++)
84 if (libwebsocket_client_rx_sm(wsi, *buf++)) {
85 lwsl_info("client_rx_sm failed\n");
94 /* LWS_CONNMODE_WS_SERVING */
96 for (n = 0; n < len; n++)
97 if (libwebsocket_parse(wsi, *buf++)) {
98 lwsl_info("libwebsocket_parse failed\n");
102 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
105 lwsl_parser("libwebsocket_parse sees parsing complete\n");
107 /* is this websocket protocol or normal http 1.0? */
109 if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
110 !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
112 /* it's not websocket.... shall we accept it as http? */
114 if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI)) {
115 lwsl_warn("Missing URI in HTTP request\n");
116 /* drop the header info */
122 lwsl_info("HTTP request for '%s'\n",
123 lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI));
125 if (libwebsocket_ensure_user_space(wsi) == NULL) {
126 /* drop the header info */
133 * Hm we still need the headers so the
134 * callback can look at leaders like the URI, but we
135 * need to transition to http union state.... hold a
136 * copy of u.hdr.ah and deallocate afterwards
140 uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI);
141 uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
143 /* union transition */
144 memset(&wsi->u, 0, sizeof(wsi->u));
146 wsi->state = WSI_STATE_HTTP;
148 if (wsi->protocol->callback)
149 n = wsi->protocol->callback(context, wsi,
151 wsi->user_space, uri_ptr, uri_len);
153 /* now drop the header info we kept a pointer to */
158 lwsl_info("LWS_CALLBACK_HTTP closing\n");
166 lwsl_err("NULL protocol at libwebsocket_read\n");
171 * Make sure user side is happy about protocol
174 while (wsi->protocol->callback) {
176 if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
177 if (wsi->protocol->name == NULL)
180 if (wsi->protocol->name && strcmp(
181 lws_hdr_simple_ptr(wsi,
183 wsi->protocol->name) == 0)
189 /* we didn't find a protocol he wanted? */
191 if (wsi->protocol->callback == NULL) {
192 if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) ==
194 lwsl_info("no protocol -> prot 0 handler\n");
195 wsi->protocol = &context->protocols[0];
197 lwsl_err("Req protocol %s not supported\n",
198 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL));
204 * Give the user code a chance to study the request and
205 * have the opportunity to deny it
208 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
209 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
210 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL),
212 lwsl_warn("User code denied connection\n");
218 * Perform the handshake according to the protocol version the
222 switch (wsi->ietf_spec_revision) {
224 lwsl_parser("lws_parse calling handshake_04\n");
225 if (handshake_0405(context, wsi)) {
226 lwsl_info("hs0405 has failed the connection\n");
232 lwsl_warn("Unknown client spec version %d\n",
233 wsi->ietf_spec_revision);
237 /* drop the header info */
242 wsi->mode = LWS_CONNMODE_WS_SERVING;
244 /* union transition */
245 memset(&wsi->u, 0, sizeof(wsi->u));
248 * create the frame buffer for this connection according to the
249 * size mentioned in the protocol definition. If 0 there, use
250 * a big default for compatibility
253 n = wsi->protocol->rx_buffer_size;
255 n = LWS_MAX_SOCKET_IO_BUF;
256 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
257 wsi->u.ws.rx_user_buffer = malloc(n);
258 if (!wsi->u.ws.rx_user_buffer) {
259 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
262 lwsl_info("Allocating RX buffer %d\n", n);
264 lwsl_parser("accepted v%02d connection\n",
265 wsi->ietf_spec_revision);
269 case WSI_STATE_AWAITING_CLOSE_ACK:
270 case WSI_STATE_ESTABLISHED:
271 #ifndef LWS_NO_CLIENT
273 case LWS_CONNMODE_WS_CLIENT:
274 for (n = 0; n < len; n++)
275 if (libwebsocket_client_rx_sm(
277 lwsl_info("client rx has bailed\n");
286 #ifndef LWS_NO_SERVER
287 /* LWS_CONNMODE_WS_SERVING */
289 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
290 lwsl_info("interpret_incoming_packet has bailed\n");
296 lwsl_err("libwebsocket_read: Unhandled state\n");
303 lwsl_info("closing connection at libwebsocket_read bail:\n");
305 libwebsocket_close_and_free_session(context, wsi,
306 LWS_CLOSE_STATUS_NOSTATUS);