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 n = lws_ssl_client_connect1(wsi);
165 case LWSCM_WSCL_WAITING_SSL:
168 n = lws_ssl_client_connect2(wsi);
177 wsi->mode = LWSCM_WSCL_ISSUE_HANDSHAKE2;
178 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
179 context->timeout_secs);
183 case LWSCM_WSCL_ISSUE_HANDSHAKE2:
184 p = lws_generate_client_handshake(wsi, p);
186 lwsl_err("Failed to generate handshake for client\n");
187 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
191 /* send our request to the server */
193 lws_latency_pre(context, wsi);
195 n = lws_ssl_capable_write(wsi, (unsigned char *)sb, p - sb);
196 lws_latency(context, wsi, "send lws_issue_raw", n,
199 case LWS_SSL_CAPABLE_ERROR:
200 lwsl_debug("ERROR writing to client socket\n");
201 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
203 case LWS_SSL_CAPABLE_MORE_SERVICE:
204 lws_callback_on_writable(wsi);
208 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
209 wsi->u.hdr.lextable_pos = 0;
210 wsi->mode = LWSCM_WSCL_WAITING_SERVER_REPLY;
211 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
212 context->timeout_secs);
215 case LWSCM_WSCL_WAITING_SERVER_REPLY:
217 /* handle server hung up on us */
219 if (pollfd->revents & LWS_POLLHUP) {
221 lwsl_debug("Server connection %p (fd=%d) dead\n",
222 (void *)wsi, pollfd->fd);
227 if (!(pollfd->revents & LWS_POLLIN))
230 /* interpret the server response */
233 * HTTP/1.1 101 Switching Protocols
235 * Connection: Upgrade
236 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
237 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
238 * Sec-WebSocket-Protocol: chat
242 * we have to take some care here to only take from the
243 * socket bytewise. The browser may (and has been seen to
244 * in the case that onopen() performs websocket traffic)
245 * coalesce both handshake response and websocket traffic
246 * in one packet, since at that point the connection is
247 * definitively ready from browser pov.
250 while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
252 n = lws_ssl_capable_read(wsi, &c, 1);
253 lws_latency(context, wsi, "send lws_issue_raw", n,
257 case LWS_SSL_CAPABLE_ERROR:
259 case LWS_SSL_CAPABLE_MORE_SERVICE:
263 if (lws_parse(wsi, c)) {
264 lwsl_warn("problems parsing header\n");
270 * hs may also be coming in multiple packets, there is a 5-sec
271 * libwebsocket timeout still active here too, so if parsing did
272 * not complete just wait for next packet coming in this state
275 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
279 * otherwise deal with the handshake. If there's any
280 * packet traffic already arrived we'll trigger poll() again
281 * right away and deal with it that way
284 return lws_client_interpret_server_handshake(wsi);
287 lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
288 wsi->vhost->protocols[0].callback(wsi,
289 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
290 wsi->user_space, NULL, 0);
291 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
294 case LWSCM_WSCL_WAITING_EXTENSION_CONNECT:
295 lwsl_ext("LWSCM_WSCL_WAITING_EXTENSION_CONNECT\n");
298 case LWSCM_WSCL_PENDING_CANDIDATE_CHILD:
299 lwsl_ext("LWSCM_WSCL_PENDING_CANDIDATE_CHILD\n");
309 * In-place str to lower case
316 *s = tolower((int)*s);
322 * lws_http_transaction_completed() - wait for new http transaction or close
323 * @wsi: websocket connection
325 * Returns 1 if the HTTP connection must close now
326 * Returns 0 and resets connection to wait for new HTTP header /
327 * transaction if possible
330 int LWS_WARN_UNUSED_RESULT
331 lws_http_transaction_completed_client(struct lws *wsi)
333 lwsl_debug("%s: wsi %p\n", __func__, wsi);
334 /* if we can't go back to accept new headers, drop the connection */
335 if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
336 lwsl_info("%s: %p: close connection\n", __func__, wsi);
340 /* otherwise set ourselves up ready to go again */
341 wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
342 wsi->mode = LWSCM_HTTP_CLIENT_ACCEPTED;
343 wsi->u.http.content_length = 0;
344 wsi->hdr_parsing_completed = 0;
346 /* He asked for it to stay alive indefinitely */
347 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
350 * As client, nothing new is going to come until we ask for it
351 * we can drop the ah, if any
354 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
355 lws_header_table_detach(wsi, 0);
358 /* If we're (re)starting on headers, need other implied init */
359 wsi->u.hdr.ues = URIES_IDLE;
361 lwsl_info("%s: %p: keep-alive await new transaction\n", __func__, wsi);
367 lws_client_interpret_server_handshake(struct lws *wsi)
369 int n, len, okay = 0, isErrorCodeReceived = 0, port = 0, ssl = 0;
370 int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
371 const char *pc, *prot, *ads = NULL, *path;
372 struct allocated_headers *ah;
374 #ifndef LWS_NO_EXTENSIONS
375 struct lws_context *context = wsi->context;
376 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
377 char *sb = (char *)&pt->serv_buf[0];
378 const struct lws_ext_options *opts;
379 const struct lws_extension *ext;
388 /* we are being an http client...
391 lws_union_transition(wsi, LWSCM_HTTP_CLIENT_ACCEPTED);
392 wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
397 * well, what the server sent looked reasonable for syntax.
398 * Now let's confirm it sent all the necessary headers
400 * http (non-ws) client will expect something like this
403 * server:.libwebsockets
404 * content-type:.text/html
405 * content-length:.17703
406 * set-cookie:.test=LWS_1456736240_336776_COOKIE;Max-Age=360000
412 wsi->u.http.connection_type = HTTP_CONNECTION_KEEP_ALIVE;
413 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
414 if (wsi->do_ws && !p) {
415 lwsl_info("no URI\n");
419 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP1_0);
420 wsi->u.http.connection_type = HTTP_CONNECTION_CLOSE;
423 lwsl_info("no URI\n");
427 if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
428 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);
432 if (lws_parse_uri(p, &prot, &ads, &port, &path))
435 if (!strcmp(prot, "wss://") || !strcmp(prot, "https://"))
438 if (lws_client_reset(wsi, ssl, ads, port, path, ads)) {
439 lwsl_err("Redirect failed\n");
447 lwsl_notice("Connection failed with code %d", n);
451 #ifdef LWS_WITH_HTTP_PROXY
452 wsi->perform_rewrite = 0;
453 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
454 if (!strncmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE),
456 wsi->perform_rewrite = 1;
460 /* allocate the per-connection user memory (if any) */
461 if (lws_ensure_user_space(wsi)) {
462 lwsl_err("Problem allocating wsi user mem\n");
466 /* he may choose to send us stuff in chunked transfer-coding */
468 wsi->chunk_remaining = 0; /* ie, next thing is chunk size */
469 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_TRANSFER_ENCODING)) {
470 wsi->chunked = !strcmp(lws_hdr_simple_ptr(wsi,
471 WSI_TOKEN_HTTP_TRANSFER_ENCODING),
473 /* first thing is hex, after payload there is crlf */
474 wsi->chunk_parser = ELCP_HEX;
477 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
478 wsi->u.http.content_length =
479 atoi(lws_hdr_simple_ptr(wsi,
480 WSI_TOKEN_HTTP_CONTENT_LENGTH));
481 lwsl_notice("%s: incoming content length %d\n", __func__,
482 wsi->u.http.content_length);
483 wsi->u.http.content_remain = wsi->u.http.content_length;
484 } else /* can't do 1.1 without a content length or chunked */
486 wsi->u.http.connection_type = HTTP_CONNECTION_CLOSE;
489 * we seem to be good to go, give client last chance to check
492 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
493 wsi->user_space, NULL, 0))
496 /* clear his proxy connection timeout */
497 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
499 wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
501 /* call him back to inform him he is up */
502 if (wsi->protocol->callback(wsi,
503 LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP,
504 wsi->user_space, NULL, 0))
507 /* free up his parsing allocations */
508 lws_header_table_detach(wsi, 0);
510 lwsl_notice("%s: client connection up\n", __func__);
515 if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
516 lwsl_info("no ACCEPT\n");
517 isErrorCodeReceived = 1;
521 if (p && strncmp(p, "101", 3)) {
523 "lws_client_handshake: got bad HTTP response '%s'\n", p);
527 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
529 lwsl_info("no UPGRADE\n");
533 if (strcmp(p, "websocket")) {
535 "lws_client_handshake: got bad Upgrade header '%s'\n", p);
539 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
541 lwsl_info("no Connection hdr\n");
545 if (strcmp(p, "upgrade")) {
546 lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
550 pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
552 lwsl_parser("lws_client_int_s_hs: no protocol list\n");
554 lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
557 * confirm the protocol the server wants to talk was in the list
558 * of protocols we offered
561 len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
563 lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
565 * no protocol name to work from,
566 * default to first protocol
568 wsi->protocol = &wsi->vhost->protocols[0];
569 goto check_extensions;
572 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
575 while (pc && *pc && !okay) {
576 if (!strncmp(pc, p, len) &&
577 (pc[len] == ',' || pc[len] == '\0')) {
581 while (*pc && *pc++ != ',')
583 while (*pc && *pc == ' ')
588 lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
593 * identify the selected protocol struct and set it
596 wsi->protocol = NULL;
597 while (wsi->vhost->protocols[n].callback && !wsi->protocol) {
598 if (strcmp(p, wsi->vhost->protocols[n].name) == 0) {
599 wsi->protocol = &wsi->vhost->protocols[n];
605 if (wsi->protocol == NULL) {
606 lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
612 #ifndef LWS_NO_EXTENSIONS
613 /* instantiate the accepted extensions */
615 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
616 lwsl_ext("no client extensions allowed by server\n");
621 * break down the list of server accepted extensions
622 * and go through matching them or identifying bogons
625 if (lws_hdr_copy(wsi, sb, LWS_MAX_SOCKET_IO_BUF, WSI_TOKEN_EXTENSIONS) < 0) {
626 lwsl_warn("ext list from server failed to copy\n");
636 if (*c && (*c != ',' && *c != '\t')) {
642 if (ignore || *c == ' ') {
648 if (n < sizeof(ext_name) - 1)
662 /* check we actually support it */
664 lwsl_notice("checking client ext %s\n", ext_name);
667 ext = wsi->vhost->extensions;
668 while (ext && ext->callback) {
669 if (strcmp(ext_name, ext->name)) {
675 lwsl_notice("instantiating client ext %s\n", ext_name);
677 /* instantiate the extension on this conn */
679 wsi->active_extensions[wsi->count_act_ext] = ext;
681 /* allow him to construct his ext instance */
683 if (ext->callback(lws_get_context(wsi), ext, wsi,
684 LWS_EXT_CB_CLIENT_CONSTRUCT,
685 (void *)&wsi->act_ext_user[wsi->count_act_ext],
687 lwsl_notice(" ext %s failed construction\n", ext_name);
693 * allow the user code to override ext defaults if it
697 if (user_callback_handle_rxflow(wsi->protocol->callback,
698 wsi, LWS_CALLBACK_WS_EXT_DEFAULTS,
699 (char *)ext->name, ext_name,
704 lws_ext_parse_options(ext, wsi, wsi->act_ext_user[
705 wsi->count_act_ext], opts, ext_name,
707 lwsl_err("%s: unable to parse user defaults '%s'",
713 * give the extension the server options
715 if (a && lws_ext_parse_options(ext, wsi,
716 wsi->act_ext_user[wsi->count_act_ext],
718 lwsl_err("%s: unable to parse remote def '%s'",
723 if (ext->callback(lws_get_context(wsi), ext, wsi,
724 LWS_EXT_CB_OPTION_CONFIRM,
725 wsi->act_ext_user[wsi->count_act_ext],
727 lwsl_err("%s: ext %s rejects server options %s",
732 wsi->count_act_ext++;
738 lwsl_warn("Unknown ext '%s'!\n", ext_name);
750 * Confirm his accept token is the one we precomputed
753 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
754 if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
755 lwsl_warn("lws_client_int_s_hs: accept '%s' wrong vs '%s'\n", p,
756 wsi->u.hdr.ah->initial_handshake_hash_base64);
760 /* allocate the per-connection user memory (if any) */
761 if (lws_ensure_user_space(wsi)) {
762 lwsl_err("Problem allocating wsi user mem\n");
767 * we seem to be good to go, give client last chance to check
770 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
771 wsi->user_space, NULL, 0))
774 /* clear his proxy connection timeout */
775 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
777 /* free up his parsing allocations */
778 lws_header_table_detach(wsi, 0);
780 lws_union_transition(wsi, LWSCM_WS_CLIENT);
781 wsi->state = LWSS_ESTABLISHED;
783 wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
786 * create the frame buffer for this connection according to the
787 * size mentioned in the protocol definition. If 0 there, then
788 * use a big default for compatibility
790 n = wsi->protocol->rx_buffer_size;
792 n = LWS_MAX_SOCKET_IO_BUF;
794 wsi->u.ws.rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */);
795 if (!wsi->u.ws.rx_ubuf) {
796 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
799 wsi->u.ws.rx_ubuf_alloc = n;
800 lwsl_info("Allocating client RX buffer %d\n", n);
802 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n,
804 lwsl_warn("Failed to set SNDBUF to %d", n);
808 lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
810 /* call him back to inform him he is up */
812 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_ESTABLISHED,
813 wsi->user_space, NULL, 0))
815 #ifndef LWS_NO_EXTENSIONS
817 * inform all extensions, not just active ones since they
820 ext = wsi->vhost->extensions;
822 while (ext && ext->callback) {
824 for (n = 0; n < wsi->count_act_ext; n++)
825 if (wsi->active_extensions[n] == ext)
826 v = wsi->act_ext_user[n];
828 ext->callback(context, ext, wsi,
829 LWS_EXT_CB_ANY_WSI_ESTABLISHED, v, NULL, 0);
837 close_reason = LWS_CLOSE_STATUS_NOSTATUS;
840 if (wsi->protocol && wsi->state == LWSS_ESTABLISHED) {
841 if (isErrorCodeReceived && p) {
842 wsi->protocol->callback(wsi,
843 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
845 (unsigned int)strlen(p));
847 wsi->protocol->callback(wsi,
848 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
849 wsi->user_space, NULL, 0);
853 lwsl_info("closing connection due to bail2 connection error\n");
855 /* closing will free up his parsing allocations */
856 lws_close_free_wsi(wsi, close_reason);
863 lws_generate_client_handshake(struct lws *wsi, char *pkt)
865 char buf[128], hash[20], key_b64[40], *p = pkt;
866 struct lws_context *context = wsi->context;
869 #ifndef LWS_NO_EXTENSIONS
870 const struct lws_extension *ext;
874 meth = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD);
883 * create the random key
885 n = lws_get_random(context, hash, 16);
887 lwsl_err("Unable to read from random dev %s\n",
888 SYSTEM_RANDOM_FILEPATH);
889 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
893 lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
897 * 04 example client handshake
900 * Host: server.example.com
902 * Connection: Upgrade
903 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
904 * Sec-WebSocket-Origin: http://example.com
905 * Sec-WebSocket-Protocol: chat, superchat
906 * Sec-WebSocket-Version: 4
909 p += sprintf(p, "%s %s HTTP/1.1\x0d\x0a", meth,
910 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
912 p += sprintf(p, "Pragma: no-cache\x0d\x0a"
913 "Cache-Control: no-cache\x0d\x0a");
915 p += sprintf(p, "Host: %s\x0d\x0a",
916 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
918 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
919 p += sprintf(p, "Origin: http://%s\x0d\x0a",
920 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
923 p += sprintf(p, "Upgrade: websocket\x0d\x0a"
924 "Connection: Upgrade\x0d\x0a"
925 "Sec-WebSocket-Key: ");
927 p += strlen(key_b64);
928 p += sprintf(p, "\x0d\x0a");
929 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
930 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
931 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
933 /* tell the server what extensions we could support */
935 #ifndef LWS_NO_EXTENSIONS
936 ext = wsi->vhost->extensions;
937 while (ext && ext->callback) {
938 n = lws_ext_cb_all_exts(context, wsi,
939 LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION,
940 (char *)ext->name, 0);
941 if (n) { /* an extension vetos us */
942 lwsl_ext("ext %s vetoed\n", (char *)ext->name);
946 n = wsi->vhost->protocols[0].callback(wsi,
947 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
948 wsi->user_space, (char *)ext->name, 0);
951 * zero return from callback means
952 * go ahead and allow the extension,
953 * it's what we get if the callback is
967 p += sprintf(p, "Sec-WebSocket-Extensions: ");
968 p += sprintf(p, "%s", ext->client_offer);
974 p += sprintf(p, "\x0d\x0a");
977 if (wsi->ietf_spec_revision)
978 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
979 wsi->ietf_spec_revision);
981 /* prepare the expected server accept response */
983 key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
984 n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
986 lws_SHA1((unsigned char *)buf, n, (unsigned char *)hash);
988 lws_b64_encode_string(hash, 20,
989 wsi->u.hdr.ah->initial_handshake_hash_base64,
990 sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
993 /* give userland a chance to append, eg, cookies */
995 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
996 wsi->user_space, &p, (pkt + LWS_MAX_SOCKET_IO_BUF) - p - 12);
998 p += sprintf(p, "\x0d\x0a");