2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 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"
24 const struct lws_tokens lws_tokens[WSI_TOKEN_COUNT] = {
27 { "Connection:", 11 },
28 { "Sec-WebSocket-Key1:", 19 },
29 { "Sec-WebSocket-Key2:", 19 },
30 { "Sec-WebSocket-Protocol:", 23 },
33 { "Sec-WebSocket-Draft:", 20 },
37 int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
41 switch (wsi->parser_state) {
42 case WSI_TOKEN_GET_URI:
44 case WSI_TOKEN_CONNECTION:
47 case WSI_TOKEN_PROTOCOL:
48 case WSI_TOKEN_UPGRADE:
49 case WSI_TOKEN_ORIGIN:
51 case WSI_TOKEN_CHALLENGE:
53 debug("WSI_TOKEN_(%d) '%c'\n", wsi->parser_state, c);
55 /* collect into malloc'd buffers */
56 /* optional space swallow */
57 if (!wsi->utf8_token[wsi->parser_state].token_len && c == ' ')
60 /* special case space terminator for get-uri */
61 if (wsi->parser_state == WSI_TOKEN_GET_URI && c == ' ') {
62 wsi->utf8_token[wsi->parser_state].token[
63 wsi->utf8_token[wsi->parser_state].token_len] = '\0';
64 wsi->parser_state = WSI_TOKEN_SKIPPING;
68 /* allocate appropriate memory */
69 if (wsi->utf8_token[wsi->parser_state].token_len ==
70 wsi->current_alloc_len - 1) {
72 wsi->current_alloc_len += LWS_ADDITIONAL_HDR_ALLOC;
73 if (wsi->current_alloc_len >= LWS_MAX_HEADER_LEN) {
74 /* it's waaay to much payload, fail it */
75 strcpy(wsi->utf8_token[wsi->parser_state].token,
76 "!!! Length exceeded maximum supported !!!");
77 wsi->parser_state = WSI_TOKEN_SKIPPING;
80 wsi->utf8_token[wsi->parser_state].token =
81 realloc(wsi->utf8_token[wsi->parser_state].token,
82 wsi->current_alloc_len);
86 if (wsi->parser_state != WSI_TOKEN_CHALLENGE && c == '\x0d') {
87 wsi->utf8_token[wsi->parser_state].token[
88 wsi->utf8_token[wsi->parser_state].token_len] = '\0';
89 wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
93 wsi->utf8_token[wsi->parser_state].token[
94 wsi->utf8_token[wsi->parser_state].token_len++] = c;
96 /* special payload limiting */
97 if (wsi->parser_state == WSI_TOKEN_CHALLENGE &&
98 wsi->utf8_token[wsi->parser_state].token_len == 8) {
99 debug("Setting WSI_PARSING_COMPLETE\n");
100 wsi->parser_state = WSI_PARSING_COMPLETE;
106 /* collecting and checking a name part */
107 case WSI_TOKEN_NAME_PART:
108 debug("WSI_TOKEN_NAME_PART '%c'\n", c);
110 if (wsi->name_buffer_pos == sizeof(wsi->name_buffer) - 1) {
111 /* name bigger than we can handle, skip until next */
112 wsi->parser_state = WSI_TOKEN_SKIPPING;
115 wsi->name_buffer[wsi->name_buffer_pos++] = c;
116 wsi->name_buffer[wsi->name_buffer_pos] = '\0';
118 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
119 if (wsi->name_buffer_pos != lws_tokens[n].token_len)
121 if (strcmp(lws_tokens[n].token, wsi->name_buffer))
123 debug("known hdr '%s'\n", wsi->name_buffer);
124 wsi->parser_state = WSI_TOKEN_GET_URI + n;
125 wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC;
126 wsi->utf8_token[wsi->parser_state].token =
127 malloc(wsi->current_alloc_len);
128 wsi->utf8_token[wsi->parser_state].token_len = 0;
132 /* colon delimiter means we just don't know this name */
134 if (wsi->parser_state == WSI_TOKEN_NAME_PART && c == ':') {
135 debug("skipping unknown header '%s'\n",
137 wsi->parser_state = WSI_TOKEN_SKIPPING;
141 /* don't look for payload when it can just be http headers */
143 if (wsi->parser_state == WSI_TOKEN_CHALLENGE &&
144 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len) {
145 /* they're HTTP headers, not websocket upgrade! */
146 debug("Setting WSI_PARSING_COMPLETE "
147 "from http headers\n");
148 wsi->parser_state = WSI_PARSING_COMPLETE;
152 /* skipping arg part of a name we didn't recognize */
153 case WSI_TOKEN_SKIPPING:
154 debug("WSI_TOKEN_SKIPPING '%c'\n", c);
156 wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
158 case WSI_TOKEN_SKIPPING_SAW_CR:
159 debug("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
161 wsi->parser_state = WSI_TOKEN_NAME_PART;
163 wsi->parser_state = WSI_TOKEN_SKIPPING;
164 wsi->name_buffer_pos = 0;
166 /* we're done, ignore anything else */
167 case WSI_PARSING_COMPLETE:
168 debug("WSI_PARSING_COMPLETE '%c'\n", c);
171 default: /* keep gcc happy */
179 static int libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c)
182 unsigned char buf[2];
184 switch (wsi->lws_rx_parse_state) {
187 switch (wsi->ietf_spec_revision) {
188 /* Firefox 4.0b6 likes this as of 30 Oct */
191 wsi->lws_rx_parse_state = LWS_RXPS_SEEN_76_FF;
198 wsi->lws_rx_parse_state = LWS_RXPS_EAT_UNTIL_76_FF;
199 wsi->rx_user_buffer_head = 0;
202 case LWS_RXPS_EAT_UNTIL_76_FF:
204 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
207 wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
208 (wsi->rx_user_buffer_head++)] = c;
210 if (wsi->rx_user_buffer_head != MAX_USER_RX_BUFFER)
213 if (wsi->protocol->callback)
214 wsi->protocol->callback(wsi, LWS_CALLBACK_RECEIVE,
216 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
217 wsi->rx_user_buffer_head);
218 wsi->rx_user_buffer_head = 0;
220 case LWS_RXPS_SEEN_76_FF:
224 debug("Seen that client is requesting "
225 "a v76 close, sending ack\n");
228 n = libwebsocket_write(wsi, buf, 2, LWS_WRITE_HTTP);
230 fprintf(stderr, "ERROR writing to socket");
233 debug(" v76 close ack sent, server closing skt\n");
234 /* returning < 0 will get it closed in parent */
237 case LWS_RXPS_PULLING_76_LENGTH:
239 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
246 int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
247 unsigned char *buf, size_t len)
252 fprintf(stderr, "received %d byte packet\n", (int)len);
253 for (n = 0; n < len; n++)
254 fprintf(stderr, "%02X ", buf[n]);
255 fprintf(stderr, "\n");
257 /* let the rx protocol state machine have as much as it needs */
260 while (wsi->lws_rx_parse_state !=
261 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED && n < len)
262 if (libwebsocket_rx_sm(wsi, buf[n++]) < 0)
271 * libwebsocket_write() - Apply protocol then write data to client
272 * @wsi: Websocket instance (available from user callback)
273 * @buf: The data to send. For data being sent on a websocket
274 * connection (ie, not default http), this buffer MUST have
275 * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
276 * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
277 * in the buffer after (buf + len). This is so the protocol
278 * header and trailer data can be added in-situ.
279 * @len: Count of the data bytes in the payload starting from buf
280 * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
281 * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
282 * data on a websockets connection. Remember to allow the extra
283 * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
286 * This function provides the way to issue data back to the client
287 * for both http and websocket protocols.
289 * In the case of sending using websocket protocol, be sure to allocate
290 * valid storage before and after buf as explained above. This scheme
291 * allows maximum efficiency of sending data and protocol in a single
292 * packet while not burdening the user code with any protocol knowledge.
295 int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
296 size_t len, enum libwebsocket_write_protocol protocol)
301 unsigned int shift = 7;
303 if (protocol == LWS_WRITE_HTTP)
306 /* websocket protocol, either binary or text */
308 if (wsi->state != WSI_STATE_ESTABLISHED)
311 switch (wsi->ietf_spec_revision) {
312 /* chrome likes this as of 30 Oct */
313 /* Firefox 4.0b6 likes this as of 30 Oct */
315 if (protocol == LWS_WRITE_BINARY) {
316 /* in binary mode we send 7-bit used length blocks */
318 while (len & (127 << shift)) {
327 ((len >> shift) & 127) | 0x80;
330 ((len >> shift) & 127);
337 /* frame type = text, length-free spam mode */
340 buf[len] = 0xff; /* EOT marker */
365 /* just an unimplemented spec right now apparently */
368 if (protocol == LWS_WRITE_BINARY)
385 buf[-8] = (len >> 56) & 0x7f;
406 for (n = 0; n < (len + pre + post); n++)
407 fprintf(stderr, "%02X ", buf[n - pre]);
409 fprintf(stderr, "\n");
413 #ifdef LWS_OPENSSL_SUPPORT
415 n = SSL_write(wsi->ssl, buf - pre, len + pre + post);
417 fprintf(stderr, "ERROR writing to socket");
422 n = send(wsi->sock, buf - pre, len + pre + post, 0);
424 fprintf(stderr, "ERROR writing to socket");
427 #ifdef LWS_OPENSSL_SUPPORT
430 debug("written %d bytes to client\n", (int)len);
437 * libwebsockets_serve_http_file() - Send a file back to the client using http
438 * @wsi: Websocket instance (available from user callback)
439 * @file: The file to issue over http
440 * @content_type: The http content type, eg, text/html
442 * This function is intended to be called from the callback in response
443 * to http requests from the client. It allows the callback to issue
444 * local files down the http link in a single step.
447 int libwebsockets_serve_http_file(struct libwebsocket *wsi, const char *file,
448 const char *content_type)
456 fd = open(file, O_RDONLY);
458 p += sprintf(p, "HTTP/1.0 400 Bad\x0d\x0a"
459 "Server: libwebsockets\x0d\x0a"
462 libwebsocket_write(wsi, (unsigned char *)buf, p - buf,
469 p += sprintf(p, "HTTP/1.0 200 OK\x0d\x0a"
470 "Server: libwebsockets\x0d\x0a"
471 "Content-Type: %s\x0d\x0a"
472 "Content-Length: %u\x0d\x0a"
473 "\x0d\x0a", content_type, (unsigned int)stat.st_size);
475 libwebsocket_write(wsi, (unsigned char *)buf, p - buf, LWS_WRITE_HTTP);
479 n = read(fd, buf, 512);
480 libwebsocket_write(wsi, (unsigned char *)buf, n,