]> prime8.dev >> repos - ttyd.git/commitdiff
http: handle lws_send_pipe_choked state
authorShuanglei Tao <tsl0922@gmail.com>
Sun, 23 Jun 2019 12:10:09 +0000 (20:10 +0800)
committerShuanglei Tao <tsl0922@gmail.com>
Sun, 23 Jun 2019 12:10:09 +0000 (20:10 +0800)
src/http.c

index 6dfd8bf159d9cf94b33441c1c2a08e9415647b47..f3bd462b0056cbe1758dfb53c2ec4e60328db28e 100644 (file)
@@ -77,6 +77,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
     struct pss_http *pss = (struct pss_http *) user;
     unsigned char buffer[4096 + LWS_PRE], *p, *end;
     char buf[256];
+    bool done = false;
 
     switch (reason) {
         case LWS_CALLBACK_HTTP:
@@ -164,17 +165,26 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
                 } else if (m != -1 && m < n) {
                     n = m;
                 }
-                if (pss->ptr + n > pss->buffer + pss->len)
+                if (pss->ptr + n > pss->buffer + pss->len) {
                     n = (int) (pss->len - (pss->ptr - pss->buffer));
+                    done = true;
+                }
                 memcpy(buffer + LWS_PRE, pss->ptr, n);
                 pss->ptr += n;
                 if (lws_write_http(wsi, buffer + LWS_PRE, (size_t) n) < n) {
                     if (pss->buffer != (char *) index_html) free(pss->buffer);
                     return -1;
                 }
-            } while (!lws_send_pipe_choked(wsi) && pss->ptr != pss->buffer + pss->len);
+            } while (!lws_send_pipe_choked(wsi) && !done);
+
+            if (!done && pss->ptr < pss->buffer + pss->len) {
+                lws_callback_on_writable(wsi);
+                break;
+            }
 
-            if (pss->buffer != (char *) index_html) free(pss->buffer);
+            if (pss->buffer != (char *) index_html) {
+                free(pss->buffer);
+            }
             goto try_to_reuse;
 
         case LWS_CALLBACK_HTTP_FILE_COMPLETION: