From ea3f178a65ef8884998d361bf0edb88cc77aee93 Mon Sep 17 00:00:00 2001 From: chowette Date: Wed, 8 Jul 2020 19:55:43 +0200 Subject: [PATCH 1/4] Change escaping of raw text to use long string lua has [[ ]] for raw string embedding This allow us to not track string quotes and output bigger chunk. The downside is that we are more likely to hit the buffer size limit of 2k, and miss a lot of preallocated buffer use. So we need to track the length of the line and break. This change also preserve blank line in raw text. --- components/http/preprocessor.c | 225 ++++++++++++--------------------- 1 file changed, 83 insertions(+), 142 deletions(-) diff --git a/components/http/preprocessor.c b/components/http/preprocessor.c index 96d06b575..84be481c0 100644 --- a/components/http/preprocessor.c +++ b/components/http/preprocessor.c @@ -51,19 +51,21 @@ #include +// from httpsrv.c +#define PRINTF_BUFFER_SIZE_INITIAL 256 +#define PRINT_MAXLINE 200 + int http_preprocess_lua_page(const char *ipath, const char *opath) { FILE *ifp; // Input file FILE *ofp; // Output file int c; - int nested = 0; - int print = 0; - char string; - char lua = 0; - char delim; - char io_write = 0; - char add_cr = 0; + int lc = 0; + char lua = 0; // lua mode char buff[6]; + // avoid line longer than PRINTF_BUFFER_SIZE_INITIAL, as they will trigger a memory allocation in do_print() + // they may even silently fail if they are longer than BUFFER_SIZE_MAX + int line_len = 0; // length of the current verbatim line const char *bt = ""; @@ -81,151 +83,90 @@ int http_preprocess_lua_page(const char *ipath, const char *opath) { // Open output file ofp = fopen(opath,"w+"); if (!ofp) { - fclose(ifp); + fclose(ifp); return -1; } - string = 0; - *cbuff = '\0'; - - fprintf(ofp, "do\n"); - fprintf(ofp, "local print = net.service.http.print_chunk\n"); + *cbuff = '\0'; + fprintf(ofp, "do\n"); + fprintf(ofp, "local print = http_internal_handle and net.service.http.print_chunk or print\n"); // can be run in console + fprintf(ofp, "local _w = http_internal_handle and net.service.http.print_chunk or io.write\n"); while((c = fgetc(ifp)) != EOF) { - if (c == '"') { - if (!string) { - delim = '\"'; - string = 1; - } else { - if (c == delim) { - string = 0; - } - } - } else if (c == '\'') { - if (!string) { - delim = '\''; - string = 1; - } else { - if (c == delim) { - string = 0; - } - } - } - - if (c == *cbt) { - nested++; - - cbt++; - if (!*cbt) { - lua = 1; - add_cr = 1; - cbuff = buff; - - if (nested > 1) { - if (print) { - fprintf(ofp, "\")\n"); - io_write = 1; - } - } - } else { - *cbuff++ = c; - } - - *cbuff = '\0'; - continue; - } else if (c == *cet) { - nested--; - - cet++; - if (!*cet) { - lua = 0; - add_cr = 1; - - if (nested > 0) { - if (print) { - fprintf(ofp, "\nprint(\""); - io_write = 1; - } - } - - cbuff = buff; - } else { - *cbuff++ = c; - } - - *cbuff = '\0'; - continue; - } else { - cbt = bt; - cet = et; - - cbuff = buff; - - while (*cbuff) { - if (c == '\n') { - if (io_write) { - fprintf(ofp, "\")\n"); - io_write = 0; - print = 0; - } - } else if (c == '\r') { - continue; - } else if (c == '\"') { - fprintf(ofp, "\\%c",c); - } else { - if (!io_write) { - if (add_cr) { - fprintf(ofp, "\n"); - add_cr = 0; - } - fprintf(ofp, "print(\""); - io_write = 1; - print = 1; - } - fprintf(ofp, "%c",*cbuff); - } - - cbuff++; - } - - if (!lua) { - if (c == '\n') { - if (io_write) { - fprintf(ofp, "\")\n"); - io_write = 0; - print = 0; - } - } else if (c == '\r') { - continue; - } else if (c == '\"') { - fprintf(ofp, "\\%c",c); - } else { - if (!io_write) { - if (add_cr) { - fprintf(ofp, "\n"); - add_cr = 0; - } - fprintf(ofp, "print(\""); - io_write = 1; - print = 1; - } - fprintf(ofp, "%c",c); - } - } else { - fprintf(ofp, "%c",c); - } - - cbuff = buff; - *cbuff = '\0'; - } + + if (!lua && (c == *cbt)) { // in opening lua mark + cbt++; + if (!*cbt) { // end of mark reached, switch to lua + lua = 1; + cbuff = buff; // drop buffer content + if (line_len > 0) { + fprintf(ofp, "]]\n"); + } + line_len = 0; + } else { + *cbuff++ = c; + } + + *cbuff = '\0'; + } else if (lua && (c == *cet)) { // in closing lua mark + cet++; + if (!*cet) { // end of mark, switch to text + lua = 0; + cbuff = buff; // drop buffer content + fprintf(ofp, "\n"); // end of user lua statement + } else { + *cbuff++ = c; + } + + *cbuff = '\0'; + } else { // not in mark + cbt = bt; + cet = et; + + // output buffered mark + cbuff = buff; // reset mark buffer + if (*cbuff) { + if (!lua && line_len == 0) { + fprintf(ofp, "_w[[\n"); + } + fprintf(ofp, "%s", cbuff); // we can output buffer in one go, as it is only a part of the mark like " PRINT_MAXLINE) { + fprintf(ofp, "\n]]\n"); + line_len = 0; + } + } else if (c == '\r') { + lc = c; // remember last char + continue; + } + if (0 == line_len) { + fprintf(ofp, "_w[[\n"); + } + line_len++; + } + fprintf(ofp, "%c", c); + } + lc = c; // remember last char } - if (io_write) { - fprintf(ofp, "\")\n"); + if (line_len > 0) { + fprintf(ofp, "]]\n"); } - fprintf(ofp, "end"); + fprintf(ofp, "end"); fclose(ifp); fclose(ofp); From a2dfc4687cec40af519c6165793254edcf5cf415 Mon Sep 17 00:00:00 2001 From: chowette Date: Fri, 10 Jul 2020 12:54:18 +0200 Subject: [PATCH 2/4] change extension of http server file to` .luasp` Avoid confusion with real lua file, and allow editor highlighting based on extension Note that preprocessed file end up with extension `.luaspp` --- components/http/fs/www/{index.lua => index.luasp} | 0 components/http/httpsrv.c | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename components/http/fs/www/{index.lua => index.luasp} (100%) diff --git a/components/http/fs/www/index.lua b/components/http/fs/www/index.luasp similarity index 100% rename from components/http/fs/www/index.lua rename to components/http/fs/www/index.luasp diff --git a/components/http/httpsrv.c b/components/http/httpsrv.c index 332ee764d..2b5db1774 100644 --- a/components/http/httpsrv.c +++ b/components/http/httpsrv.c @@ -131,11 +131,11 @@ typedef struct { static http_server_config http_normal = HTTP_Normal_initializer; static http_server_config http_secure = HTTP_Secure_initializer; -int is_lua(char *name) { +int is_luasp(char *name) { char *ext = strrchr(name, '.'); if (!ext) return 0; - if (strcmp(ext, ".lua") == 0) return 1; + if (strcmp(ext, ".luasp") == 0) return 1; return 0; } @@ -636,7 +636,7 @@ void send_file(http_request_handle *request, char *path, struct stat *statbuf) { FILE *file = fopen(path, "r"); if (!file) { send_error(request, 403, "Forbidden", NULL, "Access denied."); - } else if (is_lua(path)) { + } else if (is_luasp(path)) { fclose(file); lua_State *L = luaS_callback_state(http_callback); From 392c38e0a272939cc2798d44f9f53f923b6e5730 Mon Sep 17 00:00:00 2001 From: chowette Date: Thu, 16 Jul 2020 20:11:24 +0200 Subject: [PATCH 3/4] add support for tag `` --- .board | 1 + components/http/preprocessor.c | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .board diff --git a/.board b/.board new file mode 100644 index 000000000..e6d8ee0e4 --- /dev/null +++ b/.board @@ -0,0 +1 @@ +DOIT-ESP32-DEVKIT-V1 diff --git a/components/http/preprocessor.c b/components/http/preprocessor.c index 84be481c0..535ea4f27 100644 --- a/components/http/preprocessor.c +++ b/components/http/preprocessor.c @@ -61,7 +61,11 @@ int http_preprocess_lua_page(const char *ipath, const char *opath) { int c; int lc = 0; - char lua = 0; // lua mode + char lua = 0; // kind of text we are currently reading + // 0: verbatim text; + // 1: lua tag found, we still need to decide if there is a '=' to follow + // 2: statment lua code (eg: ) + // 3: expression lua code ( eg: ) char buff[6]; // avoid line longer than PRINTF_BUFFER_SIZE_INITIAL, as they will trigger a memory allocation in do_print() // they may even silently fail if they are longer than BUFFER_SIZE_MAX @@ -109,18 +113,28 @@ int http_preprocess_lua_page(const char *ipath, const char *opath) { } *cbuff = '\0'; + } else if (lua == 1 && c == '=') { // shortand lua tag + lua = 3; + fprintf(ofp, "_w("); } else if (lua && (c == *cet)) { // in closing lua mark cet++; if (!*cet) { // end of mark, switch to text - lua = 0; cbuff = buff; // drop buffer content - fprintf(ofp, "\n"); // end of user lua statement + if (lua == 3) { + fprintf(ofp, ")\n"); // end of lua expression + } else { + fprintf(ofp, "\n"); // end of user lua statement + } + lua = 0; } else { *cbuff++ = c; } *cbuff = '\0'; } else { // not in mark + if (lua == 1) { + lua = 2; // normal lua code + } cbt = bt; cet = et; From 92f52e87e8ec3c427344f7154c7b632a459f64b3 Mon Sep 17 00:00:00 2001 From: chowette Date: Fri, 17 Jul 2020 12:34:02 +0200 Subject: [PATCH 4/4] removed .board content --- .board | 1 - 1 file changed, 1 deletion(-) diff --git a/.board b/.board index e6d8ee0e4..e69de29bb 100644 --- a/.board +++ b/.board @@ -1 +0,0 @@ -DOIT-ESP32-DEVKIT-V1