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"
24 int lws_handshake_client(struct libwebsocket *wsi, unsigned char **buf, size_t len)
29 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
30 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
31 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
32 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
33 case LWS_CONNMODE_WS_CLIENT:
34 for (n = 0; n < len; n++)
35 if (libwebsocket_client_rx_sm(wsi, *(*buf)++)) {
36 lwsl_debug("client_rx_sm failed\n");
46 int lws_client_socket_service(struct libwebsocket_context *context,
47 struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd)
50 char *p = (char *)&context->service_buffer[0];
56 case LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT:
59 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
60 * timeout protection set in client-handshake.c
63 if (lws_client_connect_2(context, wsi) == NULL) {
65 lwsl_client("closed\n");
69 /* either still pending connection, or changed mode */
72 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
74 /* handle proxy hung up on us */
76 if (pollfd->revents & LWS_POLLHUP) {
78 lwsl_warn("Proxy connection %p (fd=%d) dead\n",
79 (void *)wsi, pollfd->fd);
81 libwebsocket_close_and_free_session(context, wsi,
82 LWS_CLOSE_STATUS_NOSTATUS);
86 n = recv(wsi->sock, (char *)context->service_buffer,
87 sizeof(context->service_buffer), 0);
90 if (LWS_ERRNO == LWS_EAGAIN) {
92 "Proxy read returned EAGAIN... retrying\n");
96 libwebsocket_close_and_free_session(context, wsi,
97 LWS_CLOSE_STATUS_NOSTATUS);
98 lwsl_err("ERROR reading from proxy socket\n");
102 context->service_buffer[13] = '\0';
103 if (strcmp((char *)context->service_buffer, "HTTP/1.0 200 ") &&
104 strcmp((char *)context->service_buffer, "HTTP/1.1 200 ")
106 libwebsocket_close_and_free_session(context, wsi,
107 LWS_CLOSE_STATUS_NOSTATUS);
108 lwsl_err("ERROR proxy: %s\n", context->service_buffer);
112 /* clear his proxy connection timeout */
114 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
118 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
121 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
122 * timeout protection set in client-handshake.c
126 * take care of our lws_callback_on_writable
127 * happening at a time when there's no real connection yet
129 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
131 lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE);
133 #ifdef LWS_OPENSSL_SUPPORT
134 /* we can retry this... just cook the SSL BIO the first time */
136 if (wsi->use_ssl && !wsi->ssl) {
137 #if defined(CYASSL_SNI_HOST_NAME) || defined(WOLFSSL_SNI_HOST_NAME) || defined(SSL_CTRL_SET_TLSEXT_HOSTNAME)
138 const char *hostname = lws_hdr_simple_ptr(wsi,
139 _WSI_TOKEN_CLIENT_HOST);
142 wsi->ssl = SSL_new(context->ssl_client_ctx);
144 SSL_set_mode(wsi->ssl,
145 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
148 * use server name indication (SNI), if supported,
149 * when establishing connection
152 #ifdef USE_OLD_CYASSL
153 #ifdef CYASSL_SNI_HOST_NAME
154 CyaSSL_UseSNI(wsi->ssl, CYASSL_SNI_HOST_NAME,
155 hostname, strlen(hostname));
158 #ifdef WOLFSSL_SNI_HOST_NAME
159 wolfSSL_UseSNI(wsi->ssl, WOLFSSL_SNI_HOST_NAME,
160 hostname, strlen(hostname));
164 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
165 SSL_set_tlsext_host_name(wsi->ssl, hostname);
171 * wolfSSL/CyaSSL does certificate verification differently
173 * If we should ignore the certificate, we need to set
174 * this before SSL_new and SSL_connect is called.
175 * Otherwise the connect will simply fail with error
178 #ifdef USE_OLD_CYASSL
179 if (wsi->use_ssl == 2)
180 CyaSSL_set_verify(wsi->ssl,
181 SSL_VERIFY_NONE, NULL);
183 if (wsi->use_ssl == 2)
184 wolfSSL_set_verify(wsi->ssl,
185 SSL_VERIFY_NONE, NULL);
187 #endif /* USE_WOLFSSL */
190 BIO_new_socket(wsi->sock, BIO_NOCLOSE);
191 SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
194 #ifdef USE_OLD_CYASSL
195 CyaSSL_set_using_nonblock(wsi->ssl, 1);
197 wolfSSL_set_using_nonblock(wsi->ssl, 1);
200 BIO_set_nbio(wsi->client_bio, 1); /* nonblocking */
203 SSL_set_ex_data(wsi->ssl,
204 openssl_websocket_private_data_index,
209 lws_latency_pre(context, wsi);
210 n = SSL_connect(wsi->ssl);
211 lws_latency(context, wsi,
212 "SSL_connect LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE",
216 n = SSL_get_error(wsi->ssl, n);
218 if (n == SSL_ERROR_WANT_READ)
221 if (n == SSL_ERROR_WANT_WRITE) {
223 * wants us to retry connect due to
224 * state of the underlying ssl layer...
225 * but since it may be stalled on
226 * blocked write, no incoming data may
227 * arrive to trigger the retry.
228 * Force (possibly many times if the SSL
229 * state persists in returning the
230 * condition code, but other sockets
231 * are getting serviced inbetweentimes)
232 * us to get called back when writable.
236 "SSL_connect WANT_WRITE... retrying\n");
237 lws_callback_on_writable(
240 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SSL;
242 return 0; /* no error */
249 * retry if new data comes until we
250 * run into the connection timeout or win
254 if (n != SSL_ERROR_NONE) {
255 lwsl_err("SSL connect error %lu: %s\n",
258 (char *)context->service_buffer));
267 case LWS_CONNMODE_WS_CLIENT_WAITING_SSL:
271 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_SSL) {
272 lws_latency_pre(context, wsi);
273 n = SSL_connect(wsi->ssl);
274 lws_latency(context, wsi,
275 "SSL_connect LWS_CONNMODE_WS_CLIENT_WAITING_SSL",
279 n = SSL_get_error(wsi->ssl, n);
281 if (n == SSL_ERROR_WANT_READ)
284 if (n == SSL_ERROR_WANT_WRITE) {
286 * wants us to retry connect due to
287 * state of the underlying ssl layer...
288 * but since it may be stalled on
289 * blocked write, no incoming data may
290 * arrive to trigger the retry.
291 * Force (possibly many times if the SSL
292 * state persists in returning the
293 * condition code, but other sockets
294 * are getting serviced inbetweentimes)
295 * us to get called back when writable.
298 lwsl_info("SSL_connect WANT_WRITE... retrying\n");
299 lws_callback_on_writable(context, wsi);
308 * retry if new data comes until we
309 * run into the connection timeout or win
312 if (n != SSL_ERROR_NONE) {
313 lwsl_err("SSL connect error %lu: %s\n",
314 n, ERR_error_string(n,
315 (char *)context->service_buffer));
323 * See comment above about wolfSSL certificate
326 lws_latency_pre(context, wsi);
327 n = SSL_get_verify_result(wsi->ssl);
328 lws_latency(context, wsi,
329 "SSL_get_verify_result LWS_CONNMODE..HANDSHAKE",
332 if (n != X509_V_OK) {
333 if ((n == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
334 n == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) && wsi->use_ssl == 2) {
335 lwsl_notice("accepting self-signed certificate\n");
337 lwsl_err("server's cert didn't look good, X509_V_ERR = %d: %s\n",
338 n, ERR_error_string(n, (char *)context->service_buffer));
339 libwebsocket_close_and_free_session(context,
340 wsi, LWS_CLOSE_STATUS_NOSTATUS);
344 #endif /* USE_WOLFSSL */
349 wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2;
351 PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
356 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2:
357 p = libwebsockets_generate_client_handshake(context, wsi, p);
359 lwsl_err("Failed to generate handshake for client\n");
360 libwebsocket_close_and_free_session(context, wsi,
361 LWS_CLOSE_STATUS_NOSTATUS);
365 /* send our request to the server */
367 lws_latency_pre(context, wsi);
369 n = lws_ssl_capable_write(wsi, context->service_buffer, p - (char *)context->service_buffer);
370 lws_latency(context, wsi, "send lws_issue_raw", n, n == p - (char *)context->service_buffer);
372 case LWS_SSL_CAPABLE_ERROR:
373 lwsl_debug("ERROR writing to client socket\n");
374 libwebsocket_close_and_free_session(context, wsi,
375 LWS_CLOSE_STATUS_NOSTATUS);
377 case LWS_SSL_CAPABLE_MORE_SERVICE:
378 lws_callback_on_writable(context, wsi);
382 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
383 wsi->u.hdr.lextable_pos = 0;
384 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
386 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
390 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
392 /* handle server hung up on us */
394 if (pollfd->revents & LWS_POLLHUP) {
396 lwsl_debug("Server connection %p (fd=%d) dead\n",
397 (void *)wsi, pollfd->fd);
402 if (!(pollfd->revents & LWS_POLLIN))
405 /* interpret the server response */
408 * HTTP/1.1 101 Switching Protocols
410 * Connection: Upgrade
411 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
412 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
413 * Sec-WebSocket-Protocol: chat
417 * we have to take some care here to only take from the
418 * socket bytewise. The browser may (and has been seen to
419 * in the case that onopen() performs websocket traffic)
420 * coalesce both handshake response and websocket traffic
421 * in one packet, since at that point the connection is
422 * definitively ready from browser pov.
425 while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
427 n = lws_ssl_capable_read(context, wsi, &c, 1);
428 lws_latency(context, wsi, "send lws_issue_raw", n, n == 1);
430 case LWS_SSL_CAPABLE_ERROR:
432 case LWS_SSL_CAPABLE_MORE_SERVICE:
436 if (libwebsocket_parse(context, wsi, c)) {
437 lwsl_warn("problems parsing header\n");
443 * hs may also be coming in multiple packets, there is a 5-sec
444 * libwebsocket timeout still active here too, so if parsing did
445 * not complete just wait for next packet coming in this state
448 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
452 * otherwise deal with the handshake. If there's any
453 * packet traffic already arrived we'll trigger poll() again
454 * right away and deal with it that way
457 return lws_client_interpret_server_handshake(context, wsi);
461 "closing connection at LWS_CONNMODE...SERVER_REPLY\n");
462 libwebsocket_close_and_free_session(context, wsi,
463 LWS_CLOSE_STATUS_NOSTATUS);
466 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
467 lwsl_ext("LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
470 case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
471 lwsl_ext("LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
482 * In-place str to lower case
489 *s = tolower((int)*s);
495 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
496 struct libwebsocket *wsi)
502 int isErrorCodeReceived = 0;
503 #ifndef LWS_NO_EXTENSIONS
505 struct libwebsocket_extension *ext;
511 int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
514 * well, what the server sent looked reasonable for syntax.
515 * Now let's confirm it sent all the necessary headers
518 if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
519 lwsl_info("no ACCEPT\n");
520 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
521 isErrorCodeReceived = 1;
525 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
527 lwsl_info("no URI\n");
530 if (p && strncmp(p, "101", 3)) {
532 "lws_client_handshake: got bad HTTP response '%s'\n", p);
536 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
538 lwsl_info("no UPGRADE\n");
542 if (strcmp(p, "websocket")) {
544 "lws_client_handshake: got bad Upgrade header '%s'\n", p);
548 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
550 lwsl_info("no Connection hdr\n");
554 if (strcmp(p, "upgrade")) {
555 lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
559 pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
561 lwsl_parser("lws_client_int_s_hs: no protocol list\n");
563 lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
566 * confirm the protocol the server wants to talk was in the list
567 * of protocols we offered
570 len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
573 lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
575 * no protocol name to work from,
576 * default to first protocol
578 wsi->protocol = &context->protocols[0];
579 goto check_extensions;
582 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
585 while (pc && *pc && !okay) {
586 if (!strncmp(pc, p, len) &&
587 (pc[len] == ',' || pc[len] == '\0')) {
591 while (*pc && *pc++ != ',')
593 while (*pc && *pc == ' ')
598 lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
603 * identify the selected protocol struct and set it
606 wsi->protocol = NULL;
607 while (context->protocols[n].callback && !wsi->protocol) {
608 if (strcmp(p, context->protocols[n].name) == 0) {
609 wsi->protocol = &context->protocols[n];
615 if (wsi->protocol == NULL) {
616 lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
622 #ifndef LWS_NO_EXTENSIONS
623 /* instantiate the accepted extensions */
625 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
626 lwsl_ext("no client extenstions allowed by server\n");
631 * break down the list of server accepted extensions
632 * and go through matching them or identifying bogons
635 if (lws_hdr_copy(wsi, (char *)context->service_buffer,
636 sizeof(context->service_buffer), WSI_TOKEN_EXTENSIONS) < 0) {
637 lwsl_warn("ext list from server failed to copy\n");
641 c = (char *)context->service_buffer;
645 if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
647 if (n < sizeof(ext_name) - 1)
660 /* check we actually support it */
662 lwsl_ext("checking client ext %s\n", ext_name);
665 ext = wsi->protocol->owning_server->extensions;
666 while (ext && ext->callback) {
668 if (strcmp(ext_name, ext->name)) {
675 lwsl_ext("instantiating client ext %s\n", ext_name);
677 /* instantiate the extension on this conn */
679 wsi->active_extensions_user[
680 wsi->count_active_extensions] =
681 lws_zalloc(ext->per_session_data_size);
682 if (wsi->active_extensions_user[
683 wsi->count_active_extensions] == NULL) {
684 lwsl_err("Out of mem\n");
687 wsi->active_extensions[
688 wsi->count_active_extensions] = ext;
690 /* allow him to construct his context */
692 ext->callback(wsi->protocol->owning_server,
694 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
695 wsi->active_extensions_user[
696 wsi->count_active_extensions],
699 wsi->count_active_extensions++;
705 lwsl_warn("Unknown ext '%s'!\n", ext_name);
716 * Confirm his accept token is the one we precomputed
719 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
720 if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
721 lwsl_warn("lws_client_int_s_hs: accept %s wrong vs %s\n", p,
722 wsi->u.hdr.ah->initial_handshake_hash_base64);
726 /* allocate the per-connection user memory (if any) */
727 if (libwebsocket_ensure_user_space(wsi)) {
728 lwsl_err("Problem allocating wsi user mem\n");
733 * we seem to be good to go, give client last chance to check
737 wsi->protocol->callback(context, wsi,
738 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
739 wsi->user_space, NULL, 0);
741 /* clear his proxy connection timeout */
743 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
745 /* free up his parsing allocations */
747 lws_free(wsi->u.hdr.ah);
749 lws_union_transition(wsi, LWS_CONNMODE_WS_CLIENT);
750 wsi->state = WSI_STATE_ESTABLISHED;
752 wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
755 * create the frame buffer for this connection according to the
756 * size mentioned in the protocol definition. If 0 there, then
757 * use a big default for compatibility
760 n = wsi->protocol->rx_buffer_size;
762 n = LWS_MAX_SOCKET_IO_BUF;
763 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
764 wsi->u.ws.rx_user_buffer = lws_malloc(n);
765 if (!wsi->u.ws.rx_user_buffer) {
766 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
769 lwsl_info("Allocating client RX buffer %d\n", n);
771 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof n)) {
772 lwsl_warn("Failed to set SNDBUF to %d", n);
776 lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
778 /* call him back to inform him he is up */
780 wsi->protocol->callback(context, wsi,
781 LWS_CALLBACK_CLIENT_ESTABLISHED,
782 wsi->user_space, NULL, 0);
783 #ifndef LWS_NO_EXTENSIONS
785 * inform all extensions, not just active ones since they
789 ext = context->extensions;
791 while (ext && ext->callback) {
793 for (n = 0; n < wsi->count_active_extensions; n++)
794 if (wsi->active_extensions[n] == ext)
795 v = wsi->active_extensions_user[n];
797 ext->callback(context, ext, wsi,
798 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
806 lws_free2(wsi->u.ws.rx_user_buffer);
807 close_reason = LWS_CLOSE_STATUS_NOSTATUS;
811 if (isErrorCodeReceived && p) {
812 wsi->protocol->callback(context, wsi,
813 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
814 wsi->user_space, p, (unsigned int)strlen(p));
816 wsi->protocol->callback(context, wsi,
817 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
818 wsi->user_space, NULL, 0);
822 lwsl_info("closing connection due to bail2 connection error\n");
824 /* free up his parsing allocations */
826 lws_free2(wsi->u.hdr.ah);
828 libwebsocket_close_and_free_session(context, wsi, close_reason);
835 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
836 struct libwebsocket *wsi, char *pkt)
843 #ifndef LWS_NO_EXTENSIONS
844 struct libwebsocket_extension *ext;
849 * create the random key
852 n = lws_get_random(context, hash, 16);
854 lwsl_err("Unable to read from random dev %s\n",
855 SYSTEM_RANDOM_FILEPATH);
856 libwebsocket_close_and_free_session(context, wsi,
857 LWS_CLOSE_STATUS_NOSTATUS);
861 lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
864 * 00 example client handshake
866 * GET /socket.io/websocket HTTP/1.1
868 * Connection: Upgrade
869 * Host: 127.0.0.1:9999
870 * Origin: http://127.0.0.1
871 * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^
872 * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
873 * Cookie: socketio=websocket
877 * 04 example client handshake
880 * Host: server.example.com
882 * Connection: Upgrade
883 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
884 * Sec-WebSocket-Origin: http://example.com
885 * Sec-WebSocket-Protocol: chat, superchat
886 * Sec-WebSocket-Version: 4
889 p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a",
890 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
893 "Pragma: no-cache\x0d\x0a""Cache-Control: no-cache\x0d\x0a");
895 p += sprintf(p, "Host: %s\x0d\x0a",
896 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
898 "Upgrade: websocket\x0d\x0a""Connection: Upgrade\x0d\x0a""Sec-WebSocket-Key: ");
900 p += strlen(key_b64);
901 p += sprintf(p, "\x0d\x0a");
902 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
903 p += sprintf(p, "Origin: http://%s\x0d\x0a",
904 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
906 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
907 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
908 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
910 /* tell the server what extensions we could support */
912 p += sprintf(p, "Sec-WebSocket-Extensions: ");
913 #ifndef LWS_NO_EXTENSIONS
914 ext = context->extensions;
915 while (ext && ext->callback) {
917 n = lws_ext_callback_for_each_extension_type(context, wsi,
918 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
919 (char *)ext->name, 0);
920 if (n) { /* an extension vetos us */
921 lwsl_ext("ext %s vetoed\n", (char *)ext->name);
926 n = context->protocols[0].callback(context, wsi,
927 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
928 wsi->user_space, (char *)ext->name, 0);
931 * zero return from callback means
932 * go ahead and allow the extension,
933 * it's what we get if the callback is
946 p += sprintf(p, "%s", ext->name);
952 p += sprintf(p, "\x0d\x0a");
954 if (wsi->ietf_spec_revision)
955 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
956 wsi->ietf_spec_revision);
958 /* give userland a chance to append, eg, cookies */
960 context->protocols[0].callback(context, wsi,
961 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
962 NULL, &p, (pkt + sizeof(context->service_buffer)) - p - 12);
964 p += sprintf(p, "\x0d\x0a");
966 /* prepare the expected server accept response */
968 key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
969 n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
971 lws_SHA1((unsigned char *)buf, n, (unsigned char *)hash);
973 lws_b64_encode_string(hash, 20,
974 wsi->u.hdr.ah->initial_handshake_hash_base64,
975 sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));