2013-01-16 11:53:05 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2014-04-02 19:45:42 +08:00
|
|
|
* Copyright (C) 2010-2014 Andy Green <andy@warmcat.com>
|
2013-01-16 11:53:05 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
ah http1.1 deal with pipelined headers properly
Connections must hold an ah for the whole time they are
processing one header set, even if eg, the headers are
fragmented and it involves network roundtrip times.
However on http1.1 / keepalive, it must drop the ah when
there are no more header sets to deal with, and reacquire
the ah later when more data appears. It's because the
time between header sets / http1.1 requests is unbounded
and the ah would be tied up forever.
But in the case that we got pipelined http1.1 requests,
even partial already buffered, we must keep the ah,
resetting it instead of dropping it. Because we store
the rx data conveniently in a per-tsi buffer since it only
does one thing at a time per thread, we cannot go back to
the event loop to await a new ah inside one service action.
But no problem since we definitely already have an ah,
let's just reuse it at http completion time if more rx is
already buffered.
NB: attack.sh makes request with echo | nc, this
accidentally sends a trailing '\n' from the echo showing
this problem. With this patch attack.sh can complete well.
Signed-off-by: Andy Green <andy.green@linaro.org>
2016-01-30 11:43:10 +08:00
|
|
|
int
|
|
|
|
lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len)
|
2014-04-03 09:03:37 +08:00
|
|
|
{
|
2016-01-11 11:34:01 +08:00
|
|
|
int m;
|
2015-12-28 14:24:49 +08:00
|
|
|
|
2014-04-03 09:03:37 +08:00
|
|
|
switch (wsi->mode) {
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_WAITING_PROXY_REPLY:
|
|
|
|
case LWSCM_WSCL_ISSUE_HANDSHAKE:
|
|
|
|
case LWSCM_WSCL_WAITING_SERVER_REPLY:
|
|
|
|
case LWSCM_WSCL_WAITING_EXTENSION_CONNECT:
|
|
|
|
case LWSCM_WS_CLIENT:
|
2016-01-11 11:34:01 +08:00
|
|
|
while (len) {
|
2015-12-28 14:24:49 +08:00
|
|
|
/*
|
|
|
|
* we were accepting input but now we stopped doing so
|
|
|
|
*/
|
|
|
|
if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_debug("%s: caching %d\n", __func__, len);
|
|
|
|
lws_rxflow_cache(wsi, *buf, 0, len);
|
2015-12-28 14:24:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-11 11:34:01 +08:00
|
|
|
if (wsi->u.ws.rx_draining_ext) {
|
|
|
|
m = lws_rx_sm(wsi, 0);
|
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-28 14:24:49 +08:00
|
|
|
/* account for what we're using in rxflow buffer */
|
|
|
|
if (wsi->rxflow_buffer)
|
|
|
|
wsi->rxflow_pos++;
|
|
|
|
|
2015-12-04 09:23:56 +08:00
|
|
|
if (lws_client_rx_sm(wsi, *(*buf)++)) {
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_debug("client_rx_sm exited\n");
|
|
|
|
return -1;
|
2014-04-03 09:03:37 +08:00
|
|
|
}
|
2016-01-11 11:34:01 +08:00
|
|
|
len--;
|
2015-12-28 14:24:49 +08:00
|
|
|
}
|
|
|
|
lwsl_debug("%s: finished with %d\n", __func__, len);
|
2014-04-03 09:03:37 +08:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-12-28 14:24:49 +08:00
|
|
|
|
2014-04-03 09:03:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
ah http1.1 deal with pipelined headers properly
Connections must hold an ah for the whole time they are
processing one header set, even if eg, the headers are
fragmented and it involves network roundtrip times.
However on http1.1 / keepalive, it must drop the ah when
there are no more header sets to deal with, and reacquire
the ah later when more data appears. It's because the
time between header sets / http1.1 requests is unbounded
and the ah would be tied up forever.
But in the case that we got pipelined http1.1 requests,
even partial already buffered, we must keep the ah,
resetting it instead of dropping it. Because we store
the rx data conveniently in a per-tsi buffer since it only
does one thing at a time per thread, we cannot go back to
the event loop to await a new ah inside one service action.
But no problem since we definitely already have an ah,
let's just reuse it at http completion time if more rx is
already buffered.
NB: attack.sh makes request with echo | nc, this
accidentally sends a trailing '\n' from the echo showing
this problem. With this patch attack.sh can complete well.
Signed-off-by: Andy Green <andy.green@linaro.org>
2016-01-30 11:43:10 +08:00
|
|
|
int
|
|
|
|
lws_client_socket_service(struct lws_context *context, struct lws *wsi,
|
|
|
|
struct lws_pollfd *pollfd)
|
2013-01-16 11:53:05 +08:00
|
|
|
{
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
char *p = (char *)&pt->serv_buf[0];
|
|
|
|
char *sb = p;
|
2014-04-05 16:48:48 +01:00
|
|
|
unsigned char c;
|
2015-12-06 08:40:00 +08:00
|
|
|
int n, len;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
switch (wsi->mode) {
|
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_WAITING_CONNECT:
|
2013-09-20 20:26:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
|
|
|
|
* timeout protection set in client-handshake.c
|
|
|
|
*/
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (!lws_client_connect_2(wsi)) {
|
2013-09-20 20:26:12 +08:00
|
|
|
/* closed */
|
|
|
|
lwsl_client("closed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* either still pending connection, or changed mode */
|
|
|
|
return 0;
|
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_WAITING_PROXY_REPLY:
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* handle proxy hung up on us */
|
|
|
|
|
2014-03-30 09:18:05 +02:00
|
|
|
if (pollfd->revents & LWS_POLLHUP) {
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
lwsl_warn("Proxy connection %p (fd=%d) dead\n",
|
2015-12-15 21:15:58 +08:00
|
|
|
(void *)wsi, pollfd->fd);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
2013-01-16 13:40:43 +08:00
|
|
|
return 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
n = recv(wsi->sock, sb, LWS_MAX_SOCKET_IO_BUF, 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
if (n < 0) {
|
2014-02-28 12:37:52 +01:00
|
|
|
if (LWS_ERRNO == LWS_EAGAIN) {
|
2015-12-06 08:40:00 +08:00
|
|
|
lwsl_debug("Proxy read returned EAGAIN... retrying\n");
|
2013-10-25 15:50:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
2013-01-16 11:53:05 +08:00
|
|
|
lwsl_err("ERROR reading from proxy socket\n");
|
2013-01-16 13:40:43 +08:00
|
|
|
return 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
pt->serv_buf[13] = '\0';
|
|
|
|
if (strcmp(sb, "HTTP/1.0 200 ") &&
|
2016-01-29 21:18:54 +08:00
|
|
|
strcmp(sb, "HTTP/1.1 200 ")) {
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
2016-01-19 03:34:24 +08:00
|
|
|
lwsl_err("ERROR proxy: %s\n", sb);
|
2013-01-16 13:40:43 +08:00
|
|
|
return 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clear his proxy connection timeout */
|
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* fallthru */
|
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_ISSUE_HANDSHAKE:
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
|
|
|
|
* timeout protection set in client-handshake.c
|
2016-01-29 21:18:54 +08:00
|
|
|
*
|
2015-12-04 08:43:54 +08:00
|
|
|
* take care of our lws_callback_on_writable
|
2013-01-16 11:53:05 +08:00
|
|
|
* happening at a time when there's no real connection yet
|
|
|
|
*/
|
2014-04-02 14:25:10 +08:00
|
|
|
if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
|
|
|
|
return -1;
|
2013-12-18 09:48:26 +08:00
|
|
|
|
2014-04-03 10:17:00 +08:00
|
|
|
#ifdef LWS_OPENSSL_SUPPORT
|
2013-02-11 17:13:32 +08:00
|
|
|
/* we can retry this... just cook the SSL BIO the first time */
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
if (wsi->use_ssl && !wsi->ssl) {
|
2015-08-09 22:56:32 +02:00
|
|
|
#if defined(CYASSL_SNI_HOST_NAME) || defined(WOLFSSL_SNI_HOST_NAME) || defined(SSL_CTRL_SET_TLSEXT_HOSTNAME)
|
2014-04-08 16:15:02 +01:00
|
|
|
const char *hostname = lws_hdr_simple_ptr(wsi,
|
2015-06-30 14:35:01 -04:00
|
|
|
_WSI_TOKEN_CLIENT_HOST);
|
2014-04-27 13:22:09 +02:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
wsi->ssl = SSL_new(context->ssl_client_ctx);
|
2015-08-08 18:54:49 +02:00
|
|
|
#ifndef USE_WOLFSSL
|
2014-03-23 11:53:07 +08:00
|
|
|
SSL_set_mode(wsi->ssl,
|
|
|
|
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
|
2014-03-28 14:00:01 +08:00
|
|
|
#endif
|
2014-04-02 19:45:42 +08:00
|
|
|
/*
|
|
|
|
* use server name indication (SNI), if supported,
|
|
|
|
* when establishing connection
|
|
|
|
*/
|
2015-08-08 18:54:49 +02:00
|
|
|
#ifdef USE_WOLFSSL
|
2015-08-09 22:56:32 +02:00
|
|
|
#ifdef USE_OLD_CYASSL
|
|
|
|
#ifdef CYASSL_SNI_HOST_NAME
|
|
|
|
CyaSSL_UseSNI(wsi->ssl, CYASSL_SNI_HOST_NAME,
|
|
|
|
hostname, strlen(hostname));
|
|
|
|
#endif
|
|
|
|
#else
|
2015-08-08 18:54:49 +02:00
|
|
|
#ifdef WOLFSSL_SNI_HOST_NAME
|
|
|
|
wolfSSL_UseSNI(wsi->ssl, WOLFSSL_SNI_HOST_NAME,
|
2014-04-02 19:45:42 +08:00
|
|
|
hostname, strlen(hostname));
|
2014-03-23 12:24:40 +08:00
|
|
|
#endif
|
2015-08-09 22:56:32 +02:00
|
|
|
#endif
|
2014-03-23 12:24:40 +08:00
|
|
|
#else
|
2014-04-27 12:52:21 +02:00
|
|
|
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
|
2014-04-02 19:45:42 +08:00
|
|
|
SSL_set_tlsext_host_name(wsi->ssl, hostname);
|
2014-03-23 12:24:40 +08:00
|
|
|
#endif
|
2014-04-27 12:52:21 +02:00
|
|
|
#endif
|
2014-03-23 12:24:40 +08:00
|
|
|
|
2015-08-08 18:54:49 +02:00
|
|
|
#ifdef USE_WOLFSSL
|
2013-02-11 17:13:32 +08:00
|
|
|
/*
|
2015-08-09 22:56:32 +02:00
|
|
|
* wolfSSL/CyaSSL does certificate verification differently
|
2013-02-11 17:13:32 +08:00
|
|
|
* 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
|
|
|
|
*/
|
2015-08-09 22:56:32 +02:00
|
|
|
#ifdef USE_OLD_CYASSL
|
|
|
|
if (wsi->use_ssl == 2)
|
|
|
|
CyaSSL_set_verify(wsi->ssl,
|
|
|
|
SSL_VERIFY_NONE, NULL);
|
|
|
|
#else
|
2013-02-11 17:13:32 +08:00
|
|
|
if (wsi->use_ssl == 2)
|
2015-08-08 18:54:49 +02:00
|
|
|
wolfSSL_set_verify(wsi->ssl,
|
2013-02-11 17:13:32 +08:00
|
|
|
SSL_VERIFY_NONE, NULL);
|
2015-08-09 22:56:32 +02:00
|
|
|
#endif
|
2015-08-08 18:54:49 +02:00
|
|
|
#endif /* USE_WOLFSSL */
|
2013-02-11 17:13:32 +08:00
|
|
|
|
|
|
|
wsi->client_bio =
|
|
|
|
BIO_new_socket(wsi->sock, BIO_NOCLOSE);
|
2013-01-16 11:53:05 +08:00
|
|
|
SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
|
|
|
|
|
2015-08-08 18:54:49 +02:00
|
|
|
#ifdef USE_WOLFSSL
|
2015-08-09 22:56:32 +02:00
|
|
|
#ifdef USE_OLD_CYASSL
|
|
|
|
CyaSSL_set_using_nonblock(wsi->ssl, 1);
|
|
|
|
#else
|
2015-08-08 18:54:49 +02:00
|
|
|
wolfSSL_set_using_nonblock(wsi->ssl, 1);
|
2015-08-09 22:56:32 +02:00
|
|
|
#endif
|
2013-02-11 17:13:32 +08:00
|
|
|
#else
|
2013-01-28 17:48:21 +08:00
|
|
|
BIO_set_nbio(wsi->client_bio, 1); /* nonblocking */
|
2013-02-11 17:13:32 +08:00
|
|
|
#endif
|
2013-01-28 17:48:21 +08:00
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
SSL_set_ex_data(wsi->ssl,
|
|
|
|
openssl_websocket_private_data_index,
|
|
|
|
context);
|
2013-02-06 15:29:18 +09:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
if (wsi->use_ssl) {
|
2013-01-29 12:37:35 +08:00
|
|
|
lws_latency_pre(context, wsi);
|
2013-01-16 11:53:05 +08:00
|
|
|
n = SSL_connect(wsi->ssl);
|
2013-02-11 17:13:32 +08:00
|
|
|
lws_latency(context, wsi,
|
2016-01-29 21:18:54 +08:00
|
|
|
"SSL_connect LWSCM_WSCL_ISSUE_HANDSHAKE", n, n > 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
n = SSL_get_error(wsi->ssl, n);
|
|
|
|
|
2015-11-24 15:53:05 +08:00
|
|
|
if (n == SSL_ERROR_WANT_READ)
|
|
|
|
goto some_wait;
|
|
|
|
|
|
|
|
if (n == SSL_ERROR_WANT_WRITE) {
|
2013-01-16 11:53:05 +08:00
|
|
|
/*
|
2013-02-11 17:13:32 +08:00
|
|
|
* 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.
|
2013-01-16 11:53:05 +08:00
|
|
|
*/
|
2016-01-29 21:18:54 +08:00
|
|
|
lwsl_info("%s: WANT_WRITE... retrying\n", __func__);
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2015-11-24 15:53:05 +08:00
|
|
|
some_wait:
|
2015-12-17 17:03:59 +08:00
|
|
|
wsi->mode = LWSCM_WSCL_WAITING_SSL;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
return 0; /* no error */
|
|
|
|
}
|
|
|
|
n = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n <= 0) {
|
|
|
|
/*
|
|
|
|
* retry if new data comes until we
|
|
|
|
* run into the connection timeout or win
|
|
|
|
*/
|
2013-10-25 15:52:47 +02:00
|
|
|
n = ERR_get_error();
|
|
|
|
if (n != SSL_ERROR_NONE) {
|
|
|
|
lwsl_err("SSL connect error %lu: %s\n",
|
2016-01-19 03:34:24 +08:00
|
|
|
n, ERR_error_string(n, sb));
|
2016-05-03 08:08:32 +08:00
|
|
|
goto bail3;
|
2013-10-25 15:52:47 +02:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
2013-10-24 22:27:08 +08:00
|
|
|
} else
|
|
|
|
wsi->ssl = NULL;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2013-10-25 15:52:47 +02:00
|
|
|
/* fallthru */
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_WAITING_SSL:
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2013-10-24 22:27:08 +08:00
|
|
|
if (wsi->use_ssl) {
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->mode == LWSCM_WSCL_WAITING_SSL) {
|
2013-10-24 22:27:08 +08:00
|
|
|
lws_latency_pre(context, wsi);
|
|
|
|
n = SSL_connect(wsi->ssl);
|
|
|
|
lws_latency(context, wsi,
|
2015-12-17 17:03:59 +08:00
|
|
|
"SSL_connect LWSCM_WSCL_WAITING_SSL",
|
2015-11-24 15:53:05 +08:00
|
|
|
n, n > 0);
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2013-10-24 22:27:08 +08:00
|
|
|
if (n < 0) {
|
|
|
|
n = SSL_get_error(wsi->ssl, n);
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-11-24 15:53:05 +08:00
|
|
|
if (n == SSL_ERROR_WANT_READ)
|
|
|
|
goto some_wait;
|
|
|
|
|
|
|
|
if (n == SSL_ERROR_WANT_WRITE) {
|
2013-10-24 22:27:08 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-11-24 15:53:05 +08:00
|
|
|
lwsl_info("SSL_connect WANT_WRITE... retrying\n");
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-11-24 15:53:05 +08:00
|
|
|
goto some_wait;
|
2013-10-24 22:27:08 +08:00
|
|
|
}
|
|
|
|
n = -1;
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2013-10-24 22:27:08 +08:00
|
|
|
if (n <= 0) {
|
|
|
|
/*
|
|
|
|
* retry if new data comes until we
|
|
|
|
* run into the connection timeout or win
|
|
|
|
*/
|
2013-10-25 15:52:47 +02:00
|
|
|
n = ERR_get_error();
|
|
|
|
if (n != SSL_ERROR_NONE) {
|
|
|
|
lwsl_err("SSL connect error %lu: %s\n",
|
2016-01-19 03:34:24 +08:00
|
|
|
n, ERR_error_string(n, sb));
|
2016-05-03 08:08:32 +08:00
|
|
|
goto bail3;
|
2013-10-25 15:52:47 +02:00
|
|
|
}
|
2013-10-24 22:27:08 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-08-08 18:54:49 +02:00
|
|
|
#ifndef USE_WOLFSSL
|
2013-02-11 17:13:32 +08:00
|
|
|
/*
|
2015-08-08 18:54:49 +02:00
|
|
|
* See comment above about wolfSSL certificate
|
2013-02-11 17:13:32 +08:00
|
|
|
* verification
|
|
|
|
*/
|
2013-01-29 12:37:35 +08:00
|
|
|
lws_latency_pre(context, wsi);
|
2013-01-16 11:53:05 +08:00
|
|
|
n = SSL_get_verify_result(wsi->ssl);
|
2013-02-11 17:13:32 +08:00
|
|
|
lws_latency(context, wsi,
|
|
|
|
"SSL_get_verify_result LWS_CONNMODE..HANDSHAKE",
|
|
|
|
n, n > 0);
|
2014-10-30 18:26:14 +00:00
|
|
|
|
|
|
|
if (n != X509_V_OK) {
|
2014-11-18 09:28:06 +08:00
|
|
|
if ((n == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
|
|
|
|
n == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) && wsi->use_ssl == 2) {
|
2014-10-30 18:26:14 +00:00
|
|
|
lwsl_notice("accepting self-signed certificate\n");
|
|
|
|
} else {
|
2014-11-18 09:28:06 +08:00
|
|
|
lwsl_err("server's cert didn't look good, X509_V_ERR = %d: %s\n",
|
2016-01-19 03:34:24 +08:00
|
|
|
n, ERR_error_string(n, sb));
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi,
|
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
2016-05-03 08:08:32 +08:00
|
|
|
return -1;
|
2014-10-30 18:26:14 +00:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
2015-08-08 18:54:49 +02:00
|
|
|
#endif /* USE_WOLFSSL */
|
2013-01-16 11:53:05 +08:00
|
|
|
} else
|
|
|
|
wsi->ssl = NULL;
|
2013-02-11 17:13:32 +08:00
|
|
|
#endif
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
wsi->mode = LWSCM_WSCL_ISSUE_HANDSHAKE2;
|
2015-12-06 10:05:37 +08:00
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
|
2016-02-15 20:36:02 +08:00
|
|
|
context->timeout_secs);
|
2014-04-05 16:48:48 +01:00
|
|
|
|
|
|
|
/* fallthru */
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_ISSUE_HANDSHAKE2:
|
2015-12-15 21:15:58 +08:00
|
|
|
p = lws_generate_client_handshake(wsi, p);
|
2013-01-16 13:40:43 +08:00
|
|
|
if (p == NULL) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_err("Failed to generate handshake for client\n");
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
2013-01-16 13:40:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* send our request to the server */
|
|
|
|
|
2013-01-29 12:37:35 +08:00
|
|
|
lws_latency_pre(context, wsi);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
n = lws_ssl_capable_write(wsi, (unsigned char *)sb, p - sb);
|
2015-12-06 10:05:37 +08:00
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n,
|
2016-01-19 03:34:24 +08:00
|
|
|
n == p - sb);
|
2014-04-05 16:48:48 +01:00
|
|
|
switch (n) {
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2013-01-16 11:53:05 +08:00
|
|
|
lwsl_debug("ERROR writing to client socket\n");
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
2013-01-16 13:40:43 +08:00
|
|
|
return 0;
|
2014-04-05 16:48:48 +01:00
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2014-04-05 16:48:48 +01:00
|
|
|
break;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
|
|
|
|
wsi->u.hdr.lextable_pos = 0;
|
2015-12-17 17:03:59 +08:00
|
|
|
wsi->mode = LWSCM_WSCL_WAITING_SERVER_REPLY;
|
2015-12-06 10:05:37 +08:00
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
|
2016-02-15 20:36:02 +08:00
|
|
|
context->timeout_secs);
|
2013-01-16 11:53:05 +08:00
|
|
|
break;
|
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_WAITING_SERVER_REPLY:
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* handle server hung up on us */
|
|
|
|
|
2014-03-30 09:18:05 +02:00
|
|
|
if (pollfd->revents & LWS_POLLHUP) {
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
lwsl_debug("Server connection %p (fd=%d) dead\n",
|
|
|
|
(void *)wsi, pollfd->fd);
|
|
|
|
|
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
|
2014-03-30 09:18:05 +02:00
|
|
|
if (!(pollfd->revents & LWS_POLLIN))
|
2013-03-09 09:09:46 +08:00
|
|
|
break;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* interpret the server response */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HTTP/1.1 101 Switching Protocols
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
|
|
|
|
* Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
|
|
|
|
* Sec-WebSocket-Protocol: chat
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we have to take some care here to only take from the
|
|
|
|
* socket bytewise. The browser may (and has been seen to
|
|
|
|
* in the case that onopen() performs websocket traffic)
|
|
|
|
* coalesce both handshake response and websocket traffic
|
|
|
|
* in one packet, since at that point the connection is
|
|
|
|
* definitively ready from browser pov.
|
|
|
|
*/
|
|
|
|
len = 1;
|
2013-02-11 17:13:32 +08:00
|
|
|
while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
|
2015-12-06 10:05:37 +08:00
|
|
|
len > 0) {
|
2015-12-15 21:15:58 +08:00
|
|
|
n = lws_ssl_capable_read(wsi, &c, 1);
|
2015-12-06 10:05:37 +08:00
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n,
|
|
|
|
n == 1);
|
2014-04-05 16:48:48 +01:00
|
|
|
switch (n) {
|
2016-01-14 15:17:28 +08:00
|
|
|
case 0:
|
2014-04-05 16:48:48 +01:00
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2013-01-30 12:27:27 +08:00
|
|
|
goto bail3;
|
2014-04-05 16:48:48 +01:00
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
|
|
|
return 0;
|
2013-02-11 20:03:59 +08:00
|
|
|
}
|
2013-01-30 12:27:27 +08:00
|
|
|
|
2015-12-15 21:15:58 +08:00
|
|
|
if (lws_parse(wsi, c)) {
|
2013-02-11 20:03:59 +08:00
|
|
|
lwsl_warn("problems parsing header\n");
|
2013-02-04 08:55:42 +08:00
|
|
|
goto bail3;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hs may also be coming in multiple packets, there is a 5-sec
|
|
|
|
* libwebsocket timeout still active here too, so if parsing did
|
|
|
|
* not complete just wait for next packet coming in this state
|
|
|
|
*/
|
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
|
2013-01-16 11:53:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* otherwise deal with the handshake. If there's any
|
|
|
|
* packet traffic already arrived we'll trigger poll() again
|
|
|
|
* right away and deal with it that way
|
|
|
|
*/
|
|
|
|
|
2015-12-15 21:15:58 +08:00
|
|
|
return lws_client_interpret_server_handshake(wsi);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
bail3:
|
2015-12-06 10:05:37 +08:00
|
|
|
lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
|
2016-05-03 08:08:32 +08:00
|
|
|
wsi->context->protocols[0].callback(wsi,
|
|
|
|
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
|
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
2013-09-18 21:01:02 +08:00
|
|
|
return -1;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_WAITING_EXTENSION_CONNECT:
|
|
|
|
lwsl_ext("LWSCM_WSCL_WAITING_EXTENSION_CONNECT\n");
|
2013-01-16 11:53:05 +08:00
|
|
|
break;
|
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
case LWSCM_WSCL_PENDING_CANDIDATE_CHILD:
|
|
|
|
lwsl_ext("LWSCM_WSCL_PENDING_CANDIDATE_CHILD\n");
|
2013-01-16 11:53:05 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In-place str to lower case
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
strtolower(char *s)
|
|
|
|
{
|
|
|
|
while (*s) {
|
2013-04-25 09:16:30 +08:00
|
|
|
*s = tolower((int)*s);
|
2013-01-16 11:53:05 +08:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_client_interpret_server_handshake(struct lws *wsi)
|
2013-01-16 11:53:05 +08:00
|
|
|
{
|
2016-01-19 03:34:24 +08:00
|
|
|
int n, len, okay = 0, isErrorCodeReceived = 0, port = 0, ssl = 0;
|
2015-12-15 21:15:58 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
2015-12-06 08:40:00 +08:00
|
|
|
int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
|
2016-01-14 11:37:56 +08:00
|
|
|
const char *pc, *prot, *ads = NULL, *path;
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
char *p;
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
char *sb = (char *)&pt->serv_buf[0];
|
2016-01-11 11:34:01 +08:00
|
|
|
const struct lws_ext_options *opts;
|
2016-01-19 03:34:24 +08:00
|
|
|
const struct lws_extension *ext;
|
2015-12-06 08:40:00 +08:00
|
|
|
char ext_name[128];
|
2016-01-11 11:34:01 +08:00
|
|
|
const char *c, *a;
|
2015-12-30 12:12:58 +08:00
|
|
|
char ignore;
|
2015-12-06 10:05:37 +08:00
|
|
|
int more = 1;
|
2015-12-06 08:40:00 +08:00
|
|
|
void *v;
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* well, what the server sent looked reasonable for syntax.
|
|
|
|
* Now let's confirm it sent all the necessary headers
|
|
|
|
*/
|
2016-01-13 18:48:50 +08:00
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
|
|
|
|
if (!p) {
|
|
|
|
lwsl_info("no URI\n");
|
|
|
|
goto bail3;
|
|
|
|
}
|
2016-01-14 11:37:56 +08:00
|
|
|
n = atoi(p);
|
|
|
|
if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);
|
|
|
|
if (!p)
|
|
|
|
goto bail3;
|
|
|
|
|
|
|
|
if (lws_parse_uri(p, &prot, &ads, &port, &path))
|
|
|
|
goto bail3;
|
|
|
|
|
|
|
|
if (!strcmp(prot, "wss://") || !strcmp(prot, "https://"))
|
|
|
|
ssl = 1;
|
2016-01-13 18:48:50 +08:00
|
|
|
|
2016-01-14 11:37:56 +08:00
|
|
|
if (lws_client_reset(wsi, ssl, ads, port, path, ads)) {
|
|
|
|
lwsl_err("Redirect failed\n");
|
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-02-11 20:03:59 +08:00
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
|
|
|
|
lwsl_info("no ACCEPT\n");
|
2015-08-21 16:15:36 +05:30
|
|
|
isErrorCodeReceived = 1;
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
goto bail3;
|
2013-02-11 20:03:59 +08:00
|
|
|
}
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
|
|
|
|
if (p && strncmp(p, "101", 3)) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_warn(
|
|
|
|
"lws_client_handshake: got bad HTTP response '%s'\n", p);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
|
2013-02-11 20:03:59 +08:00
|
|
|
if (!p) {
|
|
|
|
lwsl_info("no UPGRADE\n");
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
goto bail3;
|
2013-02-11 20:03:59 +08:00
|
|
|
}
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
strtolower(p);
|
|
|
|
if (strcmp(p, "websocket")) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_warn(
|
|
|
|
"lws_client_handshake: got bad Upgrade header '%s'\n", p);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
|
2013-02-11 20:03:59 +08:00
|
|
|
if (!p) {
|
|
|
|
lwsl_info("no Connection hdr\n");
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
goto bail3;
|
2013-02-11 20:03:59 +08:00
|
|
|
}
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
strtolower(p);
|
|
|
|
if (strcmp(p, "upgrade")) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
|
2013-02-11 13:04:45 +08:00
|
|
|
pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
|
2015-12-06 10:05:37 +08:00
|
|
|
if (!pc) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_parser("lws_client_int_s_hs: no protocol list\n");
|
2015-11-08 12:10:26 +08:00
|
|
|
} else
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* confirm the protocol the server wants to talk was in the list
|
|
|
|
* of protocols we offered
|
|
|
|
*/
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
|
|
|
|
if (!len) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
|
2013-01-16 11:53:05 +08:00
|
|
|
/*
|
|
|
|
* no protocol name to work from,
|
|
|
|
* default to first protocol
|
|
|
|
*/
|
|
|
|
wsi->protocol = &context->protocols[0];
|
|
|
|
goto check_extensions;
|
|
|
|
}
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
|
|
|
|
len = strlen(p);
|
|
|
|
|
2014-11-30 12:43:51 +08:00
|
|
|
while (pc && *pc && !okay) {
|
2013-02-11 17:13:32 +08:00
|
|
|
if (!strncmp(pc, p, len) &&
|
2014-12-10 16:32:44 +01:00
|
|
|
(pc[len] == ',' || pc[len] == '\0')) {
|
2013-01-16 11:53:05 +08:00
|
|
|
okay = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-10 16:32:44 +01:00
|
|
|
while (*pc && *pc++ != ',')
|
|
|
|
;
|
|
|
|
while (*pc && *pc == ' ')
|
2013-01-16 11:53:05 +08:00
|
|
|
pc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!okay) {
|
2014-12-01 19:28:28 +08:00
|
|
|
lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* identify the selected protocol struct and set it
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
wsi->protocol = NULL;
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
while (context->protocols[n].callback && !wsi->protocol) {
|
|
|
|
if (strcmp(p, context->protocols[n].name) == 0) {
|
2013-01-16 11:53:05 +08:00
|
|
|
wsi->protocol = &context->protocols[n];
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
break;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->protocol == NULL) {
|
2014-12-01 19:28:28 +08:00
|
|
|
lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
check_extensions:
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-01-16 11:53:05 +08:00
|
|
|
/* instantiate the accepted extensions */
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
|
2016-02-16 15:19:36 +02:00
|
|
|
lwsl_ext("no client extensions allowed by server\n");
|
2013-01-16 11:53:05 +08:00
|
|
|
goto check_accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* break down the list of server accepted extensions
|
|
|
|
* and go through matching them or identifying bogons
|
|
|
|
*/
|
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
if (lws_hdr_copy(wsi, sb, LWS_MAX_SOCKET_IO_BUF, WSI_TOKEN_EXTENSIONS) < 0) {
|
2013-02-11 20:03:59 +08:00
|
|
|
lwsl_warn("ext list from server failed to copy\n");
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
goto bail2;
|
2013-02-11 20:03:59 +08:00
|
|
|
}
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
c = sb;
|
2013-01-16 11:53:05 +08:00
|
|
|
n = 0;
|
2015-12-30 12:12:58 +08:00
|
|
|
ignore = 0;
|
2016-01-11 11:34:01 +08:00
|
|
|
a = NULL;
|
2013-01-16 11:53:05 +08:00
|
|
|
while (more) {
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (*c && (*c != ',' && *c != '\t')) {
|
|
|
|
if (*c == ';') {
|
2015-12-30 12:12:58 +08:00
|
|
|
ignore = 1;
|
2016-01-11 11:34:01 +08:00
|
|
|
if (!a)
|
|
|
|
a = c + 1;
|
|
|
|
}
|
|
|
|
if (ignore || *c == ' ') {
|
2015-12-30 12:12:58 +08:00
|
|
|
c++;
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-11 11:34:01 +08:00
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
ext_name[n] = *c++;
|
|
|
|
if (n < sizeof(ext_name) - 1)
|
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ext_name[n] = '\0';
|
2015-12-30 12:12:58 +08:00
|
|
|
ignore = 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
if (!*c)
|
|
|
|
more = 0;
|
|
|
|
else {
|
|
|
|
c++;
|
|
|
|
if (!n)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check we actually support it */
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_notice("checking client ext %s\n", ext_name);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
n = 0;
|
2015-12-17 18:25:25 +08:00
|
|
|
ext = lws_get_context(wsi)->extensions;
|
2013-01-16 11:53:05 +08:00
|
|
|
while (ext && ext->callback) {
|
|
|
|
if (strcmp(ext_name, ext->name)) {
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 1;
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_notice("instantiating client ext %s\n", ext_name);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* instantiate the extension on this conn */
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->active_extensions[wsi->count_act_ext] = ext;
|
|
|
|
|
|
|
|
/* allow him to construct his ext instance */
|
|
|
|
|
2016-04-01 09:30:09 +08:00
|
|
|
if (ext->callback(lws_get_context(wsi), ext, wsi,
|
2016-01-11 11:34:01 +08:00
|
|
|
LWS_EXT_CB_CLIENT_CONSTRUCT,
|
|
|
|
(void *)&wsi->act_ext_user[wsi->count_act_ext],
|
2016-04-01 09:30:09 +08:00
|
|
|
(void *)&opts, 0)) {
|
|
|
|
lwsl_notice(" ext %s failed construction\n", ext_name);
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-11 11:34:01 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* allow the user code to override ext defaults if it
|
|
|
|
* wants to
|
|
|
|
*/
|
|
|
|
ext_name[0] = '\0';
|
|
|
|
if (user_callback_handle_rxflow(wsi->protocol->callback,
|
|
|
|
wsi, LWS_CALLBACK_WS_EXT_DEFAULTS,
|
|
|
|
(char *)ext->name, ext_name,
|
|
|
|
sizeof(ext_name)))
|
|
|
|
goto bail2;
|
|
|
|
|
|
|
|
if (ext_name[0] &&
|
|
|
|
lws_ext_parse_options(ext, wsi, wsi->act_ext_user[
|
|
|
|
wsi->count_act_ext], opts, ext_name,
|
|
|
|
strlen(ext_name))) {
|
|
|
|
lwsl_err("%s: unable to parse user defaults '%s'",
|
|
|
|
__func__, ext_name);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
/*
|
|
|
|
* give the extension the server options
|
|
|
|
*/
|
2016-01-19 20:01:33 +08:00
|
|
|
if (a && lws_ext_parse_options(ext, wsi,
|
|
|
|
wsi->act_ext_user[wsi->count_act_ext],
|
|
|
|
opts, a, c - a)) {
|
|
|
|
lwsl_err("%s: unable to parse remote def '%s'",
|
|
|
|
__func__, a);
|
2016-01-11 11:34:01 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (ext->callback(lws_get_context(wsi), ext, wsi,
|
|
|
|
LWS_EXT_CB_OPTION_CONFIRM,
|
|
|
|
wsi->act_ext_user[wsi->count_act_ext],
|
|
|
|
NULL, 0)) {
|
2016-01-19 20:01:33 +08:00
|
|
|
lwsl_err("%s: ext %s rejects server options %s",
|
|
|
|
ext->name, a);
|
2016-01-11 11:34:01 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->count_act_ext++;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_warn("Unknown ext '%s'!\n", ext_name);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
a = NULL;
|
2013-01-16 11:53:05 +08:00
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
check_accept:
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Confirm his accept token is the one we precomputed
|
|
|
|
*/
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
|
2013-02-18 10:38:45 +08:00
|
|
|
if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
|
2016-01-18 11:49:41 +08:00
|
|
|
lwsl_warn("lws_client_int_s_hs: accept '%s' wrong vs '%s'\n", p,
|
2013-02-18 10:38:45 +08:00
|
|
|
wsi->u.hdr.ah->initial_handshake_hash_base64);
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the per-connection user memory (if any) */
|
2015-12-04 09:23:56 +08:00
|
|
|
if (lws_ensure_user_space(wsi)) {
|
2013-02-11 20:03:59 +08:00
|
|
|
lwsl_err("Problem allocating wsi user mem\n");
|
2013-01-16 11:53:05 +08:00
|
|
|
goto bail2;
|
2013-02-11 20:03:59 +08:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2013-02-06 15:15:25 +09:00
|
|
|
/*
|
|
|
|
* we seem to be good to go, give client last chance to check
|
|
|
|
* headers and OK it
|
|
|
|
*/
|
2016-01-29 21:18:54 +08:00
|
|
|
if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
|
2015-12-18 00:50:14 +08:00
|
|
|
wsi->user_space, NULL, 0))
|
|
|
|
goto bail2;
|
2013-02-06 15:15:25 +09:00
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
/* clear his proxy connection timeout */
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2013-02-06 15:15:25 +09:00
|
|
|
/* free up his parsing allocations */
|
ah owns rxbuf
This is intended to solve a longstanding problem with the
relationship between http/1.1 keep-alive and the service
loop.
Ah now contain an rx buffer which is used during header
processing, and the ah may not be detached from the wsi
until the rx buffer is exhausted.
Having the rx buffer in the ah means we can delay using the
rx until a later service loop.
Ah which have pending rx force POLLIN service on the wsi
they are attached to automatically, so we can interleave
general service / connections with draining each ah rx
buffer.
The possible http/1.1 situations and their dispositions are:
1) exactly one set of http headers come. After processing,
the ah is detached since no pending rx left. If more
headers come later, a fresh ah is aqcuired when available
and the rx flow control blocks the read until then.
2) more that one whole set of headers come and we remain in
http mode (no upgrade). The ah is left attached and
returns to the service loop after the first set of headers.
We will get forced service due to the ah having pending
content (respecting flowcontrol) and process the pending
rx in the ah. If we use it all up, we will detach the
ah.
3) one set of http headers come with ws traffic appended.
We service the headers, do the upgrade, and keep the ah
until the remaining ws content is used. When we
exhausted the ws traffix in the ah rx buffer, we
detach the ah.
Since there can be any amount of http/1.1 pipelining on a
connection, and each may be expensive to service, it's now
enforced there is a return to the service loop after each
header set is serviced on a connection.
When I added the forced service for ah with pending buffering,
I added support for it to the windows plat code. However this
is untested.
Signed-off-by: Andy Green <andy.green@linaro.org>
2016-02-15 12:37:04 +08:00
|
|
|
lws_header_table_detach(wsi);
|
2013-02-06 15:15:25 +09:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
lws_union_transition(wsi, LWSCM_WS_CLIENT);
|
|
|
|
wsi->state = LWSS_ESTABLISHED;
|
2013-01-21 11:04:23 +08:00
|
|
|
|
2014-10-08 12:00:53 +08:00
|
|
|
wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
|
2013-03-16 11:24:23 +08:00
|
|
|
|
2013-02-06 21:10:16 +09:00
|
|
|
/*
|
|
|
|
* 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;
|
2016-01-11 11:34:01 +08:00
|
|
|
n += LWS_PRE;
|
|
|
|
wsi->u.ws.rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */);
|
|
|
|
if (!wsi->u.ws.rx_ubuf) {
|
2013-02-06 21:10:16 +09:00
|
|
|
lwsl_err("Out of Mem allocating rx buffer %d\n", n);
|
2013-03-23 09:53:17 +08:00
|
|
|
goto bail2;
|
2013-02-06 21:10:16 +09:00
|
|
|
}
|
2016-01-12 23:05:02 +08:00
|
|
|
wsi->u.ws.rx_ubuf_alloc = n;
|
2013-02-06 21:10:16 +09:00
|
|
|
lwsl_info("Allocating client RX buffer %d\n", n);
|
|
|
|
|
2015-12-06 10:05:37 +08:00
|
|
|
if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n,
|
|
|
|
sizeof n)) {
|
2013-03-23 09:53:17 +08:00
|
|
|
lwsl_warn("Failed to set SNDBUF to %d", n);
|
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
|
|
|
|
|
|
|
|
/* call him back to inform him he is up */
|
|
|
|
|
2015-12-18 00:50:14 +08:00
|
|
|
if (wsi->protocol->callback(wsi, LWS_CALLBACK_CLIENT_ESTABLISHED,
|
|
|
|
wsi->user_space, NULL, 0))
|
|
|
|
goto bail3;
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-01-16 11:53:05 +08:00
|
|
|
/*
|
|
|
|
* inform all extensions, not just active ones since they
|
|
|
|
* already know
|
|
|
|
*/
|
|
|
|
ext = context->extensions;
|
|
|
|
|
|
|
|
while (ext && ext->callback) {
|
|
|
|
v = NULL;
|
2016-01-11 11:34:01 +08:00
|
|
|
for (n = 0; n < wsi->count_act_ext; n++)
|
2013-01-16 11:53:05 +08:00
|
|
|
if (wsi->active_extensions[n] == ext)
|
2016-01-11 11:34:01 +08:00
|
|
|
v = wsi->act_ext_user[n];
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
ext->callback(context, ext, wsi,
|
2016-01-11 11:34:01 +08:00
|
|
|
LWS_EXT_CB_ANY_WSI_ESTABLISHED, v, NULL, 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
ext++;
|
|
|
|
}
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bail3:
|
2013-03-23 09:53:17 +08:00
|
|
|
close_reason = LWS_CLOSE_STATUS_NOSTATUS;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
bail2:
|
2016-06-13 08:43:03 +02:00
|
|
|
if (wsi->protocol && wsi->state == LWSS_ESTABLISHED) {
|
2015-08-21 16:15:36 +05:30
|
|
|
if (isErrorCodeReceived && p) {
|
2015-12-17 07:54:44 +08:00
|
|
|
wsi->protocol->callback(wsi,
|
2015-08-21 16:15:36 +05:30
|
|
|
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
|
2015-12-06 08:40:00 +08:00
|
|
|
wsi->user_space, p,
|
|
|
|
(unsigned int)strlen(p));
|
2015-08-21 16:15:36 +05:30
|
|
|
} else {
|
2015-12-17 07:54:44 +08:00
|
|
|
wsi->protocol->callback(wsi,
|
2015-08-21 16:15:36 +05:30
|
|
|
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
|
2015-12-06 08:40:00 +08:00
|
|
|
wsi->user_space, NULL, 0);
|
2015-08-21 16:15:36 +05:30
|
|
|
}
|
|
|
|
}
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
|
2013-01-30 12:27:27 +08:00
|
|
|
lwsl_info("closing connection due to bail2 connection error\n");
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
|
2016-01-13 18:48:50 +08:00
|
|
|
/* closing will free up his parsing allocations */
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, close_reason);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_generate_client_handshake(struct lws *wsi, char *pkt)
|
2013-01-16 11:53:05 +08:00
|
|
|
{
|
2015-12-06 08:40:00 +08:00
|
|
|
char buf[128], hash[20], key_b64[40], *p = pkt;
|
2015-12-15 21:15:58 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
2013-01-16 11:53:05 +08:00
|
|
|
int n;
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2015-12-11 10:45:35 +08:00
|
|
|
const struct lws_extension *ext;
|
2013-01-16 11:53:05 +08:00
|
|
|
int ext_count = 0;
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* create the random key
|
|
|
|
*/
|
2015-12-04 08:43:54 +08:00
|
|
|
n = lws_get_random(context, hash, 16);
|
2013-01-16 11:53:05 +08:00
|
|
|
if (n != 16) {
|
|
|
|
lwsl_err("Unable to read from random dev %s\n",
|
2015-12-06 10:05:37 +08:00
|
|
|
SYSTEM_RANDOM_FILEPATH);
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
2013-01-16 11:53:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 00 example client handshake
|
|
|
|
*
|
|
|
|
* GET /socket.io/websocket HTTP/1.1
|
|
|
|
* Upgrade: WebSocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Host: 127.0.0.1:9999
|
|
|
|
* Origin: http://127.0.0.1
|
|
|
|
* Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^
|
|
|
|
* Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
|
|
|
|
* Cookie: socketio=websocket
|
|
|
|
*
|
|
|
|
* (Á®Ä0¶†≥
|
|
|
|
*
|
|
|
|
* 04 example client handshake
|
|
|
|
*
|
|
|
|
* GET /chat HTTP/1.1
|
|
|
|
* Host: server.example.com
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
|
|
|
|
* Sec-WebSocket-Origin: http://example.com
|
|
|
|
* Sec-WebSocket-Protocol: chat, superchat
|
|
|
|
* Sec-WebSocket-Version: 4
|
|
|
|
*/
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a",
|
2015-12-06 08:40:00 +08:00
|
|
|
lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2015-12-06 08:40:00 +08:00
|
|
|
p += sprintf(p, "Pragma: no-cache\x0d\x0a"
|
|
|
|
"Cache-Control: no-cache\x0d\x0a");
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
p += sprintf(p, "Host: %s\x0d\x0a",
|
2015-12-06 08:40:00 +08:00
|
|
|
lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
|
|
|
|
p += sprintf(p, "Upgrade: websocket\x0d\x0a"
|
|
|
|
"Connection: Upgrade\x0d\x0a"
|
|
|
|
"Sec-WebSocket-Key: ");
|
2013-01-21 11:04:49 +08:00
|
|
|
strcpy(p, key_b64);
|
|
|
|
p += strlen(key_b64);
|
2013-01-16 11:53:05 +08:00
|
|
|
p += sprintf(p, "\x0d\x0a");
|
2013-02-11 13:04:45 +08:00
|
|
|
if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
|
2013-10-25 17:13:11 +08:00
|
|
|
p += sprintf(p, "Origin: http://%s\x0d\x0a",
|
2013-02-11 17:13:32 +08:00
|
|
|
lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
|
2013-02-11 13:04:45 +08:00
|
|
|
|
|
|
|
if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
|
2013-01-16 11:53:05 +08:00
|
|
|
p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
|
2013-02-11 17:13:32 +08:00
|
|
|
lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* tell the server what extensions we could support */
|
|
|
|
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-01-16 11:53:05 +08:00
|
|
|
ext = context->extensions;
|
|
|
|
while (ext && ext->callback) {
|
2015-12-17 17:03:59 +08:00
|
|
|
n = lws_ext_cb_all_exts(context, wsi,
|
2016-01-11 11:34:01 +08:00
|
|
|
LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION,
|
2015-12-06 08:40:00 +08:00
|
|
|
(char *)ext->name, 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
if (n) { /* an extension vetos us */
|
|
|
|
lwsl_ext("ext %s vetoed\n", (char *)ext->name);
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-17 07:54:44 +08:00
|
|
|
n = context->protocols[0].callback(wsi,
|
2013-01-16 11:53:05 +08:00
|
|
|
LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
|
|
|
|
wsi->user_space, (char *)ext->name, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* zero return from callback means
|
|
|
|
* go ahead and allow the extension,
|
|
|
|
* it's what we get if the callback is
|
|
|
|
* unhandled
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply it */
|
|
|
|
|
|
|
|
if (ext_count)
|
|
|
|
*p++ = ',';
|
2016-03-09 23:35:41 +08:00
|
|
|
else
|
|
|
|
p += sprintf(p, "Sec-WebSocket-Extensions: ");
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
p += sprintf(p, "%s", ext->client_offer);
|
2013-01-16 11:53:05 +08:00
|
|
|
ext_count++;
|
|
|
|
|
|
|
|
ext++;
|
|
|
|
}
|
2016-03-09 23:35:41 +08:00
|
|
|
if (ext_count)
|
|
|
|
p += sprintf(p, "\x0d\x0a");
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
if (wsi->ietf_spec_revision)
|
|
|
|
p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
|
2015-12-06 08:40:00 +08:00
|
|
|
wsi->ietf_spec_revision);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
/* give userland a chance to append, eg, cookies */
|
|
|
|
|
2016-01-29 21:18:54 +08:00
|
|
|
context->protocols[0].callback(wsi, LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
|
2016-05-03 21:41:24 +08:00
|
|
|
wsi->user_space, &p, (pkt + LWS_MAX_SOCKET_IO_BUF) - p - 12);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
p += sprintf(p, "\x0d\x0a");
|
|
|
|
|
|
|
|
/* prepare the expected server accept response */
|
|
|
|
|
2013-02-12 10:07:22 +08:00
|
|
|
key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
|
|
|
|
n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
|
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_SHA1((unsigned char *)buf, n, (unsigned char *)hash);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
lws_b64_encode_string(hash, 20,
|
2015-12-06 08:40:00 +08:00
|
|
|
wsi->u.hdr.ah->initial_handshake_hash_base64,
|
|
|
|
sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|