From 60a9bf1ab6ae66bd2234ef3728e9aeb6657cfc42 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Sat, 1 Sep 2018 20:37:18 +0800 Subject: [PATCH] http: write index html with buffer --- src/http.c | 26 +++++++++++++++++--------- src/server.h | 1 + 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/http.c b/src/http.c index ca2bf2a..1704e91 100644 --- a/src/http.c +++ b/src/http.c @@ -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)) { diff --git a/src/server.h b/src/server.h index 95e44ff..239f88a 100644 --- a/src/server.h +++ b/src/server.h @@ -54,6 +54,7 @@ struct tty_client { struct pss_http { char path[128]; char *buffer; + char *ptr; size_t len; }; -- 2.43.4