client: fix hdr stash leak
[platform/upstream/libwebsockets.git] / lib / client.c
index 1103079..3c8986c 100755 (executable)
@@ -37,12 +37,17 @@ lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len)
                         * we were accepting input but now we stopped doing so
                         */
                        if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
-                               lwsl_debug("%s: caching %d\n", __func__, len);
+                               lwsl_debug("%s: caching %ld\n", __func__, (long)len);
                                lws_rxflow_cache(wsi, *buf, 0, len);
                                return 0;
                        }
                        if (wsi->u.ws.rx_draining_ext) {
-                               m = lws_rx_sm(wsi, 0);
+#if !defined(LWS_NO_CLIENT)
+                               if (wsi->mode == LWSCM_WS_CLIENT)
+                                       m = lws_client_rx_sm(wsi, 0);
+                               else
+#endif
+                                       m = lws_rx_sm(wsi, 0);
                                if (m < 0)
                                        return -1;
                                continue;
@@ -57,7 +62,7 @@ lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len)
                        }
                        len--;
                }
-               lwsl_debug("%s: finished with %d\n", __func__, len);
+               lwsl_debug("%s: finished with %ld\n", __func__, (long)len);
                return 0;
        default:
                break;
@@ -66,6 +71,12 @@ lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len)
        return 0;
 }
 
+LWS_VISIBLE LWS_EXTERN void
+lws_client_http_body_pending(struct lws *wsi, int something_left_to_send)
+{
+       wsi->client_http_body_pending = !!something_left_to_send;
+}
+
 int
 lws_client_socket_service(struct lws_context *context, struct lws *wsi,
                          struct lws_pollfd *pollfd)
@@ -75,7 +86,10 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
        const char *cce = NULL;
        unsigned char c;
        char *sb = p;
