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); }
26 #ifndef LWS_NO_EXTENSIONS
28 lws_extension_server_handshake(struct lws *wsi, char **p, int budget)
30 struct lws_context *context = wsi->context;
31 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
32 char ext_name[64], *args, *end = (*p) + budget - 1;
33 const struct lws_ext_options *opts, *po;
34 const struct lws_extension *ext;
35 struct lws_ext_option_arg oa;
42 * Figure out which extensions the client has that we want to
43 * enable on this connection, and give him back the list
45 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
49 * break down the list of client extensions
53 if (lws_hdr_copy(wsi, (char *)pt->serv_buf, context->pt_serv_buf_size,
54 WSI_TOKEN_EXTENSIONS) < 0)
57 c = (char *)pt->serv_buf;
58 lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
59 wsi->count_act_ext = 0;
65 * We may get a simple request
67 * Sec-WebSocket-Extensions: permessage-deflate
69 * or an elaborated one with requested options
71 * Sec-WebSocket-Extensions: permessage-deflate; \
72 * server_no_context_takeover; \
73 * client_no_context_takeover
78 if (*c && (*c != ',' && *c != '\t')) {
83 if (ignore || *c == ' ') {
88 if (n < sizeof(ext_name) - 1)
103 while (args && *args && *args == ' ')
106 /* check a client's extension against our support */
108 ext = wsi->vhost->extensions;
110 while (ext && ext->callback) {
112 if (strcmp(ext_name, ext->name)) {
118 * oh, we do support this one he asked for... but let's
119 * confirm he only gave it once
121 for (m = 0; m < wsi->count_act_ext; m++)
122 if (wsi->active_extensions[m] == ext) {
123 lwsl_info("extension mentioned twice\n");
124 return 1; /* shenanigans */
128 * ask user code if it's OK to apply it on this
129 * particular connection + protocol
131 m = (wsi->protocol->callback)(wsi,
132 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
133 wsi->user_space, ext_name, 0);
136 * zero return from callback means go ahead and allow
137 * the extension, it's what we get if the callback is
149 /* instantiate the extension on this conn */
151 wsi->active_extensions[wsi->count_act_ext] = ext;
153 /* allow him to construct his context */
155 if (ext->callback(lws_get_context(wsi), ext, wsi,
156 LWS_EXT_CB_CONSTRUCT,
157 (void *)&wsi->act_ext_user[
160 lwsl_notice("ext %s failed construction\n",
172 "\x0d\x0aSec-WebSocket-Extensions: ");
173 *p += lws_snprintf(*p, (end - *p), "%s", ext_name);
176 * go through the options trying to apply the
180 lwsl_debug("ext args %s", args);
182 while (args && *args && *args != ',') {
187 lwsl_debug("'%s' '%s'\n", po->name, args);
188 /* only support arg-less options... */
189 if (po->type == EXTARG_NONE &&
190 !strncmp(args, po->name,
192 oa.option_name = NULL;
193 oa.option_index = po - opts;
195 lwsl_debug("setting %s\n", po->name);
197 lws_get_context(wsi), ext, wsi,
198 LWS_EXT_CB_OPTION_SET,
203 *p += lws_snprintf(*p, (end - *p), "; %s", po->name);
204 lwsl_debug("adding option %s\n", po->name);
209 while (*args && *args != ',' && *args != ';')
213 wsi->count_act_ext++;
214 lwsl_parser("count_act_ext <- %d\n",
228 handshake_0405(struct lws_context *context, struct lws *wsi)
230 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
231 unsigned char hash[20];
236 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
237 !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
238 lwsl_parser("handshake_04 missing pieces\n");
239 /* completed header processing, but missing some bits */
243 if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= MAX_WEBSOCKET_04_KEY_LEN) {
244 lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
249 * since key length is restricted above (currently 128), cannot
252 n = sprintf((char *)pt->serv_buf,
253 "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
254 lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
256 lws_SHA1(pt->serv_buf, n, hash);
258 accept_len = lws_b64_encode_string((char *)hash, 20,
259 (char *)pt->serv_buf, context->pt_serv_buf_size);
260 if (accept_len < 0) {
261 lwsl_warn("Base64 encoded hash too long\n");
265 /* allocate the per-connection user memory (if any) */
266 if (lws_ensure_user_space(wsi))
269 /* create the response packet */
271 /* make a buffer big enough for everything */
273 response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
275 LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
276 "Upgrade: WebSocket\x0d\x0a"
277 "Connection: Upgrade\x0d\x0a"
278 "Sec-WebSocket-Accept: ");
279 strcpy(p, (char *)pt->serv_buf);
282 /* we can only return the protocol header if:
283 * - one came in, and ... */
284 if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL) &&
285 /* - it is not an empty string */
286 wsi->protocol->name &&
287 wsi->protocol->name[0]) {
288 LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
289 p += lws_snprintf(p, 128, "%s", wsi->protocol->name);
292 #ifndef LWS_NO_EXTENSIONS
294 * Figure out which extensions the client has that we want to
295 * enable on this connection, and give him back the list.
297 * Give him a limited write bugdet
299 if (lws_extension_server_handshake(wsi, &p, 192))
303 //LWS_CPYAPP(p, "\x0d\x0a""An-unknown-header: blah");
305 /* end of response packet */
307 LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
309 if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
310 response, p - response)) {
312 /* okay send the handshake response accepting the connection */
314 lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
315 #if defined(DEBUG) && ! defined(LWS_WITH_ESP8266)
316 fwrite(response, 1, p - response, stderr);
318 n = lws_write(wsi, (unsigned char *)response,
319 p - response, LWS_WRITE_HTTP_HEADERS);
320 if (n != (p - response)) {
321 lwsl_debug("handshake_0405: ERROR writing to socket\n");
327 /* alright clean up and set ourselves into established state */
329 wsi->state = LWSS_ESTABLISHED;
330 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
333 const char * uri_ptr =
334 lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI);
335 int uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
336 const struct lws_http_mount *hit =
337 lws_find_mount(wsi, uri_ptr, uri_len);
338 if (hit && hit->cgienv &&
339 wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_PMO,
340 wsi->user_space, (void *)hit->cgienv, 0))
348 /* caller will free up his parsing allocations */