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"
28 #ifdef LWS_BUILTIN_GETIFADDRS
29 #include <getifaddrs.h>
34 #include <sys/socket.h>
38 #ifdef LWS_OPENSSL_SUPPORT
39 extern int openssl_websocket_private_data_index;
42 int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd)
49 #ifdef LWS_OPENSSL_SUPPORT
50 char ssl_err_buf[512];
55 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
57 /* handle proxy hung up on us */
59 if (pollfd->revents & (POLLERR | POLLHUP)) {
61 lwsl_warn("Proxy connection %p (fd=%d) dead\n",
62 (void *)wsi, pollfd->fd);
64 libwebsocket_close_and_free_session(context, wsi,
65 LWS_CLOSE_STATUS_NOSTATUS);
69 n = recv(wsi->sock, pkt, sizeof pkt, 0);
71 libwebsocket_close_and_free_session(context, wsi,
72 LWS_CLOSE_STATUS_NOSTATUS);
73 lwsl_err("ERROR reading from proxy socket\n");
78 if (strcmp(pkt, "HTTP/1.0 200 ") != 0) {
79 libwebsocket_close_and_free_session(context, wsi,
80 LWS_CLOSE_STATUS_NOSTATUS);
81 lwsl_err("ERROR from proxy: %s\n", pkt);
85 /* clear his proxy connection timeout */
87 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
91 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
94 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
95 * timeout protection set in client-handshake.c
98 #ifdef LWS_OPENSSL_SUPPORT
101 * take care of our libwebsocket_callback_on_writable
102 * happening at a time when there's no real connection yet
105 pollfd->events &= ~POLLOUT;
107 /* external POLL support via protocol 0 */
108 context->protocols[0].callback(context, wsi,
109 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
110 (void *)(long)wsi->sock, NULL, POLLOUT);
112 /* we can retry this... so just cook the SSL BIO the first time */
114 if (wsi->use_ssl && !wsi->ssl) {
116 wsi->ssl = SSL_new(context->ssl_client_ctx);
117 wsi->client_bio = BIO_new_socket(wsi->sock,
119 SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
121 SSL_set_ex_data(wsi->ssl,
122 openssl_websocket_private_data_index,
127 n = SSL_connect(wsi->ssl);
130 n = SSL_get_error(wsi->ssl, n);
132 if (n == SSL_ERROR_WANT_READ ||
133 n == SSL_ERROR_WANT_WRITE) {
135 * wants us to retry connect due to state of the
136 * underlying ssl layer... but since it may be
137 * stalled on blocked write, no incoming data may
138 * arrive to trigger the retry. Force (possibly
139 * many if the SSL state persists in returning the
140 * condition code, but other sockets are getting
141 * serviced inbetweentimes) us to get called back
145 lwsl_info("SSL_connect -> SSL_ERROR_WANT_... retrying\n");
146 libwebsocket_callback_on_writable(context, wsi);
148 return 0; /* no error */
155 * retry if new data comes until we
156 * run into the connection timeout or win
159 lwsl_err("SSL connect error %s\n",
160 ERR_error_string(ERR_get_error(),
165 n = SSL_get_verify_result(wsi->ssl);
166 if ((n != X509_V_OK) && (
167 n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
168 wsi->use_ssl != 2)) {
170 lwsl_err("server's cert didn't "
171 "look good %d\n", n);
172 libwebsocket_close_and_free_session(context,
173 wsi, LWS_CLOSE_STATUS_NOSTATUS);
180 p = libwebsockets_generate_client_handshake(context, wsi, p);
182 lwsl_err("Failed to generate handshake for client, closing it\n");
183 libwebsocket_close_and_free_session(context, wsi,
184 LWS_CLOSE_STATUS_NOSTATUS);
188 /* send our request to the server */
190 #ifdef LWS_OPENSSL_SUPPORT
192 n = SSL_write(wsi->ssl, pkt, p - pkt);
195 n = send(wsi->sock, pkt, p - pkt, 0);
198 lwsl_debug("ERROR writing to client socket\n");
199 libwebsocket_close_and_free_session(context, wsi,
200 LWS_CLOSE_STATUS_NOSTATUS);
204 wsi->parser_state = WSI_TOKEN_NAME_PART;
205 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
206 libwebsocket_set_timeout(wsi,
207 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, AWAITING_TIMEOUT);
211 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
213 /* handle server hung up on us */
215 if (pollfd->revents & (POLLERR | POLLHUP)) {
217 lwsl_debug("Server connection %p (fd=%d) dead\n",
218 (void *)wsi, pollfd->fd);
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.
245 while (wsi->parser_state != WSI_PARSING_COMPLETE && len > 0) {
246 #ifdef LWS_OPENSSL_SUPPORT
248 len = SSL_read(wsi->ssl, &c, 1);
251 len = recv(wsi->sock, &c, 1, 0);
253 libwebsocket_parse(wsi, c);
257 * hs may also be coming in multiple packets, there is a 5-sec
258 * libwebsocket timeout still active here too, so if parsing did
259 * not complete just wait for next packet coming in this state
262 if (wsi->parser_state != WSI_PARSING_COMPLETE)
266 * otherwise deal with the handshake. If there's any
267 * packet traffic already arrived we'll trigger poll() again
268 * right away and deal with it that way
271 return lws_client_interpret_server_handshake(context, wsi);
275 free(wsi->c_protocol);
276 libwebsocket_close_and_free_session(context, wsi,
277 LWS_CLOSE_STATUS_NOSTATUS);
280 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
281 lwsl_ext("LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
284 case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
285 lwsl_ext("LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
296 * In-place str to lower case
309 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
310 struct libwebsocket *wsi)
312 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
313 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
321 struct libwebsocket_extension *ext;
325 static const char magic_websocket_04_masking_guid[] =
326 "61AC5F19-FBBA-4540-B96F-6561F1AB40A8";
331 * HTTP/1.1 101 WebSocket Protocol Handshake
333 * Connection: Upgrade
334 * Sec-WebSocket-Origin: http://127.0.0.1
335 * Sec-WebSocket-Location: ws://127.0.0.1:9999/socket.io/websocket
340 if (wsi->ietf_spec_revision == 0) {
341 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
342 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
343 !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
344 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
345 (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
346 wsi->c_protocol != NULL)) {
347 lwsl_parser("libwebsocket_client_handshake "
348 "missing required header(s)\n");
350 lwsl_parser("%s", pkt);
354 strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
355 if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
356 lwsl_warn("libwebsocket_client_handshake "
357 "server sent bad HTTP response '%s'\n",
358 wsi->utf8_token[WSI_TOKEN_HTTP].token);
362 if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < 16) {
363 lwsl_parser("libwebsocket_client_handshake "
364 "challenge reply too short %d\n",
366 WSI_TOKEN_CHALLENGE].token_len);
368 lwsl_parser("%s", pkt);
373 goto select_protocol;
377 * well, what the server sent looked reasonable for syntax.
378 * Now let's confirm it sent all the necessary headers
381 lwsl_parser("WSI_TOKEN_HTTP: %d\n",
382 wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
383 lwsl_parser("WSI_TOKEN_UPGRADE: %d\n",
384 wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
385 lwsl_parser("WSI_TOKEN_CONNECTION: %d\n",
386 wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
387 lwsl_parser("WSI_TOKEN_ACCEPT: %d\n",
388 wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
389 lwsl_parser("WSI_TOKEN_NONCE: %d\n",
390 wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
391 lwsl_parser("WSI_TOKEN_PROTOCOL: %d\n",
392 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
394 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
395 !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
396 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
397 !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
398 (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
399 wsi->ietf_spec_revision == 4) ||
400 (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
401 wsi->c_protocol != NULL)) {
402 lwsl_parser("libwebsocket_client_handshake "
403 "missing required header(s)\n");
405 lwsl_parser("%s", pkt);
410 * Everything seems to be there, now take a closer look at what
414 strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
415 if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
416 lwsl_warn("libwebsocket_client_handshake "
417 "server sent bad HTTP response '%s'\n",
418 wsi->utf8_token[WSI_TOKEN_HTTP].token);
422 strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
423 if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
425 lwsl_warn("libwebsocket_client_handshake server "
426 "sent bad Upgrade header '%s'\n",
427 wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
431 strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
432 if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
434 lwsl_warn("libwebsocket_client_handshake server "
435 "sent bad Connection hdr '%s'\n",
436 wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
441 pc = wsi->c_protocol;
443 lwsl_parser("lws_client_interpret_server_handshake: "
444 "NULL c_protocol\n");
446 lwsl_parser("lws_client_interpret_server_handshake: "
447 "cPprotocol='%s'\n", pc);
450 * confirm the protocol the server wants to talk was in the list
451 * of protocols we offered
454 if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
456 lwsl_warn("lws_client_interpret_server_handshake "
457 "WSI_TOKEN_PROTOCOL is null\n");
459 * no protocol name to work from,
460 * default to first protocol
462 wsi->protocol = &context->protocols[0];
463 wsi->c_callback = wsi->protocol->callback;
464 free(wsi->c_protocol);
466 goto check_extensions;
469 while (*pc && !okay) {
470 if ((!strncmp(pc, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
471 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
472 (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
473 pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
477 while (*pc && *pc != ',')
479 while (*pc && *pc != ' ')
483 /* done with him now */
486 free(wsi->c_protocol);
489 lwsl_err("libwebsocket_client_handshake server "
490 "sent bad protocol '%s'\n",
491 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
496 * identify the selected protocol struct and set it
499 wsi->protocol = NULL;
500 while (context->protocols[n].callback && !wsi->protocol) { /* Stop after finding first one?? */
501 if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
502 context->protocols[n].name) == 0) {
503 wsi->protocol = &context->protocols[n];
504 wsi->c_callback = wsi->protocol->callback;
509 if (wsi->protocol == NULL) {
510 lwsl_err("libwebsocket_client_handshake server "
511 "requested protocol '%s', which we "
512 "said we supported but we don't!\n",
513 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
520 /* instantiate the accepted extensions */
522 if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) {
523 lwsl_ext("no client extenstions allowed by server\n");
528 * break down the list of server accepted extensions
529 * and go through matching them or identifying bogons
532 c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
536 if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
538 if (n < sizeof(ext_name) - 1)
551 /* check we actually support it */
553 lwsl_ext("checking client ext %s\n", ext_name);
556 ext = wsi->protocol->owning_server->extensions;
557 while (ext && ext->callback) {
559 if (strcmp(ext_name, ext->name)) {
566 lwsl_ext("instantiating client ext %s\n", ext_name);
568 /* instantiate the extension on this conn */
570 wsi->active_extensions_user[
571 wsi->count_active_extensions] =
572 malloc(ext->per_session_data_size);
573 if (wsi->active_extensions_user[
574 wsi->count_active_extensions] == NULL) {
575 lwsl_err("Out of mem\n");
578 memset(wsi->active_extensions_user[
579 wsi->count_active_extensions], 0,
580 ext->per_session_data_size);
581 wsi->active_extensions[
582 wsi->count_active_extensions] = ext;
584 /* allow him to construct his context */
586 ext->callback(wsi->protocol->owning_server,
588 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
589 wsi->active_extensions_user[
590 wsi->count_active_extensions],
593 wsi->count_active_extensions++;
599 lwsl_warn("Server said we should use"
600 "an unknown extension '%s'!\n", ext_name);
610 if (wsi->ietf_spec_revision == 0) {
612 if (memcmp(wsi->initial_handshake_hash_base64,
613 wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) {
614 lwsl_warn("libwebsocket_client_handshake "
615 "failed 00 challenge compare\n");
617 lwsl_warn("%s", pkt);
625 * Confirm his accept token is the one we precomputed
628 if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
629 wsi->initial_handshake_hash_base64)) {
630 lwsl_warn("libwebsocket_client_handshake server "
631 "sent bad ACCEPT '%s' vs computed '%s'\n",
632 wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
633 wsi->initial_handshake_hash_base64);
637 if (wsi->ietf_spec_revision == 4) {
639 * Calculate the 04 masking key to use when
640 * sending data to server
643 strcpy((char *)buf, wsi->key_b64);
644 p = (char *)buf + strlen(wsi->key_b64);
645 strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token);
646 p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len;
647 strcpy(p, magic_websocket_04_masking_guid);
648 SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
652 /* allocate the per-connection user memory (if any) */
653 if (wsi->protocol->per_session_data_size &&
654 !libwebsocket_ensure_user_space(wsi))
657 /* clear his proxy connection timeout */
659 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
661 /* mark him as being alive */
663 wsi->state = WSI_STATE_ESTABLISHED;
664 wsi->mode = LWS_CONNMODE_WS_CLIENT;
666 lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
668 /* call him back to inform him he is up */
670 wsi->protocol->callback(context, wsi,
671 LWS_CALLBACK_CLIENT_ESTABLISHED,
672 wsi->user_space, NULL, 0);
675 * inform all extensions, not just active ones since they
679 ext = context->extensions;
681 while (ext && ext->callback) {
683 for (n = 0; n < wsi->count_active_extensions; n++)
684 if (wsi->active_extensions[n] == ext)
685 v = wsi->active_extensions_user[n];
687 ext->callback(context, ext, wsi,
688 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
696 free(wsi->c_protocol);
699 if (wsi->c_callback) wsi->c_callback(context, wsi,
700 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
703 libwebsocket_close_and_free_session(context, wsi,
704 LWS_CLOSE_STATUS_NOSTATUS); // But this should be LWS_CLOSE_STATUS_PROTOCOL_ERR
709 void libwebsockets_00_spaceout(char *key, int spaces, int seed)
715 if (*key && (seed & 1))
719 p = key + strlen(key);
728 void libwebsockets_00_spam(char *key, int count, int seed)
735 if (*key && (seed & 1))
739 p = key + strlen(key);
744 *key++ = 0x21 + ((seed & 0xffff) % 15);
745 /* 4 would use it up too fast.. not like it matters */
751 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
752 struct libwebsocket *wsi, char *pkt)
757 struct libwebsocket_extension *ext;
758 struct libwebsocket_extension *ext1;
760 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
761 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
762 static const char magic_websocket_guid[] =
763 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
766 * create the random key
769 n = libwebsockets_get_random(context, hash, 16);
771 lwsl_err("Unable to read from random dev %s\n",
772 SYSTEM_RANDOM_FILEPATH);
778 free(wsi->c_protocol);
779 libwebsocket_close_and_free_session(context, wsi,
780 LWS_CLOSE_STATUS_NOSTATUS);
784 lws_b64_encode_string(hash, 16, wsi->key_b64,
785 sizeof wsi->key_b64);
788 * 00 example client handshake
790 * GET /socket.io/websocket HTTP/1.1
792 * Connection: Upgrade
793 * Host: 127.0.0.1:9999
794 * Origin: http://127.0.0.1
795 * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^
796 * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
797 * Cookie: socketio=websocket
801 * 04 example client handshake
804 * Host: server.example.com
806 * Connection: Upgrade
807 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
808 * Sec-WebSocket-Origin: http://example.com
809 * Sec-WebSocket-Protocol: chat, superchat
810 * Sec-WebSocket-Version: 4
813 p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path);
815 p += sprintf(p, "Pragma: no-cache\x0d\x0a"
816 "Cache-Control: no-cache\x0d\x0a");
818 if (wsi->ietf_spec_revision == 0) {
819 unsigned char spaces_1, spaces_2;
820 unsigned int max_1, max_2;
821 unsigned int num_1, num_2;
822 unsigned long product_1, product_2;
829 libwebsockets_get_random(context, &spaces_1, sizeof(char));
830 libwebsockets_get_random(context, &spaces_2, sizeof(char));
832 spaces_1 = (spaces_1 % 12) + 1;
833 spaces_2 = (spaces_2 % 12) + 1;
835 max_1 = 4294967295 / spaces_1;
836 max_2 = 4294967295 / spaces_2;
838 libwebsockets_get_random(context, &num_1, sizeof(int));
839 libwebsockets_get_random(context, &num_2, sizeof(int));
841 num_1 = (num_1 % max_1);
842 num_2 = (num_2 % max_2);
844 challenge[0] = num_1 >> 24;
845 challenge[1] = num_1 >> 16;
846 challenge[2] = num_1 >> 8;
847 challenge[3] = num_1;
848 challenge[4] = num_2 >> 24;
849 challenge[5] = num_2 >> 16;
850 challenge[6] = num_2 >> 8;
851 challenge[7] = num_2;
853 product_1 = num_1 * spaces_1;
854 product_2 = num_2 * spaces_2;
856 sprintf(key_1, "%lu", product_1);
857 sprintf(key_2, "%lu", product_2);
859 libwebsockets_get_random(context, &seed, sizeof(int));
860 libwebsockets_get_random(context, &count, sizeof(int));
862 libwebsockets_00_spam(key_1, (count % 12) + 1, seed);
864 libwebsockets_get_random(context, &seed, sizeof(int));
865 libwebsockets_get_random(context, &count, sizeof(int));
867 libwebsockets_00_spam(key_2, (count % 12) + 1, seed);
869 libwebsockets_get_random(context, &seed, sizeof(int));
871 libwebsockets_00_spaceout(key_1, spaces_1, seed);
872 libwebsockets_00_spaceout(key_2, spaces_2, seed >> 16);
874 p += sprintf(p, "Upgrade: WebSocket\x0d\x0a"
875 "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a",
878 p += sprintf(p, "Origin: %s\x0d\x0a", wsi->c_origin);
881 p += sprintf(p, "Sec-WebSocket-Protocol: %s"
882 "\x0d\x0a", wsi->c_protocol);
884 p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a", key_1);
885 p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a", key_2);
887 /* give userland a chance to append, eg, cookies */
889 context->protocols[0].callback(context, wsi,
890 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
891 NULL, &p, (pkt + sizeof(pkt)) - p - 12);
893 p += sprintf(p, "\x0d\x0a");
895 if (libwebsockets_get_random(context, p, 8) != 8)
897 memcpy(&challenge[8], p, 8);
900 /* precompute what we want to see from the server */
902 MD5((unsigned char *)challenge, 16,
903 (unsigned char *)wsi->initial_handshake_hash_base64);
908 p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host);
909 p += sprintf(p, "Upgrade: websocket\x0d\x0a"
910 "Connection: Upgrade\x0d\x0a"
911 "Sec-WebSocket-Key: ");
912 strcpy(p, wsi->key_b64);
913 p += strlen(wsi->key_b64);
914 p += sprintf(p, "\x0d\x0a");
916 if (wsi->ietf_spec_revision == 13) {
917 p += sprintf(p, "Origin: %s\x0d\x0a",
921 p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a",
926 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
929 /* tell the server what extensions we could support */
931 p += sprintf(p, "Sec-WebSocket-Extensions: ");
933 ext = context->extensions;
934 while (ext && ext->callback) {
937 ext1 = context->extensions;
939 while (ext1 && ext1->callback) {
940 n |= ext1->callback(context, ext1, wsi,
941 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
942 NULL, (char *)ext->name, 0);
947 if (n) { /* an extension vetos us */
948 lwsl_ext("ext %s vetoed\n", (char *)ext->name);
953 n = context->protocols[0].callback(context, wsi,
954 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
955 wsi->user_space, (char *)ext->name, 0);
958 * zero return from callback means
959 * go ahead and allow the extension,
960 * it's what we get if the callback is
973 p += sprintf(p, "%s", ext->name);
979 p += sprintf(p, "\x0d\x0a");
981 if (wsi->ietf_spec_revision)
982 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
983 wsi->ietf_spec_revision);
985 /* give userland a chance to append, eg, cookies */
987 context->protocols[0].callback(context, wsi,
988 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
989 NULL, &p, (pkt + sizeof(pkt)) - p - 12);
991 p += sprintf(p, "\x0d\x0a");
993 /* prepare the expected server accept response */
995 strcpy((char *)buf, wsi->key_b64);
996 strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid);
998 SHA1(buf, strlen((char *)buf), (unsigned char *)hash);
1000 lws_b64_encode_string(hash, 20,
1001 wsi->initial_handshake_hash_base64,
1002 sizeof wsi->initial_handshake_hash_base64);
1010 /* done with these now */
1015 free(wsi->c_origin);