-       int n, len;
+       int n = 0, len = 0;
+#if defined(LWS_WITH_SOCKS5)
+       char conn_mode = 0, pending_timeout = 0;
+#endif
 
        switch (wsi->mode) {
 
@@ -95,6 +109,195 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
                /* either still pending connection, or changed mode */
                return 0;
 
+#if defined(LWS_WITH_SOCKS5)
+       /* SOCKS Greeting Reply */
+       case LWSCM_WSCL_WAITING_SOCKS_GREETING_REPLY:
+
+               /* handle proxy hung up on us */
+
+               if (pollfd->revents & LWS_POLLHUP) {
+
+                       lwsl_warn("SOCKS connection %p (fd=%d) dead\n",
+                                 (void *)wsi, pollfd->fd);
+
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       return 0;
+               }
+
+               n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 0);
+               if (n < 0) {
+                       if (LWS_ERRNO == LWS_EAGAIN) {
+                               lwsl_debug("SOCKS read returned EAGAIN..."
+                                       "retrying\n");
+                               return 0;
+                       }
+
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       lwsl_err("ERROR reading from SOCKS socket\n");
+                       return 0;
+               }
+
+               /* processing greeting reply */
+               if (pt->serv_buf[0] == SOCKS_VERSION_5
+                       && pt->serv_buf[1] == SOCKS_AUTH_NO_AUTH)
+               {
+                       lwsl_client("%s\n", "SOCKS greeting reply received "
+                               "- No Authentication Method");
+                       socks_generate_msg(wsi, SOCKS_MSG_CONNECT, (size_t *)&len);
+
+                       conn_mode = LWSCM_WSCL_WAITING_SOCKS_CONNECT_REPLY;
+                       pending_timeout = PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
+                       lwsl_client("%s\n", "Sending SOCKS connect command");
+               }
+               else if (pt->serv_buf[0] == SOCKS_VERSION_5
+                               && pt->serv_buf[1] == SOCKS_AUTH_USERNAME_PASSWORD)
+               {
+                       lwsl_client("%s\n", "SOCKS greeting reply received "
+                               "- User Name Password Method");
+                       socks_generate_msg(wsi, SOCKS_MSG_USERNAME_PASSWORD,
+                               (size_t *)&len);
+
+                       conn_mode = LWSCM_WSCL_WAITING_SOCKS_AUTH_REPLY;
+                       pending_timeout = PENDING_TIMEOUT_AWAITING_SOCKS_AUTH_REPLY;
+                       lwsl_client("%s\n", "Sending SOCKS user/password");
+               }
+               else
+               {
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       lwsl_err("ERROR SOCKS greeting reply failed, method "
+                               "code: %d\n", pt->serv_buf[1]);
+                       return 0;
+               }
+
+               n = send(wsi->desc.sockfd, (char *)pt->serv_buf, len,
+                        MSG_NOSIGNAL);
+               if (n < 0) {
+                       lwsl_debug("ERROR writing socks command to socks proxy "
+                               "socket\n");
+                       return 0;
+               }
+
+               lws_set_timeout(wsi, pending_timeout, AWAITING_TIMEOUT);
+               wsi->mode = conn_mode;
+
+               break;
+       /* SOCKS auth Reply */
+       case LWSCM_WSCL_WAITING_SOCKS_AUTH_REPLY:
+
+               /* handle proxy hung up on us */
+
+               if (pollfd->revents & LWS_POLLHUP) {
+
+                       lwsl_warn("SOCKS connection %p (fd=%d) dead\n",
+                                 (void *)wsi, pollfd->fd);
+
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       return 0;
+               }
+
+               n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 0);
+               if (n < 0) {
+                       if (LWS_ERRNO == LWS_EAGAIN) {
+                               lwsl_debug("SOCKS read returned EAGAIN... "
+                                       "retrying\n");
+                               return 0;
+                       }
+
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       lwsl_err("ERROR reading from socks socket\n");
+                       return 0;
+               }
+
+               /* processing auth reply */
+               if (pt->serv_buf[0] == SOCKS_SUBNEGOTIATION_VERSION_1
+                       && pt->serv_buf[1] == SOCKS_SUBNEGOTIATION_STATUS_SUCCESS)
+               {
+                       lwsl_client("%s\n", "SOCKS password reply recieved - "
+                               "successful");
+                       socks_generate_msg(wsi, SOCKS_MSG_CONNECT, (size_t *)&len);
+
+                       conn_mode = LWSCM_WSCL_WAITING_SOCKS_CONNECT_REPLY;
+                       pending_timeout =
+                               PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
+                       lwsl_client("%s\n", "Sending SOCKS connect command");
+               }
+               else
+               {
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       lwsl_err("ERROR : SOCKS user/password reply failed, "
+                               "error code: %d\n", pt->serv_buf[1]);
+                       return 0;
+               }
+
+               n = send(wsi->desc.sockfd, (char *)pt->serv_buf, len,
+                        MSG_NOSIGNAL);
+               if (n < 0) {
+                       lwsl_debug("ERROR writing connect command to SOCKS "
+                               "socket\n");
+                       return 0;
+               }
+
+               lws_set_timeout(wsi, pending_timeout, AWAITING_TIMEOUT);
+               wsi->mode = conn_mode;
+
+               break;
+
+       /* SOCKS connect command Reply */
+       case LWSCM_WSCL_WAITING_SOCKS_CONNECT_REPLY:
+
+               /* handle proxy hung up on us */
+
+               if (pollfd->revents & LWS_POLLHUP) {
+
+                       lwsl_warn("SOCKS connection %p (fd=%d) dead\n",
+                                 (void *)wsi, pollfd->fd);
+
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       return 0;
+               }
+
+               n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 0);
+               if (n < 0) {
+                       if (LWS_ERRNO == LWS_EAGAIN) {
+                               lwsl_debug("SOCKS read returned EAGAIN... "
+                                       "retrying\n");
+                               return 0;
+                       }
+
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       lwsl_err("ERROR reading from socks socket\n");
+                       return 0;
+               }
+
+               /* processing connect reply */
+               if (pt->serv_buf[0] == SOCKS_VERSION_5
+                       && pt->serv_buf[1] == SOCKS_REQUEST_REPLY_SUCCESS)
+               {
+                       lwsl_client("%s\n", "SOCKS connect reply recieved - "
+                               "successful");
+               }
+               else
+               {
+                       lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                       lwsl_err("ERROR SOCKS connect reply failed, error "
+                               "code: %d\n", pt->serv_buf[1]);
+                       return 0;
+               }
+
+               /* free stash since we are done with it */
+               lws_free_set_NULL(wsi->u.hdr.stash);
+
+               if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
+                       wsi->vhost->socks_proxy_address))
+                       goto bail3;
+               wsi->c_port = wsi->vhost->socks_proxy_port;
+
+               /* clear his proxy connection timeout */
+
+               lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
+
+               goto start_ws_hanshake;
+#endif
        case LWSCM_WSCL_WAITING_PROXY_REPLY:
 
                /* handle proxy hung up on us */
