2010-11-08 20:20:42 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
2010-11-13 10:03:47 +00:00
|
|
|
*
|
2010-11-08 20:20:42 +00:00
|
|
|
* Copyright (C) 2010 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
|
|
|
|
2010-11-15 22:08:00 +00:00
|
|
|
static int interpret_key(const char *key, unsigned long *result)
|
2010-11-08 20:20:42 +00:00
|
|
|
{
|
|
|
|
char digits[20];
|
|
|
|
int digit_pos = 0;
|
|
|
|
const char *p = key;
|
2010-11-15 22:08:00 +00:00
|
|
|
unsigned int spaces = 0;
|
|
|
|
unsigned long long acc;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
while (*p) {
|
|
|
|
if (isdigit(*p)) {
|
|
|
|
if (digit_pos == sizeof(digits) - 1)
|
|
|
|
return -1;
|
|
|
|
digits[digit_pos++] = *p;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
digits[digit_pos] = '\0';
|
|
|
|
if (!digit_pos)
|
|
|
|
return -2;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
while (*key) {
|
|
|
|
if (*key == ' ')
|
|
|
|
spaces++;
|
|
|
|
key++;
|
|
|
|
}
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
if (!spaces)
|
|
|
|
return -3;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-15 22:08:00 +00:00
|
|
|
/*
|
|
|
|
* long long is absolutely needed since "digits" can be a multiple
|
|
|
|
* of a 32-bit range number
|
|
|
|
*/
|
|
|
|
acc = atoll(digits);
|
|
|
|
*result = acc / spaces;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to take care about parsing because the headers may be split
|
|
|
|
* into multiple fragments. They may contain unknown headers with arbitrary
|
|
|
|
* argument lengths. So, we parse using a single-character at a time state
|
|
|
|
* machine that is completely independent of packet size.
|
|
|
|
*/
|
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
int
|
2010-11-08 20:20:42 +00:00
|
|
|
libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
char *p;
|
2010-11-15 22:08:00 +00:00
|
|
|
unsigned long key1, key2;
|
2010-11-08 20:20:42 +00:00
|
|
|
unsigned char sum[16];
|
|
|
|
char *response;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
switch (wsi->state) {
|
|
|
|
case WSI_STATE_HTTP:
|
|
|
|
wsi->state = WSI_STATE_HTTP_HEADERS;
|
|
|
|
wsi->parser_state = WSI_TOKEN_NAME_PART;
|
|
|
|
/* fallthru */
|
|
|
|
case WSI_STATE_HTTP_HEADERS:
|
2010-11-13 10:03:47 +00:00
|
|
|
|
|
|
|
debug("issuing %d bytes to parser\n", (int)len);
|
2010-11-08 20:20:42 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fwrite(buf, 1, len, stderr);
|
|
|
|
#endif
|
2010-11-13 10:03:47 +00:00
|
|
|
for (n = 0; n < len; n++)
|
2010-11-08 20:20:42 +00:00
|
|
|
libwebsocket_parse(wsi, *buf++);
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
if (wsi->parser_state != WSI_PARSING_COMPLETE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* is this websocket protocol or normal http 1.0? */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
|
|
|
|
!wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len) {
|
2010-11-12 10:44:16 +00:00
|
|
|
if (wsi->protocol->callback)
|
2010-11-13 10:03:47 +00:00
|
|
|
(wsi->protocol->callback)(wsi,
|
|
|
|
LWS_CALLBACK_HTTP, wsi->user_space,
|
2010-11-11 12:28:29 +00:00
|
|
|
wsi->utf8_token[WSI_TOKEN_GET_URI].token, 0);
|
2010-11-08 20:20:42 +00:00
|
|
|
wsi->state = WSI_STATE_HTTP;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Websocket - confirm we have all the necessary pieces */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
if (!wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len ||
|
|
|
|
!wsi->utf8_token[WSI_TOKEN_HOST].token_len ||
|
|
|
|
!wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
|
|
|
|
!wsi->utf8_token[WSI_TOKEN_KEY1].token_len ||
|
|
|
|
!wsi->utf8_token[WSI_TOKEN_KEY2].token_len)
|
|
|
|
/* completed header processing, but missing some bits */
|
|
|
|
goto bail;
|
2010-11-11 12:28:29 +00:00
|
|
|
|
2010-11-11 12:48:13 +00:00
|
|
|
/* are we happy about the draft version client side wants? */
|
|
|
|
|
|
|
|
if (wsi->utf8_token[WSI_TOKEN_DRAFT].token) {
|
|
|
|
wsi->ietf_spec_revision =
|
|
|
|
atoi(wsi->utf8_token[WSI_TOKEN_DRAFT].token);
|
|
|
|
switch (wsi->ietf_spec_revision) {
|
|
|
|
case 76:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Rejecting handshake on seeing "
|
|
|
|
"unsupported draft request %d\n",
|
|
|
|
wsi->ietf_spec_revision);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 12:28:29 +00:00
|
|
|
/* Make sure user side is happy about protocol */
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
while (wsi->protocol->callback) {
|
|
|
|
|
|
|
|
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) {
|
|
|
|
if (wsi->protocol->name == NULL)
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
if (strcmp(
|
|
|
|
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
|
|
|
|
wsi->protocol->name) == 0)
|
|
|
|
break;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
wsi->protocol++;
|
|
|
|
}
|
2010-11-12 11:15:49 +00:00
|
|
|
|
|
|
|
/* we didn't find a protocol he wanted? */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
if (wsi->protocol->callback == NULL) {
|
|
|
|
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL)
|
|
|
|
fprintf(stderr, "[no protocol] "
|
|
|
|
"not supported (use NULL .name)\n");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Requested protocol %s "
|
|
|
|
"not supported\n",
|
|
|
|
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the per-connection user memory (if any) */
|
|
|
|
|
|
|
|
if (wsi->protocol->per_session_data_size) {
|
|
|
|
wsi->user_space = malloc(
|
|
|
|
wsi->protocol->per_session_data_size);
|
|
|
|
if (wsi->user_space == NULL) {
|
|
|
|
fprintf(stderr, "Out of memory for "
|
|
|
|
"conn user space\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
2010-11-12 11:15:49 +00:00
|
|
|
} else
|
|
|
|
wsi->user_space = NULL;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* create the response packet */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* make a buffer big enough for everything */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
response = malloc(256 +
|
|
|
|
wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len +
|
|
|
|
wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len +
|
|
|
|
wsi->utf8_token[WSI_TOKEN_HOST].token_len +
|
|
|
|
wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len +
|
|
|
|
wsi->utf8_token[WSI_TOKEN_GET_URI].token_len +
|
|
|
|
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
|
|
|
|
if (!response) {
|
|
|
|
fprintf(stderr, "Out of memory for response buffer\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
p = response;
|
|
|
|
strcpy(p, "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
|
|
|
|
"Upgrade: WebSocket\x0d\x0a");
|
|
|
|
p += strlen("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
|
|
|
|
"Upgrade: WebSocket\x0d\x0a");
|
|
|
|
strcpy(p, "Connection: Upgrade\x0d\x0a"
|
|
|
|
"Sec-WebSocket-Origin: ");
|
|
|
|
p += strlen("Connection: Upgrade\x0d\x0a"
|
|
|
|
"Sec-WebSocket-Origin: ");
|
|
|
|
strcpy(p, wsi->utf8_token[WSI_TOKEN_ORIGIN].token);
|
|
|
|
p += wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len;
|
|
|
|
#ifdef LWS_OPENSSL_SUPPORT
|
|
|
|
if (use_ssl) {
|
|
|
|
strcpy(p, "\x0d\x0aSec-WebSocket-Location: wss://");
|
|
|
|
p += strlen("\x0d\x0aSec-WebSocket-Location: wss://");
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
strcpy(p, "\x0d\x0aSec-WebSocket-Location: ws://");
|
|
|
|
p += strlen("\x0d\x0aSec-WebSocket-Location: ws://");
|
|
|
|
#ifdef LWS_OPENSSL_SUPPORT
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
strcpy(p, wsi->utf8_token[WSI_TOKEN_HOST].token);
|
|
|
|
p += wsi->utf8_token[WSI_TOKEN_HOST].token_len;
|
|
|
|
strcpy(p, wsi->utf8_token[WSI_TOKEN_GET_URI].token);
|
|
|
|
p += wsi->utf8_token[WSI_TOKEN_GET_URI].token_len;
|
|
|
|
|
|
|
|
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) {
|
|
|
|
strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: ");
|
|
|
|
p += strlen("\x0d\x0aSec-WebSocket-Protocol: ");
|
|
|
|
strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
|
|
|
|
p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(p, "\x0d\x0a\x0d\x0a");
|
|
|
|
p += strlen("\x0d\x0a\x0d\x0a");
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* convert the two keys into 32-bit integers */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
if (interpret_key(wsi->utf8_token[WSI_TOKEN_KEY1].token, &key1))
|
|
|
|
goto bail;
|
|
|
|
if (interpret_key(wsi->utf8_token[WSI_TOKEN_KEY2].token, &key2))
|
|
|
|
goto bail;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* lay them out in network byte order (MSB first */
|
|
|
|
|
|
|
|
sum[0] = key1 >> 24;
|
|
|
|
sum[1] = key1 >> 16;
|
|
|
|
sum[2] = key1 >> 8;
|
|
|
|
sum[3] = key1;
|
|
|
|
sum[4] = key2 >> 24;
|
|
|
|
sum[5] = key2 >> 16;
|
|
|
|
sum[6] = key2 >> 8;
|
|
|
|
sum[7] = key2;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* follow them with the challenge token we were sent */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
memcpy(&sum[8], wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 8);
|
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
/*
|
2010-11-08 20:20:42 +00:00
|
|
|
* compute the md5sum of that 16-byte series and use as our
|
|
|
|
* payload after our headers
|
|
|
|
*/
|
|
|
|
|
2010-11-08 21:04:23 +00:00
|
|
|
libwebsockets_md5(sum, 16, (unsigned char *)p);
|
2010-11-08 20:20:42 +00:00
|
|
|
p += 16;
|
|
|
|
|
|
|
|
/* it's complete: go ahead and send it */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
|
|
|
debug("issuing response packet %d len\n", (int)(p - response));
|
2010-11-08 20:20:42 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fwrite(response, 1, p - response, stderr);
|
|
|
|
#endif
|
2010-11-08 21:04:23 +00:00
|
|
|
n = libwebsocket_write(wsi, (unsigned char *)response,
|
|
|
|
p - response, LWS_WRITE_HTTP);
|
2010-11-08 20:20:42 +00:00
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr, "ERROR writing to socket");
|
|
|
|
goto bail;
|
|
|
|
}
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* alright clean up and set ourselves into established state */
|
|
|
|
|
|
|
|
free(response);
|
|
|
|
wsi->state = WSI_STATE_ESTABLISHED;
|
|
|
|
wsi->lws_rx_parse_state = LWS_RXPS_NEW;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* notify user code that we're ready to roll */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
if (wsi->protocol->callback)
|
|
|
|
wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
|
2010-11-12 11:15:49 +00:00
|
|
|
wsi->user_space, NULL, 0);
|
2010-11-08 20:20:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WSI_STATE_ESTABLISHED:
|
|
|
|
if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0)
|
|
|
|
goto bail;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
return 0;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
bail:
|
|
|
|
libwebsocket_close_and_free_session(wsi);
|
|
|
|
return -1;
|
|
|
|
}
|