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 extern int handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi);
61 libwebsocket_read(struct libwebsocket_context *context,
62 struct libwebsocket *wsi, unsigned char * buf, size_t len)
67 case WSI_STATE_HTTP_ISSUING_FILE:
69 wsi->state = WSI_STATE_HTTP_HEADERS;
70 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
71 wsi->u.hdr.lextable_pos = 0;
73 case WSI_STATE_HTTP_HEADERS:
75 lwsl_parser("issuing %d bytes to parser\n", (int)len);
77 //fwrite(buf, 1, len, stderr);
82 // lwsl_info("mode=%d\n", wsi->mode);
85 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
86 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
87 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
88 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
89 case LWS_CONNMODE_WS_CLIENT:
90 for (n = 0; n < len; n++)
91 libwebsocket_client_rx_sm(wsi, *buf++);
99 /* LWS_CONNMODE_WS_SERVING */
101 for (n = 0; n < len; n++)
102 libwebsocket_parse(wsi, *buf++);
104 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
107 lwsl_parser("seem to be serving, mode is %d\n", wsi->mode);
109 lwsl_parser("libwebsocket_parse sees parsing complete\n");
111 /* is this websocket protocol or normal http 1.0? */
113 if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
114 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len) {
115 wsi->state = WSI_STATE_HTTP;
116 if (wsi->protocol->callback)
117 if (wsi->protocol->callback(context, wsi,
118 LWS_CALLBACK_HTTP, wsi->user_space,
119 wsi->utf8_token[WSI_TOKEN_GET_URI].token,
120 wsi->utf8_token[WSI_TOKEN_GET_URI].token_len)) {
121 lwsl_info("LWS_CALLBACK_HTTP wanted to close\n");
128 lwsl_err("NULL protocol at libwebsocket_read\n");
134 * Make sure user side is happy about protocol
137 while (wsi->protocol->callback) {
139 if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) {
140 if (wsi->protocol->name == NULL)
143 if (wsi->protocol->name && strcmp(
144 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
145 wsi->protocol->name) == 0)
151 /* we didn't find a protocol he wanted? */
153 if (wsi->protocol->callback == NULL) {
154 if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) {
155 lwsl_info("[no protocol] "
156 "mapped to protocol 0 handler\n");
157 wsi->protocol = &context->protocols[0];
159 lwsl_err("Requested protocol %s "
161 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
167 * find out which spec version the client is using
168 * if this header is not given, we default to 00 (aka 76)
171 if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len)
172 wsi->ietf_spec_revision =
173 atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token);
176 * Give the user code a chance to study the request and
177 * have the opportunity to deny it
180 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
181 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
182 &wsi->utf8_token[0], NULL, 0)) {
183 lwsl_warn("User code denied connection\n");
189 * Perform the handshake according to the protocol version the
193 switch (wsi->ietf_spec_revision) {
195 lwsl_parser("libwebsocket_parse calling handshake_04\n");
196 if (handshake_0405(context, wsi)) {
197 lwsl_info("handshake_0405 xor 05 has failed the connection\n");
203 lwsl_warn("Unknown client spec version %d\n",
204 wsi->ietf_spec_revision);
208 wsi->mode = LWS_CONNMODE_WS_SERVING;
210 /* union transition */
211 memset(&wsi->u, 0, sizeof wsi->u);
213 lwsl_parser("accepted v%02d connection\n",
214 wsi->ietf_spec_revision);
218 case WSI_STATE_AWAITING_CLOSE_ACK:
219 case WSI_STATE_ESTABLISHED:
220 #ifndef LWS_NO_CLIENT
222 case LWS_CONNMODE_WS_CLIENT:
223 for (n = 0; n < len; n++)
224 if (libwebsocket_client_rx_sm(wsi, *buf++) < 0) {
225 lwsl_info("client rx has bailed\n");
234 #ifndef LWS_NO_SERVER
235 /* LWS_CONNMODE_WS_SERVING */
237 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
238 lwsl_info("interpret_incoming_packet has bailed\n");
244 lwsl_err("libwebsocket_read: Unhandled state\n");
251 lwsl_info("closing connection at libwebsocket_read bail:\n");
252 libwebsocket_close_and_free_session(context, wsi,
253 LWS_CLOSE_STATUS_NOSTATUS);