HTTP_PROXY: make usable
[platform/upstream/libwebsockets.git] / lib / parsers.c
index 26f53f4..9cd70e0 100644 (file)
@@ -181,7 +181,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);
@@ -321,15 +321,18 @@ 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);
@@ -813,9 +816,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;
                        }
@@ -948,36 +958,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;
                }
@@ -1231,6 +1276,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");
@@ -1406,6 +1454,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
@@ -1419,10 +1470,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) {