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 lws *wsi, char **p)
29 struct lws_context *context = wsi->context;
30 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
31 const struct lws_extension *ext;
40 * Figure out which extensions the client has that we want to
41 * enable on this connection, and give him back the list
43 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
47 * break down the list of client extensions
51 if (lws_hdr_copy(wsi, (char *)pt->serv_buf, LWS_MAX_SOCKET_IO_BUF,
52 WSI_TOKEN_EXTENSIONS) < 0)
55 c = (char *)pt->serv_buf;
56 lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
57 wsi->count_act_ext = 0;
62 if (*c && (*c != ',' && *c != '\t')) {
65 if (ignore || *c == ' ') {
70 if (n < sizeof(ext_name) - 1)
85 /* check a client's extension against our support */
87 ext = wsi->vhost->extensions;
89 while (ext && ext->callback) {
91 if (strcmp(ext_name, ext->name)) {
96 m = ext->callback(lws_get_context(wsi), ext, wsi,
97 LWS_EXT_CB_ARGS_VALIDATE,
105 * oh, we do support this one he asked for... but let's
106 * ask user code if it's OK to apply it on this
107 * particular connection + protocol
109 m = wsi->vhost->protocols[0].callback(wsi,
110 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
111 wsi->user_space, ext_name, 0);
114 * zero return from callback means go ahead and allow
115 * the extension, it's what we get if the callback is
128 /* instantiate the extension on this conn */
130 wsi->active_extensions[wsi->count_act_ext] = ext;
132 /* allow him to construct his context */
134 if (ext->callback(lws_get_context(wsi), ext, wsi,
135 LWS_EXT_CB_CONSTRUCT,
136 (void *)&wsi->act_ext_user[wsi->count_act_ext],
138 lwsl_notice("ext %s failed construction\n", ext_name);
147 LWS_CPYAPP(*p, "\x0d\x0aSec-WebSocket-Extensions: ");
148 *p += sprintf(*p, "%s", ext_name);
150 wsi->count_act_ext++;
151 lwsl_parser("count_act_ext <- %d\n", wsi->count_act_ext);
163 handshake_0405(struct lws_context *context, struct lws *wsi)
165 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
166 unsigned char hash[20];
171 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
172 !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
173 lwsl_parser("handshake_04 missing pieces\n");
174 /* completed header processing, but missing some bits */
178 if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= 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 *)pt->serv_buf,
188 "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
189 lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
191 lws_SHA1(pt->serv_buf, n, hash);
193 accept_len = lws_b64_encode_string((char *)hash, 20, (char *)pt->serv_buf,
194 LWS_MAX_SOCKET_IO_BUF);
195 if (accept_len < 0) {
196 lwsl_warn("Base64 encoded hash too long\n");
200 /* allocate the per-connection user memory (if any) */
201 if (lws_ensure_user_space(wsi))
204 /* create the response packet */
206 /* make a buffer big enough for everything */
208 response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
210 LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
211 "Upgrade: WebSocket\x0d\x0a"
212 "Connection: Upgrade\x0d\x0a"
213 "Sec-WebSocket-Accept: ");
214 strcpy(p, (char *)pt->serv_buf);
217 if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
218 LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
219 n = lws_hdr_copy(wsi, p, 128, WSI_TOKEN_PROTOCOL);
225 #ifndef LWS_NO_EXTENSIONS
227 * Figure out which extensions the client has that we want to
228 * enable on this connection, and give him back the list
230 if (lws_extension_server_handshake(wsi, &p))
234 //LWS_CPYAPP(p, "\x0d\x0a""An-unknown-header: blah");
236 /* end of response packet */
238 LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
240 if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
241 response, p - response)) {
243 /* okay send the handshake response accepting the connection */
245 lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
247 fwrite(response, 1, p - response, stderr);
249 n = lws_write(wsi, (unsigned char *)response,
250 p - response, LWS_WRITE_HTTP_HEADERS);
251 if (n != (p - response)) {
252 lwsl_debug("handshake_0405: ERROR writing to socket\n");
258 /* alright clean up and set ourselves into established state */
260 wsi->state = LWSS_ESTABLISHED;
261 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
263 /* notify user code that we're ready to roll */
265 if (wsi->protocol->callback)
266 if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
268 #ifdef LWS_OPENSSL_SUPPORT
280 /* caller will free up his parsing allocations */