updated spec enabled ssl
[profile/ivi/libwebsockets.git] / lib / client.c
index a11de1f..3da9326 100644 (file)
 #include <netdb.h>
 #endif
 
-#ifdef LWS_OPENSSL_SUPPORT
-extern int openssl_websocket_private_data_index;
-#endif                           
-
-int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd)
+int lws_client_socket_service(struct libwebsocket_context *context,
+                               struct libwebsocket *wsi, struct pollfd *pollfd)
 {
        int n;
-       char pkt[1024];
-       char *p = &pkt[0];
+       char *p = (char *)&context->service_buffer[0];
        int len;
        char c;
-#ifdef LWS_OPENSSL_SUPPORT
-       char ssl_err_buf[512];
-#endif
 
        switch (wsi->mode) {
 
@@ -66,7 +59,8 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                        return 0;
                }
 
-               n = recv(wsi->sock, pkt, sizeof pkt, 0);
+               n = recv(wsi->sock, context->service_buffer,
+                                       sizeof(context->service_buffer), 0);
                if (n < 0) {
                        libwebsocket_close_and_free_session(context, wsi,
                                                     LWS_CLOSE_STATUS_NOSTATUS);
@@ -74,11 +68,11 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                        return 0;
                }
 
-               pkt[13] = '\0';
-               if (strcmp(pkt, "HTTP/1.0 200 ") != 0) {
+               context->service_buffer[13] = '\0';
+               if (strcmp((char *)context->service_buffer, "HTTP/1.0 200 ")) {
                        libwebsocket_close_and_free_session(context, wsi,
                                                     LWS_CLOSE_STATUS_NOSTATUS);
-                       lwsl_err("ERROR from proxy: %s\n", pkt);
+                       lwsl_err("ERROR proxy: %s\n", context->service_buffer);
                        return 0;
                }
 
@@ -107,24 +101,49 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                /* external POLL support via protocol 0 */
                context->protocols[0].callback(context, wsi,
                        LWS_CALLBACK_CLEAR_MODE_POLL_FD,
-                       (void *)(long)wsi->sock, NULL, POLLOUT);
+                       wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
 
-               /* we can retry this... so just cook the SSL BIO the first time */
+               /* we can retry this... just cook the SSL BIO the first time */
 
                if (wsi->use_ssl && !wsi->ssl) {
 
                        wsi->ssl = SSL_new(context->ssl_client_ctx);
-                       wsi->client_bio = BIO_new_socket(wsi->sock,
-                                                                  BIO_NOCLOSE);
+
+#ifdef USE_CYASSL
+                       /*
+                        * CyaSSL does certificate verification differently
+                        * from OpenSSL.
+                        * If we should ignore the certificate, we need to set
+                        * this before SSL_new and SSL_connect is called.
+                        * Otherwise the connect will simply fail with error
+                        * code -155
+                        */
+                       if (wsi->use_ssl == 2)
+                               CyaSSL_set_verify(wsi->ssl,
+                                                       SSL_VERIFY_NONE, NULL);
+#endif /* USE_CYASSL */
+
+                       wsi->client_bio =
+                               BIO_new_socket(wsi->sock, BIO_NOCLOSE);
                        SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
 
+#ifdef USE_CYASSL
+                       CyaSSL_set_using_nonblock(wsi->ssl, 1);
+#else
+                       BIO_set_nbio(wsi->client_bio, 1); /* nonblocking */
+#endif
+
                        SSL_set_ex_data(wsi->ssl,
                                        openssl_websocket_private_data_index,
                                                                       context);
-               }               
+               }
 
                if (wsi->use_ssl) {
+                       lws_latency_pre(context, wsi);
                        n = SSL_connect(wsi->ssl);
+                       lws_latency(context, wsi,
+                         "SSL_connect LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE",
+                                                                     n, n > 0);
 
                        if (n < 0) {
                                n = SSL_get_error(wsi->ssl, n);
@@ -132,18 +151,22 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                                if (n == SSL_ERROR_WANT_READ ||
                                        n == SSL_ERROR_WANT_WRITE) {
                                        /*
-                                        * wants us to retry connect due to state of the
-                                        * underlying ssl layer... but since it may be
-                                        * stalled on blocked write, no incoming data may
-                                        * arrive to trigger the retry.  Force (possibly
-                                        * many if the SSL state persists in returning the
-                                        * condition code, but other sockets are getting
-                                        * serviced inbetweentimes) us to get called back
-                                        * when writable.
+                                        * wants us to retry connect due to
+                                        * state of the underlying ssl layer...
+                                        * but since it may be stalled on
+                                        * blocked write, no incoming data may
+                                        * arrive to trigger the retry.
+                                        * Force (possibly many times if the SSL
+                                        * state persists in returning the
+                                        * condition code, but other sockets
+                                        * are getting serviced inbetweentimes)
+                                        * us to get called back when writable.
                                         */
 
-                                       lwsl_info("SSL_connect -> SSL_ERROR_WANT_... retrying\n");
-                                       libwebsocket_callback_on_writable(context, wsi);
+                                       lwsl_info(
+                                            "SSL_connect WANT_... retrying\n");
+                                       libwebsocket_callback_on_writable(
+                                                                 context, wsi);
 
                                        return 0; /* no error */
                                }
@@ -156,30 +179,41 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                                 * run into the connection timeout or win
                                 */
 
-                               lwsl_err("SSL connect error %s\n",
+                               lwsl_err("SSL connect error %lu: %s\n", 
+                                       ERR_get_error(),
                                        ERR_error_string(ERR_get_error(),
-                                                                 ssl_err_buf));
+                                             (char *)context->service_buffer));
                                return 0;
                        }
 
+                       #ifndef USE_CYASSL
+                       /*
+                        * See comment above about CyaSSL certificate
+                        * verification
+                        */
+                       lws_latency_pre(context, wsi);
                        n = SSL_get_verify_result(wsi->ssl);
+                       lws_latency(context, wsi,
+                               "SSL_get_verify_result LWS_CONNMODE..HANDSHAKE",
+                                                                     n, n > 0);
                        if ((n != X509_V_OK) && (
                                n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
                                                           wsi->use_ssl != 2)) {
 
-                               lwsl_err("server's cert didn't "
-                                                          "look good %d\n", n);
+                               lwsl_err(
+                                     "server's cert didn't look good %d\n", n);
                                libwebsocket_close_and_free_session(context,
                                                wsi, LWS_CLOSE_STATUS_NOSTATUS);
                                return 0;
                        }
+#endif /* USE_CYASSL */
                } else
                        wsi->ssl = NULL;
