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"
24 #define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
25 #ifndef LWS_NO_EXTENSIONS
27 lws_extension_server_handshake(struct libwebsocket_context *context,
28 struct libwebsocket *wsi, char **p)
33 struct libwebsocket_extension *ext;
38 * Figure out which extensions the client has that we want to
39 * enable on this connection, and give him back the list
42 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
46 * break down the list of client extensions
50 if (lws_hdr_copy(wsi, (char *)context->service_buffer,
51 sizeof(context->service_buffer),
52 WSI_TOKEN_EXTENSIONS) < 0)
55 c = (char *)context->service_buffer;
56 lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
57 wsi->count_active_extensions = 0;
61 if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
63 if (n < sizeof(ext_name) - 1)
76 /* check a client's extension against our support */
78 ext = wsi->protocol->owning_server->extensions;
80 while (ext && ext->callback) {
82 if (strcmp(ext_name, ext->name)) {
88 * oh, we do support this one he
89 * asked for... but let's ask user
90 * code if it's OK to apply it on this
91 * particular connection + protocol
94 n = wsi->protocol->owning_server->
95 protocols[0].callback(
96 wsi->protocol->owning_server,
98 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
99 wsi->user_space, ext_name, 0);
102 * zero return from callback means
103 * go ahead and allow the extension,
104 * it's what we get if the callback is
119 "\x0d\x0aSec-WebSocket-Extensions: ");
120 *p += sprintf(*p, "%s", ext_name);
123 /* instantiate the extension on this conn */
125 wsi->active_extensions_user[
126 wsi->count_active_extensions] =
127 malloc(ext->per_session_data_size);
128 if (wsi->active_extensions_user[
129 wsi->count_active_extensions] == NULL) {
130 lwsl_err("Out of mem\n");
133 memset(wsi->active_extensions_user[
134 wsi->count_active_extensions], 0,
135 ext->per_session_data_size);
137 wsi->active_extensions[
138 wsi->count_active_extensions] = ext;
140 /* allow him to construct his context */
142 ext->callback(wsi->protocol->owning_server,
144 LWS_EXT_CALLBACK_CONSTRUCT,
145 wsi->active_extensions_user[
146 wsi->count_active_extensions], NULL, 0);
148 wsi->count_active_extensions++;
149 lwsl_parser("count_active_extensions <- %d\n",
150 wsi->count_active_extensions);
162 handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
164 unsigned char hash[20];
170 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
171 !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
172 lwsl_parser("handshake_04 missing pieces\n");
173 /* completed header processing, but missing some bits */
177 if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >=
178 MAX_WEBSOCKET_04_KEY_LEN) {
179 lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
184 * since key length is restricted above (currently 128), cannot
187 n = sprintf((char *)context->service_buffer,
188 "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
189 lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
191 libwebsockets_SHA1(context->service_buffer, n, hash);
193 accept_len = lws_b64_encode_string((char *)hash, 20,
194 (char *)context->service_buffer,
195 sizeof(context->service_buffer));
196 if (accept_len < 0) {
197 lwsl_warn("Base64 encoded hash too long\n");
201 /* allocate the per-connection user memory (if any) */
202 if (libwebsocket_ensure_user_space(wsi))
205 /* create the response packet */
207 /* make a buffer big enough for everything */
209 response = (char *)context->service_buffer + MAX_WEBSOCKET_04_KEY_LEN + LWS_SEND_BUFFER_PRE_PADDING;
211 LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
212 "Upgrade: WebSocket\x0d\x0a"
213 "Connection: Upgrade\x0d\x0a"
214 "Sec-WebSocket-Accept: ");
215 strcpy(p, (char *)context->service_buffer);
218 if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
219 LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
220 n = lws_hdr_copy(wsi, p, 128, WSI_TOKEN_PROTOCOL);
226 #ifndef LWS_NO_EXTENSIONS
228 * Figure out which extensions the client has that we want to
229 * enable on this connection, and give him back the list
231 if (lws_extension_server_handshake(context, wsi, &p))
234 /* end of response packet */
236 LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
238 if (!lws_any_extension_handled(context, wsi,
239 LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
240 response, p - response)) {
242 /* okay send the handshake response accepting the connection */
244 lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
246 fwrite(response, 1, p - response, stderr);
248 n = libwebsocket_write(wsi, (unsigned char *)response,
249 p - response, LWS_WRITE_HTTP_HEADERS);
250 if (n != (p - response)) {
251 lwsl_debug("handshake_0405: ERROR writing to socket\n");
257 /* alright clean up and set ourselves into established state */
259 wsi->state = WSI_STATE_ESTABLISHED;
260 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
262 /* notify user code that we're ready to roll */
264 if (wsi->protocol->callback)
265 wsi->protocol->callback(wsi->protocol->owning_server,
266 wsi, LWS_CALLBACK_ESTABLISHED,
267 wsi->user_space, NULL, 0);
273 /* free up his parsing allocations */