@@ -108,7 +311,7 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
                        return 0;
                }
 
-               n = recv(wsi->sock, sb, context->pt_serv_buf_size, 0);
+               n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 0);
                if (n < 0) {
                        if (LWS_ERRNO == LWS_EAGAIN) {
                                lwsl_debug("Proxy read returned EAGAIN... retrying\n");
@@ -143,14 +346,19 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
                 * take care of our lws_callback_on_writable
                 * happening at a time when there's no real connection yet
                 */
+#if defined(LWS_WITH_SOCKS5)
+start_ws_hanshake:
+#endif
                if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
                        return -1;
 
 #ifdef LWS_OPENSSL_SUPPORT
                /* we can retry this... just cook the SSL BIO the first time */
 
-               if (wsi->use_ssl && !wsi->ssl)
-                       lws_ssl_client_bio_create(wsi);
+               if (wsi->use_ssl && !wsi->ssl) {
+                       if (lws_ssl_client_bio_create(wsi))
+                               return -1;
+               }
 
                if (wsi->use_ssl) {
                        n = lws_ssl_client_connect1(wsi);
@@ -188,6 +396,9 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
        case LWSCM_WSCL_ISSUE_HANDSHAKE2:
                p = lws_generate_client_handshake(wsi, p);
                if (p == NULL) {
+                       if (wsi->mode == LWSCM_RAW)
+                               return 0;
+
                        lwsl_err("Failed to generate handshake for client\n");
                        lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
                        return 0;
@@ -210,6 +421,24 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
                        break;
                }
 
+               if (wsi->client_http_body_pending) {
+                       wsi->mode = LWSCM_WSCL_ISSUE_HTTP_BODY;
+                       lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
+                                       context->timeout_secs);
+                       /* user code must ask for writable callback */
+                       break;
+               }
+
+               goto client_http_body_sent;
+
+       case LWSCM_WSCL_ISSUE_HTTP_BODY:
+               if (wsi->client_http_body_pending) {
+                       lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
+                                       context->timeout_secs);
+                       /* user code must ask for writable callback */
+                       break;
+               }
+client_http_body_sent:
                wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
                wsi->u.hdr.lextable_pos = 0;
                wsi->mode = LWSCM_WSCL_WAITING_SERVER_REPLY;
@@ -291,7 +520,9 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
 
 bail3:
                lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
-               wsi->vhost->protocols[0].callback(wsi,
+               if (cce)
+                       lwsl_info("reason: %s\n", cce);
+               wsi->protocol->callback(wsi,
                        LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
                        wsi->user_space, (void *)cce, cce ? strlen(cce) : 0);
                wsi->already_did_cce = 1;
@@ -320,7 +551,12 @@ static void
 strtolower(char *s)
 {
        while (*s) {
+#ifdef LWS_PLAT_OPTEE
+               int tolower_optee(int c);
+               *s = tolower_optee((int)*s);
+#else
                *s = tolower((int)*s);
+#endif
                s++;
        }
 }
@@ -335,6 +571,9 @@ lws_http_transaction_completed_client(struct lws *wsi)
                return 1;
        }
 
+       /* we don't support chained client connections yet */
+       return 1;
+#if 0
        /* otherwise set ourselves up ready to go again */
        wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
        wsi->mode = LWSCM_HTTP_CLIENT_ACCEPTED;
@@ -349,7 +588,7 @@ lws_http_transaction_completed_client(struct lws *wsi)
         * we can drop the ah, if any
         */
        if (wsi->u.hdr.ah) {
-               wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
+               lws_header_table_force_to_detachable_state(wsi);
                lws_header_table_detach(wsi, 0);
        }
 
@@ -359,6 +598,16 @@ lws_http_transaction_completed_client(struct lws *wsi)
        lwsl_info("%s: %p: keep-alive await new transaction\n", __func__, wsi);
 
        return 0;
+#endif
+}
+
+LWS_VISIBLE LWS_EXTERN unsigned int
+lws_http_client_http_response(struct lws *wsi)
+{
+       if (!wsi->u.http.ah)
+               return 0;
+
+       return wsi->u.http.ah->http_response;
 }
 
 int