-       #endif
+#endif
 
                p = libwebsockets_generate_client_handshake(context, wsi, p);
                if (p == NULL) {
-                       lwsl_err("Failed to generate handshake for client, closing it\n");
+                       lwsl_err("Failed to generate handshake for client\n");
                        libwebsocket_close_and_free_session(context, wsi,
                                                     LWS_CLOSE_STATUS_NOSTATUS);
                        return 0;
@@ -187,12 +221,18 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
 
                /* send our request to the server */
 
-       #ifdef LWS_OPENSSL_SUPPORT
+               lws_latency_pre(context, wsi);
+#ifdef LWS_OPENSSL_SUPPORT
                if (wsi->use_ssl)
-                       n = SSL_write(wsi->ssl, pkt, p - pkt);
+                       n = SSL_write(wsi->ssl, context->service_buffer,
+                                          p - (char *)context->service_buffer);
                else
-       #endif
-                       n = send(wsi->sock, pkt, p - pkt, 0);
+#endif
+                       n = send(wsi->sock, context->service_buffer,
+                                       p - (char *)context->service_buffer, 0);
+               lws_latency(context, wsi,
+                       "send or SSL_write LWS_CONNMODE...HANDSHAKE",
+                                                                    n, n >= 0);
 
                if (n < 0) {
                        lwsl_debug("ERROR writing to client socket\n");
@@ -201,12 +241,12 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                        return 0;
                }
 
-               wsi->parser_state = WSI_TOKEN_NAME_PART;
-               wsi->lextable_pos = 0;
+               wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
+               wsi->u.hdr.lextable_pos = 0;
                wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
                libwebsocket_set_timeout(wsi,
-                               PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, AWAITING_TIMEOUT);
-
+                               PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
+                                                             AWAITING_TIMEOUT);
                break;
 
        case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
