#include "private-libwebsockets.h"
-unsigned char lextable[] = {
+const unsigned char lextable[] = {
#include "lextable.h"
};
}
void
+_lws_header_table_reset(struct allocated_headers *ah)
+{
+ /* init the ah to reflect no headers or data have appeared yet */
+ memset(ah->frag_index, 0, sizeof(ah->frag_index));
+ ah->nfrag = 0;
+ ah->pos = 0;
+ ah->http_response = 0;
+}
+
+// doesn't scrub the ah rxbuffer by default, parent must do if needed
+
+void
lws_header_table_reset(struct lws *wsi, int autoservice)
{
struct allocated_headers *ah = wsi->u.hdr.ah;
/* ah also concurs with ownership */
assert(ah->wsi == wsi);
- /* init the ah to reflect no headers or data have appeared yet */
- memset(ah->frag_index, 0, sizeof(ah->frag_index));
- ah->nfrag = 0;
- ah->pos = 0;
-
- /* and reset the rx state */
- ah->rxpos = 0;
- ah->rxlen = 0;
+ _lws_header_table_reset(ah);
/* since we will restart the ah, our new headers are not completed */
// wsi->hdr_parsing_completed = 0;
struct lws **pwsi;
int n;
- lwsl_info("%s: wsi %p: ah %p (tsi %d)\n", __func__, (void *)wsi,
- (void *)wsi->u.hdr.ah, wsi->tsi);
+ lwsl_info("%s: wsi %p: ah %p (tsi %d, count = %d) in\n", __func__, (void *)wsi,
+ (void *)wsi->u.hdr.ah, wsi->tsi, pt->ah_count_in_use);
/* if we are already bound to one, just clear it down */
if (wsi->u.hdr.ah) {
/* if already waiting on list, if no new ah just ret */
if (pt->ah_count_in_use ==
context->max_http_header_pool) {
- lwsl_err("ah wl denied\n");
+ lwsl_notice("%s: no free ah to attach\n", __func__);
goto bail;
}
/* new ah.... remove ourselves from waiting list */
_lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
- lwsl_info("%s: wsi %p: ah %p: count %d (on exit)\n", __func__,
+ lwsl_info("%s: did attach wsi %p: ah %p: count %d (on exit)\n", __func__,
(void *)wsi, (void *)wsi->u.hdr.ah, pt->ah_count_in_use);
lws_pt_unlock(pt);
reset:
+
+ /* and reset the rx state */
+ wsi->u.hdr.ah->rxpos = 0;
+ wsi->u.hdr.ah->rxlen = 0;
+
lws_header_table_reset(wsi, autoservice);
time(&wsi->u.hdr.ah->assigned);
time_t now;
lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__,
- (void *)wsi, (void *)wsi->u.hdr.ah, wsi->tsi,
+ (void *)wsi, (void *)ah, wsi->tsi,
pt->ah_count_in_use);
if (wsi->u.hdr.preamble_rx)
/* may not be detached while he still has unprocessed rx */
if (ah && ah->rxpos != ah->rxlen) {
- lwsl_err("%s: %p: rxpos:%d, rxlen:%d\n", __func__, wsi,
+ lwsl_err("%s: %p: CANNOT DETACH rxpos:%d, rxlen:%d\n", __func__, wsi,
ah->rxpos, ah->rxlen);
assert(ah->rxpos == ah->rxlen);
+
+ return 0;
}
lws_pt_lock(pt);
}
/* we did have an ah attached */
time(&now);
- if (now - wsi->u.hdr.ah->assigned > 3) {
+ if (ah->assigned && now - ah->assigned > 3) {
/*
* we're detaching the ah, but it was held an
* unreasonably long time
lwsl_notice("%s: wsi %p: ah held %ds, "
"ah.rxpos %d, ah.rxlen %d, mode/state %d %d,"
"wsi->more_rx_waiting %d\n", __func__, wsi,
- (int)(now - wsi->u.hdr.ah->assigned),
+ (int)(now - ah->assigned),
ah->rxpos, ah->rxlen, wsi->mode, wsi->state,
wsi->more_rx_waiting);
}
+ ah->assigned = 0;
+
/* if we think we're detaching one, there should be one in use */
assert(pt->ah_count_in_use > 0);
/* and this specific one should have been in use */
- assert(wsi->u.hdr.ah->in_use);
+ assert(ah->in_use);
wsi->u.hdr.ah = NULL;
ah->wsi = NULL; /* no owner */
wsi->u.hdr.ah = ah;
ah->wsi = wsi; /* new owner */
+ /* and reset the rx state */
+ ah->rxpos = 0;
+ ah->rxlen = 0;
lws_header_table_reset(wsi, autoservice);
time(&wsi->u.hdr.ah->assigned);
- assert(wsi->position_in_fds_table != -1);
-
- lwsl_info("%s: Enabling %p POLLIN\n", __func__, wsi);
+ /* clients acquire the ah and then insert themselves in fds table... */
+ if (wsi->position_in_fds_table != -1) {
+ lwsl_info("%s: Enabling %p POLLIN\n", __func__, wsi);
- /* he has been stuck waiting for an ah, but now his wait is over,
- * let him progress
- */
- _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
+ /* he has been stuck waiting for an ah, but now his wait is over,
+ * let him progress
+ */
+ _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
+ }
/* point prev guy to next guy in list instead */
*pwsi = wsi->u.hdr.ah_wait_list;
pt->ah_wait_list_length--;
#ifndef LWS_NO_CLIENT
- if (wsi->state == LWSS_CLIENT_UNCONNECTED)
+ if (wsi->state == LWSS_CLIENT_UNCONNECTED) {
+ lws_pt_unlock(pt);
+
if (!lws_client_connect_via_info2(wsi)) {
/* our client connect has failed, the wsi
* has been closed
*/
- lws_pt_unlock(pt);
return -1;
}
+ return 0;
+ }
#endif
assert(!!pt->ah_wait_list_length == !!(int)(long)pt->ah_wait_list);
bail:
+ lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__,
+ (void *)wsi, (void *)ah, wsi->tsi,
+ pt->ah_count_in_use);
lws_pt_unlock(pt);
return 0;
WSI_TOKEN_PUT_URI,
WSI_TOKEN_PATCH_URI,
WSI_TOKEN_DELETE_URI,
+ WSI_TOKEN_CONNECT,
};
struct allocated_headers *ah = wsi->u.hdr.ah;
struct lws_context *context = wsi->context;
}
/*
* hm it's an unknown http method from a client in fact,
- * treat as dangerous
+ * it cannot be valid http
*/
if (m == ARRAY_SIZE(methods)) {
+ /*
+ * are we set up to accept raw in these cases?
+ */
+ if (lws_check_opt(wsi->vhost->options,
+ LWS_SERVER_OPTION_FALLBACK_TO_RAW))
+ return 2; /* transition to raw */
+
lwsl_info("Unknown method - dropping\n");
goto forbid;
}
return wsi->u.ws.frame_is_binary;
}
+void
+lws_add_wsi_to_draining_ext_list(struct lws *wsi)
+{
+ struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
+
+ if (wsi->u.ws.rx_draining_ext)
+ return;
+
+ lwsl_ext("%s: RX EXT DRAINING: Adding to list\n", __func__);
+
+ wsi->u.ws.rx_draining_ext = 1;
+ wsi->u.ws.rx_draining_ext_list = pt->rx_draining_ext_list;
+ pt->rx_draining_ext_list = wsi;
+}
+
+void
+lws_remove_wsi_from_draining_ext_list(struct lws *wsi)
+{
+ struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
+ struct lws **w = &pt->rx_draining_ext_list;
+
+ if (!wsi->u.ws.rx_draining_ext)
+ return;
+
+ lwsl_ext("%s: RX EXT DRAINING: Removing from list\n", __func__);
+
+ wsi->u.ws.rx_draining_ext = 0;
+
+ /* remove us from context draining ext list */
+ while (*w) {
+ if (*w == wsi) {
+ /* if us, point it instead to who we were pointing to */
+ *w = wsi->u.ws.rx_draining_ext_list;
+ break;
+ }
+ w = &((*w)->u.ws.rx_draining_ext_list);
+ }
+ wsi->u.ws.rx_draining_ext_list = NULL;
+}
+
+/*
+ * client-parser.c: lws_client_rx_sm() needs to be roughly kept in
+ * sync with changes here, esp related to ext draining
+ */
+
int
lws_rx_sm(struct lws *wsi, unsigned char c)
{
- struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
int callback_action = LWS_CALLBACK_RECEIVE;
int ret = 0, n, rx_draining_ext = 0;
struct lws_tokens eff_buf;
+ eff_buf.token = NULL;
+ eff_buf.token_len = 0;
if (wsi->socket_is_permanently_unusable)
return -1;
switch (wsi->lws_rx_parse_state) {
case LWS_RXPS_NEW:
if (wsi->u.ws.rx_draining_ext) {
- struct lws **w = &pt->rx_draining_ext_list;
-
eff_buf.token = NULL;
eff_buf.token_len = 0;
- wsi->u.ws.rx_draining_ext = 0;
- /* remove us from context draining ext list */
- while (*w) {
- if (*w == wsi) {
- *w = wsi->u.ws.rx_draining_ext_list;
- break;
- }
- w = &((*w)->u.ws.rx_draining_ext_list);
- }
- wsi->u.ws.rx_draining_ext_list = NULL;
+ lws_remove_wsi_from_draining_ext_list(wsi);
rx_draining_ext = 1;
- lwsl_err("%s: doing draining flow\n", __func__);
+ lwsl_debug("%s: doing draining flow\n", __func__);
goto drain_extension;
}
case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
assert(wsi->u.ws.rx_ubuf);
+ if (wsi->u.ws.rx_draining_ext)
+ goto drain_extension;
+
if (wsi->u.ws.rx_ubuf_head + LWS_PRE >=
wsi->u.ws.rx_ubuf_alloc) {
lwsl_err("Attempted overflow \n");
/* if he sends us 2 CLOSE, kill him */
return -1;
+ if (lws_partial_buffered(wsi)) {
+ /*
+ * if we're in the middle of something,
+ * we can't do a normal close response and
+ * have to just close our end.
+ */
+ wsi->socket_is_permanently_unusable = 1;
+ lwsl_parser("Closing on peer close due to Pending tx\n");
+ return -1;
+ }
+
if (user_callback_handle_rxflow(
wsi->protocol->callback, wsi,
LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE],
wsi->u.ws.rx_ubuf_head);
+ if (wsi->pending_timeout == PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG) {
+ lwsl_info("received expected PONG on wsi %p\n", wsi);
+ lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
+ }
+
/* issue it */
callback_action = LWS_CALLBACK_RECEIVE_PONG;
break;
goto already_done;
n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0);
+ /* eff_buf may be pointing somewhere completely different now,
+ * it's the output
+ */
if (n < 0) {
/*
* we may rely on this to get RX, just drop connection
if (n && eff_buf.token_len) {
/* extension had more... main loop will come back */
- wsi->u.ws.rx_draining_ext = 1;
- wsi->u.ws.rx_draining_ext_list = pt->rx_draining_ext_list;
- pt->rx_draining_ext_list = wsi;
- }
+ lws_add_wsi_to_draining_ext_list(wsi);
+ } else
+ lws_remove_wsi_from_draining_ext_list(wsi);
if (eff_buf.token_len > 0 ||
callback_action == LWS_CALLBACK_RECEIVE_PONG) {
* to expect in that state and can deal with it in bulk more efficiently.
*/
-void
+int
lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf,
size_t *len)
{
/* we want to leave 1 byte for the parser to handle properly */
if (avail <= 1)
- return;
+ return 0;
avail--;
rx_ubuf = wsi->u.ws.rx_ubuf + LWS_PRE + wsi->u.ws.rx_ubuf_head;
wsi->u.ws.rx_ubuf_head += avail;
wsi->u.ws.rx_packet_length -= avail;
*len -= avail;
+
+ return avail;
}