diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2022-02-09 02:44:25 -0500 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2022-02-19 02:40:37 -0500 |
commit | d0273c8af5e17d025cce3ff4f89bdc984b157635 (patch) | |
tree | 65acdfe002eb4b14fde097742549712bfa280fc5 /src/mod_magnet_cache.c | |
parent | a162fc70e9bca2b6e0d51c26d9b19ebd110e3578 (diff) | |
download | lighttpd-git-d0273c8af5e17d025cce3ff4f89bdc984b157635.tar.gz |
[mod_magnet] defer req_env init unless needed
defer req_env initialization unless req_env might be needed by script
Diffstat (limited to 'src/mod_magnet_cache.c')
-rw-r--r-- | src/mod_magnet_cache.c | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/mod_magnet_cache.c b/src/mod_magnet_cache.c index a476dd39..56f313ff 100644 --- a/src/mod_magnet_cache.c +++ b/src/mod_magnet_cache.c @@ -5,7 +5,8 @@ #include <errno.h> #include <stdlib.h> -#include <unistd.h> /* read() */ +#include <string.h> /* strstr() */ +#include <unistd.h> /* lseek() read() */ #include <lualib.h> #include <lauxlib.h> @@ -54,7 +55,7 @@ static lua_State *script_cache_load_script(script * const sc, int etag_flags) stat_cache_entry * const sce = stat_cache_get_entry_open(&sc->name, 1); buffer_clear(&sc->etag); - if (NULL == sce || sce->fd < 0) { + if (NULL == sce || sce->fd < 0 || -1 == lseek(sce->fd, 0, SEEK_SET)) { /*(sce->fd < 0 might indicate empty file, which is not a valid script)*/ if (NULL != sce) errno = EBADF; return NULL; @@ -64,7 +65,7 @@ static lua_State *script_cache_load_script(script * const sc, int etag_flags) buffer_copy_buffer(&sc->etag, etag); const off_t sz = sce->st.st_size; - char * const buf = malloc(sz); + char * const buf = malloc(sz+1); force_assert(buf); ssize_t rd = 0; @@ -78,6 +79,10 @@ static lua_State *script_cache_load_script(script * const sc, int etag_flags) return NULL; } + /*(coarse heuristic to detect if script needs req_env initialized)*/ + buf[sz] = '\0'; /* for strstr() */ + sc->req_env_init = (NULL != strstr(buf, "req_env")); + int rc = luaL_loadbuffer(sc->L, buf, (size_t)sz, sc->name.ptr); free(buf); |