lighttpd 1.4.x
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.3 KiB

#include "first.h"
#include "mod_magnet_cache.h"
#include "log.h"
#include "stat_cache.h"
#include <stdlib.h>
#include <time.h>
#include <lualib.h>
#include <lauxlib.h>
static script *script_init() {
script *sc;
sc = calloc(1, sizeof(*sc));
sc->name = buffer_init();
sc->etag = buffer_init();
return sc;
static void script_free(script *sc) {
if (!sc) return;
lua_pop(sc->L, 1); /* the function copy */
#if 0
script_cache *script_cache_init() {
script_cache *p = calloc(1, sizeof(script_cache));
return p;
void script_cache_free_data(script_cache *p) {
size_t i;
if (!p) return;
for (i = 0; i < p->used; i++) {
lua_State *script_cache_get_script(script_cache *cache, buffer *name, int etag_flags) {
script *sc = NULL;
stat_cache_entry *sce;
for (uint32_t i = 0; i < cache->used; ++i, sc = NULL) {
sc = cache->ptr[i];
if (!buffer_is_equal(name, sc->name)) continue;
sc->last_used = log_epoch_secs;
/* oops, the script failed last time */
if (lua_gettop(sc->L) == 0) break;
force_assert(lua_gettop(sc->L) == 1);
sce = stat_cache_get_entry(sc->name);
if (NULL == sce) {
lua_pop(sc->L, 1); /* pop the old function */
const buffer *etag = stat_cache_etag_get(sce, etag_flags);
if (NULL == etag || !buffer_is_equal(sc->etag, etag)) {
/* the etag is outdated, reload the function */
lua_pop(sc->L, 1);
force_assert(lua_isfunction(sc->L, -1));
return sc->L;
/* if the script was script already loaded but either got changed or
* failed to load last time */
if (sc == NULL) {
sc = script_init();
if (cache->used == cache->size) {
cache->size += 16;
cache->ptr = realloc(cache->ptr, cache->size * sizeof(*(cache->ptr)));
cache->ptr[cache->used++] = sc;
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <> git-svn-id: svn:// 152afb58-edef-0310-8abb-c4023f1b3aa9
8 years ago
buffer_copy_buffer(sc->name, name);
sc->L = luaL_newstate();
sc->last_used = log_epoch_secs;
if (0 != luaL_loadfile(sc->L, name->ptr)) {
/* oops, an error, return it */
return sc->L;
sce = stat_cache_get_entry(sc->name);
if (sce) {
buffer_copy_buffer(sc->etag, stat_cache_etag_get(sce, etag_flags));
force_assert(lua_isfunction(sc->L, -1));
return sc->L;