lws_intptr_t
[platform/upstream/libwebsockets.git] / lib / parsers.c
index 4feac10..3f47be9 100644 (file)
@@ -61,6 +61,18 @@ lextable_decode(int pos, char c)
 }
 
 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;
@@ -72,17 +84,13 @@ lws_header_table_reset(struct lws *wsi, int autoservice)
        /* 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;
+       _lws_header_table_reset(ah);
 
-       /* and reset the rx state */
-       ah->rxpos = 0;
-       ah->rxlen = 0;
+        wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
+        wsi->u.hdr.lextable_pos = 0;
 
        /* since we will restart the ah, our new headers are not completed */
-       // wsi->hdr_parsing_completed = 0;
+       wsi->hdr_parsing_completed = 0;
 
        /*
         * if we inherited pending rx (from socket adoption deferred
@@ -151,7 +159,7 @@ lws_header_table_attach(struct lws *wsi, int autoservice)
         * weren't able to deliver it right now
         */
        if (pt->ah_count_in_use == context->max_http_header_pool) {
-               lwsl_notice("%s: adding %p to ah waiting list\n", __func__, wsi);
+               lwsl_info("%s: adding %p to ah waiting list\n", __func__, wsi);
                wsi->u.hdr.ah_wait_list = pt->ah_wait_list;
                pt->ah_wait_list = wsi;
                pt->ah_wait_list_length++;
@@ -176,12 +184,17 @@ lws_header_table_attach(struct lws *wsi, int autoservice)
 
        _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);
 
@@ -202,6 +215,24 @@ bail:
        return 1;
 }
 
+void
+lws_header_table_force_to_detachable_state(struct lws *wsi)
+{
+       if (wsi->u.hdr.ah) {
+               wsi->u.hdr.ah->rxpos = -1;
+               wsi->u.hdr.ah->rxlen = -1;
+               wsi->hdr_parsing_completed = 1;
+       }
+}
+
+int
+lws_header_table_is_in_detachable_state(struct lws *wsi)
+{
+       struct allocated_headers *ah = wsi->u.hdr.ah;
+
+       return ah && ah->rxpos == ah->rxlen && wsi->hdr_parsing_completed;
+}
+
 int lws_header_table_detach(struct lws *wsi, int autoservice)
 {
        struct lws_context *context = wsi->context;
@@ -211,6 +242,9 @@ int lws_header_table_detach(struct lws *wsi, int autoservice)
        struct lws **pwsi;
        time_t now;
 
+       if (!ah)
+               return 0;
+
        lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__,
                  (void *)wsi, (void *)ah, wsi->tsi,
                  pt->ah_count_in_use);
@@ -219,11 +253,9 @@ int lws_header_table_detach(struct lws *wsi, int autoservice)
                lws_free_set_NULL(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: CANNOT DETACH rxpos:%d, rxlen:%d\n", __func__, wsi,
-                               ah->rxpos, ah->rxlen);
-               assert(ah->rxpos == ah->rxlen);
-
+       if (!lws_header_table_is_in_detachable_state(wsi)) {
+               lwsl_err("%s: %p: CANNOT DETACH rxpos:%d, rxlen:%d, wsi->hdr_parsing_completed = %d\n", __func__, wsi,
+                               ah->rxpos, ah->rxlen, wsi->hdr_parsing_completed);
                return 0;
        }
 
@@ -288,6 +320,9 @@ int lws_header_table_detach(struct lws *wsi, int autoservice)
 
        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);
 
@@ -308,18 +343,21 @@ int lws_header_table_detach(struct lws *wsi, int autoservice)
        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);
+       assert(!!pt->ah_wait_list_length == !!(lws_intptr_t)pt->ah_wait_list);
 bail:
        lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__,
          (void *)wsi, (void *)ah, wsi->tsi,
@@ -528,6 +566,7 @@ lws_parse(struct lws *wsi, unsigned char c)
                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;
@@ -799,9 +838,16 @@ swallow:
                                }
                        /*
                         * 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;
                        }
@@ -934,36 +980,71 @@ LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
        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;
                }
@@ -1217,6 +1298,9 @@ handle_first:
        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");
@@ -1392,6 +1476,9 @@ drain_extension:
                        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
@@ -1405,10 +1492,9 @@ drain_extension:
 
                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) {
@@ -1455,7 +1541,7 @@ lws_remaining_packet_payload(struct lws *wsi)
  * 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)
 {
@@ -1480,7 +1566,7 @@ lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf,
 
        /* 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;
@@ -1510,4 +1596,6 @@ lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf,
        wsi->u.ws.rx_ubuf_head += avail;
        wsi->u.ws.rx_packet_length -= avail;
        *len -= avail;
+
+       return avail;
 }