@@ -221,6 +261,10 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                        goto bail3;
                }
 
+               if (!(pollfd->revents & POLLIN)) {
+                       lwsl_warn("server reply no POLLIN\n");
+                       goto bail3;
+               }
 
                /* interpret the server response */
 
@@ -243,15 +287,30 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                 */
 
                len = 1;
-               while (wsi->parser_state != WSI_PARSING_COMPLETE && len > 0) {
+               while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
+                                                                     len > 0) {
 #ifdef LWS_OPENSSL_SUPPORT
-                       if (wsi->use_ssl)
+                       if (wsi->use_ssl) {
                                len = SSL_read(wsi->ssl, &c, 1);
-                        else
+                               if (len < 0) {
+                                       n = SSL_get_error(wsi->ssl, len);
+                                       if (n ==  SSL_ERROR_WANT_READ ||
+                                                    n ==  SSL_ERROR_WANT_WRITE)
+                                               return 0;
+                               }
+                       } else
 #endif
                                len = recv(wsi->sock, &c, 1, 0);
 
-                       libwebsocket_parse(wsi, c);
+                       if (len < 0) {
+                               lwsl_warn("error on parsing recv\n");
+                               goto bail3;
+                       }
+
+                       if (libwebsocket_parse(wsi, c)) {
+                               lwsl_warn("problems parsing header\n");
+                               goto bail3;
+                       }
                }
 
                /*
@@ -260,7 +319,7 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                 * not complete just wait for next packet coming in this state
                 */
 
-               if (wsi->parser_state != WSI_PARSING_COMPLETE)
+               if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
                        break;
 
                /*
@@ -272,8 +331,8 @@ int lws_client_socket_service(struct libwebsocket_context *context, struct libwe
                return lws_client_interpret_server_handshake(context, wsi);
 
 bail3:
-               if (wsi->c_protocol)
-                       free(wsi->c_protocol);
+               lwsl_info(
+                       "closing connection at LWS_CONNMODE...SERVER_REPLY\n");
                libwebsocket_close_and_free_session(context, wsi,
                                                    LWS_CLOSE_STATUS_NOSTATUS);
                return 0;
@@ -310,12 +369,10 @@ int
 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
                struct libwebsocket *wsi)
 {
-       unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
-                       MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
-       char pkt[1024];
-       char *p = &pkt[0];
        const char *pc;
        int okay = 0;
+       char *p;
+       int len;
 #ifndef LWS_NO_EXTENSIONS
        char ext_name[128];
        struct libwebsocket_extension *ext;
@@ -323,155 +380,81 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context,
        int more = 1;
        const char *c;
 #endif
-       int len = 0;
        int n;
-       static const char magic_websocket_04_masking_guid[] =
-                                        "61AC5F19-FBBA-4540-B96F-6561F1AB40A8";
-
-       /*
-        * 00 / 76 -->
-        *
-        * HTTP/1.1 101 WebSocket Protocol Handshake
-        * Upgrade: WebSocket
-        * Connection: Upgrade
-        * Sec-WebSocket-Origin: http://127.0.0.1
-        * Sec-WebSocket-Location: ws://127.0.0.1:9999/socket.io/websocket
-        *
-        * xxxxxxxxxxxxxxxx
-        */
-
-       if (wsi->ietf_spec_revision == 0) {
-               if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
-                       !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
-                       !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
-                       !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len
-               ) {
-                       lwsl_parser("libwebsocket_client_handshake "
-                                       "missing required header(s)\n");
-                       pkt[len] = '\0';
-                       lwsl_parser("%s", pkt);
-                       goto bail3;
-               }
-
-               strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
-               if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
-                       lwsl_warn("libwebsocket_client_handshake "
-                               "server sent bad HTTP response '%s'\n",
-                               wsi->utf8_token[WSI_TOKEN_HTTP].token);
-                       goto bail3;
-               }
-
-               if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < 16) {
-                       lwsl_parser("libwebsocket_client_handshake "
-                               "challenge reply too short %d\n",
-                               wsi->utf8_token[
-                                       WSI_TOKEN_CHALLENGE].token_len);
-                       pkt[len] = '\0';
-                       lwsl_parser("%s", pkt);
-                       goto bail3;
-
-               }
-
-               goto select_protocol;
-       }
 
        /*
         * well, what the server sent looked reasonable for syntax.
         * Now let's confirm it sent all the necessary headers
         */