@@ -368,8 +617,9 @@ lws_client_interpret_server_handshake(struct lws *wsi)
        int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
        struct lws_context *context = wsi->context;
        const char *pc, *prot, *ads = NULL, *path, *cce = NULL;
-       struct allocated_headers *ah;
-       char *p;
+       struct allocated_headers *ah = NULL;
+       char *p, *q;
+       char new_path[300];
 #ifndef LWS_NO_EXTENSIONS
        struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
        char *sb = (char *)&pt->serv_buf[0];
@@ -381,14 +631,17 @@ lws_client_interpret_server_handshake(struct lws *wsi)
        int more = 1;
        void *v;
 #endif
+       if (wsi->u.hdr.stash)
+               lws_free_set_NULL(wsi->u.hdr.stash);
 
+       ah = wsi->u.hdr.ah;
        if (!wsi->do_ws) {
                /* we are being an http client...
                 */
-               ah = wsi->u.hdr.ah;
                lws_union_transition(wsi, LWSCM_HTTP_CLIENT_ACCEPTED);
                wsi->state = LWSS_CLIENT_HTTP_ESTABLISHED;
                wsi->u.http.ah = ah;
+               ah->http_response = 0;
        }
 
        /*
@@ -424,6 +677,9 @@ lws_client_interpret_server_handshake(struct lws *wsi)
                goto bail3;
        }
        n = atoi(p);
+       if (ah)
+               ah->http_response = n;
+
        if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
                p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);
                if (!p) {
@@ -431,26 +687,79 @@ lws_client_interpret_server_handshake(struct lws *wsi)
                        goto bail3;
                }
 
-               if (lws_parse_uri(p, &prot, &ads, &port, &path)) {
-                       cce = "HS: URI did not parse";
-                       goto bail3;
+               /* Relative reference absolute path */
+               if (p[0] == '/')
+               {
+#ifdef LWS_OPENSSL_SUPPORT
+                       ssl = wsi->use_ssl;
+#endif
+                       ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
+                       port = wsi->c_port;
+                       path = p + 1; /* +1 as lws_client_reset expects leading / to be omitted */
                }
+               /* Absolute (Full) URI */
+               else if (strchr(p, ':'))
+               {
+                       if (lws_parse_uri(p, &prot, &ads, &port, &path)) {
+                               cce = "HS: URI did not parse";
+                               goto bail3;
+                       }
 
-               if (!strcmp(prot, "wss://") || !strcmp(prot, "https://"))
-                       ssl = 1;
+                       if (!strcmp(prot, "wss") || !strcmp(prot, "https"))
+                               ssl = 1;
+               }
+               /* Relative reference relative path */
+               else
+               {
+                       /* This doesn't try to calculate an absolute path, that will be left to the server */
+#ifdef LWS_OPENSSL_SUPPORT
+                       ssl = wsi->use_ssl;
+#endif
+                       ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
+                       port = wsi->c_port;
+                       path = new_path + 1; /* +1 as lws_client_reset expects leading / to be omitted */
+                       strncpy(new_path, lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI), sizeof(new_path));
+                       new_path[sizeof(new_path) - 1] = '\0';
+                       q = strrchr(new_path, '/');
+                       if (q)
+                       {
+                               strncpy(q + 1, p, sizeof(new_path) - (q - new_path) - 1);
+                               new_path[sizeof(new_path) - 1] = '\0';
+                       }
+                       else
+                       {
+                               path = p;
+                       }
+               }
 
