2013-01-18 11:43:21 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2013 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"
|
|
|
|
|
|
|
|
#define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
|
2014-04-10 15:15:13 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2014-04-10 14:08:10 +08:00
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_extension_server_handshake(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, char **p)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char *c;
|
|
|
|
char ext_name[128];
|
|
|
|
struct libwebsocket_extension *ext;
|
|
|
|
int ext_count = 0;
|
|
|
|
int more = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out which extensions the client has that we want to
|
|
|
|
* enable on this connection, and give him back the list
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* break down the list of client extensions
|
|
|
|
* and go through them
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (lws_hdr_copy(wsi, (char *)context->service_buffer,
|
|
|
|
sizeof(context->service_buffer),
|
|
|
|
WSI_TOKEN_EXTENSIONS) < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
c = (char *)context->service_buffer;
|
|
|
|
lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
|
|
|
|
wsi->count_active_extensions = 0;
|
|
|
|
n = 0;
|
|
|
|
while (more) {
|
|
|
|
|
|
|
|
if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
|
|
|
|
ext_name[n] = *c++;
|
|
|
|
if (n < sizeof(ext_name) - 1)
|
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ext_name[n] = '\0';
|
|
|
|
if (!*c)
|
|
|
|
more = 0;
|
|
|
|
else {
|
|
|
|
c++;
|
|
|
|
if (!n)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check a client's extension against our support */
|
|
|
|
|
|
|
|
ext = wsi->protocol->owning_server->extensions;
|
|
|
|
|
|
|
|
while (ext && ext->callback) {
|
|
|
|
|
|
|
|
if (strcmp(ext_name, ext->name)) {
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* oh, we do support this one he
|
|
|
|
* asked for... but let's ask user
|
|
|
|
* code if it's OK to apply it on this
|
|
|
|
* particular connection + protocol
|
|
|
|
*/
|
|
|
|
|
|
|
|
n = wsi->protocol->owning_server->
|
|
|
|
protocols[0].callback(
|
|
|
|
wsi->protocol->owning_server,
|
|
|
|
wsi,
|
|
|
|
LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
|
|
|
|
wsi->user_space, ext_name, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* zero return from callback means
|
|
|
|
* go ahead and allow the extension,
|
|
|
|
* it's what we get if the callback is
|
|
|
|
* unhandled
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply it */
|
|
|
|
|
|
|
|
if (ext_count)
|
|
|
|
*(*p)++ = ',';
|
|
|
|
else
|
|
|
|
LWS_CPYAPP(*p,
|
|
|
|
"\x0d\x0aSec-WebSocket-Extensions: ");
|
|
|
|
*p += sprintf(*p, "%s", ext_name);
|
|
|
|
ext_count++;
|
|
|
|
|
|
|
|
/* instantiate the extension on this conn */
|
|
|
|
|
|
|
|
wsi->active_extensions_user[
|
|
|
|
wsi->count_active_extensions] =
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_zalloc(ext->per_session_data_size);
|
2014-04-10 14:08:10 +08:00
|
|
|
if (wsi->active_extensions_user[
|
|
|
|
wsi->count_active_extensions] == NULL) {
|
|
|
|
lwsl_err("Out of mem\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wsi->active_extensions[
|
|
|
|
wsi->count_active_extensions] = ext;
|
|
|
|
|
|
|
|
/* allow him to construct his context */
|
|
|
|
|
|
|
|
ext->callback(wsi->protocol->owning_server,
|
|
|
|
ext, wsi,
|
|
|
|
LWS_EXT_CALLBACK_CONSTRUCT,
|
|
|
|
wsi->active_extensions_user[
|
|
|
|
wsi->count_active_extensions], NULL, 0);
|
|
|
|
|
|
|
|
wsi->count_active_extensions++;
|
|
|
|
lwsl_parser("count_active_extensions <- %d\n",
|
|
|
|
wsi->count_active_extensions);
|
|
|
|
|
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
}
|
2014-12-04 23:59:35 +01:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-10 15:15:13 +08:00
|
|
|
#endif
|
2013-01-18 11:43:21 +08:00
|
|
|
int
|
|
|
|
handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
|
|
|
|
{
|
|
|
|
unsigned char hash[20];
|
|
|
|
int n;
|
|
|
|
char *response;
|
|
|
|
char *p;
|
|
|
|
int accept_len;
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
|
|
|
|
!lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
|
2013-01-18 11:43:21 +08:00
|
|
|
lwsl_parser("handshake_04 missing pieces\n");
|
|
|
|
/* completed header processing, but missing some bits */
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >=
|
|
|
|
MAX_WEBSOCKET_04_KEY_LEN) {
|
|
|
|
lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
|
2013-01-18 11:43:21 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2013-02-12 10:07:22 +08:00
|
|
|
/*
|
|
|
|
* since key length is restricted above (currently 128), cannot
|
|
|
|
* overflow
|
|
|
|
*/
|
|
|
|
n = sprintf((char *)context->service_buffer,
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
"%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
|
|
lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
|
2013-01-18 11:43:21 +08:00
|
|
|
|
2014-09-26 05:56:23 +08:00
|
|
|
libwebsockets_SHA1(context->service_buffer, n, hash);
|
2013-01-18 11:43:21 +08:00
|
|
|
|
2013-02-10 15:34:59 +08:00
|
|
|
accept_len = lws_b64_encode_string((char *)hash, 20,
|
|
|
|
(char *)context->service_buffer,
|
2013-02-11 17:13:32 +08:00
|
|
|
sizeof(context->service_buffer));
|
2013-01-18 11:43:21 +08:00
|
|
|
if (accept_len < 0) {
|
|
|
|
lwsl_warn("Base64 encoded hash too long\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the per-connection user memory (if any) */
|
2013-02-18 16:30:10 +08:00
|
|
|
if (libwebsocket_ensure_user_space(wsi))
|
2013-01-18 11:43:21 +08:00
|
|
|
goto bail;
|
|
|
|
|
|
|
|
/* create the response packet */
|
|
|
|
|
|
|
|
/* make a buffer big enough for everything */
|
|
|
|
|
2014-10-08 12:00:53 +08:00
|
|
|
response = (char *)context->service_buffer + MAX_WEBSOCKET_04_KEY_LEN + LWS_SEND_BUFFER_PRE_PADDING;
|
2013-01-18 11:43:21 +08:00
|
|
|
p = response;
|
|
|
|
LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
|
|
|
|
"Upgrade: WebSocket\x0d\x0a"
|
|
|
|
"Connection: Upgrade\x0d\x0a"
|
|
|
|
"Sec-WebSocket-Accept: ");
|
2013-02-10 15:34:59 +08:00
|
|
|
strcpy(p, (char *)context->service_buffer);
|
2013-01-18 11:43:21 +08:00
|
|
|
p += accept_len;
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
|
2013-01-18 11:43:21 +08:00
|
|
|
LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
|
2014-12-01 19:28:28 +08:00
|
|
|
n = lws_hdr_copy(wsi, p, 128, WSI_TOKEN_PROTOCOL);
|
|
|
|
if (n < 0)
|
|
|
|
goto bail;
|
|
|
|
p += n;
|
2013-01-18 11:43:21 +08:00
|
|
|
}
|
|
|
|
|
2014-04-10 15:15:13 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-01-18 11:43:21 +08:00
|
|
|
/*
|
|
|
|
* Figure out which extensions the client has that we want to
|
|
|
|
* enable on this connection, and give him back the list
|
|
|
|
*/
|
2014-04-10 14:08:10 +08:00
|
|
|
if (lws_extension_server_handshake(context, wsi, &p))
|
|
|
|
goto bail;
|
2014-04-10 15:15:13 +08:00
|
|
|
#endif
|
2013-01-18 11:43:21 +08:00
|
|
|
/* end of response packet */
|
|
|
|
|
|
|
|
LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
|
2014-04-02 19:45:42 +08:00
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
if (!lws_any_extension_handled(context, wsi,
|
|
|
|
LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
|
2013-02-11 17:13:32 +08:00
|
|
|
response, p - response)) {
|
2014-04-02 19:45:42 +08:00
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
/* okay send the handshake response accepting the connection */
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
|
2014-04-10 14:08:10 +08:00
|
|
|
#ifdef DEBUG
|
2013-01-18 11:43:21 +08:00
|
|
|
fwrite(response, 1, p - response, stderr);
|
2014-04-10 14:08:10 +08:00
|
|
|
#endif
|
2013-01-18 11:43:21 +08:00
|
|
|
n = libwebsocket_write(wsi, (unsigned char *)response,
|
2014-10-08 12:00:53 +08:00
|
|
|
p - response, LWS_WRITE_HTTP_HEADERS);
|
2013-02-23 10:50:10 +08:00
|
|
|
if (n != (p - response)) {
|
2013-01-18 11:43:21 +08:00
|
|
|
lwsl_debug("handshake_0405: ERROR writing to socket\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* alright clean up and set ourselves into established state */
|
|
|
|
|
|
|
|
wsi->state = WSI_STATE_ESTABLISHED;
|
|
|
|
wsi->lws_rx_parse_state = LWS_RXPS_NEW;
|
|
|
|
|
|
|
|
/* notify user code that we're ready to roll */
|
|
|
|
|
|
|
|
if (wsi->protocol->callback)
|
|
|
|
wsi->protocol->callback(wsi->protocol->owning_server,
|
|
|
|
wsi, LWS_CALLBACK_ESTABLISHED,
|
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
bail:
|
2013-02-06 15:15:25 +09:00
|
|
|
/* free up his parsing allocations */
|
2014-11-07 11:21:09 +08:00
|
|
|
lws_free_header_table(wsi);
|
2013-01-18 11:43:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|