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"
|
|
|
|
|
2014-04-03 09:03:37 +08:00
|
|
|
int lws_handshake_client(struct libwebsocket *wsi, unsigned char **buf, size_t len)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
switch (wsi->mode) {
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
|
|
|
|
case LWS_CONNMODE_WS_CLIENT:
|
|
|
|
for (n = 0; n < len; n++)
|
|
|
|
if (libwebsocket_client_rx_sm(wsi, *(*buf)++)) {
|
|
|
|
lwsl_debug("client_rx_sm failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
int lws_client_socket_service(struct libwebsocket_context *context,
|
2014-04-02 19:45:42 +08:00
|
|
|
struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd)
|
2013-01-16 11:53:05 +08:00
|
|
|
{
|
|
|
|
int n;
|
2013-02-10 11:03:32 +08:00
|
|
|
char *p = (char *)&context->service_buffer[0];
|
2013-01-16 11:53:05 +08:00
|
|
|
int len;
|
2014-04-05 16:48:48 +01:00
|
|
|
unsigned char c;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
switch (wsi->mode) {
|
|
|
|
|
2013-09-20 20:26:12 +08:00
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
|
|
|
|
* timeout protection set in client-handshake.c
|
|
|
|
*/
|
|
|
|
|
2013-10-26 20:23:00 +08:00
|
|
|
if (libwebsocket_client_connect_2(context, wsi) == NULL) {
|
2013-09-20 20:26:12 +08:00
|
|
|
/* closed */
|
|
|
|
lwsl_client("closed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* either still pending connection, or changed mode */
|
|
|
|
return 0;
|
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
|
|
|
|
|
|
|
|
/* 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",
|
|
|
|
(void *)wsi, pollfd->fd);
|
|
|
|
|
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
2013-01-16 13:40:43 +08:00
|
|
|
return 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
2013-02-10 11:03:32 +08:00
|
|
|
n = recv(wsi->sock, context->service_buffer,
|
2013-02-11 17:13:32 +08:00
|
|
|
sizeof(context->service_buffer), 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
if (n < 0) {
|
2013-10-25 15:50:21 +02:00
|
|
|
|
2014-02-28 12:37:52 +01:00
|
|
|
if (LWS_ERRNO == LWS_EAGAIN) {
|
2013-10-25 15:50:21 +02:00
|
|
|
lwsl_debug(
|
|
|
|
"Proxy read returned EAGAIN... retrying\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-10 11:03:32 +08:00
|
|
|
context->service_buffer[13] = '\0';
|
|
|
|
if (strcmp((char *)context->service_buffer, "HTTP/1.0 200 ")) {
|
2013-01-16 11:53:05 +08:00
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_err("ERROR proxy: %s\n", context->service_buffer);
|
2013-01-16 13:40:43 +08:00
|
|
|
return 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clear his proxy connection timeout */
|
|
|
|
|
|
|
|
libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
|
|
|
|
|
|
|
/* fallthru */
|
|
|
|
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
|
|
|
|
* timeout protection set in client-handshake.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* take care of our libwebsocket_callback_on_writable
|
|
|
|
* 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) {
|
2014-04-27 13:22:09 +02:00
|
|
|
#if defined(CYASSL_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,
|
|
|
|
_WSI_TOKEN_CLIENT_PEER_ADDRESS);
|
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);
|
2014-03-28 14:00:01 +08:00
|
|
|
#ifndef USE_CYASSL
|
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
|
|
|
|
*/
|
2014-03-23 12:24:40 +08:00
|
|
|
#ifdef USE_CYASSL
|
|
|
|
#ifdef CYASSL_SNI_HOST_NAME
|
2014-04-02 19:45:42 +08:00
|
|
|
CyaSSL_UseSNI(wsi->ssl, CYASSL_SNI_HOST_NAME,
|
|
|
|
hostname, strlen(hostname));
|
2014-03-23 12:24:40 +08:00
|
|
|
#endif
|
|
|
|
#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
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
#ifdef USE_CYASSL
|
|
|
|
/*
|
|
|
|
* CyaSSL does certificate verification differently
|
|
|
|
* from OpenSSL.
|
|
|
|
* If we should ignore the certificate, we need to set
|
|
|
|
* this before SSL_new and SSL_connect is called.
|
|
|
|
* Otherwise the connect will simply fail with error
|
|
|
|
* code -155
|
|
|
|
*/
|
|
|
|
if (wsi->use_ssl == 2)
|
|
|
|
CyaSSL_set_verify(wsi->ssl,
|
|
|
|
SSL_VERIFY_NONE, NULL);
|
|
|
|
#endif /* USE_CYASSL */
|
|
|
|
|
|
|
|
wsi->client_bio =
|
|
|
|
BIO_new_socket(wsi->sock, BIO_NOCLOSE);
|
2013-01-16 11:53:05 +08:00
|
|
|
SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
#ifdef USE_CYASSL
|
2013-02-06 15:29:18 +09:00
|
|
|
CyaSSL_set_using_nonblock(wsi->ssl, 1);
|
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,
|
|
|
|
"SSL_connect LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE",
|
|
|
|
n, n > 0);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
n = SSL_get_error(wsi->ssl, n);
|
|
|
|
|
|
|
|
if (n == SSL_ERROR_WANT_READ ||
|
|
|
|
n == SSL_ERROR_WANT_WRITE) {
|
|
|
|
/*
|
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
|
|
|
*/
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_info(
|
|
|
|
"SSL_connect WANT_... retrying\n");
|
|
|
|
libwebsocket_callback_on_writable(
|
|
|
|
context, wsi);
|
2013-10-24 22:27:08 +08:00
|
|
|
|
|
|
|
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SSL;
|
|
|
|
|
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",
|
2013-11-22 13:14:26 +02:00
|
|
|
n,
|
|
|
|
ERR_error_string(n,
|
2013-10-25 15:52:47 +02:00
|
|
|
(char *)context->service_buffer));
|
|
|
|
return 0;
|
|
|
|
}
|
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 */
|
|
|
|
|
2013-10-24 22:27:08 +08:00
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_SSL:
|
|
|
|
|
|
|
|
if (wsi->use_ssl) {
|
|
|
|
|
|
|
|
if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_SSL) {
|
|
|
|
lws_latency_pre(context, wsi);
|
|
|
|
n = SSL_connect(wsi->ssl);
|
|
|
|
lws_latency(context, wsi,
|
2013-10-25 15:52:47 +02:00
|
|
|
"SSL_connect LWS_CONNMODE_WS_CLIENT_WAITING_SSL",
|
2013-10-24 22:27:08 +08:00
|
|
|
n, n > 0);
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
n = SSL_get_error(wsi->ssl, n);
|
|
|
|
|
|
|
|
if (n == SSL_ERROR_WANT_READ ||
|
|
|
|
n == SSL_ERROR_WANT_WRITE) {
|
|
|
|
/*
|
|
|
|
* wants us to retry connect due to
|
|
|
|
* state of the underlying ssl layer...
|
|
|
|
* but since it may be stalled on
|
|
|
|
* blocked write, no incoming data may
|
|
|
|
* arrive to trigger the retry.
|
|
|
|
* Force (possibly many times if the SSL
|
|
|
|
* state persists in returning the
|
|
|
|
* condition code, but other sockets
|
|
|
|
* are getting serviced inbetweentimes)
|
|
|
|
* us to get called back when writable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lwsl_info(
|
|
|
|
"SSL_connect WANT_... retrying\n");
|
|
|
|
libwebsocket_callback_on_writable(
|
|
|
|
context, wsi);
|
|
|
|
|
|
|
|
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SSL;
|
|
|
|
|
|
|
|
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",
|
2013-11-22 13:14:26 +02:00
|
|
|
n,
|
|
|
|
ERR_error_string(n,
|
2013-10-25 15:52:47 +02:00
|
|
|
(char *)context->service_buffer));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-24 22:27:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 15:29:18 +09:00
|
|
|
#ifndef USE_CYASSL
|
2013-02-11 17:13:32 +08:00
|
|
|
/*
|
|
|
|
* See comment above about CyaSSL certificate
|
|
|
|
* 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",
|
|
|
|
n, ERR_error_string(n, (char *)context->service_buffer));
|
2014-10-30 18:26:14 +00:00
|
|
|
libwebsocket_close_and_free_session(context,
|
|
|
|
wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
2013-02-11 17:13:32 +08:00
|
|
|
#endif /* USE_CYASSL */
|
2013-01-16 11:53:05 +08:00
|
|
|
} else
|
|
|
|
wsi->ssl = NULL;
|
2013-02-11 17:13:32 +08:00
|
|
|
#endif
|
2014-04-05 16:48:48 +01:00
|
|
|
|
|
|
|
wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2;
|
|
|
|
libwebsocket_set_timeout(wsi,
|
|
|
|
PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
|
|
|
|
AWAITING_TIMEOUT);
|
|
|
|
|
|
|
|
/* fallthru */
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2014-04-05 16:48:48 +01:00
|
|
|
case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2:
|
2013-01-16 11:53:05 +08:00
|
|
|
p = libwebsockets_generate_client_handshake(context, 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");
|
2013-01-16 13:40:43 +08:00
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
|
|
|
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
|
|
|
|
2014-04-05 16:48:48 +01:00
|
|
|
n = lws_ssl_capable_write(wsi, context->service_buffer, p - (char *)context->service_buffer);
|
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n, n == p - (char *)context->service_buffer);
|
|
|
|
switch (n) {
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2013-01-16 11:53:05 +08:00
|
|
|
lwsl_debug("ERROR writing to client socket\n");
|
|
|
|
libwebsocket_close_and_free_session(context, 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:
|
|
|
|
libwebsocket_callback_on_writable(context, wsi);
|
|
|
|
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;
|
2013-01-16 11:53:05 +08:00
|
|
|
wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
|
|
|
|
libwebsocket_set_timeout(wsi,
|
2013-02-11 17:13:32 +08:00
|
|
|
PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
|
|
|
|
AWAITING_TIMEOUT);
|
2013-01-16 11:53:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
|
|
|
|
|
|
|
|
/* 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 &&
|
|
|
|
len > 0) {
|
2014-10-09 08:14:30 +08:00
|
|
|
n = lws_ssl_capable_read(context, wsi, &c, 1);
|
2014-04-05 16:48:48 +01:00
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n, n == 1);
|
|
|
|
switch (n) {
|
|
|
|
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
|
|
|
|
2014-06-29 00:25:19 -04:00
|
|
|
if (libwebsocket_parse(context, 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
return lws_client_interpret_server_handshake(context, wsi);
|
|
|
|
|
|
|
|
bail3:
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_info(
|
|
|
|
"closing connection at LWS_CONNMODE...SERVER_REPLY\n");
|
2013-01-16 11:53:05 +08:00
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
2013-09-18 21:01:02 +08:00
|
|
|
return -1;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
|
|
|
|
lwsl_ext("LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
|
|
|
|
lwsl_ext("LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
|
|
|
|
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
|
|
|
|
lws_client_interpret_server_handshake(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi)
|
|
|
|
{
|
|
|
|
const char *pc;
|
|
|
|
int okay = 0;
|
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;
|
|
|
|
int len;
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-01-16 11:53:05 +08:00
|
|
|
char ext_name[128];
|
|
|
|
struct libwebsocket_extension *ext;
|
|
|
|
void *v;
|
2013-01-20 17:08:31 +08:00
|
|
|
int more = 1;
|
|
|
|
const char *c;
|
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
int n;
|
2013-03-23 09:53:17 +08:00
|
|
|
int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
|
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
|
|
|
|
*/
|
|
|
|
|
2013-02-11 20:03:59 +08:00
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
|
|
|
|
lwsl_info("no ACCEPT\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
|
|
|
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
|
2013-02-11 20:03:59 +08:00
|
|
|
if (!p) {
|
|
|
|
lwsl_info("no URI\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
|
|
|
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);
|
2013-01-16 11:53:05 +08:00
|
|
|
if (pc == NULL)
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_parser("lws_client_int_s_hs: no protocol list\n");
|
2013-01-16 11:53:05 +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-01-16 11:53:05 +08:00
|
|
|
|
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) &&
|
|
|
|
(pc[len] == ',' || pc[len] == '\0')) {
|
2013-01-16 11:53:05 +08:00
|
|
|
okay = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (*pc && *pc != ',')
|
|
|
|
pc++;
|
2014-12-01 19:28:28 +08:00
|
|
|
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)) {
|
2013-01-16 11:53:05 +08:00
|
|
|
lwsl_ext("no client extenstions allowed by server\n");
|
|
|
|
goto check_accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* break down the list of server accepted extensions
|
|
|
|
* and go through matching them or identifying bogons
|
|
|
|
*/
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
if (lws_hdr_copy(wsi, (char *)context->service_buffer,
|
2013-02-11 20:03:59 +08:00
|
|
|
sizeof(context->service_buffer), WSI_TOKEN_EXTENSIONS) < 0) {
|
|
|
|
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
|
|
|
|
|
|
|
c = (char *)context->service_buffer;
|
2013-01-16 11:53:05 +08:00
|
|
|
n = 0;
|
|
|
|
while (more) {
|
|
|
|
|
|
|
|
if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
|
|
|
|
ext_name[n] = *c++;
|
|
|
|
if (n < sizeof(ext_name) - 1)
|
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ext_name[n] = '\0';
|
|
|
|
if (!*c)
|
|
|
|
more = 0;
|
|
|
|
else {
|
|
|
|
c++;
|
|
|
|
if (!n)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check we actually support it */
|
|
|
|
|
|
|
|
lwsl_ext("checking client ext %s\n", ext_name);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
ext = wsi->protocol->owning_server->extensions;
|
|
|
|
while (ext && ext->callback) {
|
|
|
|
|
|
|
|
if (strcmp(ext_name, ext->name)) {
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
lwsl_ext("instantiating client ext %s\n", ext_name);
|
|
|
|
|
|
|
|
/* instantiate the extension on this conn */
|
|
|
|
|
|
|
|
wsi->active_extensions_user[
|
|
|
|
wsi->count_active_extensions] =
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_zalloc(ext->per_session_data_size);
|
2013-01-16 11:53:05 +08:00
|
|
|
if (wsi->active_extensions_user[
|
|
|
|
wsi->count_active_extensions] == NULL) {
|
|
|
|
lwsl_err("Out of mem\n");
|
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
wsi->active_extensions[
|
|
|
|
wsi->count_active_extensions] = ext;
|
|
|
|
|
|
|
|
/* allow him to construct his context */
|
|
|
|
|
|
|
|
ext->callback(wsi->protocol->owning_server,
|
|
|
|
ext, wsi,
|
|
|
|
LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
|
|
|
|
wsi->active_extensions_user[
|
|
|
|
wsi->count_active_extensions],
|
|
|
|
NULL, 0);
|
|
|
|
|
|
|
|
wsi->count_active_extensions++;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2013-02-11 17:13:32 +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) */
|
2013-02-18 16:30:10 +08:00
|
|
|
if (libwebsocket_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
|
|
|
|
*/
|
|
|
|
|
|
|
|
wsi->protocol->callback(context, wsi,
|
|
|
|
LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
|
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
/* clear his proxy connection timeout */
|
|
|
|
|
|
|
|
libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
|
|
|
|
2013-02-06 15:15:25 +09:00
|
|
|
/* free up his parsing allocations */
|
2014-11-08 11:18:47 +08:00
|
|
|
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_free(wsi->u.hdr.ah);
|
2013-02-06 15:15:25 +09:00
|
|
|
|
2014-11-08 11:18:47 +08:00
|
|
|
lws_union_transition(wsi, LWS_CONNMODE_WS_CLIENT);
|
2013-01-16 11:53:05 +08:00
|
|
|
wsi->state = WSI_STATE_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;
|
|
|
|
n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
|
2014-12-04 23:59:35 +01:00
|
|
|
wsi->u.ws.rx_user_buffer = lws_malloc(n);
|
2013-02-06 21:10:16 +09:00
|
|
|
if (!wsi->u.ws.rx_user_buffer) {
|
|
|
|
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
|
|
|
}
|
|
|
|
lwsl_info("Allocating client RX buffer %d\n", n);
|
|
|
|
|
2014-02-28 00:37:20 +01: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 */
|
|
|
|
|
|
|
|
wsi->protocol->callback(context, wsi,
|
|
|
|
LWS_CALLBACK_CLIENT_ESTABLISHED,
|
|
|
|
wsi->user_space, NULL, 0);
|
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;
|
|
|
|
for (n = 0; n < wsi->count_active_extensions; n++)
|
|
|
|
if (wsi->active_extensions[n] == ext)
|
|
|
|
v = wsi->active_extensions_user[n];
|
|
|
|
|
|
|
|
ext->callback(context, ext, wsi,
|
|
|
|
LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
|
|
|
|
ext++;
|
|
|
|
}
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bail3:
|
2014-12-05 00:09:20 +01:00
|
|
|
lws_free2(wsi->u.ws.rx_user_buffer);
|
2013-03-23 09:53:17 +08:00
|
|
|
close_reason = LWS_CLOSE_STATUS_NOSTATUS;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
bail2:
|
2013-02-11 13:04:45 +08:00
|
|
|
if (wsi->protocol)
|
|
|
|
wsi->protocol->callback(context, wsi,
|
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
|
|
|
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
|
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
|
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
|
|
|
|
2013-02-06 15:15:25 +09:00
|
|
|
/* free up his parsing allocations */
|
|
|
|
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_free(wsi->u.hdr.ah);
|
2013-02-06 15:15:25 +09:00
|
|
|
|
2013-03-23 09:53:17 +08:00
|
|
|
libwebsocket_close_and_free_session(context, wsi, close_reason);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, char *pkt)
|
|
|
|
{
|
2013-02-10 11:03:32 +08:00
|
|
|
char buf[128];
|
2013-01-16 11:53:05 +08:00
|
|
|
char hash[20];
|
2013-01-21 11:04:49 +08:00
|
|
|
char key_b64[40];
|
2013-01-16 11:53:05 +08:00
|
|
|
char *p = pkt;
|
|
|
|
int n;
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-01-16 11:53:05 +08:00
|
|
|
struct libwebsocket_extension *ext;
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
n = libwebsockets_get_random(context, hash, 16);
|
|
|
|
if (n != 16) {
|
|
|
|
lwsl_err("Unable to read from random dev %s\n",
|
|
|
|
SYSTEM_RANDOM_FILEPATH);
|
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
|
|
|
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",
|
|
|
|
lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2013-02-11 17:13:32 +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",
|
|
|
|
lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
|
|
|
|
p += sprintf(p,
|
2013-02-11 20:03:59 +08:00
|
|
|
"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 */
|
|
|
|
|
|
|
|
p += sprintf(p, "Sec-WebSocket-Extensions: ");
|
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) {
|
|
|
|
|
2014-04-02 19:45:42 +08:00
|
|
|
n = lws_ext_callback_for_each_extension_type(context, wsi,
|
|
|
|
LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
|
|
|
|
(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = context->protocols[0].callback(context, wsi,
|
|
|
|
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++ = ',';
|
|
|
|
p += sprintf(p, "%s", ext->name);
|
|
|
|
ext_count++;
|
|
|
|
|
|
|
|
ext++;
|
|
|
|
}
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
p += sprintf(p, "\x0d\x0a");
|
|
|
|
|
|
|
|
if (wsi->ietf_spec_revision)
|
|
|
|
p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
|
|
|
|
wsi->ietf_spec_revision);
|
|
|
|
|
|
|
|
/* give userland a chance to append, eg, cookies */
|
|
|
|
|
|
|
|
context->protocols[0].callback(context, wsi,
|
|
|
|
LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
|
2013-02-10 11:03:32 +08:00
|
|
|
NULL, &p, (pkt + sizeof(context->service_buffer)) - 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);
|
|
|
|
|
2014-09-26 05:56:23 +08:00
|
|
|
libwebsockets_SHA1((unsigned char *)buf, n, (unsigned char *)hash);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
lws_b64_encode_string(hash, 20,
|
2013-02-18 10:38:45 +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;
|
|
|
|
}
|
|
|
|
|