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 %ld\n", __func__, (long)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 %ld\n", __func__, (long)len);
69 LWS_VISIBLE LWS_EXTERN void
70 lws_client_http_body_pending(struct lws *wsi, int something_left_to_send)
72 wsi->client_http_body_pending = !!something_left_to_send;
76 lws_client_socket_service(struct lws_context *context, struct lws *wsi,
77 struct lws_pollfd *pollfd)
79 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
80 char *p = (char *)&pt->serv_buf[0];
81 const char *cce = NULL;
88 case LWSCM_WSCL_WAITING_CONNECT:
91 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
92 * timeout protection set in client-handshake.c
95 if (!lws_client_connect_2(wsi)) {
97 lwsl_client("closed\n");
101 /* either still pending connection, or changed mode */
104 case LWSCM_WSCL_WAITING_PROXY_REPLY:
106 /* handle proxy hung up on us */
108 if (pollfd->revents & LWS_POLLHUP) {
110 lwsl_warn("Proxy connection %p (fd=%d) dead\n",
111 (void *)wsi, pollfd->fd);
113 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
117 n = recv(wsi->sock, sb, context->pt_serv_buf_size, 0);
119 if (LWS_ERRNO == LWS_EAGAIN) {
120 lwsl_debug("Proxy read returned EAGAIN... retrying\n");
124 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
125 lwsl_err("ERROR reading from proxy socket\n");
129 pt->serv_buf[13] = '\0';
130 if (strcmp(sb, "HTTP/1.0 200 ") &&
131 strcmp(sb, "HTTP/1.1 200 ")) {
132 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
133 lwsl_err("ERROR proxy: %s\n", sb);
137 /* clear his proxy connection timeout */
139 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
143 case LWSCM_WSCL_ISSUE_HANDSHAKE:
146 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
147 * timeout protection set in client-handshake.c
149 * take care of our lws_callback_on_writable
150 * happening at a time when there's no real connection yet
152 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
155 #ifdef LWS_OPENSSL_SUPPORT
156 /* we can retry this... just cook the SSL BIO the first time */
158 if (wsi->use_ssl && !wsi->ssl) {
159 if (lws_ssl_client_bio_create(wsi))
164 n = lws_ssl_client_connect1(wsi);
168 cce = "lws_ssl_client_connect1 failed";
176 case LWSCM_WSCL_WAITING_SSL:
179 n = lws_ssl_client_connect2(wsi);
183 cce = "lws_ssl_client_connect2 failed";
190 wsi->mode = LWSCM_WSCL_ISSUE_HANDSHAKE2;
191 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
192 context->timeout_secs);
196 case LWSCM_WSCL_ISSUE_HANDSHAKE2:
197 p = lws_generate_client_handshake(wsi, p);
199 lwsl_err("Failed to generate handshake for client\n");
200 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
204 /* send our request to the server */
206 lws_latency_pre(context, wsi);
208 n = lws_ssl_capable_write(wsi, (unsigned char *)sb, p - sb);
209 lws_latency(context, wsi, "send lws_issue_raw", n,
212 case LWS_SSL_CAPABLE_ERROR:
213 lwsl_debug("ERROR writing to client socket\n");
214 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
216 case LWS_SSL_CAPABLE_MORE_SERVICE:
217 lws_callback_on_writable(wsi);
221 if (wsi->client_http_body_pending) {
222 wsi->mode = LWSCM_WSCL_ISSUE_HTTP_BODY;
223 lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
224 context->timeout_secs);
225 /* user code must ask for writable callback */
229 goto client_http_body_sent;
231 case LWSCM_WSCL_ISSUE_HTTP_BODY:
232 if (wsi->client_http_body_pending) {
233 lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
234 context->timeout_secs);
235 /* user code must ask for writable callback */
238 client_http_body_sent:
239 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
240 wsi->u.hdr.lextable_pos = 0;
241 wsi->mode = LWSCM_WSCL_WAITING_SERVER_REPLY;
242 lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
243 context->timeout_secs);
246 case LWSCM_WSCL_WAITING_SERVER_REPLY:
248 /* handle server hung up on us */
250 if (pollfd->revents & LWS_POLLHUP) {
252 lwsl_debug("Server connection %p (fd=%d) dead\n",
253 (void *)wsi, pollfd->fd);
254 cce = "Peer hung up";
258 if (!(pollfd->revents & LWS_POLLIN))
261 /* interpret the server response */
264 * HTTP/1.1 101 Switching Protocols
266 * Connection: Upgrade
267 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
268 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
269 * Sec-WebSocket-Protocol: chat
273 * we have to take some care here to only take from the
274 * socket bytewise. The browser may (and has been seen to
275 * in the case that onopen() performs websocket traffic)
276 * coalesce both handshake response and websocket traffic
277 * in one packet, since at that point the connection is
278 * definitively ready from browser pov.
281 while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
283 n = lws_ssl_capable_read(wsi, &c, 1);
284 lws_latency(context, wsi, "send lws_issue_raw", n,
288 case LWS_SSL_CAPABLE_ERROR:
291 case LWS_SSL_CAPABLE_MORE_SERVICE:
295 if (lws_parse(wsi, c)) {
296 lwsl_warn("problems parsing header\n");
302 * hs may also be coming in multiple packets, there is a 5-sec
303 * libwebsocket timeout still active here too, so if parsing did
304 * not complete just wait for next packet coming in this state
307 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
311 * otherwise deal with the handshake. If there's any
312 * packet traffic already arrived we'll trigger poll() again
313 * right away and deal with it that way
316 return lws_client_interpret_server_handshake(wsi);
319 lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
320 wsi->vhost->protocols[0].callback(wsi,
321 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
322 wsi->user_space, (void *)cce, cce ? strlen(cce) : 0);
323 wsi->already_did_cce = 1;
324 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
327 case LWSCM_WSCL_WAITING_EXTENSION_CONNECT:
328 lwsl_ext("LWSCM_WSCL_WAITING_EXTENSION_CONNECT\n");
331 case LWSCM_WSCL_PENDING_CANDIDATE_CHILD:
332 lwsl_ext("LWSCM_WSCL_PENDING_CANDIDATE_CHILD\n");
342 * In-place str to lower case
349 #ifdef LWS_PLAT_OPTEE
350 int tolower_optee(int c);
351 *s = tolower_optee((int)*s);
353 *s = tolower((int)*s);
359 int LWS_WARN_UNUSED_RESULT
360 lws_http_transaction_completed_client(struct lws *wsi)
362 lwsl_debug("%s: wsi %p\n", __func__, wsi);
363 /* if we can't go back to accept new headers, drop the connection */
364 if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
365 lwsl_info("%s: %p: close connection\n", __func__, wsi);
369 /* otherwise set ourselves up ready to go again */
370 wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
371 wsi->mode = LWSCM_HTTP_CLIENT_ACCEPTED;
372 wsi->u.http.content_length = 0;
373 wsi->hdr_parsing_completed = 0;
375 /* He asked for it to stay alive indefinitely */
376 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
379 * As client, nothing new is going to come until we ask for it
380 * we can drop the ah, if any
383 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
384 lws_header_table_detach(wsi, 0);
387 /* If we're (re)starting on headers, need other implied init */
388 wsi->u.hdr.ues = URIES_IDLE;
390 lwsl_info("%s: %p: keep-alive await new transaction\n", __func__, wsi);
395 LWS_VISIBLE LWS_EXTERN unsigned int
396 lws_http_client_http_response(struct lws *wsi)
401 return wsi->u.http.ah->http_response;
405 lws_client_interpret_server_handshake(struct lws *wsi)
407 int n, len, okay = 0, port = 0, ssl = 0;
408 int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
409 struct lws_context *context = wsi->context;
410 const char *pc, *prot, *ads = NULL, *path, *cce = NULL;
411 struct allocated_headers *ah = NULL;
413 #ifndef LWS_NO_EXTENSIONS
414 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
415 char *sb = (char *)&pt->serv_buf[0];
416 const struct lws_ext_options *opts;
417 const struct lws_extension *ext;
426 /* we are being an http client...
429 lws_union_transition(wsi, LWSCM_HTTP_CLIENT_ACCEPTED);
430 wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
432 ah->http_response = 0;
436 * well, what the server sent looked reasonable for syntax.
437 * Now let's confirm it sent all the necessary headers
439 * http (non-ws) client will expect something like this
442 * server:.libwebsockets
443 * content-type:.text/html
444 * content-length:.17703
445 * set-cookie:.test=LWS_1456736240_336776_COOKIE;Max-Age=360000
451 wsi->u.http.connection_type = HTTP_CONNECTION_KEEP_ALIVE;
452 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
453 if (wsi->do_ws && !p) {
454 lwsl_info("no URI\n");
455 cce = "HS: URI missing";
459 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP1_0);
460 wsi->u.http.connection_type = HTTP_CONNECTION_CLOSE;
463 cce = "HS: URI missing";
464 lwsl_info("no URI\n");
469 ah->http_response = n;
471 if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
472 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);
474 cce = "HS: Redirect code but no Location";
478 if (lws_parse_uri(p, &prot, &ads, &port, &path)) {
479 cce = "HS: URI did not parse";
483 if (!strcmp(prot, "wss") || !strcmp(prot, "https"))
486 if (!lws_client_reset(wsi, ssl, ads, port, path, ads)) {
487 lwsl_err("Redirect failed\n");
488 cce = "HS: Redirect failed";
495 if (n != 200 && n != 304) {
496 lwsl_notice("Connection failed with code %d\n", n);
497 cce = "HS: Server did not return 200 or 304";
501 #ifdef LWS_WITH_HTTP_PROXY
502 wsi->perform_rewrite = 0;
503 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
504 if (!strncmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE),
506 wsi->perform_rewrite = 1;
510 /* allocate the per-connection user memory (if any) */
511 if (lws_ensure_user_space(wsi)) {
512 lwsl_err("Problem allocating wsi user mem\n");
517 /* he may choose to send us stuff in chunked transfer-coding */
519 wsi->chunk_remaining = 0; /* ie, next thing is chunk size */
520 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_TRANSFER_ENCODING)) {
521 wsi->chunked = !strcmp(lws_hdr_simple_ptr(wsi,
522 WSI_TOKEN_HTTP_TRANSFER_ENCODING),
524 /* first thing is hex, after payload there is crlf */
525 wsi->chunk_parser = ELCP_HEX;
528 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
529 wsi->u.http.content_length =
530 atoi(lws_hdr_simple_ptr(wsi,
531 WSI_TOKEN_HTTP_CONTENT_LENGTH));
532 lwsl_notice("%s: incoming content length %d\n", __func__,
533 wsi->u.http.content_length);
534 wsi->u.http.content_remain = wsi->u.http.content_length;
535 } else /* can't do 1.1 without a content length or chunked */
537 wsi->u.http.connection_type = HTTP_CONNECTION_CLOSE;
540 * we seem to be good to go, give client last chance to check
543 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
544 wsi->user_space, NULL, 0)) {
546 cce = "HS: disallowed by client filter";
550 /* clear his proxy connection timeout */
551 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
553 wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
555 /* call him back to inform him he is up */
556 if (wsi->protocol->callback(wsi,
557 LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP,
558 wsi->user_space, NULL, 0)) {
559 cce = "HS: disallowed at ESTABLISHED";
563 /* free up his parsing allocations */
564 lws_header_table_detach(wsi, 0);
566 lwsl_notice("%s: client connection up\n", __func__);
571 if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
572 lwsl_info("no ACCEPT\n");
573 cce = "HS: ACCEPT missing";
577 if (p && strncmp(p, "101", 3)) {
579 "lws_client_handshake: got bad HTTP response '%s'\n", p);
580 cce = "HS: ws upgrade response not 101";
584 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
586 lwsl_info("no UPGRADE\n");
587 cce = "HS: UPGRADE missing";
591 if (strcmp(p, "websocket")) {
593 "lws_client_handshake: got bad Upgrade header '%s'\n", p);
594 cce = "HS: Upgrade to something other than websocket";
598 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
600 lwsl_info("no Connection hdr\n");
601 cce = "HS: CONNECTION missing";
605 if (strcmp(p, "upgrade")) {
606 lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
607 cce = "HS: UPGRADE malformed";
611 pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
613 lwsl_parser("lws_client_int_s_hs: no protocol list\n");
615 lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
618 * confirm the protocol the server wants to talk was in the list
619 * of protocols we offered
622 len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
624 lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
626 * no protocol name to work from,
627 * default to first protocol
630 wsi->protocol = &wsi->vhost->protocols[0];
631 goto check_extensions;
634 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
637 while (pc && *pc && !okay) {
638 if (!strncmp(pc, p, len) &&
639 (pc[len] == ',' || pc[len] == '\0')) {
643 while (*pc && *pc++ != ',')
645 while (*pc && *pc == ' ')
650 lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
651 cce = "HS: PROTOCOL malformed";
656 * identify the selected protocol struct and set it
659 wsi->protocol = NULL;
660 while (wsi->vhost->protocols[n].callback && !wsi->protocol) {
661 if (strcmp(p, wsi->vhost->protocols[n].name) == 0) {
662 wsi->protocol = &wsi->vhost->protocols[n];
668 if (wsi->protocol == NULL) {
669 lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
670 cce = "HS: Cannot match protocol";
676 * stitch protocol choice into the vh protocol linked list
677 * We always insert ourselves at the start of the list
682 //lwsl_err("%s: pre insert vhost start wsi %p, that wsi prev == %p\n",
684 // wsi->vhost->same_vh_protocol_list[n],
685 // wsi->same_vh_protocol_prev);
686 wsi->same_vh_protocol_prev = /* guy who points to us */
687 &wsi->vhost->same_vh_protocol_list[n];
688 wsi->same_vh_protocol_next = /* old first guy is our next */
689 wsi->vhost->same_vh_protocol_list[n];
690 /* we become the new first guy */
691 wsi->vhost->same_vh_protocol_list[n] = wsi;
693 if (wsi->same_vh_protocol_next)
694 /* old first guy points back to us now */
695 wsi->same_vh_protocol_next->same_vh_protocol_prev =
696 &wsi->same_vh_protocol_next;
698 #ifndef LWS_NO_EXTENSIONS
699 /* instantiate the accepted extensions */
701 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
702 lwsl_ext("no client extensions allowed by server\n");
707 * break down the list of server accepted extensions
708 * and go through matching them or identifying bogons
711 if (lws_hdr_copy(wsi, sb, context->pt_serv_buf_size, WSI_TOKEN_EXTENSIONS) < 0) {
712 lwsl_warn("ext list from server failed to copy\n");
713 cce = "HS: EXT: list too big";
723 if (*c && (*c != ',' && *c != '\t')) {
729 if (ignore || *c == ' ') {
735 if (n < sizeof(ext_name) - 1)
749 /* check we actually support it */
751 lwsl_notice("checking client ext %s\n", ext_name);
754 ext = wsi->vhost->extensions;
755 while (ext && ext->callback) {
756 if (strcmp(ext_name, ext->name)) {
762 lwsl_notice("instantiating client ext %s\n", ext_name);
764 /* instantiate the extension on this conn */
766 wsi->active_extensions[wsi->count_act_ext] = ext;
768 /* allow him to construct his ext instance */
770 if (ext->callback(lws_get_context(wsi), ext, wsi,
771 LWS_EXT_CB_CLIENT_CONSTRUCT,
772 (void *)&wsi->act_ext_user[wsi->count_act_ext],
774 lwsl_notice(" ext %s failed construction\n", ext_name);
780 * allow the user code to override ext defaults if it
784 if (user_callback_handle_rxflow(wsi->protocol->callback,
785 wsi, LWS_CALLBACK_WS_EXT_DEFAULTS,
786 (char *)ext->name, ext_name,
788 cce = "HS: EXT: failed setting defaults";
793 lws_ext_parse_options(ext, wsi, wsi->act_ext_user[
794 wsi->count_act_ext], opts, ext_name,
796 lwsl_err("%s: unable to parse user defaults '%s'",
798 cce = "HS: EXT: failed parsing defaults";
803 * give the extension the server options
805 if (a && lws_ext_parse_options(ext, wsi,
806 wsi->act_ext_user[wsi->count_act_ext],
808 lwsl_err("%s: unable to parse remote def '%s'",
810 cce = "HS: EXT: failed parsing options";
814 if (ext->callback(lws_get_context(wsi), ext, wsi,
815 LWS_EXT_CB_OPTION_CONFIRM,
816 wsi->act_ext_user[wsi->count_act_ext],
818 lwsl_err("%s: ext %s rejects server options %s",
819 __func__, ext->name, a);
820 cce = "HS: EXT: Rejects server options";
824 wsi->count_act_ext++;
830 lwsl_warn("Unknown ext '%s'!\n", ext_name);
831 cce = "HS: EXT: unknown ext";
843 * Confirm his accept token is the one we precomputed
846 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
847 if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
848 lwsl_warn("lws_client_int_s_hs: accept '%s' wrong vs '%s'\n", p,
849 wsi->u.hdr.ah->initial_handshake_hash_base64);
850 cce = "HS: Accept hash wrong";
854 /* allocate the per-connection user memory (if any) */
855 if (lws_ensure_user_space(wsi)) {
856 lwsl_err("Problem allocating wsi user mem\n");
862 * we seem to be good to go, give client last chance to check
865 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
866 wsi->user_space, NULL, 0)) {
867 cce = "HS: Rejected by filter cb";
871 /* clear his proxy connection timeout */
872 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
874 /* free up his parsing allocations */
875 lws_header_table_detach(wsi, 0);
877 lws_union_transition(wsi, LWSCM_WS_CLIENT);
878 wsi->state = LWSS_ESTABLISHED;
879 lws_restart_ws_ping_pong_timer(wsi);
881 wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
884 * create the frame buffer for this connection according to the
885 * size mentioned in the protocol definition. If 0 there, then
886 * use a big default for compatibility
888 n = wsi->protocol->rx_buffer_size;
890 n = context->pt_serv_buf_size;
892 wsi->u.ws.rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */);
893 if (!wsi->u.ws.rx_ubuf) {
894 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
898 wsi->u.ws.rx_ubuf_alloc = n;
899 lwsl_info("Allocating client RX buffer %d\n", n);
901 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n,
903 lwsl_warn("Failed to set SNDBUF to %d", n);
904 cce = "HS: SO_SNDBUF failed";
908 lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
910 /* call him back to inform him he is up */
912 if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_ESTABLISHED,
913 wsi->user_space, NULL, 0)) {
914 cce = "HS: Rejected at CLIENT_ESTABLISHED";
917 #ifndef LWS_NO_EXTENSIONS
919 * inform all extensions, not just active ones since they
922 ext = wsi->vhost->extensions;
924 while (ext && ext->callback) {
926 for (n = 0; n < wsi->count_act_ext; n++)
927 if (wsi->active_extensions[n] == ext)
928 v = wsi->act_ext_user[n];
930 ext->callback(context, ext, wsi,
931 LWS_EXT_CB_ANY_WSI_ESTABLISHED, v, NULL, 0);
939 close_reason = LWS_CLOSE_STATUS_NOSTATUS;
943 wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
944 wsi->user_space, (void *)cce,
945 (unsigned int)strlen(cce));
946 wsi->already_did_cce = 1;
948 lwsl_info("closing connection due to bail2 connection error\n");
950 /* closing will free up his parsing allocations */
951 lws_close_free_wsi(wsi, close_reason);
958 lws_generate_client_handshake(struct lws *wsi, char *pkt)
960 char buf[128], hash[20], key_b64[40], *p = pkt;
961 struct lws_context *context = wsi->context;
964 #ifndef LWS_NO_EXTENSIONS
965 const struct lws_extension *ext;
969 meth = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD);
978 * create the random key
980 n = lws_get_random(context, hash, 16);
982 lwsl_err("Unable to read from random dev %s\n",
983 SYSTEM_RANDOM_FILEPATH);
984 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
988 lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
992 * 04 example client handshake
995 * Host: server.example.com
997 * Connection: Upgrade
998 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
999 * Sec-WebSocket-Origin: http://example.com
1000 * Sec-WebSocket-Protocol: chat, superchat
1001 * Sec-WebSocket-Version: 4
1004 p += sprintf(p, "%s %s HTTP/1.1\x0d\x0a", meth,
1005 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
1007 p += sprintf(p, "Pragma: no-cache\x0d\x0a"
1008 "Cache-Control: no-cache\x0d\x0a");
1010 p += sprintf(p, "Host: %s\x0d\x0a",
1011 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
1013 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN)) {
1014 if (lws_check_opt(context->options, LWS_SERVER_OPTION_JUST_USE_RAW_ORIGIN))
1015 p += sprintf(p, "Origin: %s\x0d\x0a",
1016 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
1018 p += sprintf(p, "Origin: http://%s\x0d\x0a",
1019 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
1023 p += sprintf(p, "Upgrade: websocket\x0d\x0a"
1024 "Connection: Upgrade\x0d\x0a"
1025 "Sec-WebSocket-Key: ");
1027 p += strlen(key_b64);
1028 p += sprintf(p, "\x0d\x0a");
1029 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
1030 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
1031 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
1033 /* tell the server what extensions we could support */
1035 #ifndef LWS_NO_EXTENSIONS
1036 ext = wsi->vhost->extensions;
1037 while (ext && ext->callback) {
1038 n = lws_ext_cb_all_exts(context, wsi,
1039 LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION,
1040 (char *)ext->name, 0);
1041 if (n) { /* an extension vetos us */
1042 lwsl_ext("ext %s vetoed\n", (char *)ext->name);
1046 n = wsi->vhost->protocols[0].callback(wsi,
1047 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
1048 wsi->user_space, (char *)ext->name, 0);
1051 * zero return from callback means
1052 * go ahead and allow the extension,
1053 * it's what we get if the callback is
1067 p += sprintf(p, "Sec-WebSocket-Extensions: ");
1068 p += sprintf(p, "%s", ext->client_offer);
1074 p += sprintf(p, "\x0d\x0a");
1077 if (wsi->ietf_spec_revision)
1078 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
1079 wsi->ietf_spec_revision);
1081 /* prepare the expected server accept response */
1083 key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
1084 n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
1086 lws_SHA1((unsigned char *)buf, n, (unsigned char *)hash);
1088 lws_b64_encode_string(hash, 20,
1089 wsi->u.hdr.ah->initial_handshake_hash_base64,
1090 sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
1093 /* give userland a chance to append, eg, cookies */
1095 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
1096 wsi->user_space, &p, (pkt + context->pt_serv_buf_size) - p - 12);
1098 p += sprintf(p, "\x0d\x0a");