2013-01-16 11:53:05 +08:00
|
|
|
/*
|
2017-11-16 09:37:04 +08:00
|
|
|
* libwebsockets - lib/client/client.c
|
2013-01-16 11:53:05 +08:00
|
|
|
*
|
2017-11-16 09:37:04 +08:00
|
|
|
* Copyright (C) 2010-2017 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
|
|
|
|
*/
|
|
|
|
|
2018-05-03 10:49:36 +08:00
|
|
|
#include "core/private.h"
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2016-08-08 21:54:30 +08:00
|
|
|
LWS_VISIBLE LWS_EXTERN void
|
|
|
|
lws_client_http_body_pending(struct lws *wsi, int something_left_to_send)
|
|
|
|
{
|
|
|
|
wsi->client_http_body_pending = !!something_left_to_send;
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
/*
|
|
|
|
* return self, or queued client wsi we are acting on behalf of
|
2018-04-30 19:17:32 +08:00
|
|
|
*
|
|
|
|
* That is the TAIL of the queue (new queue elements are added at the HEAD)
|
2018-03-26 12:05:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct lws *
|
|
|
|
lws_client_wsi_effective(struct lws *wsi)
|
|
|
|
{
|
2018-04-30 19:17:32 +08:00
|
|
|
struct lws_dll_lws *tail = NULL;
|
2018-03-26 12:05:04 +08:00
|
|
|
|
|
|
|
if (!wsi->transaction_from_pipeline_queue ||
|
|
|
|
!wsi->dll_client_transaction_queue_head.next)
|
|
|
|
return wsi;
|
|
|
|
|
2018-04-30 19:17:32 +08:00
|
|
|
lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
|
|
|
|
wsi->dll_client_transaction_queue_head.next) {
|
|
|
|
tail = d;
|
|
|
|
} lws_end_foreach_dll_safe(d, d1);
|
2018-03-26 12:05:04 +08:00
|
|
|
|
2018-04-30 19:17:32 +08:00
|
|
|
return lws_container_of(tail, struct lws,
|
|
|
|
dll_client_transaction_queue);
|
2018-03-26 12:05:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return self or the guy we are queued under
|
2018-04-27 09:13:23 +08:00
|
|
|
*
|
|
|
|
* REQUIRES VHOST LOCK HELD
|
2018-03-26 12:05:04 +08:00
|
|
|
*/
|
|
|
|
|
2018-04-27 09:13:23 +08:00
|
|
|
static struct lws *
|
|
|
|
_lws_client_wsi_master(struct lws *wsi)
|
2018-03-26 12:05:04 +08:00
|
|
|
{
|
|
|
|
struct lws *wsi_eff = wsi;
|
|
|
|
struct lws_dll_lws *d;
|
|
|
|
|
|
|
|
d = wsi->dll_client_transaction_queue.prev;
|
|
|
|
while (d) {
|
|
|
|
wsi_eff = lws_container_of(d, struct lws,
|
|
|
|
dll_client_transaction_queue_head);
|
|
|
|
|
|
|
|
d = d->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wsi_eff;
|
|
|
|
}
|
|
|
|
|
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
|
2018-03-26 12:05:04 +08:00
|
|
|
lws_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd,
|
|
|
|
struct lws *wsi_conn)
|
2013-01-16 11:53:05 +08:00
|
|
|
{
|
2018-03-26 12:05:04 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2018-03-25 07:03:10 +08:00
|
|
|
char *p = (char *)&pt->serv_buf[0];
|
2018-03-26 12:05:04 +08:00
|
|
|
struct lws *w;
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2018-03-25 07:03:10 +08:00
|
|
|
char ebuf[128];
|
|
|
|
#endif
|
2016-07-01 08:54:39 +08:00
|
|
|
const char *cce = NULL;
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
|
|
|
ssize_t len = 0;
|
2014-04-05 16:48:48 +01:00
|
|
|
unsigned char c;
|
2018-04-27 19:16:50 +08:00
|
|
|
#endif
|
2016-07-01 08:54:39 +08:00
|
|
|
char *sb = p;
|
2017-09-01 15:37:53 +08:00
|
|
|
int n = 0;
|
2017-05-05 11:38:34 -04:00
|
|
|
#if defined(LWS_WITH_SOCKS5)
|
2018-05-25 21:43:31 +08:00
|
|
|
int conn_mode = 0, pending_timeout = 0;
|
2017-05-05 11:38:34 -04:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
if ((pollfd->revents & LWS_POLLOUT) &&
|
|
|
|
wsi->keepalive_active &&
|
|
|
|
wsi->dll_client_transaction_queue_head.next) {
|
2018-04-30 19:17:32 +08:00
|
|
|
struct lws *wfound = NULL;
|
2018-03-26 12:05:04 +08:00
|
|
|
|
|
|
|
lwsl_debug("%s: pollout HANDSHAKE2\n", __func__);
|
|
|
|
|
2018-04-30 19:17:32 +08:00
|
|
|
/*
|
|
|
|
* We have a transaction queued that wants to pipeline.
|
|
|
|
*
|
|
|
|
* We have to allow it to send headers strictly in the order
|
|
|
|
* that it was queued, ie, tail-first.
|
|
|
|
*/
|
2018-03-26 12:05:04 +08:00
|
|
|
lws_vhost_lock(wsi->vhost);
|
|
|
|
lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
|
|
|
|
wsi->dll_client_transaction_queue_head.next) {
|
|
|
|
struct lws *w = lws_container_of(d, struct lws,
|
|
|
|
dll_client_transaction_queue);
|
|
|
|
|
2018-04-30 19:17:32 +08:00
|
|
|
lwsl_debug("%s: %p states 0x%x\n", __func__, w, w->wsistate);
|
|
|
|
if (lwsi_state(w) == LRS_H1C_ISSUE_HANDSHAKE2)
|
|
|
|
wfound = w;
|
2018-03-26 12:05:04 +08:00
|
|
|
} lws_end_foreach_dll_safe(d, d1);
|
|
|
|
|
2018-04-30 19:17:32 +08:00
|
|
|
if (wfound) {
|
|
|
|
/*
|
|
|
|
* pollfd has the master sockfd in it... we
|
|
|
|
* need to use that in HANDSHAKE2 to understand
|
|
|
|
* which wsi to actually write on
|
|
|
|
*/
|
|
|
|
lws_client_socket_service(wfound, pollfd, wsi);
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
} else
|
|
|
|
lwsl_debug("%s: didn't find anything in txn q in HS2\n",
|
|
|
|
__func__);
|
|
|
|
|
|
|
|
lws_vhost_unlock(wsi->vhost);
|
2018-04-17 15:35:15 +08:00
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
switch (lwsi_state(wsi)) {
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_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;
|
|
|
|
|
2017-05-05 11:38:34 -04:00
|
|
|
#if defined(LWS_WITH_SOCKS5)
|
|
|
|
/* SOCKS Greeting Reply */
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_WAITING_SOCKS_GREETING_REPLY:
|
|
|
|
case LRS_WAITING_SOCKS_AUTH_REPLY:
|
|
|
|
case LRS_WAITING_SOCKS_CONNECT_REPLY:
|
2017-05-05 11:38:34 -04:00
|
|
|
|
|
|
|
/* handle proxy hung up on us */
|
|
|
|
|
|
|
|
if (pollfd->revents & LWS_POLLHUP) {
|
|
|
|
lwsl_warn("SOCKS connection %p (fd=%d) dead\n",
|
|
|
|
(void *)wsi, pollfd->fd);
|
2017-09-01 19:09:47 +08:00
|
|
|
goto bail3;
|
2017-05-05 11:38:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 0);
|
|
|
|
if (n < 0) {
|
|
|
|
if (LWS_ERRNO == LWS_EAGAIN) {
|
2017-09-01 19:09:47 +08:00
|
|
|
lwsl_debug("SOCKS read EAGAIN, retrying\n");
|
2017-05-05 11:38:34 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
lwsl_err("ERROR reading from SOCKS socket\n");
|
2017-09-01 19:09:47 +08:00
|
|
|
goto bail3;
|
2017-05-05 11:38:34 -04:00
|
|
|
}
|
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
switch (lwsi_state(wsi)) {
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_WAITING_SOCKS_GREETING_REPLY:
|
2017-09-01 19:09:47 +08:00
|
|
|
if (pt->serv_buf[0] != SOCKS_VERSION_5)
|
|
|
|
goto socks_reply_fail;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
if (pt->serv_buf[1] == SOCKS_AUTH_NO_AUTH) {
|
2017-11-16 09:37:04 +08:00
|
|
|
lwsl_client("SOCKS GR: No Auth Method\n");
|
2017-09-01 19:09:47 +08:00
|
|
|
socks_generate_msg(wsi, SOCKS_MSG_CONNECT, &len);
|
2018-04-02 11:55:17 +08:00
|
|
|
conn_mode = LRS_WAITING_SOCKS_CONNECT_REPLY;
|
2017-11-16 09:37:04 +08:00
|
|
|
pending_timeout =
|
|
|
|
PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
|
2017-09-01 19:09:47 +08:00
|
|
|
goto socks_send;
|
|
|
|
}
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
if (pt->serv_buf[1] == SOCKS_AUTH_USERNAME_PASSWORD) {
|
2017-11-16 09:37:04 +08:00
|
|
|
lwsl_client("SOCKS GR: User/Pw Method\n");
|
|
|
|
socks_generate_msg(wsi,
|
|
|
|
SOCKS_MSG_USERNAME_PASSWORD,
|
|
|
|
&len);
|
2018-04-02 11:55:17 +08:00
|
|
|
conn_mode = LRS_WAITING_SOCKS_AUTH_REPLY;
|
2017-11-16 09:37:04 +08:00
|
|
|
pending_timeout =
|
|
|
|
PENDING_TIMEOUT_AWAITING_SOCKS_AUTH_REPLY;
|
2017-09-01 19:09:47 +08:00
|
|
|
goto socks_send;
|
2017-05-05 11:38:34 -04:00
|
|
|
}
|
2017-09-01 19:09:47 +08:00
|
|
|
goto socks_reply_fail;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_WAITING_SOCKS_AUTH_REPLY:
|
2017-09-01 19:09:47 +08:00
|
|
|
if (pt->serv_buf[0] != SOCKS_SUBNEGOTIATION_VERSION_1 ||
|
|
|
|
pt->serv_buf[1] != SOCKS_SUBNEGOTIATION_STATUS_SUCCESS)
|
|
|
|
goto socks_reply_fail;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
lwsl_client("SOCKS password OK, sending connect\n");
|
2017-09-01 15:37:53 +08:00
|
|
|
socks_generate_msg(wsi, SOCKS_MSG_CONNECT, &len);
|
2018-04-02 11:55:17 +08:00
|
|
|
conn_mode = LRS_WAITING_SOCKS_CONNECT_REPLY;
|
2017-11-16 09:37:04 +08:00
|
|
|
pending_timeout =
|
|
|
|
PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
|
2017-09-01 19:09:47 +08:00
|
|
|
socks_send:
|
|
|
|
n = send(wsi->desc.sockfd, (char *)pt->serv_buf, len,
|
|
|
|
MSG_NOSIGNAL);
|
|
|
|
if (n < 0) {
|
|
|
|
lwsl_debug("ERROR writing to socks proxy\n");
|
|
|
|
goto bail3;
|
2017-05-05 11:38:34 -04:00
|
|
|
}
|
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
lws_set_timeout(wsi, pending_timeout, AWAITING_TIMEOUT);
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, conn_mode);
|
2017-09-01 19:09:47 +08:00
|
|
|
break;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
socks_reply_fail:
|
|
|
|
lwsl_notice("socks reply: v%d, err %d\n",
|
|
|
|
pt->serv_buf[0], pt->serv_buf[1]);
|
|
|
|
goto bail3;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_WAITING_SOCKS_CONNECT_REPLY:
|
2017-09-01 19:09:47 +08:00
|
|
|
if (pt->serv_buf[0] != SOCKS_VERSION_5 ||
|
|
|
|
pt->serv_buf[1] != SOCKS_REQUEST_REPLY_SUCCESS)
|
|
|
|
goto socks_reply_fail;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
lwsl_client("socks connect OK\n");
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
/* free stash since we are done with it */
|
2017-12-07 10:09:56 +08:00
|
|
|
lws_client_stash_destroy(wsi);
|
2017-11-16 09:37:04 +08:00
|
|
|
if (lws_hdr_simple_create(wsi,
|
|
|
|
_WSI_TOKEN_CLIENT_PEER_ADDRESS,
|
2017-09-01 19:09:47 +08:00
|
|
|
wsi->vhost->socks_proxy_address))
|
|
|
|
goto bail3;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
wsi->c_port = wsi->vhost->socks_proxy_port;
|
2017-05-05 11:38:34 -04:00
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
/* clear his proxy connection timeout */
|
|
|
|
lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
|
|
|
goto start_ws_handshake;
|
2018-05-25 21:43:31 +08:00
|
|
|
default:
|
|
|
|
break;
|
2017-09-01 19:09:47 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-05-05 11:38:34 -04:00
|
|
|
#endif
|
2017-09-01 19:09:47 +08:00
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_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
|
|
|
|
2017-09-01 19:09:47 +08:00
|
|
|
goto bail3;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:55:56 +08:00
|
|
|
n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 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) {
|
2017-11-16 09:37:04 +08:00
|
|
|
lwsl_debug("Proxy read EAGAIN... retrying\n");
|
2013-10-25 15:50:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
lwsl_err("ERROR reading from proxy socket\n");
|
2017-09-01 19:09:47 +08:00
|
|
|
goto bail3;
|
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 ")) {
|
2016-01-19 03:34:24 +08:00
|
|
|
lwsl_err("ERROR proxy: %s\n", sb);
|
2017-09-01 19:09:47 +08:00
|
|
|
goto bail3;
|
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 */
|
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_H1C_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
|
|
|
|
*/
|
2017-05-05 11:38:34 -04:00
|
|
|
#if defined(LWS_WITH_SOCKS5)
|
2017-09-01 19:09:47 +08:00
|
|
|
start_ws_handshake:
|
2017-05-05 11:38:34 -04:00
|
|
|
#endif
|
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
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
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
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if ((wsi->tls.use_ssl & LCCSCF_USE_SSL) && !wsi->tls.ssl &&
|
2017-11-16 09:37:04 +08:00
|
|
|
lws_ssl_client_bio_create(wsi) < 0) {
|
|
|
|
cce = "bio_create failed";
|
|
|
|
goto bail3;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (wsi->tls.use_ssl & LCCSCF_USE_SSL) {
|
2016-05-03 07:26:10 +08:00
|
|
|
n = lws_ssl_client_connect1(wsi);
|
|
|
|
if (!n)
|
2016-03-28 12:43:55 +08:00
|
|
|
return 0;
|
2016-07-01 08:54:39 +08:00
|
|
|
if (n < 0) {
|
|
|
|
cce = "lws_ssl_client_connect1 failed";
|
2016-05-03 07:26:10 +08:00
|
|
|
goto bail3;
|
2016-07-01 08:54:39 +08:00
|
|
|
}
|
2013-10-24 22:27:08 +08:00
|
|
|
} else
|
2018-05-01 12:41:42 +08:00
|
|
|
wsi->tls.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
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_WAITING_SSL:
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (wsi->tls.use_ssl & LCCSCF_USE_SSL) {
|
2018-03-24 17:52:57 +01:00
|
|
|
n = lws_ssl_client_connect2(wsi, ebuf, sizeof(ebuf));
|
2016-05-03 07:26:10 +08:00
|
|
|
if (!n)
|
2016-03-28 12:43:55 +08:00
|
|
|
return 0;
|
2016-07-01 08:54:39 +08:00
|
|
|
if (n < 0) {
|
2018-03-24 17:52:57 +01:00
|
|
|
cce = ebuf;
|
2016-05-03 07:26:10 +08:00
|
|
|
goto bail3;
|
2016-07-01 08:54:39 +08:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
} else
|
2018-05-01 12:41:42 +08:00
|
|
|
wsi->tls.ssl = NULL;
|
2013-02-11 17:13:32 +08:00
|
|
|
#endif
|
2018-03-27 09:17:19 +08:00
|
|
|
#if defined (LWS_WITH_HTTP2)
|
|
|
|
if (wsi->client_h2_alpn) {
|
|
|
|
/*
|
|
|
|
* We connected to the server and set up tls, and
|
|
|
|
* negotiated "h2".
|
|
|
|
*
|
|
|
|
* So this is it, we are an h2 master client connection
|
|
|
|
* now, not an h1 client connection.
|
|
|
|
*/
|
2018-04-12 15:56:38 +08:00
|
|
|
lws_tls_server_conn_alpn(wsi);
|
2018-03-27 09:17:19 +08:00
|
|
|
|
|
|
|
/* send the H2 preface to legitimize the connection */
|
|
|
|
if (lws_h2_issue_preface(wsi)) {
|
|
|
|
cce = "error sending h2 preface";
|
|
|
|
goto bail3;
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2018-03-27 09:17:19 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_H1C_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
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_H1C_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) {
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->role_ops == &role_ops_raw_skt ||
|
|
|
|
wsi->role_ops == &role_ops_raw_file)
|
2017-03-07 16:06:05 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_err("Failed to generate handshake for client\n");
|
2018-02-03 13:48:18 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "chs");
|
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
|
|
|
|
2018-04-27 09:13:23 +08:00
|
|
|
w = _lws_client_wsi_master(wsi);
|
2018-04-17 15:35:15 +08:00
|
|
|
lwsl_info("%s: HANDSHAKE2: %p: sending headers on %p (wsistate 0x%x 0x%x)\n",
|
2018-04-11 13:39:42 +08:00
|
|
|
__func__, wsi, w, wsi->wsistate, w->wsistate);
|
2018-03-26 12:05:04 +08:00
|
|
|
|
|
|
|
n = lws_ssl_capable_write(w, (unsigned char *)sb, (int)(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");
|
2018-02-03 13:48:18 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "cws");
|
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
|
|
|
}
|
|
|
|
|
2016-08-08 21:54:30 +08:00
|
|
|
if (wsi->client_http_body_pending) {
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_ISSUE_HTTP_BODY);
|
2017-11-16 09:37:04 +08:00
|
|
|
lws_set_timeout(wsi,
|
|
|
|
PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
|
2016-08-08 21:54:30 +08:00
|
|
|
context->timeout_secs);
|
|
|
|
/* user code must ask for writable callback */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsi_set_state(wsi, LRS_WAITING_SERVER_REPLY);
|
|
|
|
wsi->hdr_parsing_completed = 0;
|
|
|
|
|
|
|
|
if (lwsi_state(w) == LRS_IDLING) {
|
|
|
|
lwsi_set_state(w, LRS_WAITING_SERVER_REPLY);
|
|
|
|
w->hdr_parsing_completed = 0;
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2018-04-27 15:20:56 +08:00
|
|
|
w->http.ah->parser_state = WSI_TOKEN_NAME_PART;
|
|
|
|
w->http.ah->lextable_pos = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
/* If we're (re)starting on headers, need other implied init */
|
2018-04-27 15:20:56 +08:00
|
|
|
wsi->http.ah->ues = URIES_IDLE;
|
2018-04-27 19:16:50 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
|
|
|
|
wsi->context->timeout_secs);
|
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
lws_callback_on_writable(w);
|
|
|
|
|
2016-08-08 21:54:30 +08:00
|
|
|
goto client_http_body_sent;
|
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_ISSUE_HTTP_BODY:
|
2016-08-08 21:54:30 +08:00
|
|
|
if (wsi->client_http_body_pending) {
|
2018-04-17 11:43:20 +08:00
|
|
|
//lws_set_timeout(wsi,
|
|
|
|
// PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
|
|
|
|
// context->timeout_secs);
|
2016-08-08 21:54:30 +08:00
|
|
|
/* user code must ask for writable callback */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
client_http_body_sent:
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2018-03-26 12:05:04 +08:00
|
|
|
/* prepare ourselves to do the parsing */
|
2018-04-27 15:20:56 +08:00
|
|
|
wsi->http.ah->parser_state = WSI_TOKEN_NAME_PART;
|
|
|
|
wsi->http.ah->lextable_pos = 0;
|
2018-04-27 19:16:50 +08:00
|
|
|
#endif
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_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;
|
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
case LRS_WAITING_SERVER_REPLY:
|
2017-10-13 10:33:02 +08:00
|
|
|
/*
|
|
|
|
* handle server hanging up on us...
|
|
|
|
* but if there is POLLIN waiting, handle that first
|
|
|
|
*/
|
|
|
|
if ((pollfd->revents & (LWS_POLLIN | LWS_POLLHUP)) ==
|
|
|
|
LWS_POLLHUP) {
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
lwsl_debug("Server connection %p (fd=%d) dead\n",
|
|
|
|
(void *)wsi, pollfd->fd);
|
2016-07-01 08:54:39 +08:00
|
|
|
cce = "Peer hung up";
|
2013-01-16 11:53:05 +08:00
|
|
|
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
|
|
|
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2017-10-13 10:33:02 +08:00
|
|
|
/* interpret the server response
|
|
|
|
*
|
2013-01-16 11:53:05 +08:00
|
|
|
* HTTP/1.1 101 Switching Protocols
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
|
|
|
|
* Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
|
|
|
|
* Sec-WebSocket-Protocol: chat
|
2017-10-13 10:33:02 +08:00
|
|
|
*
|
2013-01-16 11:53:05 +08:00
|
|
|
* 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;
|
2018-04-27 15:20:56 +08:00
|
|
|
while (wsi->http.ah->parser_state != WSI_PARSING_COMPLETE &&
|
2015-12-06 10:05:37 +08:00
|
|
|
len > 0) {
|
2018-03-07 18:15:17 +08:00
|
|
|
int plen = 1;
|
|
|
|
|
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:
|
2016-07-01 08:54:39 +08:00
|
|
|
cce = "read failed";
|
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
|
|
|
|
2018-03-07 18:15:17 +08:00
|
|
|
if (lws_parse(wsi, &c, &plen)) {
|
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
|
|
|
|
*/
|
2018-04-27 15:20:56 +08:00
|
|
|
if (wsi->http.ah->parser_state != WSI_PARSING_COMPLETE)
|
2013-01-16 11:53:05 +08:00
|
|
|
break;
|
|
|
|
|
2018-04-27 19:16:50 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
/*
|
|
|
|
* 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");
|
2017-03-16 10:46:31 +08:00
|
|
|
if (cce)
|
|
|
|
lwsl_info("reason: %s\n", cce);
|
|
|
|
wsi->protocol->callback(wsi,
|
2016-05-03 07:26:10 +08:00
|
|
|
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
|
2016-07-01 08:54:39 +08:00
|
|
|
wsi->user_space, (void *)cce, cce ? strlen(cce) : 0);
|
|
|
|
wsi->already_did_cce = 1;
|
2018-02-03 13:48:18 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "cbail3");
|
2013-09-18 21:01:02 +08:00
|
|
|
return -1;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2016-03-20 11:55:25 +08:00
|
|
|
int LWS_WARN_UNUSED_RESULT
|
2016-02-29 13:18:30 +08:00
|
|
|
lws_http_transaction_completed_client(struct lws *wsi)
|
|
|
|
{
|
2018-03-26 12:05:04 +08:00
|
|
|
struct lws *wsi_eff = lws_client_wsi_effective(wsi);
|
|
|
|
|
2018-08-15 12:49:32 +08:00
|
|
|
lwsl_info("%s: wsi: %p, wsi_eff: %p (%s)\n", __func__, wsi, wsi_eff,
|
|
|
|
wsi_eff->protocol->name);
|
2018-03-26 12:05:04 +08:00
|
|
|
|
2018-08-15 12:49:32 +08:00
|
|
|
if (user_callback_handle_rxflow(wsi_eff->protocol->callback, wsi_eff,
|
|
|
|
LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
|
|
|
|
wsi_eff->user_space, NULL, 0)) {
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsl_debug("%s: Completed call returned nonzero (role 0x%x)\n",
|
|
|
|
__func__, lwsi_role(wsi_eff));
|
2018-03-26 12:05:04 +08:00
|
|
|
return -1;
|
2016-02-29 13:18:30 +08:00
|
|
|
}
|
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
/*
|
|
|
|
* Are we constitutionally capable of having a queue, ie, we are on
|
|
|
|
* the "active client connections" list?
|
|
|
|
*
|
|
|
|
* If not, that's it for us.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (lws_dll_is_null(&wsi->dll_active_client_conns))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* if this was a queued guy, close him and remove from queue */
|
|
|
|
|
|
|
|
if (wsi->transaction_from_pipeline_queue) {
|
|
|
|
lwsl_debug("closing queued wsi %p\n", wsi_eff);
|
|
|
|
/* so the close doesn't trigger a CCE */
|
|
|
|
wsi_eff->already_did_cce = 1;
|
|
|
|
__lws_close_free_wsi(wsi_eff,
|
|
|
|
LWS_CLOSE_STATUS_CLIENT_TRANSACTION_DONE,
|
|
|
|
"queued client done");
|
|
|
|
}
|
|
|
|
|
2018-06-04 07:14:42 +08:00
|
|
|
_lws_header_table_reset(wsi->http.ah);
|
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
/* after the first one, they can only be coming from the queue */
|
|
|
|
wsi->transaction_from_pipeline_queue = 1;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->http.rx_content_length = 0;
|
|
|
|
wsi->hdr_parsing_completed = 0;
|
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
/* is there a new tail after removing that one? */
|
|
|
|
wsi_eff = lws_client_wsi_effective(wsi);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do we have something pipelined waiting?
|
|
|
|
* it's OK if he hasn't managed to send his headers yet... he's next
|
|
|
|
* in line to do that...
|
|
|
|
*/
|
|
|
|
if (wsi_eff == wsi) {
|
|
|
|
/*
|
|
|
|
* Nothing pipelined... we should hang around a bit
|
|
|
|
* in case something turns up...
|
|
|
|
*/
|
|
|
|
lwsl_info("%s: nothing pipelined waiting\n", __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsi_set_state(wsi, LRS_IDLING);
|
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_CONN_IDLE, 5);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-04-11 13:39:42 +08:00
|
|
|
* H1: we can serialize the queued guys into the same ah
|
2018-04-02 11:55:17 +08:00
|
|
|
* H2: everybody needs their own ah until their own STREAM_END
|
2018-03-26 12:05:04 +08:00
|
|
|
*/
|
|
|
|
|
2016-02-29 13:18:30 +08:00
|
|
|
/* otherwise set ourselves up ready to go again */
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_WAITING_SERVER_REPLY);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-04-27 15:20:56 +08:00
|
|
|
wsi->http.ah->parser_state = WSI_TOKEN_NAME_PART;
|
|
|
|
wsi->http.ah->lextable_pos = 0;
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
|
|
|
|
wsi->context->timeout_secs);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
|
|
|
/* If we're (re)starting on headers, need other implied init */
|
2018-04-27 15:20:56 +08:00
|
|
|
wsi->http.ah->ues = URIES_IDLE;
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
lwsl_info("%s: %p: new queued transaction as %p\n", __func__, wsi, wsi_eff);
|
|
|
|
lws_callback_on_writable(wsi);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-21 23:12:21 +08:00
|
|
|
LWS_VISIBLE LWS_EXTERN unsigned int
|
2018-07-20 08:39:00 +08:00
|
|
|
lws_http_client_http_response(struct lws *_wsi)
|
2016-10-21 23:12:21 +08:00
|
|
|
{
|
2018-07-20 08:39:00 +08:00
|
|
|
struct lws *wsi;
|
|
|
|
unsigned int resp;
|
|
|
|
|
|
|
|
if (_wsi->http.ah && _wsi->http.ah->http_response)
|
|
|
|
return _wsi->http.ah->http_response;
|
2016-10-21 23:12:21 +08:00
|
|
|
|
2018-07-20 08:39:00 +08:00
|
|
|
lws_vhost_lock(_wsi->vhost);
|
|
|
|
wsi = _lws_client_wsi_master(_wsi);
|
|
|
|
resp = wsi->http.ah->http_response;
|
|
|
|
lws_vhost_unlock(_wsi->vhost);
|
|
|
|
|
|
|
|
return resp;
|
2016-10-21 23:12:21 +08:00
|
|
|
}
|
2018-04-27 19:16:50 +08:00
|
|
|
#endif
|
2017-12-20 10:44:21 +08:00
|
|
|
#if defined(LWS_PLAT_OPTEE)
|
|
|
|
char *
|
|
|
|
strrchr(const char *s, int c)
|
|
|
|
{
|
|
|
|
char *hit = NULL;
|
|
|
|
|
|
|
|
while (*s)
|
|
|
|
if (*(s++) == (char)c)
|
|
|
|
hit = (char *)s - 1;
|
|
|
|
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define atoll atoi
|
|
|
|
#endif
|
2016-10-21 23:12:21 +08:00
|
|
|
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2013-01-16 11:53:05 +08:00
|
|
|
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
|
|
|
{
|
2018-04-11 13:39:42 +08:00
|
|
|
int n, port = 0, ssl = 0;
|
2015-12-06 08:40:00 +08:00
|
|
|
int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
|
2018-04-11 13:39:42 +08:00
|
|
|
const char *prot, *ads = NULL, *path, *cce = NULL;
|
2016-10-21 23:12:21 +08:00
|
|
|
struct allocated_headers *ah = NULL;
|
2018-03-26 12:05:04 +08:00
|
|
|
struct lws *w = lws_client_wsi_effective(wsi);
|
2017-02-22 09:25:24 +00:00
|
|
|
char *p, *q;
|
|
|
|
char new_path[300];
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2017-12-07 10:09:56 +08:00
|
|
|
lws_client_stash_destroy(wsi);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-27 15:20:56 +08:00
|
|
|
ah = wsi->http.ah;
|
2016-02-29 13:18:30 +08:00
|
|
|
if (!wsi->do_ws) {
|
|
|
|
/* we are being an http client...
|
|
|
|
*/
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_ROLE_H2)
|
2018-04-17 11:43:20 +08:00
|
|
|
if (wsi->client_h2_alpn || wsi->client_h2_substream) {
|
|
|
|
lwsl_debug("%s: %p: transitioning to h2 client\n", __func__, wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
lws_role_transition(wsi, LWSIFR_CLIENT,
|
|
|
|
LRS_ESTABLISHED, &role_ops_h2);
|
2018-04-17 11:43:20 +08:00
|
|
|
} else
|
2018-04-11 13:39:42 +08:00
|
|
|
#endif
|
2018-04-17 11:43:20 +08:00
|
|
|
{
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1)
|
|
|
|
{
|
2018-04-17 11:43:20 +08:00
|
|
|
lwsl_debug("%s: %p: transitioning to h1 client\n", __func__, wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
lws_role_transition(wsi, LWSIFR_CLIENT,
|
|
|
|
LRS_ESTABLISHED, &role_ops_h1);
|
2018-04-27 19:16:50 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2018-04-17 11:43:20 +08:00
|
|
|
}
|
2018-04-02 11:55:17 +08:00
|
|
|
|
2018-04-27 15:20:56 +08:00
|
|
|
wsi->http.ah = ah;
|
2016-10-21 23:12:21 +08:00
|
|
|
ah->http_response = 0;
|
2016-02-29 13:18:30 +08:00
|
|
|
}
|
|
|
|
|
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-02-29 13:18:30 +08:00
|
|
|
*
|
|
|
|
* http (non-ws) client will expect something like this
|
|
|
|
*
|
|
|
|
* HTTP/1.0.200
|
|
|
|
* server:.libwebsockets
|
|
|
|
* content-type:.text/html
|
|
|
|
* content-length:.17703
|
|
|
|
* set-cookie:.test=LWS_1456736240_336776_COOKIE;Max-Age=360000
|
2013-01-16 11:53:05 +08:00
|
|
|
*/
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-09-02 14:35:37 +08:00
|
|
|
wsi->http.conn_type = HTTP_CONNECTION_KEEP_ALIVE;
|
2018-03-27 09:17:19 +08:00
|
|
|
if (!wsi->client_h2_substream) {
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
|
|
|
|
if (wsi->do_ws && !p) {
|
|
|
|
lwsl_info("no URI\n");
|
|
|
|
cce = "HS: URI missing";
|
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
if (!p) {
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP1_0);
|
2018-09-02 14:35:37 +08:00
|
|
|
wsi->http.conn_type = HTTP_CONNECTION_CLOSE;
|
2018-03-27 09:17:19 +08:00
|
|
|
}
|
|
|
|
if (!p) {
|
|
|
|
cce = "HS: URI missing";
|
|
|
|
lwsl_info("no URI\n");
|
|
|
|
goto bail3;
|
|
|
|
}
|
2018-04-04 10:39:31 +08:00
|
|
|
} else {
|
2018-03-27 09:17:19 +08:00
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_STATUS);
|
2018-04-04 10:39:31 +08:00
|
|
|
if (!p) {
|
|
|
|
cce = "HS: :status missing";
|
|
|
|
lwsl_info("no status\n");
|
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 11:37:56 +08:00
|
|
|
n = atoi(p);
|
2016-10-21 23:12:21 +08:00
|
|
|
if (ah)
|
|
|
|
ah->http_response = n;
|
|
|
|
|
2016-01-14 11:37:56 +08:00
|
|
|
if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);
|
2016-07-01 08:54:39 +08:00
|
|
|
if (!p) {
|
|
|
|
cce = "HS: Redirect code but no Location";
|
2016-01-14 11:37:56 +08:00
|
|
|
goto bail3;
|
2016-07-01 08:54:39 +08:00
|
|
|
}
|
2016-01-14 11:37:56 +08:00
|
|
|
|
2017-02-22 09:25:24 +00:00
|
|
|
/* Relative reference absolute path */
|
2017-11-16 09:37:04 +08:00
|
|
|
if (p[0] == '/') {
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2018-05-01 12:41:42 +08:00
|
|
|
ssl = wsi->tls.use_ssl & LCCSCF_USE_SSL;
|
2017-02-22 09:25:24 +00:00
|
|
|
#endif
|
2017-11-16 09:37:04 +08:00
|
|
|
ads = lws_hdr_simple_ptr(wsi,
|
|
|
|
_WSI_TOKEN_CLIENT_PEER_ADDRESS);
|
2017-02-22 09:25:24 +00:00
|
|
|
port = wsi->c_port;
|
2017-11-16 09:37:04 +08:00
|
|
|
/* +1 as lws_client_reset expects leading / omitted */
|
|
|
|
path = p + 1;
|
2016-07-01 08:54:39 +08:00
|
|
|
}
|
2017-02-22 09:25:24 +00:00
|
|
|
/* Absolute (Full) URI */
|
2017-11-16 09:37:04 +08:00
|
|
|
else if (strchr(p, ':')) {
|
2017-02-22 09:25:24 +00:00
|
|
|
if (lws_parse_uri(p, &prot, &ads, &port, &path)) {
|
|
|
|
cce = "HS: URI did not parse";
|
|
|
|
goto bail3;
|
|
|
|
}
|
2016-01-14 11:37:56 +08:00
|
|
|
|
2017-02-22 09:25:24 +00:00
|
|
|
if (!strcmp(prot, "wss") || !strcmp(prot, "https"))
|
|
|
|
ssl = 1;
|
|
|
|
}
|
|
|
|
/* Relative reference relative path */
|
2017-11-16 09:37:04 +08:00
|
|
|
else {
|
|
|
|
/* This doesn't try to calculate an absolute path,
|
|
|
|
* that will be left to the server */
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2018-05-01 12:41:42 +08:00
|
|
|
ssl = wsi->tls.use_ssl & LCCSCF_USE_SSL;
|
2017-02-22 09:25:24 +00:00
|
|
|
#endif
|
2017-11-16 09:37:04 +08:00
|
|
|
ads = lws_hdr_simple_ptr(wsi,
|
|
|
|
_WSI_TOKEN_CLIENT_PEER_ADDRESS);
|
2017-02-22 09:25:24 +00:00
|
|
|
port = wsi->c_port;
|
2017-11-16 09:37:04 +08:00
|
|
|
/* +1 as lws_client_reset expects leading / omitted */
|
|
|
|
path = new_path + 1;
|
2018-03-12 09:28:26 +08:00
|
|
|
lws_strncpy(new_path, lws_hdr_simple_ptr(wsi,
|
|
|
|
_WSI_TOKEN_CLIENT_URI), sizeof(new_path));
|
2017-02-22 09:25:24 +00:00
|
|
|
q = strrchr(new_path, '/');
|
2018-03-12 09:28:26 +08:00
|
|
|
if (q)
|
|
|
|
lws_strncpy(q + 1, p, sizeof(new_path) -
|
2018-06-18 18:37:12 +08:00
|
|
|
(q - new_path) - 1);
|
2018-03-12 09:28:26 +08:00
|
|
|
else
|
2017-02-22 09:25:24 +00:00
|
|
|
path = p;
|
|
|
|
}
|
2016-01-13 18:48:50 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_WITH_TLS)
|
2018-05-01 12:41:42 +08:00
|
|
|
if ((wsi->tls.use_ssl & LCCSCF_USE_SSL) && !ssl) {
|
2017-02-22 11:25:47 +00:00
|
|
|
cce = "HS: Redirect attempted SSL downgrade";
|
|
|
|
goto bail3;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-21 22:59:00 +08:00
|
|
|
if (!lws_client_reset(&wsi, ssl, ads, port, path, ads)) {
|
|
|
|
/* there are two ways to fail out with NULL return...
|
|
|
|
* simple, early problem where the wsi is intact, or
|
|
|
|
* we went through with the reconnect attempt and the
|
|
|
|
* wsi is already closed. In the latter case, the wsi
|
|
|
|
* has beet set to NULL additionally.
|
|
|
|
*/
|
2016-01-14 11:37:56 +08:00
|
|
|
lwsl_err("Redirect failed\n");
|
2016-07-01 08:54:39 +08:00
|
|
|
cce = "HS: Redirect failed";
|
2017-02-21 22:59:00 +08:00
|
|
|
if (wsi)
|
|
|
|
goto bail3;
|
|
|
|
|
|
|
|
return 1;
|
2016-01-14 11:37:56 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-29 13:18:30 +08:00
|
|
|
|
|
|
|
if (!wsi->do_ws) {
|
|
|
|
|
2018-03-27 09:17:19 +08:00
|
|
|
/* if h1 KA is allowed, enable the queued pipeline guys */
|
2018-03-26 12:05:04 +08:00
|
|
|
|
2018-03-27 09:17:19 +08:00
|
|
|
if (!wsi->client_h2_alpn && !wsi->client_h2_substream && w == wsi) { /* ie, coming to this for the first time */
|
2018-09-02 14:35:37 +08:00
|
|
|
if (wsi->http.conn_type == HTTP_CONNECTION_KEEP_ALIVE)
|
2018-03-26 12:05:04 +08:00
|
|
|
wsi->keepalive_active = 1;
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Ugh... now the main http connection has seen
|
|
|
|
* both sides, we learn the server doesn't
|
|
|
|
* support keepalive.
|
|
|
|
*
|
|
|
|
* That means any guys queued on us are going
|
|
|
|
* to have to be restarted from connect2 with
|
|
|
|
* their own connections.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stick around telling any new guys they can't
|
|
|
|
* pipeline to this server
|
|
|
|
*/
|
|
|
|
wsi->keepalive_rejected = 1;
|
|
|
|
|
|
|
|
lws_vhost_lock(wsi->vhost);
|
|
|
|
lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
|
|
|
|
wsi->dll_client_transaction_queue_head.next) {
|
|
|
|
struct lws *ww = lws_container_of(d, struct lws,
|
|
|
|
dll_client_transaction_queue);
|
|
|
|
|
|
|
|
/* remove him from our queue */
|
|
|
|
lws_dll_lws_remove(&ww->dll_client_transaction_queue);
|
|
|
|
/* give up on pipelining */
|
|
|
|
ww->client_pipeline = 0;
|
|
|
|
|
|
|
|
/* go back to "trying to connect" state */
|
2018-04-11 13:39:42 +08:00
|
|
|
lws_role_transition(ww, LWSIFR_CLIENT,
|
|
|
|
LRS_UNCONNECTED,
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1)
|
2018-04-11 13:39:42 +08:00
|
|
|
&role_ops_h1);
|
2018-04-27 19:16:50 +08:00
|
|
|
#else
|
|
|
|
#if defined (LWS_ROLE_H2)
|
|
|
|
&role_ops_h2);
|
|
|
|
#else
|
|
|
|
&role_ops_raw);
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-03-26 12:05:04 +08:00
|
|
|
ww->user_space = NULL;
|
|
|
|
} lws_end_foreach_dll_safe(d, d1);
|
|
|
|
lws_vhost_unlock(wsi->vhost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 11:55:25 +08:00
|
|
|
#ifdef LWS_WITH_HTTP_PROXY
|
2018-04-27 14:36:10 +08:00
|
|
|
wsi->http.perform_rewrite = 0;
|
2016-04-18 17:26:14 +08:00
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
|
2017-11-16 09:37:04 +08:00
|
|
|
if (!strncmp(lws_hdr_simple_ptr(wsi,
|
|
|
|
WSI_TOKEN_HTTP_CONTENT_TYPE),
|
|
|
|
"text/html", 9))
|
2018-09-04 08:06:46 +08:00
|
|
|
wsi->http.perform_rewrite = 0;
|
2016-04-18 17:26:14 +08:00
|
|
|
}
|
2016-03-20 11:59:53 +08:00
|
|
|
#endif
|
|
|
|
|
2016-02-29 13:18:30 +08:00
|
|
|
/* allocate the per-connection user memory (if any) */
|
|
|
|
if (lws_ensure_user_space(wsi)) {
|
|
|
|
lwsl_err("Problem allocating wsi user mem\n");
|
2016-07-01 08:54:39 +08:00
|
|
|
cce = "HS: OOM";
|
2016-02-29 13:18:30 +08:00
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
2016-03-13 16:44:19 +08:00
|
|
|
/* he may choose to send us stuff in chunked transfer-coding */
|
|
|
|
wsi->chunked = 0;
|
|
|
|
wsi->chunk_remaining = 0; /* ie, next thing is chunk size */
|
2017-11-16 09:37:04 +08:00
|
|
|
if (lws_hdr_total_length(wsi,
|
|
|
|
WSI_TOKEN_HTTP_TRANSFER_ENCODING)) {
|
2016-03-13 16:44:19 +08:00
|
|
|
wsi->chunked = !strcmp(lws_hdr_simple_ptr(wsi,
|
|
|
|
WSI_TOKEN_HTTP_TRANSFER_ENCODING),
|
2017-11-16 09:37:04 +08:00
|
|
|
"chunked");
|
2016-03-13 16:44:19 +08:00
|
|
|
/* first thing is hex, after payload there is crlf */
|
|
|
|
wsi->chunk_parser = ELCP_HEX;
|
|
|
|
}
|
|
|
|
|
2016-02-29 13:18:30 +08:00
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.rx_content_length =
|
2017-06-09 20:20:42 +08:00
|
|
|
atoll(lws_hdr_simple_ptr(wsi,
|
2016-02-29 13:18:30 +08:00
|
|
|
WSI_TOKEN_HTTP_CONTENT_LENGTH));
|
2018-03-26 12:05:04 +08:00
|
|
|
lwsl_info("%s: incoming content length %llu\n",
|
2017-11-16 09:37:04 +08:00
|
|
|
__func__, (unsigned long long)
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.rx_content_length);
|
|
|
|
wsi->http.rx_content_remain =
|
|
|
|
wsi->http.rx_content_length;
|
2016-03-13 16:44:19 +08:00
|
|
|
} else /* can't do 1.1 without a content length or chunked */
|
|
|
|
if (!wsi->chunked)
|
2018-09-02 14:35:37 +08:00
|
|
|
wsi->http.conn_type =
|
2017-11-16 09:37:04 +08:00
|
|
|
HTTP_CONNECTION_CLOSE;
|
2016-02-29 13:18:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we seem to be good to go, give client last chance to check
|
|
|
|
* headers and OK it
|
|
|
|
*/
|
2018-07-20 08:39:00 +08:00
|
|
|
if (w->protocol->callback(w,
|
2017-11-16 09:37:04 +08:00
|
|
|
LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
|
2018-07-20 08:39:00 +08:00
|
|
|
w->user_space, NULL, 0)) {
|
2016-07-01 08:54:39 +08:00
|
|
|
|
|
|
|
cce = "HS: disallowed by client filter";
|
2016-02-29 13:18:30 +08:00
|
|
|
goto bail2;
|
2016-07-01 08:54:39 +08:00
|
|
|
}
|
2016-02-29 13:18:30 +08:00
|
|
|
|
|
|
|
/* clear his proxy connection timeout */
|
|
|
|
lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
|
|
|
|
|
|
|
wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
|
|
|
|
|
|
|
|
/* call him back to inform him he is up */
|
2018-07-20 08:39:00 +08:00
|
|
|
if (w->protocol->callback(w,
|
2016-04-18 17:26:14 +08:00
|
|
|
LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP,
|
2018-07-20 08:39:00 +08:00
|
|
|
w->user_space, NULL, 0)) {
|
2016-07-01 08:54:39 +08:00
|
|
|
cce = "HS: disallowed at ESTABLISHED";
|
2016-02-29 13:18:30 +08:00
|
|
|
goto bail3;
|
2016-07-01 08:54:39 +08:00
|
|
|
}
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
/*
|
|
|
|
* for pipelining, master needs to keep his ah... guys who
|
|
|
|
* queued on him can drop it now though.
|
|
|
|
*/
|
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
if (w != wsi)
|
2018-03-26 12:05:04 +08:00
|
|
|
/* free up parsing allocations for queued guy */
|
|
|
|
lws_header_table_detach(w, 0);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-03-26 12:05:04 +08:00
|
|
|
lwsl_info("%s: client connection up\n", __func__);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-07-21 10:26:47 +08:00
|
|
|
/*
|
|
|
|
* Did we get a response from the server with an explicit
|
|
|
|
* content-length of zero? If so, this transaction is already
|
|
|
|
* completed at the end of the header processing...
|
|
|
|
*/
|
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH) &&
|
|
|
|
!wsi->http.rx_content_length)
|
|
|
|
return !!lws_http_transaction_completed_client(wsi);
|
|
|
|
|
2016-02-29 13:18:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#if defined(LWS_ROLE_WS)
|
|
|
|
switch (lws_client_ws_upgrade(wsi, &cce)) {
|
|
|
|
case 2:
|
2013-03-23 09:53:17 +08:00
|
|
|
goto bail2;
|
2018-04-11 13:39:42 +08:00
|
|
|
case 3:
|
2015-12-18 00:50:14 +08:00
|
|
|
goto bail3;
|
2016-07-01 08:54:39 +08:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
return 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
bail3:
|
2013-03-23 09:53:17 +08:00
|
|
|
close_reason = LWS_CLOSE_STATUS_NOSTATUS;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
bail2:
|
2018-03-19 08:05:37 +08:00
|
|
|
if (wsi->protocol) {
|
|
|
|
n = 0;
|
|
|
|
if (cce)
|
2018-04-16 19:52:28 +08:00
|
|
|
n = (int)strlen(cce);
|
2018-07-20 08:39:00 +08:00
|
|
|
w->protocol->callback(w,
|
2017-11-16 09:37:04 +08:00
|
|
|
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
|
2018-07-20 08:39:00 +08:00
|
|
|
w->user_space, (void *)cce,
|
2018-03-19 08:05:37 +08:00
|
|
|
(unsigned int)n);
|
|
|
|
}
|
2016-07-01 08:54:39 +08:00
|
|
|
wsi->already_did_cce = 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
|
|
|
|
2018-09-04 08:06:46 +08:00
|
|
|
lwsl_info("closing connection (prot %s) "
|
|
|
|
"due to bail2 connection error: %s\n", wsi->protocol ?
|
|
|
|
wsi->protocol->name : "unknown", cce);
|
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 */
|
2018-02-03 13:48:18 +08:00
|
|
|
lws_close_free_wsi(wsi, close_reason, "c hs interp");
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2018-04-27 19:16:50 +08:00
|
|
|
#endif
|
2013-01-16 11:53:05 +08:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-04-11 13:39:42 +08:00
|
|
|
char *p = pkt;
|
2016-02-29 13:18:30 +08:00
|
|
|
const char *meth;
|
2017-03-16 10:46:31 +08:00
|
|
|
const char *pp = lws_hdr_simple_ptr(wsi,
|
|
|
|
_WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2016-02-29 13:18:30 +08:00
|
|
|
meth = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD);
|
|
|
|
if (!meth) {
|
|
|
|
meth = "GET";
|
|
|
|
wsi->do_ws = 1;
|
2017-03-16 10:46:31 +08:00
|
|
|
} else {
|
2016-02-29 13:18:30 +08:00
|
|
|
wsi->do_ws = 0;
|
2017-03-16 10:46:31 +08:00
|
|
|
}
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2017-03-07 16:06:05 +08:00
|
|
|
if (!strcmp(meth, "RAW")) {
|
|
|
|
lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
|
|
|
lwsl_notice("client transition to raw\n");
|
2017-03-16 10:46:31 +08:00
|
|
|
|
2017-03-07 16:06:05 +08:00
|
|
|
if (pp) {
|
|
|
|
const struct lws_protocols *pr;
|
|
|
|
|
|
|
|
pr = lws_vhost_name_to_protocol(wsi->vhost, pp);
|
|
|
|
|
|
|
|
if (!pr) {
|
|
|
|
lwsl_err("protocol %s not enabled on vhost\n",
|
|
|
|
pp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-02 14:35:37 +08:00
|
|
|
lws_bind_protocol(wsi, pr, __func__);
|
2017-03-07 16:06:05 +08:00
|
|
|
}
|
2017-03-16 10:46:31 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if ((wsi->protocol->callback)(wsi, LWS_CALLBACK_RAW_ADOPT,
|
|
|
|
wsi->user_space, NULL, 0))
|
2017-03-07 16:06:05 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
lws_role_transition(wsi, 0, LRS_ESTABLISHED, &role_ops_raw_skt);
|
2017-03-07 16:06:05 +08:00
|
|
|
lws_header_table_detach(wsi, 1);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:53:05 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-02-29 13:18:30 +08:00
|
|
|
p += sprintf(p, "%s %s HTTP/1.1\x0d\x0a", meth,
|
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
|
|
|
|
2018-09-04 08:06:46 +08:00
|
|
|
if (!wsi->client_pipeline)
|
|
|
|
p += sprintf(p, "connection: close\x0d\x0a");
|
|
|
|
|
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));
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2017-01-23 19:52:27 +08:00
|
|
|
if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lws_check_opt(wsi->context->options,
|
2017-11-16 09:37:04 +08:00
|
|
|
LWS_SERVER_OPTION_JUST_USE_RAW_ORIGIN))
|
2017-01-23 19:52:27 +08:00
|
|
|
p += sprintf(p, "Origin: %s\x0d\x0a",
|
2017-11-16 09:37:04 +08:00
|
|
|
lws_hdr_simple_ptr(wsi,
|
|
|
|
_WSI_TOKEN_CLIENT_ORIGIN));
|
2017-01-23 19:52:27 +08:00
|
|
|
else
|
|
|
|
p += sprintf(p, "Origin: http://%s\x0d\x0a",
|
2017-11-16 09:37:04 +08:00
|
|
|
lws_hdr_simple_ptr(wsi,
|
|
|
|
_WSI_TOKEN_CLIENT_ORIGIN));
|
2017-01-23 19:52:27 +08:00
|
|
|
}
|
2018-04-25 08:42:18 +08:00
|
|
|
#if defined(LWS_ROLE_WS)
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->do_ws)
|
|
|
|
p = lws_generate_client_ws_handshake(wsi, p);
|
2018-04-25 08:42:18 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/* give userland a chance to append, eg, cookies */
|
|
|
|
|
|
|
|
if (wsi->protocol->callback(wsi,
|
|
|
|
LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
|
|
|
|
wsi->user_space, &p,
|
|
|
|
(pkt + wsi->context->pt_serv_buf_size) - p - 12))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
p += sprintf(p, "\x0d\x0a");
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-04-27 19:16:50 +08:00
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_http_client_read(struct lws *wsi, char **buf, int *len)
|
|
|
|
{
|
|
|
|
int rlen, n;
|
|
|
|
|
|
|
|
rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
|
|
|
|
*len = 0;
|
|
|
|
|
|
|
|
// lwsl_notice("%s: rlen %d\n", __func__, rlen);
|
|
|
|
|
|
|
|
/* allow the source to signal he has data again next time */
|
|
|
|
lws_change_pollfd(wsi, 0, LWS_POLLIN);
|
|
|
|
|
|
|
|
if (rlen == LWS_SSL_CAPABLE_ERROR) {
|
|
|
|
lwsl_notice("%s: SSL capable error\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-10 08:53:41 +08:00
|
|
|
if (rlen <= 0)
|
2018-04-11 13:39:42 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
*len = rlen;
|
|
|
|
wsi->client_rx_avail = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* server may insist on transfer-encoding: chunked,
|
|
|
|
* so http client must deal with it
|
|
|
|
*/
|
|
|
|
spin_chunks:
|
|
|
|
while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
|
|
|
|
switch (wsi->chunk_parser) {
|
|
|
|
case ELCP_HEX:
|
|
|
|
if ((*buf)[0] == '\x0d') {
|
|
|
|
wsi->chunk_parser = ELCP_CR;
|
|
|
|
break;
|
2016-02-29 13:18:30 +08:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
n = char_to_hex((*buf)[0]);
|
|
|
|
if (n < 0) {
|
2018-09-10 08:53:41 +08:00
|
|
|
lwsl_info("%s: chunking failure\n", __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wsi->chunk_remaining <<= 4;
|
|
|
|
wsi->chunk_remaining |= n;
|
|
|
|
break;
|
|
|
|
case ELCP_CR:
|
|
|
|
if ((*buf)[0] != '\x0a') {
|
2018-09-10 08:53:41 +08:00
|
|
|
lwsl_info("%s: chunking failure\n", __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wsi->chunk_parser = ELCP_CONTENT;
|
|
|
|
lwsl_info("chunk %d\n", wsi->chunk_remaining);
|
|
|
|
if (wsi->chunk_remaining)
|
|
|
|
break;
|
|
|
|
lwsl_info("final chunk\n");
|
|
|
|
goto completed;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
case ELCP_CONTENT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ELCP_POST_CR:
|
|
|
|
if ((*buf)[0] != '\x0d') {
|
2018-09-10 08:53:41 +08:00
|
|
|
lwsl_info("%s: chunking failure\n", __func__);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return -1;
|
2016-02-29 13:18:30 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->chunk_parser = ELCP_POST_LF;
|
|
|
|
break;
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
case ELCP_POST_LF:
|
2018-09-10 08:53:41 +08:00
|
|
|
if ((*buf)[0] != '\x0a') {
|
|
|
|
lwsl_info("%s: chunking failure\n", __func__);
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return -1;
|
2018-09-10 08:53:41 +08:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->chunk_parser = ELCP_HEX;
|
|
|
|
wsi->chunk_remaining = 0;
|
|
|
|
break;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
(*buf)++;
|
|
|
|
(*len)--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->chunked && !wsi->chunk_remaining)
|
|
|
|
return 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->http.rx_content_remain &&
|
|
|
|
wsi->http.rx_content_remain < (unsigned int)*len)
|
|
|
|
n = (int)wsi->http.rx_content_remain;
|
|
|
|
else
|
|
|
|
n = *len;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->chunked && wsi->chunk_remaining &&
|
|
|
|
wsi->chunk_remaining < n)
|
|
|
|
n = wsi->chunk_remaining;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-09-04 08:06:46 +08:00
|
|
|
#if defined(LWS_WITH_HTTP_PROXY) && defined(LWS_WITH_HUBBUB)
|
2018-04-11 13:39:42 +08:00
|
|
|
/* hubbub */
|
2018-04-27 14:36:10 +08:00
|
|
|
if (wsi->http.perform_rewrite)
|
|
|
|
lws_rewrite_parse(wsi->http.rw, (unsigned char *)*buf, n);
|
2018-04-11 13:39:42 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct lws *wsi_eff = lws_client_wsi_effective(wsi);
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (user_callback_handle_rxflow(wsi_eff->protocol->callback,
|
|
|
|
wsi_eff, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
|
|
|
|
wsi_eff->user_space, *buf, n)) {
|
2018-09-10 08:53:41 +08:00
|
|
|
lwsl_info("%s: RECEIVE_CLIENT_HTTP_READ returned -1\n",
|
2018-04-11 13:39:42 +08:00
|
|
|
__func__);
|
2016-02-29 13:18:30 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2016-02-29 13:18:30 +08:00
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->chunked && wsi->chunk_remaining) {
|
|
|
|
(*buf) += n;
|
|
|
|
wsi->chunk_remaining -= n;
|
|
|
|
*len -= n;
|
|
|
|
}
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->chunked && !wsi->chunk_remaining)
|
|
|
|
wsi->chunk_parser = ELCP_POST_CR;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->chunked && *len)
|
|
|
|
goto spin_chunks;
|
2013-01-16 11:53:05 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->chunked)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* if we know the content length, decrement the content remaining */
|
|
|
|
if (wsi->http.rx_content_length > 0)
|
|
|
|
wsi->http.rx_content_remain -= n;
|
|
|
|
|
|
|
|
// lwsl_notice("rx_content_remain %lld, rx_content_length %lld\n",
|
|
|
|
// wsi->http.rx_content_remain, wsi->http.rx_content_length);
|
|
|
|
|
|
|
|
if (wsi->http.rx_content_remain || !wsi->http.rx_content_length)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
completed:
|
|
|
|
|
|
|
|
if (lws_http_transaction_completed_client(wsi)) {
|
|
|
|
lwsl_notice("%s: transaction completed says -1\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-01-16 11:53:05 +08:00
|
|
|
}
|
2018-04-27 19:16:50 +08:00
|
|
|
|
2018-06-04 07:14:42 +08:00
|
|
|
#endif
|