[mod_openssl] remove erroneous SSL_set_shutdown()
[lighttpd.git] / src / mod_magnet_cache.c
blob1f8de572348f3fe42de5fe0a06b9ed01cb4a6bc2
1 #include "first.h"
3 #include "mod_magnet_cache.h"
4 #include "stat_cache.h"
6 #include <stdlib.h>
7 #include <time.h>
9 #include <lualib.h>
10 #include <lauxlib.h>
12 static script *script_init() {
13 script *sc;
15 sc = calloc(1, sizeof(*sc));
16 sc->name = buffer_init();
17 sc->etag = buffer_init();
19 return sc;
22 static void script_free(script *sc) {
23 if (!sc) return;
25 lua_pop(sc->L, 1); /* the function copy */
27 buffer_free(sc->name);
28 buffer_free(sc->etag);
30 lua_close(sc->L);
32 free(sc);
35 script_cache *script_cache_init() {
36 script_cache *p;
38 p = calloc(1, sizeof(*p));
40 return p;
43 void script_cache_free(script_cache *p) {
44 size_t i;
46 if (!p) return;
48 for (i = 0; i < p->used; i++) {
49 script_free(p->ptr[i]);
52 free(p->ptr);
54 free(p);
57 lua_State *script_cache_get_script(server *srv, connection *con, script_cache *cache, buffer *name) {
58 size_t i;
59 script *sc = NULL;
60 stat_cache_entry *sce;
62 for (i = 0; i < cache->used; i++) {
63 sc = cache->ptr[i];
65 if (buffer_is_equal(name, sc->name)) {
66 sc->last_used = time(NULL);
68 /* oops, the script failed last time */
70 if (lua_gettop(sc->L) == 0) break;
71 force_assert(lua_gettop(sc->L) == 1);
73 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, sc->name, &sce)) {
74 lua_pop(sc->L, 1); /* pop the old function */
75 break;
78 if (!buffer_is_equal(sce->etag, sc->etag)) {
79 /* the etag is outdated, reload the function */
80 lua_pop(sc->L, 1);
81 break;
84 force_assert(lua_isfunction(sc->L, -1));
86 return sc->L;
89 sc = NULL;
92 /* if the script was script already loaded but either got changed or
93 * failed to load last time */
94 if (sc == NULL) {
95 sc = script_init();
97 if (cache->size == 0) {
98 cache->size = 16;
99 cache->ptr = malloc(cache->size * sizeof(*(cache->ptr)));
100 } else if (cache->used == cache->size) {
101 cache->size += 16;
102 cache->ptr = realloc(cache->ptr, cache->size * sizeof(*(cache->ptr)));
105 cache->ptr[cache->used++] = sc;
107 buffer_copy_buffer(sc->name, name);
109 sc->L = luaL_newstate();
110 luaL_openlibs(sc->L);
113 sc->last_used = time(NULL);
115 if (0 != luaL_loadfile(sc->L, name->ptr)) {
116 /* oops, an error, return it */
117 return sc->L;
120 if (HANDLER_GO_ON == stat_cache_get_entry(srv, con, sc->name, &sce)) {
121 buffer_copy_buffer(sc->etag, sce->etag);
124 force_assert(lua_isfunction(sc->L, -1));
126 return sc->L;