-               if (lws_client_reset(wsi, ssl, ads, port, path, ads)) {
+#ifdef LWS_OPENSSL_SUPPORT
+               if (wsi->use_ssl && !ssl) {
+                       cce = "HS: Redirect attempted SSL downgrade";
+                       goto bail3;
+               }
+#endif
+
+               if (!lws_client_reset(&wsi, ssl, ads, port, path, ads)) {
+                       /* there are two ways to fail out with NULL return...
+                        * simple, early problem where the wsi is intact, or
+                        * we went through with the reconnect attempt and the
+                        * wsi is already closed.  In the latter case, the wsi
+                        * has beet set to NULL additionally.
+                        */
                        lwsl_err("Redirect failed\n");
                        cce = "HS: Redirect failed";
-                       goto bail3;
+                       if (wsi)
+                               goto bail3;
+
+                       return 1;
                }
                return 0;
        }
 
        if (!wsi->do_ws) {
-               if (n != 200) {
-                       lwsl_notice("Connection failed with code %d", n);
-                       cce = "HS: Server did not return 200";
+               if (n != 200 && n != 201 && n != 304 && n != 401) {
+                       lwsl_notice("Connection failed with code %d\n", n);
+                       cce = "HS: Server unrecognized response code";
                        goto bail2;
                }
 
@@ -483,10 +792,10 @@ lws_client_interpret_server_handshake(struct lws *wsi)
 
                if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
                        wsi->u.http.content_length =
-                                       atoi(lws_hdr_simple_ptr(wsi,
+                                       atoll(lws_hdr_simple_ptr(wsi,
                                                WSI_TOKEN_HTTP_CONTENT_LENGTH));
-                       lwsl_notice("%s: incoming content length %d\n", __func__,
-                                       wsi->u.http.content_length);
+                       lwsl_notice("%s: incoming content length %llu\n", __func__,
+                                       (unsigned long long)wsi->u.http.content_length);
                        wsi->u.http.content_remain = wsi->u.http.content_length;
                } else /* can't do 1.1 without a content length or chunked */
                        if (!wsi->chunked)
@@ -582,6 +891,7 @@ lws_client_interpret_server_handshake(struct lws *wsi)
                 * no protocol name to work from,
                 * default to first protocol
                 */
+               n = 0;
                wsi->protocol = &wsi->vhost->protocols[0];
                goto check_extensions;
        }
@@ -626,8 +936,30 @@ lws_client_interpret_server_handshake(struct lws *wsi)
                goto bail2;
        }
 
-
 check_extensions:
+       /*
+        * stitch protocol choice into the vh protocol linked list
+        * We always insert ourselves at the start of the list
+        *
+        * X <-> B
+        * X <-> pAn <-> pB
+        */
+       //lwsl_err("%s: pre insert vhost start wsi %p, that wsi prev == %p\n",
+       //              __func__,
+       //              wsi->vhost->same_vh_protocol_list[n],
+       //              wsi->same_vh_protocol_prev);
+       wsi->same_vh_protocol_prev = /* guy who points to us */
+               &wsi->vhost->same_vh_protocol_list[n];
+       wsi->same_vh_protocol_next = /* old first guy is our next */
+                       wsi->vhost->same_vh_protocol_list[n];
+       /* we become the new first guy */
+       wsi->vhost->same_vh_protocol_list[n] = wsi;
+
+       if (wsi->same_vh_protocol_next)
+               /* old first guy points back to us now */
+               wsi->same_vh_protocol_next->same_vh_protocol_prev =
+                               &wsi->same_vh_protocol_next;
+
 #ifndef LWS_NO_EXTENSIONS
        /* instantiate the accepted extensions */
 
@@ -749,7 +1081,7 @@ check_extensions:
                                      wsi->act_ext_user[wsi->count_act_ext],
                                      NULL, 0)) {
                                lwsl_err("%s: ext %s rejects server options %s",
-                                        ext->name, a);
+                                        __func__, ext->name, a);
                                cce = "HS: EXT: Rejects server options";
                                goto bail2;
                        }
