Skip to content

Commit

Permalink
Multiple lua states per pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesper André Lyngesen Pedersen committed Feb 10, 2010
1 parent 4116736 commit 79c5f95
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
4 changes: 4 additions & 0 deletions scripts/test.lua
@@ -0,0 +1,4 @@
dispatch = function()

print 'argh'
end
20 changes: 14 additions & 6 deletions src/http/httpd_conf.c
Expand Up @@ -16,6 +16,7 @@
#include <util/log.h>
#include <util/lua_util.h>
#define PORT 8888
#define DEFAULT_STATE_SIZE 3

char *strdup(const char *s);

Expand All @@ -37,6 +38,8 @@ static struct mimetype json_mimetype = {"json", 4, "application/json", 16};

static int load_scripts(httpd_conf_t *conf, lua_State *L) {
int i, entry, top, length, size;
int state_size;
lua_State *new_L;
const char *file, *mimetype, *pattern;
httpd_lua_state_t *states;
top = lua_gettop(L);
Expand Down Expand Up @@ -85,14 +88,14 @@ static int load_scripts(httpd_conf_t *conf, lua_State *L) {
size = lua_objlen(L, -1);
lua_pop(L, 1);
states[i].filename = strdup(file);
states[i].L = luaL_newstate();
luaL_openlibs(states[i].L);
luaL_loadfile(states[i].L, file);
new_L = luaL_newstate();

if (lua_eval(states[i].L) != 0) {
lua_close(states[i].L);
luaL_openlibs(new_L);
luaL_loadfile(new_L, file);

if (lua_eval(new_L) != 0) {
lua_close(new_L);
free((void*)states[i].filename);
states[i].L = NULL;
if (states[i].mimetype != mimetype) {
free((void*)states[i].mimetype);
}
Expand All @@ -101,6 +104,11 @@ static int load_scripts(httpd_conf_t *conf, lua_State *L) {
lua_pop(L, 1);
continue;
}
state_size = DEFAULT_STATE_SIZE;
states[i].idle_stack_size = state_size;
states[i].idle_state_top = 0;
states[i].idle_states = malloc(sizeof(lua_State*) * state_size);
states[i].idle_states[0] = new_L;

log_debug("%s is serving %s on %s", file, states[i].mimetype, states[i].pattern);
i += 1;
Expand Down
4 changes: 3 additions & 1 deletion src/http/httpd_conf.h
Expand Up @@ -18,8 +18,10 @@ typedef struct mimetype {

typedef struct httpd_lua_state {
const char *pattern;
lua_State *L;
size_t pattern_size;
int idle_state_top;
int idle_stack_size;
lua_State **idle_states;
const char *filename;
const char *mimetype;
} httpd_lua_state_t;
Expand Down
36 changes: 33 additions & 3 deletions src/http/request_processor.c
Expand Up @@ -71,14 +71,43 @@ static int send_header(FILE *fd, int status, const char *status_msg,

}

static lua_State *get_lua_state(httpd_lua_state_t *state) {
lua_State *L;
int stack_top;
stack_top = state->idle_state_top;
if (stack_top < 0) {
log_debug("Creating new state");
L = luaL_newstate();
luaL_openlibs(L);
luaL_loadfile(L, state->filename);
lua_eval(L);
} else {
L = state->idle_states[stack_top];
state->idle_state_top -= 1;
}
return L;
}

static void return_lua_state(httpd_lua_state_t *state, lua_State *L) {
int stack_top;
stack_top = state->idle_state_top;
if (stack_top-1 < state->idle_stack_size) {
stack_top += 1;
state->idle_states[stack_top] = L;
state->idle_state_top = stack_top;
} else {
lua_close(L);
}
}

static int http_lua_eval(http_parser *parser) {
struct http_conn_info *conn_info;
lua_State *L;
conn_info = (struct http_conn_info*)parser->data;

/* Send HTTP headers to client */
send_header(conn_info->client_fd, 200, "OK", conn_info->lua->mimetype);
L = conn_info->lua->L;
L = get_lua_state(conn_info->lua);
lua_settop(L, 0);
lua_getglobal(L, "dispatch");
lua_pushstring(L, conn_info->path);
Expand All @@ -91,6 +120,7 @@ static int http_lua_eval(http_parser *parser) {
default: lua_pushstring(L, "UNKNOWN");
}
lua_eval(L);
return_lua_state(conn_info->lua, L);
return 0;
}

Expand Down Expand Up @@ -133,7 +163,7 @@ static int http_send_file(http_parser *parser) {
return 0;
}

static httpd_lua_state_t *get_lua_state(const httpd_conf_t *conf, const char* path) {
static httpd_lua_state_t *get_script(const httpd_conf_t *conf, const char* path) {
httpd_lua_state_t *state;
state = conf->lua_states;
while (state != NULL && state->pattern != NULL) {
Expand All @@ -151,7 +181,7 @@ static int start_processing(http_parser *parser) {
httpd_lua_state_t *lua_state;
conn_info = (struct http_conn_info*)parser->data;

lua_state = get_lua_state(conn_info->conf, conn_info->path);
lua_state = get_script(conn_info->conf, conn_info->path);
log_debug("%s", conn_info->path);
if (lua_state != NULL) {
conn_info->type = 'L';
Expand Down

0 comments on commit 79c5f95

Please sign in to comment.