-#if 0
-       lwsl_parser("WSI_TOKEN_HTTP: %d\n",
-                                   wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
-       lwsl_parser("WSI_TOKEN_UPGRADE: %d\n",
-                                wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
-       lwsl_parser("WSI_TOKEN_CONNECTION: %d\n",
-                             wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
-       lwsl_parser("WSI_TOKEN_ACCEPT: %d\n",
-                                 wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
-       lwsl_parser("WSI_TOKEN_NONCE: %d\n",
-                                  wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
-       lwsl_parser("WSI_TOKEN_PROTOCOL: %d\n",
-                               wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
-#endif
-       if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
-           !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
-           !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
-           !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
-           (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
-                                  wsi->ietf_spec_revision == 4)
-       ) {
-               lwsl_parser("libwebsocket_client_handshake "
-                                       "missing required header(s) revision=%d\n", wsi->ietf_spec_revision);
-               pkt[len] = '\0';
-               lwsl_parser("%s", pkt);
+
+       if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
+               lwsl_info("no ACCEPT\n");
                goto bail3;
        }
 
-       /*
-        * Everything seems to be there, now take a closer look at what
-        * is in each header
-        */
-
-       strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
-       if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
-               lwsl_warn("libwebsocket_client_handshake "
-                               "server sent bad HTTP response '%s'\n",
-                                wsi->utf8_token[WSI_TOKEN_HTTP].token);
+       p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
+       if (!p) {
+               lwsl_info("no URI\n");
+               goto bail3;
+       }
+       if (p && strncmp(p, "101", 3)) {
+               lwsl_warn(
+                      "lws_client_handshake: got bad HTTP response '%s'\n", p);
                goto bail3;
        }
 
-       strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
-       if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
-                                                        "websocket")) {
-               lwsl_warn("libwebsocket_client_handshake server "
-                               "sent bad Upgrade header '%s'\n",
-                                 wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
+       p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
+       if (!p) {
+               lwsl_info("no UPGRADE\n");
+               goto bail3;
+       }
+       strtolower(p);
+       if (strcmp(p, "websocket")) {
+               lwsl_warn(
+                     "lws_client_handshake: got bad Upgrade header '%s'\n", p);
                goto bail3;
        }
 
-       strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
-       if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
-                                                          "upgrade")) {
-               lwsl_warn("libwebsocket_client_handshake server "
-                               "sent bad Connection hdr '%s'\n",
-                          wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
+       p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
+       if (!p) {
+               lwsl_info("no Connection hdr\n");
+               goto bail3;
+       }
+       strtolower(p);
+       if (strcmp(p, "upgrade")) {
+               lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
                goto bail3;
        }
 
-select_protocol:
-       pc = wsi->c_protocol;
+       pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
        if (pc == NULL)
-               lwsl_parser("lws_client_interpret_server_handshake: "
-                                                         "NULL c_protocol\n");
+               lwsl_parser("lws_client_int_s_hs: no protocol list\n");
        else
-               lwsl_parser("lws_client_interpret_server_handshake: "
-                                                     "cPprotocol='%s'\n", pc);
+               lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
 
        /*
         * confirm the protocol the server wants to talk was in the list
         * of protocols we offered
         */
 
-       if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
+       len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
+       if (!len) {
 
-               lwsl_info("lws_client_interpret_server_handshake "
-                                              "WSI_TOKEN_PROTOCOL is null\n");
+               lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
                /*
                 * no protocol name to work from,
                 * default to first protocol
                 */
                wsi->protocol = &context->protocols[0];
-               wsi->c_callback = wsi->protocol->callback;
-               free(wsi->c_protocol);
-
                goto check_extensions;
        }
 
+       p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
+       len = strlen(p);
+
        while (*pc && !okay) {
-               if ((!strncmp(pc, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
-                wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
-                (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
-                 pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
+               if (!strncmp(pc, p, len) &&
+                                         (pc[len] == ',' || pc[len] == '\0')) {
                        okay = 1;
                        continue;
                }
@@ -481,15 +464,8 @@ select_protocol:
                        pc++;
        }
 
-       /* done with him now */
-
-       if (wsi->c_protocol)
-               free(wsi->c_protocol);
-
        if (!okay) {
-               lwsl_err("libwebsocket_client_handshake server "
-                                       "sent bad protocol '%s'\n",
-                                wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
+               lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
                goto bail2;
        }
 
@@ -498,20 +474,16 @@ select_protocol:
         */
        n = 0;
        wsi->protocol = NULL;
-       while (context->protocols[n].callback && !wsi->protocol) {  /* Stop after finding first one?? */
-               if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
-                                          context->protocols[n].name) == 0) {
+       while (context->protocols[n].callback && !wsi->protocol) {
+               if (strcmp(p, context->protocols[n].name) == 0) {
                        wsi->protocol = &context->protocols[n];
-                       wsi->c_callback = wsi->protocol->callback;
+                       break;
                }
                n++;
        }
 
        if (wsi->protocol == NULL) {
-               lwsl_err("libwebsocket_client_handshake server "
-                               "requested protocol '%s', which we "
-                               "said we supported but we don't!\n",
-                                wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
+               lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
                goto bail2;
        }
 
@@ -520,7 +492,7 @@ check_extensions:
 #ifndef LWS_NO_EXTENSIONS
        /* instantiate the accepted extensions */
 
-       if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) {
+       if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
                lwsl_ext("no client extenstions allowed by server\n");
                goto check_accept;
        }
@@ -530,7 +502,13 @@ check_extensions:
         * and go through matching them or identifying bogons
         */
 
-       c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
+       if (lws_hdr_copy(wsi, (char *)context->service_buffer,
+                  sizeof(context->service_buffer), WSI_TOKEN_EXTENSIONS) < 0) {
+               lwsl_warn("ext list from server failed to copy\n");
+               goto bail2;
+       }
+
+       c = (char *)context->service_buffer;
        n = 0;
        while (more) {
 
@@ -597,8 +575,7 @@ check_extensions:
                }
 
                if (n == 0) {
-                       lwsl_warn("Server said we should use"
-                                 "an unknown extension '%s'!\n", ext_name);
+                       lwsl_warn("Unknown ext '%s'!\n", ext_name);
                        goto bail2;
                }
 
@@ -608,62 +585,66 @@ check_extensions:
 check_accept:
 #endif
 
-       if (wsi->ietf_spec_revision == 0) {
-
-               if (memcmp(wsi->initial_handshake_hash_base64,
-                         wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) {
-                       lwsl_warn("libwebsocket_client_handshake "
-                                          "failed 00 challenge compare\n");
-                               pkt[len] = '\0';
-                               lwsl_warn("%s", pkt);
-                               goto bail2;
-               }
-
-               goto accept_ok;
-       }
-
        /*
         * Confirm his accept token is the one we precomputed
         */
 
-       if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
-                                 wsi->initial_handshake_hash_base64)) {
-               lwsl_warn("libwebsocket_client_handshake server "
-                       "sent bad ACCEPT '%s' vs computed '%s'\n",
-                       wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
-                                       wsi->initial_handshake_hash_base64);
+       p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
+       if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
+               lwsl_warn("lws_client_int_s_hs: accept %s wrong vs %s\n", p,
+                                 wsi->u.hdr.ah->initial_handshake_hash_base64);
                goto bail2;
        }
 
-       if (wsi->ietf_spec_revision == 4) {
-               /*
-                * Calculate the 04 masking key to use when
-                * sending data to server
-                */
-
-               strcpy((char *)buf, wsi->key_b64);
-               p = (char *)buf + strlen(wsi->key_b64);
-               strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token);
-               p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len;
-               strcpy(p, magic_websocket_04_masking_guid);
-               SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
-       }
-accept_ok:
-
        /* allocate the per-connection user memory (if any) */
-       if (wsi->protocol->per_session_data_size &&
-                                         !libwebsocket_ensure_user_space(wsi))
+       if (libwebsocket_ensure_user_space(wsi)) {
+               lwsl_err("Problem allocating wsi user mem\n");
                goto bail2;
+       }
+
+       /*
+        * we seem to be good to go, give client last chance to check
+        * headers and OK it
+        */
+
+       wsi->protocol->callback(context, wsi,
+                               LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
+                                                    wsi->user_space, NULL, 0);
 
        /* clear his proxy connection timeout */
 
        libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
 
+       /* free up his parsing allocations */
+       if (wsi->u.hdr.ah)
+               free(wsi->u.hdr.ah);
+
        /* mark him as being alive */
 
        wsi->state = WSI_STATE_ESTABLISHED;
        wsi->mode = LWS_CONNMODE_WS_CLIENT;
 
+       /* union transition */
+
+       memset(&wsi->u, 0, sizeof(wsi->u));
+
+       /*
+        * create the frame buffer for this connection according to the
+        * size mentioned in the protocol definition.  If 0 there, then
+        * use a big default for compatibility
+        */
+
+       n = wsi->protocol->rx_buffer_size;
+       if (!n)
+               n = LWS_MAX_SOCKET_IO_BUF;
+       n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
+       wsi->u.ws.rx_user_buffer = malloc(n);
+       if (!wsi->u.ws.rx_user_buffer) {
+               lwsl_err("Out of Mem allocating rx buffer %d\n", n);
+               goto bail3;
+       }
+       lwsl_info("Allocating client RX buffer %d\n", n);
+
        lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
 
        /* call him back to inform him he is up */
@@ -694,66 +675,34 @@ accept_ok:
        return 0;
 
 bail3:
-       if (wsi->c_protocol)
-               free(wsi->c_protocol);
 
 bail2:
-       if (wsi->c_callback) wsi->c_callback(context, wsi,
-       LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
-                        wsi->user_space,
-                        NULL, 0);
-       libwebsocket_close_and_free_session(context, wsi,
-                                                LWS_CLOSE_STATUS_NOSTATUS);  // But this should be LWS_CLOSE_STATUS_PROTOCOL_ERR
+       if (wsi->protocol)
+               wsi->protocol->callback(context, wsi,
+                       LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
+                                                     wsi->user_space, NULL, 0);
 
-       return 1;
-}
+       lwsl_info("closing connection due to bail2 connection error\n");
 
-void libwebsockets_00_spaceout(char *key, int spaces, int seed)
-{
-       char *p;
+       /* free up his parsing allocations */
 
-       key++;
-       while (spaces--) {
-               if (*key && (seed & 1))
-                       key++;
-               seed >>= 1;
-
-               p = key + strlen(key);
-               while (p >= key) {
-                       p[1] = p[0];
-                       p--;
-               }
-               *key++ = ' ';
-       }
-}
+       if (wsi->u.hdr.ah)
+               free(wsi->u.hdr.ah);
 
-void libwebsockets_00_spam(char *key, int count, int seed)
-{
-       char *p;
-
-       key++;
-       while (count--) {
-
-               if (*key && (seed & 1))
-                       key++;
-               seed >>= 1;
+       libwebsocket_close_and_free_session(context, wsi,
+                                                LWS_CLOSE_STATUS_PROTOCOL_ERR);
 
-               p = key + strlen(key);
-               while (p >= key) {
-                       p[1] = p[0];
-                       p--;
-               }
-               *key++ = 0x21 + ((seed & 0xffff) % 15);
-               /* 4 would use it up too fast.. not like it matters */
-               seed >>= 1;
-       }
+       return 1;
 }
 
+
 char *
 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
                struct libwebsocket *wsi, char *pkt)
 {
+       char buf[128];
        char hash[20];
+       char key_b64[40];
        char *p = pkt;
        int n;
 #ifndef LWS_NO_EXTENSIONS
@@ -761,10 +710,6 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
        struct libwebsocket_extension *ext1;
        int ext_count = 0;
 #endif
-       unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
-                        MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
-       static const char magic_websocket_guid[] =
-                                        "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
 
        /*
         * create the random key
@@ -774,19 +719,12 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
        if (n != 16) {
                lwsl_err("Unable to read from random dev %s\n",
                                                SYSTEM_RANDOM_FILEPATH);
-               free(wsi->c_path);
-               free(wsi->c_host);
-               if (wsi->c_origin)
-                       free(wsi->c_origin);
-               if (wsi->c_protocol)
-                       free(wsi->c_protocol);
                libwebsocket_close_and_free_session(context, wsi,
                                             LWS_CLOSE_STATUS_NOSTATUS);
                return NULL;
        }
 
-       lws_b64_encode_string(hash, 16, wsi->key_b64,
-                                                  sizeof wsi->key_b64);
+       lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
 
        /*
         * 00 example client handshake
@@ -814,121 +752,26 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
         * Sec-WebSocket-Version: 4
         */
 
-       p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path);
-
-       p += sprintf(p, "Pragma: no-cache\x0d\x0a"
-                                       "Cache-Control: no-cache\x0d\x0a");
-
-       if (wsi->ietf_spec_revision == 0) {
-               unsigned char spaces_1, spaces_2;
-               unsigned int max_1, max_2;
-               unsigned int num_1, num_2;
-               unsigned long product_1, product_2;
-               char key_1[40];
-               char key_2[40];
-               unsigned int seed;
-               unsigned int count;
-               char challenge[16];
-
-               libwebsockets_get_random(context, &spaces_1, sizeof(char));
-               libwebsockets_get_random(context, &spaces_2, sizeof(char));
-
-               spaces_1 = (spaces_1 % 12) + 1;
-               spaces_2 = (spaces_2 % 12) + 1;
-
-               max_1 = 4294967295 / spaces_1;
-               max_2 = 4294967295 / spaces_2;
-
-               libwebsockets_get_random(context, &num_1, sizeof(int));
-               libwebsockets_get_random(context, &num_2, sizeof(int));
-
-               num_1 = (num_1 % max_1);
-               num_2 = (num_2 % max_2);
-
-               challenge[0] = num_1 >> 24;
-               challenge[1] = num_1 >> 16;
-               challenge[2] = num_1 >> 8;
-               challenge[3] = num_1;
-               challenge[4] = num_2 >> 24;
-               challenge[5] = num_2 >> 16;
-               challenge[6] = num_2 >> 8;
-               challenge[7] = num_2;
-
-               product_1 = num_1 * spaces_1;
-               product_2 = num_2 * spaces_2;
-
-               sprintf(key_1, "%lu", product_1);
-               sprintf(key_2, "%lu", product_2);
-
-               libwebsockets_get_random(context, &seed, sizeof(int));
-               libwebsockets_get_random(context, &count, sizeof(int));
-
-               libwebsockets_00_spam(key_1, (count % 12) + 1, seed);
-
-               libwebsockets_get_random(context, &seed, sizeof(int));
-               libwebsockets_get_random(context, &count, sizeof(int));
-
-               libwebsockets_00_spam(key_2, (count % 12) + 1, seed);
-
-               libwebsockets_get_random(context, &seed, sizeof(int));
-
-               libwebsockets_00_spaceout(key_1, spaces_1, seed);
-               libwebsockets_00_spaceout(key_2, spaces_2, seed >> 16);
+       p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a",
+                               lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
 
-               p += sprintf(p, "Upgrade: WebSocket\x0d\x0a"
-                       "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a",
-                       wsi->c_host);
-               if (wsi->c_origin)
-                       p += sprintf(p, "Origin: %s\x0d\x0a", wsi->c_origin);
+       p += sprintf(p,
+               "Pragma: no-cache\x0d\x0a""Cache-Control: no-cache\x0d\x0a");
 
-               if (wsi->c_protocol)
-                       p += sprintf(p, "Sec-WebSocket-Protocol: %s"
-                                        "\x0d\x0a", wsi->c_protocol);
-
-               p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a", key_1);
-               p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a", key_2);
-
-               /* give userland a chance to append, eg, cookies */
-
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
-                               NULL, &p, (pkt + sizeof(pkt)) - p - 12);
-
-               p += sprintf(p, "\x0d\x0a");
-
-               if (libwebsockets_get_random(context, p, 8) != 8)
-                       return NULL;
-               memcpy(&challenge[8], p, 8);
-               p += 8;
-
-               /* precompute what we want to see from the server */
-
-               MD5((unsigned char *)challenge, 16,
-                  (unsigned char *)wsi->initial_handshake_hash_base64);
-
-               goto issue_hdr;
-       }
-
-       p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host);
-       p += sprintf(p, "Upgrade: websocket\x0d\x0a"
-                                       "Connection: Upgrade\x0d\x0a"
-                                       "Sec-WebSocket-Key: ");
-       strcpy(p, wsi->key_b64);
-       p += strlen(wsi->key_b64);
+       p += sprintf(p, "Host: %s\x0d\x0a",
+                              lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
+       p += sprintf(p,
+"Upgrade: websocket\x0d\x0a""Connection: Upgrade\x0d\x0a""Sec-WebSocket-Key: ");
+       strcpy(p, key_b64);
+       p += strlen(key_b64);
        p += sprintf(p, "\x0d\x0a");
-       if (wsi->c_origin) {
-        if (wsi->ietf_spec_revision == 13) {
-            p += sprintf(p, "Origin: %s\x0d\x0a",
-                                                        wsi->c_origin);
-        }
-        else {
-                   p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a",
-                                                        wsi->c_origin);
-        }
-    }
-       if (wsi->c_protocol)
+       if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
+               p += sprintf(p, "Origin: %s\x0d\x0a",
+                            lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
+
+       if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
                p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
-                                                      wsi->c_protocol);
+                    lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
 
        /* tell the server what extensions we could support */
 
@@ -990,33 +833,20 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
 
        context->protocols[0].callback(context, wsi,
                LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
-               NULL, &p, (pkt + sizeof(pkt)) - p - 12);
+               NULL, &p, (pkt + sizeof(context->service_buffer)) - p - 12);
 
        p += sprintf(p, "\x0d\x0a");
 
        /* prepare the expected server accept response */
 
-       strcpy((char *)buf, wsi->key_b64);
-       strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid);
+       key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
+       n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
 
-       SHA1(buf, strlen((char *)buf), (unsigned char *)hash);
+       SHA1((unsigned char *)buf, n, (unsigned char *)hash);
 
        lws_b64_encode_string(hash, 20,
-                       wsi->initial_handshake_hash_base64,
-                            sizeof wsi->initial_handshake_hash_base64);
-
-issue_hdr:
-
-#if 0
-       puts(pkt);
-#endif
-
-       /* done with these now */
-
-       free(wsi->c_path);
-       free(wsi->c_host);
-       if (wsi->c_origin)
-               free(wsi->c_origin);
+                       wsi->u.hdr.ah->initial_handshake_hash_base64,
+                         sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
 
        return p;
 }