@@ -809,6 +1141,7 @@ check_accept:
 
        lws_union_transition(wsi, LWSCM_WS_CLIENT);
        wsi->state = LWSS_ESTABLISHED;
+       lws_restart_ws_ping_pong_timer(wsi);
 
        wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
 
@@ -830,12 +1163,14 @@ check_accept:
        wsi->u.ws.rx_ubuf_alloc = n;
        lwsl_info("Allocating client RX buffer %d\n", n);
 
-       if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n,
+#if !defined(LWS_WITH_ESP32)
+       if (setsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_SNDBUF, (const char *)&n,
                       sizeof n)) {
                lwsl_warn("Failed to set SNDBUF to %d", n);
                cce = "HS: SO_SNDBUF failed";
                goto bail3;
        }
+#endif
 
        lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
 
@@ -871,9 +1206,10 @@ bail3:
        close_reason = LWS_CLOSE_STATUS_NOSTATUS;
 
 bail2:
-       wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
+       if (wsi->protocol)
+               wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
                                wsi->user_space, (void *)cce,
-                               (unsigned int) (cce ? strlen(cce): 0));
+                               (unsigned int)strlen(cce));
        wsi->already_did_cce = 1;
 
        lwsl_info("closing connection due to bail2 connection error\n");
@@ -896,13 +1232,46 @@ lws_generate_client_handshake(struct lws *wsi, char *pkt)
        const struct lws_extension *ext;
        int ext_count = 0;
 #endif
+       const char *pp = lws_hdr_simple_ptr(wsi,
+                               _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
 
        meth = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD);
        if (!meth) {
                meth = "GET";
                wsi->do_ws = 1;
-       } else
+       } else {
                wsi->do_ws = 0;
+       }
+
+       if (!strcmp(meth, "RAW")) {
+               lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
+               lwsl_notice("client transition to raw\n");
+
+               if (pp) {
+                       const struct lws_protocols *pr;
+
+                       pr = lws_vhost_name_to_protocol(wsi->vhost, pp);
+
+                       if (!pr) {
+                               lwsl_err("protocol %s not enabled on vhost\n",
+                                        pp);
+                               return NULL;
+                       }
+
+                       lws_bind_protocol(wsi, pr);
+               }
+
+               if ((wsi->protocol->callback)(wsi,
+                               LWS_CALLBACK_RAW_ADOPT,
+                               wsi->user_space, NULL, 0))
+                       return NULL;
+
+               lws_header_table_force_to_detachable_state(wsi);
+               lws_union_transition(wsi, LWSCM_RAW);
+               lws_header_table_detach(wsi, 1);
+
+               return NULL;
+       }
 
        if (wsi->do_ws) {
                /*
@@ -941,9 +1310,14 @@ lws_generate_client_handshake(struct lws *wsi, char *pkt)
        p += sprintf(p, "Host: %s\x0d\x0a",
                     lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
 
-       if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
-               p += sprintf(p, "Origin: http://%s\x0d\x0a",
-                            lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
+       if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN)) {
+               if (lws_check_opt(context->options, LWS_SERVER_OPTION_JUST_USE_RAW_ORIGIN))
+                       p += sprintf(p, "Origin: %s\x0d\x0a",
+                                    lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
+               else
+                       p += sprintf(p, "Origin: http://%s\x0d\x0a",
+                                    lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
+       }
 
        if (wsi->do_ws) {
                p += sprintf(p, "Upgrade: websocket\x0d\x0a"
@@ -1018,7 +1392,7 @@ lws_generate_client_handshake(struct lws *wsi, char *pkt)
 
        /* give userland a chance to append, eg, cookies */
 
-       wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
+       wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
                                wsi->user_space, &p, (pkt + context->pt_serv_buf_size) - p - 12);
 
        p += sprintf(p, "\x0d\x0a");