]> prime8.dev >> repos - ttyd.git/commitdiff
http: write index html with buffer
authorShuanglei Tao <tsl0922@gmail.com>
Sat, 1 Sep 2018 12:37:18 +0000 (20:37 +0800)
committerShuanglei Tao <tsl0922@gmail.com>
Sat, 1 Sep 2018 12:37:18 +0000 (20:37 +0800)
src/http.c
src/server.h

index ca2bf2a3be3546a1bc73e86d44376e03eaf0e2aa..1704e91986e452658c7b8e945843fdd9b72db015 100644 (file)
@@ -103,7 +103,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
                 goto try_to_reuse;
 #else
                 if (n > 0) {
-                    pss->buffer = strdup(buf);
+                    pss->buffer = pss->ptr = strdup(buf);
                     pss->len = n;
                     lws_callback_on_writable(wsi);
                 }
@@ -137,7 +137,7 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
                     return 1;
                 goto try_to_reuse;
 #else
-                pss->buffer = (char *) index_html;
+                pss->buffer = pss->ptr = (char *) index_html;
                 pss->len = index_html_len;
                 lws_callback_on_writable(wsi);
                 return 0;
@@ -149,15 +149,23 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, voi
             if (pss->len <= 0)
                 goto try_to_reuse;
 
-            int n = lws_write_http(wsi, pss->buffer, pss->len);
-            if (strncmp(pss->path, "/", 1) != 0)
-                free(pss->buffer);
-            if (n < pss->len) {
-                return 1;
+            if (pss ->ptr - pss->buffer == pss->len) {
+                if (pss->buffer != (char *) index_html) free(pss->buffer);
+                goto try_to_reuse;
             }
-            pss->len = 0;
 
-            goto try_to_reuse;
+            int n = sizeof(buffer) - LWS_PRE;
+            if (pss->ptr - pss->buffer + n > pss->len)
+                n = (int) (pss->len - (pss->ptr - pss->buffer));
+            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;
+            }
+
+            lws_callback_on_writable(wsi);
+            break;
 
         case LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION:
             if (!len || (SSL_get_verify_result((SSL *) in) != X509_V_OK)) {
index 95e44ff4c5db5faf7f145e113ebae727de7c3df4..239f88aec44688ff49e2c2e4b9af1083aa115e9e 100644 (file)
@@ -54,6 +54,7 @@ struct tty_client {
 struct pss_http {
     char path[128];
     char *buffer;
+    char *ptr;
     size_t len;
 };