cgi: handle no body to keep events coming
authorAndy Green <andy@warmcat.com>
Thu, 18 May 2017 15:20:12 +0000 (23:20 +0800)
committerAndy Green <andy@warmcat.com>
Thu, 18 May 2017 15:20:12 +0000 (23:20 +0800)
lib/context.c
lib/libwebsockets.c

index e9945c7..e1d8479 100644 (file)
@@ -241,7 +241,10 @@ lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
                        if (lws_cgi_write_split_stdout_headers(wsi) < 0)
                                return -1;
 
-                       wsi->reason_bf &= ~1;
+                       if (wsi->reason_bf & 8)
+                               wsi->reason_bf &= ~8;
+                       else
+                               wsi->reason_bf &= ~1;
                        break;
                }
 #endif
index 0e914f3..8781eec 100755 (executable)
@@ -2590,10 +2590,15 @@ lws_cgi_write_split_stdout_headers(struct lws *wsi)
                        /* finalize cached headers before dumping them */
                        if (lws_finalize_http_header(wsi,
                                        (unsigned char **)&wsi->cgi->headers_pos,
-                                       (unsigned char *)wsi->cgi->headers_end))
+                                       (unsigned char *)wsi->cgi->headers_end)) {
+
+                               lwsl_notice("finalize failed\n");
                                return -1;
+                       }
 
                        wsi->hdr_state = LHCS_DUMP_HEADERS;
+                       wsi->reason_bf |= 8;
+                       lws_callback_on_writable(wsi);
                        /* back to the loop for writeability again */
                        return 0;
 
@@ -2613,6 +2618,9 @@ lws_cgi_write_split_stdout_headers(struct lws *wsi)
                        if (wsi->cgi->headers_dumped == wsi->cgi->headers_pos) {
                                wsi->hdr_state = LHCS_PAYLOAD;
                                lws_free_set_NULL(wsi->cgi->headers_buf);
+                       } else {
+                               wsi->reason_bf |= 8;
+                               lws_callback_on_writable(wsi);
                        }
 
                        /* writeability becomes uncertain now we wrote
@@ -2873,6 +2881,10 @@ lws_cgi_kill_terminated(struct lws_context_per_thread *pt)
                        if (cgi->pid <= 0)
                                continue;
 
+                       /* finish sending cached headers */
+                       if (cgi->headers_buf)
+                               continue;
+
                        /* wait for stdout to be drained */
                        if (cgi->content_length > cgi->content_length_seen)
                                continue;
@@ -2939,6 +2951,10 @@ lws_cgi_kill_terminated(struct lws_context_per_thread *pt)
                        goto finish_him;
                }
 
+               /* finish sending cached headers */
+               if (cgi->headers_buf)
+                       continue;
+
                /* wait for stdout to be drained */
                if (cgi->content_length > cgi->content_length_seen)
                        continue;