lws_intptr_t
[platform/upstream/libwebsockets.git] / lib / parsers.c
index 51b9218..3f47be9 100644 (file)
@@ -86,8 +86,11 @@ lws_header_table_reset(struct lws *wsi, int autoservice)
 
        _lws_header_table_reset(ah);
 
+        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
@@ -181,7 +184,7 @@ 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);
@@ -212,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;
@@ -221,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);
@@ -229,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;
        }
 
@@ -321,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,
@@ -954,16 +979,39 @@ 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;
                }
@@ -980,7 +1028,6 @@ lws_remove_wsi_from_draining_ext_list(struct lws *wsi)
 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;
@@ -1445,10 +1492,7 @@ drain_extension:
 
                if (n && eff_buf.token_len) {
                        /* extension had more... main loop will come back */
-                       // lwsl_notice("ext has stuff to drain\n");
-                       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);