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 #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 libwebsocket_read(struct libwebsocket_context *context,
62 struct libwebsocket *wsi, unsigned char *buf, size_t len)
66 unsigned char *last_char;
71 case WSI_STATE_HTTP_ISSUING_FILE:
72 wsi->state = WSI_STATE_HTTP_HEADERS;
73 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
74 wsi->u.hdr.lextable_pos = 0;
76 case WSI_STATE_HTTP_HEADERS:
77 lwsl_parser("issuing %d bytes to parser\n", (int)len);
79 if (lws_handshake_client(wsi, &buf, len))
83 if (lws_handshake_server(context, wsi, &buf, len))
84 /* Handshake indicates this session is done. */
87 /* It's possible that we've exhausted our data already, but
88 * lws_handshake_server doesn't update len for us. Figure out how
89 * much was read, so that we can proceed appropriately: */
90 len -= (buf - last_char);
92 if (!wsi->hdr_parsing_completed)
93 /* More header content on the way */
98 case WSI_STATE_HTTP_HEADERS:
100 case WSI_STATE_HTTP_ISSUING_FILE:
102 case WSI_STATE_HTTP_BODY:
103 wsi->u.http.content_remain = wsi->u.http.content_length;
110 case WSI_STATE_HTTP_BODY:
112 while (len && wsi->u.http.content_remain) {
113 /* Copy as much as possible, up to the limit of:
114 * what we have in the read buffer (len)
115 * remaining portion of the POST body (content_remain)
117 body_chunk_len = min(wsi->u.http.content_remain,len);
118 wsi->u.http.content_remain -= body_chunk_len;
119 len -= body_chunk_len;
121 if (wsi->protocol->callback) {
122 n = wsi->protocol->callback(
123 wsi->protocol->owning_server, wsi,
124 LWS_CALLBACK_HTTP_BODY, wsi->user_space,
125 buf, body_chunk_len);
129 buf += body_chunk_len;
131 if (!wsi->u.http.content_remain) {
132 /* he sent the content in time */
133 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
134 if (wsi->protocol->callback) {
135 n = wsi->protocol->callback(
136 wsi->protocol->owning_server, wsi,
137 LWS_CALLBACK_HTTP_BODY_COMPLETION,
138 wsi->user_space, NULL, 0);
147 case WSI_STATE_ESTABLISHED:
148 case WSI_STATE_AWAITING_CLOSE_ACK:
149 if (lws_handshake_client(wsi, &buf, len))
152 case LWS_CONNMODE_WS_SERVING:
154 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
155 lwsl_info("interpret_incoming_packet has bailed\n");
162 lwsl_err("libwebsocket_read: Unhandled state\n");
167 /* Nothing more to do for now. */
168 lwsl_debug("libwebsocket_read: read_ok\n");
173 lwsl_debug("libwebsocket_read: http_complete\n");
174 /* Handle keep-alives, by preparing for a new request: */
175 if (wsi->u.http.connection_type == HTTP_CONNECTION_KEEP_ALIVE) {
176 lwsl_debug("libwebsocket_read: keep-alive\n");
177 wsi->state = WSI_STATE_HTTP;
178 wsi->mode = LWS_CONNMODE_HTTP_SERVING;
179 /* We might be streaming HTTP content, so leave the connection open.*/
180 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
182 if (lws_allocate_header_table(wsi))
185 /* If we have more data, loop back around: */
193 lwsl_debug("closing connection at libwebsocket_read bail:\n");
195 libwebsocket_close_and_free_session(context, wsi,
196 LWS_CLOSE_STATUS_NOSTATUS);