2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2014 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"
25 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len)
30 case LWSCM_WSCL_WAITING_PROXY_REPLY:
31 case LWSCM_WSCL_ISSUE_HANDSHAKE:
32 case LWSCM_WSCL_WAITING_SERVER_REPLY:
33 case LWSCM_WSCL_WAITING_EXTENSION_CONNECT:
37 * we were accepting input but now we stopped doing so
39 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
40 lwsl_debug("%s: caching %d\n", __func__, len);
41 lws_rxflow_cache(wsi, *buf, 0, len);
44 if (wsi->u.ws.rx_draining_ext) {
45 m = lws_rx_sm(wsi, 0);
50 /* account for what we're using in rxflow buffer */
51 if (wsi->rxflow_buffer)
54 if (lws_client_rx_sm(wsi, *(*buf)++)) {
55 lwsl_debug("client_rx_sm exited\n");
60 lwsl_debug("%s: finished with %d\n", __func__, len);
70 lws_client_socket_service(struct lws_context *context, struct lws *wsi,
71 struct lws_pollfd *pollfd)
73 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
74 char *p = (char *)&pt->serv_buf[0];
81 case LWSCM_WSCL_WAITING_CONNECT:
84 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
85 * timeout protection set in client-handshake.c
88 if (!lws_client_connect_2(wsi)) {
90 lwsl_client("closed\n");
94 /* either still pending connection, or changed mode */
97 case LWSCM_WSCL_WAITING_PROXY_REPLY:
99 /* handle proxy hung up on us */
101 if (pollfd->revents & LWS_POLLHUP) {
103 lwsl_warn("Proxy connection %p (fd=%d) dead\n",
104 (void *)wsi, pollfd->fd);
106 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
110 n = recv(wsi->sock, sb, LWS_MAX_SOCKET_IO_BUF, 0);
112 if (LWS_ERRNO == LWS_EAGAIN) {
113 lwsl_debug("Proxy read returned EAGAIN... retrying\n");
117 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
118 lwsl_err("ERROR reading from proxy socket\n");
122 pt->serv_buf[13] = '\0';
123 if (strcmp(sb, "HTTP/1.0 200 ") &&
124 strcmp(sb, "HTTP/1.1 200 ")) {
125 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
126 lwsl_err("ERROR proxy: %s\n", sb);
130 /* clear his proxy connection timeout */
132 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
136 case LWSCM_WSCL_ISSUE_HANDSHAKE:
139 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
140 * timeout protection set in client-handshake.c
142 * take care of our lws_callback_on_writable
143 * happening at a time when there's no real connection yet
145 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
148 #ifdef LWS_OPENSSL_SUPPORT
149 /* we can retry this... just cook the SSL BIO the first time */
151 if (wsi->use_ssl && !wsi->ssl)
152 lws_ssl_client_bio_create(wsi);
155 if (!lws_ssl_client_connect1(wsi))
162 case LWSCM_WSCL_WAITING_SSL:
165 if (!lws_ssl_client_connect2(wsi))
171 wsi->mode = LWSCM_WSCL_ISSUE_HANDSHAKE2;
172 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
173 context->timeout_secs);
177 case LWSCM_WSCL_ISSUE_HANDSHAKE2:
178 p = lws_generate_client_handshake(wsi, p);
180 lwsl_err("Failed to generate handshake for client\n");
181 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
185 /* send our request to the server */
187 lws_latency_pre(context, wsi);
189 n = lws_ssl_capable_write(wsi, (unsigned char *)sb, p - sb);
190 lws_latency(context, wsi, "send lws_issue_raw", n,
193 case LWS_SSL_CAPABLE_ERROR:
194 lwsl_debug("ERROR writing to client socket\n");
195 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
197 case LWS_SSL_CAPABLE_MORE_SERVICE:
198 lws_callback_on_writable(wsi);
202 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
203 wsi->u.hdr.lextable_pos = 0;
204 wsi->mode = LWSCM_WSCL_WAITING_SERVER_REPLY;
205 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
206 context->timeout_secs);
209 case LWSCM_WSCL_WAITING_SERVER_REPLY:
211 /* handle server hung up on us */
213 if (pollfd->revents & LWS_POLLHUP) {
215 lwsl_debug("Server connection %p (fd=%d) dead\n",
216 (void *)wsi, pollfd->fd);
221 if (!(pollfd->revents & LWS_POLLIN))
224 /* interpret the server response */
227 * HTTP/1.1 101 Switching Protocols
229 * Connection: Upgrade
230 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
231 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
232 * Sec-WebSocket-Protocol: chat
236 * we have to take some care here to only take from the
237 * socket bytewise. The browser may (and has been seen to
238 * in the case that onopen() performs websocket traffic)
239 * coalesce both handshake response and websocket traffic
240 * in one packet, since at that point the connection is
241 * definitively ready from browser pov.
244 while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
246 n = lws_ssl_capable_read(wsi, &c, 1);
247 lws_latency(context, wsi, "send lws_issue_raw", n,
251 case LWS_SSL_CAPABLE_ERROR:
253 case LWS_SSL_CAPABLE_MORE_SERVICE:
257 if (lws_parse(wsi, c)) {
258 lwsl_warn("problems parsing header\n");
264 * hs may also be coming in multiple packets, there is a 5-sec
265 * libwebsocket timeout still active here too, so if parsing did
266 * not complete just wait for next packet coming in this state
269 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
273 * otherwise deal with the handshake. If there's any
274 * packet traffic already arrived we'll trigger poll() again
275 * right away and deal with it that way
278 return lws_client_interpret_server_handshake(wsi);
281 lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
282 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
285 case LWSCM_WSCL_WAITING_EXTENSION_CONNECT:
286 lwsl_ext("LWSCM_WSCL_WAITING_EXTENSION_CONNECT\n");
289 case LWSCM_WSCL_PENDING_CANDIDATE_CHILD:
290 lwsl_ext("LWSCM_WSCL_PENDING_CANDIDATE_CHILD\n");
300 * In-place str to lower case
307 *s = tolower((int)*s);
313 * lws_http_transaction_completed() - wait for new http transaction or close
314 * @wsi: websocket connection
316 * Returns 1 if the HTTP connection must close now
317 * Returns 0 and resets connection to wait for new HTTP header /
318 * transaction if possible
321 int LWS_WARN_UNUSED_RESULT
322 lws_http_transaction_completed_client(struct lws *wsi)
324 lwsl_debug("%s: wsi %p\n", __func__, wsi);
325 /* if we can't go back to accept new headers, drop the connection */
326 if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
327 lwsl_info("%s: %p: close connection\n", __func__, wsi);
331 /* otherwise set ourselves up ready to go again */
332 wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
333 wsi->mode = LWSCM_HTTP_CLIENT_ACCEPTED;
334 wsi->u.http.content_length = 0;
335 wsi->hdr_parsing_completed = 0;
337 /* He asked for it to stay alive indefinitely */
338 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
341 * As client, nothing new is going to come until we ask for it
342 * we can drop the ah, if any
345 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
346 lws_header_table_detach(wsi, 0);
349 /* If we're (re)starting on headers, need other implied init */
350 wsi->u.hdr.ues = URIES_IDLE;
352 lwsl_info("%s: %p: keep-alive await new transaction\n", __func__, wsi);
358 lws_client_interpret_server_handshake(struct lws *wsi)
360 int n, len, okay = 0, isErrorCodeReceived = 0, port = 0, ssl = 0;
361 int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
362 const char *pc, *prot, *ads = NULL, *path;
363 struct allocated_headers *ah;
365 #ifndef LWS_NO_EXTENSIONS
366 struct lws_context *context = wsi->context;
367 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
368 char *sb = (char *)&pt->serv_buf[0];
369 const struct lws_ext_options *opts;
370 const struct lws_extension *ext;
379 /* we are being an http client...
382 lws_union_transition(wsi, LWSCM_HTTP_CLIENT_ACCEPTED);
383 wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
388 * well, what the server sent looked reasonable for syntax.
389 * Now let's confirm it sent all the necessary headers
391 * http (non-ws) client will expect something like this
394 * server:.libwebsockets
395 * content-type:.text/html
396 * content-length:.17703
397 * set-cookie:.test=LWS_1456736240_336776_COOKIE;Max-Age=360000
403 wsi->u.http.connection_type = HTTP_CONNECTION_KEEP_ALIVE;
404 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
405 if (wsi->do_ws && !p) {
406 lwsl_info("no URI\n");
410 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP1_0);
411 wsi->u.http.connection_type = HTTP_CONNECTION_CLOSE;
414 lwsl_info("no URI\n");
418 if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
419 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);
423 if (lws_parse_uri(p, &prot, &ads, &port, &path))
426 if (!strcmp(prot, "wss://") || !strcmp(prot, "https://"))
429 if (lws_client_reset(wsi, ssl, ads, port, path, ads)) {
430 lwsl_err("Redirect failed\n");
438 lwsl_notice("Connection failed with code %d", n);
442 #ifdef LWS_WITH_HTTP_PROXY
443 wsi->perform_rewrite = 0;
444 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
445 if (!strncmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE),
447 wsi->perform_rewrite = 1;
451 /* allocate the per-connection user memory (if any) */
452 if (lws_ensure_user_space(wsi)) {
453 lwsl_err("Problem allocating wsi user mem\n");
457 /* he may choose to send us stuff in chunked transfer-coding */
459 wsi->chunk_remaining = 0; /* ie, next thing is chunk size */
460 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_TRANSFER_ENCODING)) {
461 wsi->chunked = !strcmp(lws_hdr_simple_ptr(wsi,
462 WSI_TOKEN_HTTP_TRANSFER_ENCODING),
464 /* first thing is hex, after payload there is crlf */
465 wsi->chunk_parser = ELCP_HEX;
468 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
469 wsi->u.http.content_length =
470 atoi(lws_hdr_simple_ptr(wsi,
471 WSI_TOKEN_HTTP_CONTENT_LENGTH));
472 lwsl_notice("%s: incoming content length %d\n", __func__,
473 wsi->u.http.content_length);
474 wsi->u.http.content_remain = wsi->u.http.content_length;
475 } else /* can't do 1.1 without a content length or chunked */
477 wsi->u.http.connection_type = HTTP_CONNECTION_CLOSE;
480 * we seem to be good to go, give client last chance to check
483 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
484 wsi->user_space, NULL, 0))
487 /* clear his proxy connection timeout */
488 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
490 wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
492 /* call him back to inform him he is up */
493 if (wsi->protocol->callback(wsi,
494 LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP,
495 wsi->user_space, NULL, 0))
498 /* free up his parsing allocations */
499 lws_header_table_detach(wsi, 0);
501 lwsl_notice("%s: client connection up\n", __func__);
506 if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
507 lwsl_info("no ACCEPT\n");
508 isErrorCodeReceived = 1;
512 if (p && strncmp(p, "101", 3)) {
514 "lws_client_handshake: got bad HTTP response '%s'\n", p);
518 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
520 lwsl_info("no UPGRADE\n");
524 if (strcmp(p, "websocket")) {
526 "lws_client_handshake: got bad Upgrade header '%s'\n", p);
530 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
532 lwsl_info("no Connection hdr\n");
536 if (strcmp(p, "upgrade")) {
537 lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
541 pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
543 lwsl_parser("lws_client_int_s_hs: no protocol list\n");
545 lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
548 * confirm the protocol the server wants to talk was in the list
549 * of protocols we offered
552 len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
554 lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
556 * no protocol name to work from,
557 * default to first protocol
559 wsi->protocol = &wsi->vhost->protocols[0];
560 goto check_extensions;
563 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
566 while (pc && *pc && !okay) {
567 if (!strncmp(pc, p, len) &&
568 (pc[len] == ',' || pc[len] == '\0')) {
572 while (*pc && *pc++ != ',')
574 while (*pc && *pc == ' ')
579 lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
584 * identify the selected protocol struct and set it
587 wsi->protocol = NULL;
588 while (wsi->vhost->protocols[n].callback && !wsi->protocol) {
589 if (strcmp(p, wsi->vhost->protocols[n].name) == 0) {
590 wsi->protocol = &wsi->vhost->protocols[n];
596 if (wsi->protocol == NULL) {
597 lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
603 #ifndef LWS_NO_EXTENSIONS
604 /* instantiate the accepted extensions */
606 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
607 lwsl_ext("no client extensions allowed by server\n");
612 * break down the list of server accepted extensions
613 * and go through matching them or identifying bogons
616 if (lws_hdr_copy(wsi, sb, LWS_MAX_SOCKET_IO_BUF, WSI_TOKEN_EXTENSIONS) < 0) {
617 lwsl_warn("ext list from server failed to copy\n");
627 if (*c && (*c != ',' && *c != '\t')) {
633 if (ignore || *c == ' ') {
639 if (n < sizeof(ext_name) - 1)
653 /* check we actually support it */
655 lwsl_notice("checking client ext %s\n", ext_name);
658 ext = wsi->vhost->extensions;
659 while (ext && ext->callback) {
660 if (strcmp(ext_name, ext->name)) {
666 lwsl_notice("instantiating client ext %s\n", ext_name);
668 /* instantiate the extension on this conn */
670 wsi->active_extensions[wsi->count_act_ext] = ext;
672 /* allow him to construct his ext instance */
674 if (ext->callback(lws_get_context(wsi), ext, wsi,
675 LWS_EXT_CB_CLIENT_CONSTRUCT,
676 (void *)&wsi->act_ext_user[wsi->count_act_ext],
678 lwsl_notice(" ext %s failed construction\n", ext_name);
684 * allow the user code to override ext defaults if it
688 if (user_callback_handle_rxflow(wsi->protocol->callback,
689 wsi, LWS_CALLBACK_WS_EXT_DEFAULTS,
690 (char *)ext->name, ext_name,
695 lws_ext_parse_options(ext, wsi, wsi->act_ext_user[
696 wsi->count_act_ext], opts, ext_name,
698 lwsl_err("%s: unable to parse user defaults '%s'",
704 * give the extension the server options
706 if (a && lws_ext_parse_options(ext, wsi,
707 wsi->act_ext_user[wsi->count_act_ext],
709 lwsl_err("%s: unable to parse remote def '%s'",
714 if (ext->callback(lws_get_context(wsi), ext, wsi,
715 LWS_EXT_CB_OPTION_CONFIRM,
716 wsi->act_ext_user[wsi->count_act_ext],
718 lwsl_err("%s: ext %s rejects server options %s",
723 wsi->count_act_ext++;
729 lwsl_warn("Unknown ext '%s'!\n", ext_name);
741 * Confirm his accept token is the one we precomputed
744 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
745 if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
746 lwsl_warn("lws_client_int_s_hs: accept '%s' wrong vs '%s'\n", p,
747 wsi->u.hdr.ah->initial_handshake_hash_base64);
751 /* allocate the per-connection user memory (if any) */
752 if (lws_ensure_user_space(wsi)) {
753 lwsl_err("Problem allocating wsi user mem\n");
758 * we seem to be good to go, give client last chance to check
761 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
762 wsi->user_space, NULL, 0))
765 /* clear his proxy connection timeout */
766 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
768 /* free up his parsing allocations */
769 lws_header_table_detach(wsi, 0);
771 lws_union_transition(wsi, LWSCM_WS_CLIENT);
772 wsi->state = LWSS_ESTABLISHED;
774 wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
777 * create the frame buffer for this connection according to the
778 * size mentioned in the protocol definition. If 0 there, then
779 * use a big default for compatibility
781 n = wsi->protocol->rx_buffer_size;
783 n = LWS_MAX_SOCKET_IO_BUF;
785 wsi->u.ws.rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */);
786 if (!wsi->u.ws.rx_ubuf) {
787 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
790 wsi->u.ws.rx_ubuf_alloc = n;
791 lwsl_info("Allocating client RX buffer %d\n", n);
793 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n,
795 lwsl_warn("Failed to set SNDBUF to %d", n);
799 lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
801 /* call him back to inform him he is up */
803 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_ESTABLISHED,
804 wsi->user_space, NULL, 0))
806 #ifndef LWS_NO_EXTENSIONS
808 * inform all extensions, not just active ones since they
811 ext = wsi->vhost->extensions;
813 while (ext && ext->callback) {
815 for (n = 0; n < wsi->count_act_ext; n++)
816 if (wsi->active_extensions[n] == ext)
817 v = wsi->act_ext_user[n];
819 ext->callback(context, ext, wsi,
820 LWS_EXT_CB_ANY_WSI_ESTABLISHED, v, NULL, 0);
828 close_reason = LWS_CLOSE_STATUS_NOSTATUS;
831 if (wsi->protocol && wsi->state == LWSS_ESTABLISHED) {
832 if (isErrorCodeReceived && p) {
833 wsi->protocol->callback(wsi,
834 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
836 (unsigned int)strlen(p));
838 wsi->protocol->callback(wsi,
839 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
840 wsi->user_space, NULL, 0);
844 lwsl_info("closing connection due to bail2 connection error\n");
846 /* closing will free up his parsing allocations */
847 lws_close_free_wsi(wsi, close_reason);
854 lws_generate_client_handshake(struct lws *wsi, char *pkt)
856 char buf[128], hash[20], key_b64[40], *p = pkt;
857 struct lws_context *context = wsi->context;
860 #ifndef LWS_NO_EXTENSIONS
861 const struct lws_extension *ext;
865 meth = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD);
874 * create the random key
876 n = lws_get_random(context, hash, 16);
878 lwsl_err("Unable to read from random dev %s\n",
879 SYSTEM_RANDOM_FILEPATH);
880 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
884 lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
888 * 04 example client handshake
891 * Host: server.example.com
893 * Connection: Upgrade
894 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
895 * Sec-WebSocket-Origin: http://example.com
896 * Sec-WebSocket-Protocol: chat, superchat
897 * Sec-WebSocket-Version: 4
900 p += sprintf(p, "%s %s HTTP/1.1\x0d\x0a", meth,
901 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
903 p += sprintf(p, "Pragma: no-cache\x0d\x0a"
904 "Cache-Control: no-cache\x0d\x0a");
906 p += sprintf(p, "Host: %s\x0d\x0a",
907 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
909 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
910 p += sprintf(p, "Origin: http://%s\x0d\x0a",
911 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
914 p += sprintf(p, "Upgrade: websocket\x0d\x0a"
915 "Connection: Upgrade\x0d\x0a"
916 "Sec-WebSocket-Key: ");
918 p += strlen(key_b64);
919 p += sprintf(p, "\x0d\x0a");
920 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
921 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
922 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
924 /* tell the server what extensions we could support */
926 #ifndef LWS_NO_EXTENSIONS
927 ext = wsi->vhost->extensions;
928 while (ext && ext->callback) {
929 n = lws_ext_cb_all_exts(context, wsi,
930 LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION,
931 (char *)ext->name, 0);
932 if (n) { /* an extension vetos us */
933 lwsl_ext("ext %s vetoed\n", (char *)ext->name);
937 n = wsi->vhost->protocols[0].callback(wsi,
938 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
939 wsi->user_space, (char *)ext->name, 0);
942 * zero return from callback means
943 * go ahead and allow the extension,
944 * it's what we get if the callback is
958 p += sprintf(p, "Sec-WebSocket-Extensions: ");
959 p += sprintf(p, "%s", ext->client_offer);
965 p += sprintf(p, "\x0d\x0a");
968 if (wsi->ietf_spec_revision)
969 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
970 wsi->ietf_spec_revision);
972 /* prepare the expected server accept response */
974 key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
975 n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
977 lws_SHA1((unsigned char *)buf, n, (unsigned char *)hash);
979 lws_b64_encode_string(hash, 20,
980 wsi->u.hdr.ah->initial_handshake_hash_base64,
981 sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
984 /* give userland a chance to append, eg, cookies */
986 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
987 NULL, &p, (pkt + LWS_MAX_SOCKET_IO_BUF) - p - 12);
989 p += sprintf(p, "\x0d\x0a");