lighttpd1.4/src/mod_magnet.c

1118 lines
28 KiB
C
Raw Normal View History

#include "base.h"
#include "log.h"
#include "buffer.h"
#include "plugin.h"
#include "mod_magnet_cache.h"
#include "response.h"
#include "stat_cache.h"
#include "status_counter.h"
#include "etag.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <setjmp.h>
#ifdef HAVE_LUA_H
#include <lua.h>
#include <lauxlib.h>
#define MAGNET_CONFIG_RAW_URL "magnet.attract-raw-url-to"
#define MAGNET_CONFIG_PHYSICAL_PATH "magnet.attract-physical-path-to"
#define MAGNET_RESTART_REQUEST 99
/* plugin config for all request/connections */
static jmp_buf exceptionjmp;
typedef struct {
array *url_raw;
array *physical_path;
} plugin_config;
typedef struct {
PLUGIN_DATA;
script_cache *cache;
buffer *encode_buf;
plugin_config **config_storage;
plugin_config conf;
} plugin_data;
/* init the plugin data */
INIT_FUNC(mod_magnet_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
p->cache = script_cache_init();
p->encode_buf = buffer_init();
return p;
}
/* detroy the plugin data */
FREE_FUNC(mod_magnet_free) {
plugin_data *p = p_d;
UNUSED(srv);
if (!p) return HANDLER_GO_ON;
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s = p->config_storage[i];
if (NULL == s) continue;
array_free(s->url_raw);
array_free(s->physical_path);
free(s);
}
free(p->config_storage);
}
script_cache_free(p->cache);
buffer_free(p->encode_buf);
free(p);
return HANDLER_GO_ON;
}
/* handle plugin config and check values */
SETDEFAULTS_FUNC(mod_magnet_set_defaults) {
plugin_data *p = p_d;
size_t i = 0;
config_values_t cv[] = {
{ MAGNET_CONFIG_RAW_URL, NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
{ MAGNET_CONFIG_PHYSICAL_PATH, NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
if (!p) return HANDLER_ERROR;
p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s;
s = calloc(1, sizeof(plugin_config));
s->url_raw = array_init();
s->physical_path = array_init();
cv[0].destination = s->url_raw;
cv[1].destination = s->physical_path;
p->config_storage[i] = s;
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
return HANDLER_ERROR;
}
}
return HANDLER_GO_ON;
}
#define PATCH(x) \
p->conf.x = s->x;
static int mod_magnet_patch_connection(server *srv, connection *con, plugin_data *p) {
size_t i, j;
plugin_config *s = p->config_storage[0];
PATCH(url_raw);
PATCH(physical_path);
/* skip the first, the global context */
for (i = 1; i < srv->config_context->used; i++) {
data_config *dc = (data_config *)srv->config_context->data[i];
s = p->config_storage[i];
/* condition didn't match */
if (!config_check_cond(srv, con, dc)) continue;
/* merge config */
for (j = 0; j < dc->value->used; j++) {
data_unset *du = dc->value->data[j];
if (buffer_is_equal_string(du->key, CONST_STR_LEN(MAGNET_CONFIG_RAW_URL))) {
PATCH(url_raw);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN(MAGNET_CONFIG_PHYSICAL_PATH))) {
PATCH(physical_path);
}
}
}
return 0;
}
#undef PATCH
/* See http://lua-users.org/wiki/GeneralizedPairsAndIpairs for implementation details. */
/* Override the default pairs() function to allow us to use a __pairs metakey */
static int magnet_pairs(lua_State *L) {
luaL_checkany(L, 1);
if (luaL_getmetafield(L, 1, "__pairs")) {
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
} else {
lua_pushvalue(L, lua_upvalueindex(1));
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
}
/* Define a function that will iterate over an array* (in upval 1) using current position (upval 2) */
static int magnet_array_next(lua_State *L) {
data_unset *du;
data_string *ds;
data_integer *di;
size_t pos = lua_tointeger(L, lua_upvalueindex(1));
array *a = lua_touserdata(L, lua_upvalueindex(2));
lua_settop(L, 0);
if (pos >= a->used) return 0;
if (NULL != (du = a->data[pos])) {
lua_pushlstring(L, CONST_BUF_LEN(du->key));
switch (du->type) {
case TYPE_STRING:
ds = (data_string *)du;
if (!buffer_is_empty(ds->value)) {
lua_pushlstring(L, CONST_BUF_LEN(ds->value));
} else {
lua_pushnil(L);
}
break;
case TYPE_COUNT:
case TYPE_INTEGER:
di = (data_integer *)du;
lua_pushinteger(L, di->value);
break;
default:
lua_pushnil(L);
break;
}
/* Update our positional upval to reflect our new current position */
pos++;
lua_pushinteger(L, pos);
lua_replace(L, lua_upvalueindex(1));
/* Returning 2 items on the stack (key, value) */
return 2;
}
return 0;
}
/* Create the closure necessary to iterate over the array *a with the above function */
static int magnet_array_pairs(lua_State *L, array *a) {
lua_pushinteger(L, 0); /* Push our current pos (the start) into upval 1 */
lua_pushlightuserdata(L, a); /* Push our array *a into upval 2 */
lua_pushcclosure(L, magnet_array_next, 2); /* Push our new closure with 2 upvals */
return 1;
}
static int magnet_print(lua_State *L) {
const char *s = luaL_checkstring(L, 1);
server *srv;
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
log_error_write(srv, __FILE__, __LINE__, "ss",
"(lua-print)", s);
return 0;
}
static int magnet_stat(lua_State *L) {
const char *s = luaL_checkstring(L, 1);
server *srv;
connection *con;
buffer *sb;
stat_cache_entry *sce = NULL;
handler_t res;
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
sb = buffer_init_string(s);
res = stat_cache_get_entry(srv, con, sb, &sce);
buffer_free(sb);
if (HANDLER_GO_ON != res) {
lua_pushnil(L);
return 1;
}
lua_newtable(L);
lua_pushboolean(L, S_ISREG(sce->st.st_mode));
lua_setfield(L, -2, "is_file");
lua_pushboolean(L, S_ISDIR(sce->st.st_mode));
lua_setfield(L, -2, "is_dir");
lua_pushboolean(L, S_ISCHR(sce->st.st_mode));
lua_setfield(L, -2, "is_char");
lua_pushboolean(L, S_ISBLK(sce->st.st_mode));
lua_setfield(L, -2, "is_block");
lua_pushboolean(L, S_ISSOCK(sce->st.st_mode));
lua_setfield(L, -2, "is_socket");
lua_pushboolean(L, S_ISLNK(sce->st.st_mode));
lua_setfield(L, -2, "is_link");
lua_pushboolean(L, S_ISFIFO(sce->st.st_mode));
lua_setfield(L, -2, "is_fifo");
lua_pushinteger(L, sce->st.st_mtime);
lua_setfield(L, -2, "st_mtime");
lua_pushinteger(L, sce->st.st_ctime);
lua_setfield(L, -2, "st_ctime");
lua_pushinteger(L, sce->st.st_atime);
lua_setfield(L, -2, "st_atime");
lua_pushinteger(L, sce->st.st_uid);
lua_setfield(L, -2, "st_uid");
lua_pushinteger(L, sce->st.st_gid);
lua_setfield(L, -2, "st_gid");
lua_pushinteger(L, sce->st.st_size);
lua_setfield(L, -2, "st_size");
lua_pushinteger(L, sce->st.st_ino);
lua_setfield(L, -2, "st_ino");
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 <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
if (!buffer_string_is_empty(sce->etag)) {
/* we have to mutate the etag */
buffer *b = buffer_init();
etag_mutate(b, sce->etag);
lua_pushlstring(L, CONST_BUF_LEN(b));
buffer_free(b);
} else {
lua_pushnil(L);
}
lua_setfield(L, -2, "etag");
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 <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
if (!buffer_string_is_empty(sce->content_type)) {
lua_pushlstring(L, CONST_BUF_LEN(sce->content_type));
} else {
lua_pushnil(L);
}
lua_setfield(L, -2, "content-type");
return 1;
}
static int magnet_atpanic(lua_State *L) {
const char *s = luaL_checkstring(L, 1);
server *srv;
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
log_error_write(srv, __FILE__, __LINE__, "ss",
"(lua-atpanic)", s);
longjmp(exceptionjmp, 1);
}
static int magnet_reqhdr_get(lua_State *L) {
connection *con;
data_string *ds;
const char *key = luaL_checkstring(L, 2);
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
if (NULL != (ds = (data_string *)array_get_element(con->request.headers, key))) {
if (!buffer_is_empty(ds->value)) {
lua_pushlstring(L, CONST_BUF_LEN(ds->value));
} else {
lua_pushnil(L);
}
} else {
lua_pushnil(L);
}
return 1;
}
static int magnet_reqhdr_pairs(lua_State *L) {
connection *con;
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
return magnet_array_pairs(L, con->request.headers);
}
static int magnet_status_get(lua_State *L) {
data_integer *di;
server *srv;
size_t key_len = 0;
const char *key = luaL_checklstring(L, 2, &key_len);
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
di = status_counter_get_counter(srv, key, key_len);
lua_pushnumber(L, (double)di->value);
return 1;
}
static int magnet_status_set(lua_State *L) {
size_t key_len = 0;
server *srv;
const char *key = luaL_checklstring(L, 2, &key_len);
int counter = luaL_checkint(L, 3);
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
status_counter_set(srv, key, key_len, counter);
return 0;
}
static int magnet_status_pairs(lua_State *L) {
server *srv;
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
return magnet_array_pairs(L, srv->status);
}
typedef struct {
const char *name;
enum {
MAGNET_ENV_UNSET,
MAGNET_ENV_PHYICAL_PATH,
MAGNET_ENV_PHYICAL_REL_PATH,
MAGNET_ENV_PHYICAL_DOC_ROOT,
MAGNET_ENV_PHYICAL_BASEDIR,
MAGNET_ENV_URI_PATH,
MAGNET_ENV_URI_PATH_RAW,
MAGNET_ENV_URI_SCHEME,
MAGNET_ENV_URI_AUTHORITY,
MAGNET_ENV_URI_QUERY,
MAGNET_ENV_REQUEST_METHOD,
MAGNET_ENV_REQUEST_URI,
MAGNET_ENV_REQUEST_ORIG_URI,
MAGNET_ENV_REQUEST_PATH_INFO,
MAGNET_ENV_REQUEST_REMOTE_IP,
MAGNET_ENV_REQUEST_PROTOCOL
} type;
} magnet_env_t;
static const magnet_env_t magnet_env[] = {
{ "physical.path", MAGNET_ENV_PHYICAL_PATH },
{ "physical.rel-path", MAGNET_ENV_PHYICAL_REL_PATH },
{ "physical.doc-root", MAGNET_ENV_PHYICAL_DOC_ROOT },
{ "physical.basedir", MAGNET_ENV_PHYICAL_BASEDIR },
{ "uri.path", MAGNET_ENV_URI_PATH },
{ "uri.path-raw", MAGNET_ENV_URI_PATH_RAW },
{ "uri.scheme", MAGNET_ENV_URI_SCHEME },
{ "uri.authority", MAGNET_ENV_URI_AUTHORITY },
{ "uri.query", MAGNET_ENV_URI_QUERY },
{ "request.method", MAGNET_ENV_REQUEST_METHOD },
{ "request.uri", MAGNET_ENV_REQUEST_URI },
{ "request.orig-uri", MAGNET_ENV_REQUEST_ORIG_URI },
{ "request.path-info", MAGNET_ENV_REQUEST_PATH_INFO },
{ "request.remote-ip", MAGNET_ENV_REQUEST_REMOTE_IP },
{ "request.protocol", MAGNET_ENV_REQUEST_PROTOCOL },
{ NULL, MAGNET_ENV_UNSET }
};
static buffer *magnet_env_get_buffer_by_id(server *srv, connection *con, int id) {
buffer *dest = NULL;
UNUSED(srv);
/**
* map all internal variables to lua
*
*/
switch (id) {
case MAGNET_ENV_PHYICAL_PATH: dest = con->physical.path; break;
case MAGNET_ENV_PHYICAL_REL_PATH: dest = con->physical.rel_path; break;
case MAGNET_ENV_PHYICAL_DOC_ROOT: dest = con->physical.doc_root; break;
case MAGNET_ENV_PHYICAL_BASEDIR: dest = con->physical.basedir; break;
case MAGNET_ENV_URI_PATH: dest = con->uri.path; break;
case MAGNET_ENV_URI_PATH_RAW: dest = con->uri.path_raw; break;
case MAGNET_ENV_URI_SCHEME: dest = con->uri.scheme; break;
case MAGNET_ENV_URI_AUTHORITY: dest = con->uri.authority; break;
case MAGNET_ENV_URI_QUERY: dest = con->uri.query; break;
case MAGNET_ENV_REQUEST_METHOD:
buffer_copy_string(srv->tmp_buf, get_http_method_name(con->request.http_method));
dest = srv->tmp_buf;
break;
case MAGNET_ENV_REQUEST_URI: dest = con->request.uri; break;
case MAGNET_ENV_REQUEST_ORIG_URI: dest = con->request.orig_uri; break;
case MAGNET_ENV_REQUEST_PATH_INFO: dest = con->request.pathinfo; break;
case MAGNET_ENV_REQUEST_REMOTE_IP: dest = con->dst_addr_buf; break;
case MAGNET_ENV_REQUEST_PROTOCOL:
buffer_copy_string(srv->tmp_buf, get_http_version_name(con->request.http_version));
dest = srv->tmp_buf;
break;
case MAGNET_ENV_UNSET: break;
}
return dest;
}
static buffer *magnet_env_get_buffer(server *srv, connection *con, const char *key) {
size_t i;
for (i = 0; magnet_env[i].name; i++) {
if (0 == strcmp(key, magnet_env[i].name)) break;
}
return magnet_env_get_buffer_by_id(srv, con, magnet_env[i].type);
}
static int magnet_env_get(lua_State *L) {
server *srv;
connection *con;
const char *key = luaL_checkstring(L, 2);
buffer *dest = NULL;
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
dest = magnet_env_get_buffer(srv, con, key);
if (!buffer_is_empty(dest)) {
lua_pushlstring(L, CONST_BUF_LEN(dest));
} else {
lua_pushnil(L);
}
return 1;
}
static int magnet_env_set(lua_State *L) {
server *srv;
connection *con;
const char *key = luaL_checkstring(L, 2);
const char *val = luaL_checkstring(L, 3);
buffer *dest = NULL;
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
if (NULL != (dest = magnet_env_get_buffer(srv, con, key))) {
buffer_copy_string(dest, val);
} else {
/* couldn't save */
return luaL_error(L, "couldn't store '%s' in lighty.env[]", key);
}
return 0;
}
static int magnet_env_next(lua_State *L) {
server *srv;
connection *con;
int pos = lua_tointeger(L, lua_upvalueindex(1));
buffer *dest;
lua_pushstring(L, "lighty.srv");
lua_gettable(L, LUA_REGISTRYINDEX);
srv = lua_touserdata(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
lua_settop(L, 0);
if (NULL == magnet_env[pos].name) return 0; /* end of list */
lua_pushstring(L, magnet_env[pos].name);
dest = magnet_env_get_buffer_by_id(srv, con, magnet_env[pos].type);
if (!buffer_is_empty(dest)) {
lua_pushlstring(L, CONST_BUF_LEN(dest));
} else {
lua_pushnil(L);
}
/* Update our positional upval to reflect our new current position */
pos++;
lua_pushinteger(L, pos);
lua_replace(L, lua_upvalueindex(1));
/* Returning 2 items on the stack (key, value) */
return 2;
}
static int magnet_env_pairs(lua_State *L) {
lua_pushinteger(L, 0); /* Push our current pos (the start) into upval 1 */
lua_pushcclosure(L, magnet_env_next, 1); /* Push our new closure with 1 upvals */
return 1;
}
static int magnet_cgi_get(lua_State *L) {
connection *con;
data_string *ds;
const char *key = luaL_checkstring(L, 2);
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
ds = (data_string *)array_get_element(con->environment, key);
if (NULL != ds && !buffer_is_empty(ds->value))
lua_pushlstring(L, CONST_BUF_LEN(ds->value));
else
lua_pushnil(L);
return 1;
}
static int magnet_cgi_set(lua_State *L) {
connection *con;
const char *key = luaL_checkstring(L, 2);
const char *val = luaL_checkstring(L, 3);
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
array_set_key_value(con->environment, key, strlen(key), val, strlen(val));
return 0;
}
static int magnet_cgi_pairs(lua_State *L) {
connection *con;
lua_pushstring(L, "lighty.con");
lua_gettable(L, LUA_REGISTRYINDEX);
con = lua_touserdata(L, -1);
lua_pop(L, 1);
return magnet_array_pairs(L, con->environment);
}
static int magnet_copy_response_header(server *srv, connection *con, plugin_data *p, lua_State *L) {
UNUSED(p);
/**
* get the environment of the function
*/
lua_getfenv(L, -1); /* -1 is the function */
/* lighty.header */
lua_getfield(L, -1, "lighty"); /* lighty.* from the env */
force_assert(lua_istable(L, -1));
lua_getfield(L, -1, "header"); /* lighty.header */
if (lua_istable(L, -1)) {
/* header is found, and is a table */
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
if (lua_isstring(L, -1) && lua_isstring(L, -2)) {
const char *key, *val;
size_t key_len, val_len;
key = lua_tolstring(L, -2, &key_len);
val = lua_tolstring(L, -1, &val_len);
response_header_overwrite(srv, con, key, key_len, val, val_len);
}
lua_pop(L, 1);
}
}
lua_pop(L, 1); /* pop the header-table */
lua_pop(L, 1); /* pop the lighty-env */
lua_pop(L, 1); /* pop the function env */
return 0;
}
/**
* walk through the content array
*
* content = { "<pre>", { file = "/content" } , "</pre>" }
*
* header["Content-Type"] = "text/html"
*
* return 200
*/
static int magnet_attach_content(server *srv, connection *con, plugin_data *p, lua_State *L) {
UNUSED(p);
/**
* get the environment of the function
*/
force_assert(lua_isfunction(L, -1));
lua_getfenv(L, -1); /* -1 is the function */
lua_getfield(L, -1, "lighty"); /* lighty.* from the env */
force_assert(lua_istable(L, -1));
lua_getfield(L, -1, "content"); /* lighty.content */
if (lua_istable(L, -1)) {
int i;
/* header is found, and is a table */
for (i = 1; ; i++) {
lua_rawgeti(L, -1, i);
/* -1 is the value and should be the value ... aka a table */
if (lua_isstring(L, -1)) {
size_t s_len = 0;
const char *s = lua_tolstring(L, -1, &s_len);
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 <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
chunkqueue_append_mem(con->write_queue, s, s_len);
} else if (lua_istable(L, -1)) {
lua_getfield(L, -1, "filename");
lua_getfield(L, -2, "length");
lua_getfield(L, -3, "offset");
if (lua_isstring(L, -3)) { /* filename has to be a string */
buffer *fn;
stat_cache_entry *sce;
const char *fn_str;
handler_t res;
fn_str = lua_tostring(L, -3);
fn = buffer_init_string(fn_str);
res = stat_cache_get_entry(srv, con, fn, &sce);
if (HANDLER_GO_ON == res) {
off_t off = 0;
off_t len = 0;