File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / lighttpd / src / mod_magnet_cache.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Nov 2 10:35:00 2016 UTC (7 years, 8 months ago) by misho
Branches: lighttpd, MAIN
CVS tags: v1_4_41p8, HEAD
lighttpd 1.4.41

    1: #include "first.h"
    2: 
    3: #include "mod_magnet_cache.h"
    4: #include "stat_cache.h"
    5: 
    6: #include <stdlib.h>
    7: #include <time.h>
    8: #include <assert.h>
    9: 
   10: #ifdef HAVE_LUA_H
   11: #include <lualib.h>
   12: #include <lauxlib.h>
   13: 
   14: static script *script_init() {
   15: 	script *sc;
   16: 
   17: 	sc = calloc(1, sizeof(*sc));
   18: 	sc->name = buffer_init();
   19: 	sc->etag = buffer_init();
   20: 
   21: 	return sc;
   22: }
   23: 
   24: static void script_free(script *sc) {
   25: 	if (!sc) return;
   26: 
   27: 	lua_pop(sc->L, 1); /* the function copy */
   28: 
   29: 	buffer_free(sc->name);
   30: 	buffer_free(sc->etag);
   31: 
   32: 	lua_close(sc->L);
   33: 
   34: 	free(sc);
   35: }
   36: 
   37: script_cache *script_cache_init() {
   38: 	script_cache *p;
   39: 
   40: 	p = calloc(1, sizeof(*p));
   41: 
   42: 	return p;
   43: }
   44: 
   45: void script_cache_free(script_cache *p) {
   46: 	size_t i;
   47: 
   48: 	if (!p) return;
   49: 
   50: 	for (i = 0; i < p->used; i++) {
   51: 		script_free(p->ptr[i]);
   52: 	}
   53: 
   54: 	free(p->ptr);
   55: 
   56: 	free(p);
   57: }
   58: 
   59: lua_State *script_cache_get_script(server *srv, connection *con, script_cache *cache, buffer *name) {
   60: 	size_t i;
   61: 	script *sc = NULL;
   62: 	stat_cache_entry *sce;
   63: 
   64: 	for (i = 0; i < cache->used; i++) {
   65: 		sc = cache->ptr[i];
   66: 
   67: 		if (buffer_is_equal(name, sc->name)) {
   68: 			sc->last_used = time(NULL);
   69: 
   70: 			/* oops, the script failed last time */
   71: 
   72: 			if (lua_gettop(sc->L) == 0) break;
   73: 			force_assert(lua_gettop(sc->L) == 1);
   74: 
   75: 			if (HANDLER_ERROR == stat_cache_get_entry(srv, con, sc->name, &sce)) {
   76: 				lua_pop(sc->L, 1); /* pop the old function */
   77: 				break;
   78: 			}
   79: 
   80: 			if (!buffer_is_equal(sce->etag, sc->etag)) {
   81: 				/* the etag is outdated, reload the function */
   82: 				lua_pop(sc->L, 1);
   83: 				break;
   84: 			}
   85: 
   86: 			force_assert(lua_isfunction(sc->L, -1));
   87: 
   88: 			return sc->L;
   89: 		}
   90: 
   91: 		sc = NULL;
   92: 	}
   93: 
   94: 	/* if the script was script already loaded but either got changed or
   95: 	 * failed to load last time */
   96: 	if (sc == NULL) {
   97: 		sc = script_init();
   98: 
   99: 		if (cache->size == 0) {
  100: 			cache->size = 16;
  101: 			cache->ptr = malloc(cache->size * sizeof(*(cache->ptr)));
  102: 		} else if (cache->used == cache->size) {
  103: 			cache->size += 16;
  104: 			cache->ptr = realloc(cache->ptr, cache->size * sizeof(*(cache->ptr)));
  105: 		}
  106: 
  107: 		cache->ptr[cache->used++] = sc;
  108: 
  109: 		buffer_copy_buffer(sc->name, name);
  110: 
  111: 		sc->L = luaL_newstate();
  112: 		luaL_openlibs(sc->L);
  113: 	}
  114: 
  115: 	sc->last_used = time(NULL);
  116: 
  117: 	if (0 != luaL_loadfile(sc->L, name->ptr)) {
  118: 		/* oops, an error, return it */
  119: 		return sc->L;
  120: 	}
  121: 
  122: 	if (HANDLER_GO_ON == stat_cache_get_entry(srv, con, sc->name, &sce)) {
  123: 		buffer_copy_buffer(sc->etag, sce->etag);
  124: 	}
  125: 
  126: 	force_assert(lua_isfunction(sc->L, -1));
  127: 
  128: 	return sc->L;
  129: }
  130: 
  131: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>