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 = lws_get_context(wsi)->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 = lws_get_context(wsi)->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
129 LWS_CPYAPP(*p, "\x0d\x0aSec-WebSocket-Extensions: ");
130 *p += sprintf(*p, "%s", ext_name);
133 /* instantiate the extension on this conn */
135 wsi->active_extensions[wsi->count_act_ext] = ext;
137 /* allow him to construct his context */
139 ext->callback(lws_get_context(wsi), ext, wsi,
140 LWS_EXT_CB_CONSTRUCT,
141 (void *)&wsi->act_ext_user[wsi->count_act_ext],
144 wsi->count_act_ext++;
145 lwsl_parser("count_act_ext <- %d\n", wsi->count_act_ext);
157 handshake_0405(struct lws_context *context, struct lws *wsi)
159 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
160 unsigned char hash[20];
165 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
166 !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
167 lwsl_parser("handshake_04 missing pieces\n");
168 /* completed header processing, but missing some bits */
172 if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= MAX_WEBSOCKET_04_KEY_LEN) {
173 lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
178 * since key length is restricted above (currently 128), cannot
181 n = sprintf((char *)pt->serv_buf,
182 "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
183 lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
185 lws_SHA1(pt->serv_buf, n, hash);
187 accept_len = lws_b64_encode_string((char *)hash, 20, (char *)pt->serv_buf,
188 LWS_MAX_SOCKET_IO_BUF);
189 if (accept_len < 0) {
190 lwsl_warn("Base64 encoded hash too long\n");
194 /* allocate the per-connection user memory (if any) */
195 if (lws_ensure_user_space(wsi))
198 /* create the response packet */
200 /* make a buffer big enough for everything */
202 response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
204 LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
205 "Upgrade: WebSocket\x0d\x0a"
206 "Connection: Upgrade\x0d\x0a"
207 "Sec-WebSocket-Accept: ");
208 strcpy(p, (char *)pt->serv_buf);
211 if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
212 LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
213 n = lws_hdr_copy(wsi, p, 128, WSI_TOKEN_PROTOCOL);
219 #ifndef LWS_NO_EXTENSIONS
221 * Figure out which extensions the client has that we want to
222 * enable on this connection, and give him back the list
224 if (lws_extension_server_handshake(wsi, &p))
228 //LWS_CPYAPP(p, "\x0d\x0a""An-unknown-header: blah");
230 /* end of response packet */
232 LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
234 if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
235 response, p - response)) {
237 /* okay send the handshake response accepting the connection */
239 lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
241 fwrite(response, 1, p - response, stderr);
243 n = lws_write(wsi, (unsigned char *)response,
244 p - response, LWS_WRITE_HTTP_HEADERS);
245 if (n != (p - response)) {
246 lwsl_debug("handshake_0405: ERROR writing to socket\n");
252 /* alright clean up and set ourselves into established state */
254 wsi->state = LWSS_ESTABLISHED;
255 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
257 /* notify user code that we're ready to roll */
259 if (wsi->protocol->callback)
260 if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
262 #ifdef LWS_OPENSSL_SUPPORT
274 /* caller will free up his parsing allocations */