2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2006-09-10 22:01:43 +00:00
|
|
|
#include "base.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
2016-03-30 10:39:33 +00:00
|
|
|
#include "http_chunk.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2006-09-10 22:01:43 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#include "mod_magnet_cache.h"
|
2019-05-14 05:00:29 +00:00
|
|
|
#include "sock_addr.h"
|
2006-09-10 22:01:43 +00:00
|
|
|
#include "stat_cache.h"
|
2006-09-14 12:00:32 +00:00
|
|
|
#include "status_counter.h"
|
2006-10-06 08:27:50 +00:00
|
|
|
#include "etag.h"
|
2006-09-10 22:01:43 +00:00
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
2006-09-15 11:49:57 +00:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
2006-09-10 22:01:43 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
#define LUA_RIDX_LIGHTTPD_REQUEST "lighty.request"
|
2016-01-03 14:48:09 +00:00
|
|
|
|
2006-09-15 10:00:46 +00:00
|
|
|
#define MAGNET_RESTART_REQUEST 99
|
2006-09-10 22:01:43 +00:00
|
|
|
|
|
|
|
/* plugin config for all request/connections */
|
|
|
|
|
2006-09-20 14:40:28 +00:00
|
|
|
static jmp_buf exceptionjmp;
|
|
|
|
|
2006-09-10 22:01:43 +00:00
|
|
|
typedef struct {
|
2019-10-26 06:00:09 +00:00
|
|
|
const array *url_raw;
|
|
|
|
const array *physical_path;
|
2006-09-10 22:01:43 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-26 06:00:09 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
script_cache cache;
|
2006-09-10 22:01:43 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_magnet_init) {
|
2019-10-26 06:00:09 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2006-09-10 22:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FREE_FUNC(mod_magnet_free) {
|
2019-10-26 06:00:09 +00:00
|
|
|
plugin_data *p = p_d;
|
|
|
|
script_cache_free_data(&p->cache);
|
2006-09-10 22:01:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
static void mod_magnet_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* magnet.attract-raw-url-to */
|
|
|
|
pconf->url_raw = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 1: /* magnet.attract-physical-path-to */
|
|
|
|
pconf->physical_path = cpv->v.a;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
2006-09-10 22:01:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
static void mod_magnet_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_magnet_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2019-10-17 05:27:52 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_magnet_patch_config(request_st * const r, plugin_data * const p) {
|
2020-01-11 16:07:43 +00:00
|
|
|
p->conf = p->defaults; /* copy small struct instead of memcpy() */
|
|
|
|
/*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
|
2019-10-26 06:00:09 +00:00
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-10-26 06:00:09 +00:00
|
|
|
mod_magnet_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_magnet_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("magnet.attract-raw-url-to"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-26 06:00:09 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("magnet.attract-physical-path-to"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-26 06:00:09 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_magnet"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* process and validate config directives
|
|
|
|
* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* magnet.attract-raw-url-to */
|
|
|
|
case 1: /* magnet.attract-physical-path-to */
|
2019-11-26 07:13:05 +00:00
|
|
|
for (uint32_t j = 0; j < cpv->v.a->used; ++j) {
|
|
|
|
data_string *ds = (data_string *)cpv->v.a->data[j];
|
|
|
|
if (buffer_string_is_empty(&ds->value)) {
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__,
|
|
|
|
"unexpected (blank) value for %s; "
|
|
|
|
"expected list of \"scriptpath\"", cpk[cpv->k_id].k);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 06:00:09 +00:00
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
/* initialize p->defaults from global config context */
|
|
|
|
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
|
|
|
if (-1 != cpv->k_id)
|
|
|
|
mod_magnet_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
return HANDLER_GO_ON;
|
2006-09-10 22:01:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 13:16:31 +00:00
|
|
|
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
|
|
|
|
/* lua5.1 backward compat definition */
|
|
|
|
static void lua_pushglobaltable(lua_State *L) { /* (-0, +1, -) */
|
2016-01-03 14:48:09 +00:00
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
|
|
|
}
|
2016-03-19 13:16:31 +00:00
|
|
|
#endif
|
2016-01-03 14:48:09 +00:00
|
|
|
|
|
|
|
static void magnet_setfenv_mainfn(lua_State *L, int funcIndex) { /* (-1, 0, -) */
|
|
|
|
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
|
|
|
|
/* set "_ENV" upvalue, which should be the first upvalue of a "main" lua
|
|
|
|
* function if it uses any global names
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char* first_upvalue_name = lua_getupvalue(L, funcIndex, 1);
|
|
|
|
if (NULL == first_upvalue_name) return; /* doesn't have any upvalues */
|
|
|
|
lua_pop(L, 1); /* only need the name of the upvalue, not the value */
|
|
|
|
|
|
|
|
if (0 != strcmp(first_upvalue_name, "_ENV")) return;
|
|
|
|
|
|
|
|
if (NULL == lua_setupvalue(L, funcIndex, 1)) {
|
|
|
|
/* pop value if lua_setupvalue didn't set the (not existing) upvalue */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
lua_setfenv(L, funcIndex);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
|
|
|
|
/* lua 5.2 already supports __pairs */
|
2009-10-12 19:49:43 +00:00
|
|
|
|
2016-01-03 14:48:09 +00:00
|
|
|
/* See http://lua-users.org/wiki/GeneralizedPairsAndIpairs for implementation details.
|
|
|
|
* Override the default pairs() function to allow us to use a __pairs metakey
|
|
|
|
*/
|
2009-10-12 19:49:43 +00:00
|
|
|
static int magnet_pairs(lua_State *L) {
|
2016-01-03 14:48:09 +00:00
|
|
|
luaL_checkany(L, 1); /* "self" */
|
2009-10-12 19:49:43 +00:00
|
|
|
|
|
|
|
if (luaL_getmetafield(L, 1, "__pairs")) {
|
2016-01-03 14:48:09 +00:00
|
|
|
/* call __pairs(self) */
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_call(L, 1, 3);
|
2009-10-12 19:49:43 +00:00
|
|
|
} else {
|
2016-01-03 14:48:09 +00:00
|
|
|
/* call <original-pairs-method>(self) */
|
2009-10-12 19:49:43 +00:00
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
2016-01-03 14:48:09 +00:00
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_call(L, 1, 3);
|
2009-10-12 19:49:43 +00:00
|
|
|
}
|
2016-01-03 14:48:09 +00:00
|
|
|
return 3;
|
2009-10-12 19:49:43 +00:00
|
|
|
}
|
2016-01-03 14:48:09 +00:00
|
|
|
#endif
|
2009-10-12 19:49:43 +00:00
|
|
|
|
2018-04-07 18:27:07 +00:00
|
|
|
static void magnet_push_buffer(lua_State *L, const buffer *b) {
|
|
|
|
if (!buffer_is_empty(b))
|
|
|
|
lua_pushlstring(L, CONST_BUF_LEN(b));
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
#if 0
|
2018-04-07 18:27:07 +00:00
|
|
|
static int magnet_array_get_element(lua_State *L, const array *a) {
|
|
|
|
/* __index: param 1 is the (empty) table the value was not found in */
|
|
|
|
size_t klen;
|
|
|
|
const char * const k = luaL_checklstring(L, 2, &klen);
|
2019-10-10 03:24:25 +00:00
|
|
|
const data_string * const ds = (const data_string *)
|
|
|
|
array_get_element_klen(a, k, klen);
|
2019-10-13 08:59:57 +00:00
|
|
|
magnet_push_buffer(L, NULL != ds ? &ds->value : NULL);
|
2018-04-07 18:27:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-09-09 05:50:33 +00:00
|
|
|
#endif
|
2018-04-07 18:27:07 +00:00
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
/* 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])) {
|
2019-10-13 07:37:59 +00:00
|
|
|
lua_pushlstring(L, CONST_BUF_LEN(&du->key));
|
2009-10-12 19:49:43 +00:00
|
|
|
switch (du->type) {
|
|
|
|
case TYPE_STRING:
|
|
|
|
ds = (data_string *)du;
|
2019-10-13 08:59:57 +00:00
|
|
|
magnet_push_buffer(L, &ds->value);
|
2009-10-12 19:49:43 +00:00
|
|
|
break;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static request_st * magnet_get_request(lua_State *L) {
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, LUA_RIDX_LIGHTTPD_REQUEST);
|
|
|
|
request_st * const r = lua_touserdata(L, -1);
|
2016-01-03 14:48:09 +00:00
|
|
|
lua_pop(L, 1);
|
2020-01-13 02:51:12 +00:00
|
|
|
return r;
|
2016-01-03 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *ptr;
|
|
|
|
size_t len;
|
|
|
|
} const_buffer;
|
|
|
|
|
2020-02-24 23:48:03 +00:00
|
|
|
static const_buffer magnet_checkconstbuffer(lua_State *L, int idx) {
|
2016-01-03 14:48:09 +00:00
|
|
|
const_buffer cb;
|
2020-02-24 23:48:03 +00:00
|
|
|
cb.ptr = luaL_checklstring(L, idx, &cb.len);
|
2016-01-03 14:48:09 +00:00
|
|
|
return cb;
|
|
|
|
}
|
|
|
|
|
2020-02-24 23:48:03 +00:00
|
|
|
static buffer* magnet_checkbuffer(lua_State *L, int idx) {
|
|
|
|
const_buffer cb = magnet_checkconstbuffer(L, idx);
|
2016-01-03 14:48:09 +00:00
|
|
|
buffer *b = buffer_init();
|
|
|
|
buffer_copy_string_len(b, cb.ptr, cb.len);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int magnet_print(lua_State *L) {
|
2018-04-07 20:09:09 +00:00
|
|
|
const_buffer cb = magnet_checkconstbuffer(L, 1);
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__, "(lua-print) %s", cb.ptr);
|
2006-09-10 22:01:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-06 08:27:50 +00:00
|
|
|
static int magnet_stat(lua_State *L) {
|
2019-12-05 08:16:25 +00:00
|
|
|
buffer * const sb = magnet_checkbuffer(L, 1);
|
|
|
|
stat_cache_entry * const sce = stat_cache_get_entry(sb);
|
|
|
|
buffer_free(sb);
|
|
|
|
if (NULL == sce) {
|
2015-02-08 19:10:44 +00:00
|
|
|
lua_pushnil(L);
|
2006-10-06 08:27:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-03 14:48:09 +00:00
|
|
|
lua_newtable(L); // return value
|
2006-10-06 11:21:00 +00:00
|
|
|
|
|
|
|
lua_pushboolean(L, S_ISREG(sce->st.st_mode));
|
|
|
|
lua_setfield(L, -2, "is_file");
|
2016-01-03 14:48:09 +00:00
|
|
|
|
2006-10-06 11:21:00 +00:00
|
|
|
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");
|
|
|
|
|
2006-10-06 08:27:50 +00:00
|
|
|
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");
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
const buffer *etag = stat_cache_etag_get(sce, r->conf.etag_flags);
|
2019-12-05 08:16:25 +00:00
|
|
|
if (!buffer_string_is_empty(etag)) {
|
2006-10-07 07:46:22 +00:00
|
|
|
/* we have to mutate the etag */
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer * const tb = r->tmp_buf;
|
2019-12-05 08:16:25 +00:00
|
|
|
etag_mutate(tb, etag);
|
2019-11-26 07:13:05 +00:00
|
|
|
lua_pushlstring(L, CONST_BUF_LEN(tb));
|
2006-10-07 07:46:22 +00:00
|
|
|
} else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
2006-10-06 08:27:50 +00:00
|
|
|
lua_setfield(L, -2, "etag");
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
const buffer *content_type = stat_cache_content_type_get(sce, r);
|
2019-12-05 08:16:25 +00:00
|
|
|
if (!buffer_string_is_empty(content_type)) {
|
|
|
|
lua_pushlstring(L, CONST_BUF_LEN(content_type));
|
2006-10-06 11:21:00 +00:00
|
|
|
} else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
lua_setfield(L, -2, "content-type");
|
|
|
|
|
2006-10-06 08:27:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-20 14:40:28 +00:00
|
|
|
static int magnet_atpanic(lua_State *L) {
|
2018-04-07 20:09:09 +00:00
|
|
|
const_buffer cb = magnet_checkconstbuffer(L, 1);
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__, "(lua-atpanic) %s", cb.ptr);
|
2006-09-20 14:40:28 +00:00
|
|
|
longjmp(exceptionjmp, 1);
|
|
|
|
}
|
|
|
|
|
2006-09-11 20:37:29 +00:00
|
|
|
static int magnet_reqhdr_get(lua_State *L) {
|
2018-09-09 05:50:33 +00:00
|
|
|
/* __index: param 1 is the (empty) table the value was not found in */
|
|
|
|
size_t klen;
|
|
|
|
const char * const k = luaL_checklstring(L, 2, &klen);
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
2019-10-13 17:57:25 +00:00
|
|
|
const buffer * const vb =
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_request_get(r, HTTP_HEADER_UNSPECIFIED, k, klen);
|
2018-09-09 05:50:33 +00:00
|
|
|
magnet_push_buffer(L, NULL != vb ? vb : NULL);
|
|
|
|
return 1;
|
2006-09-11 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
static int magnet_reqhdr_pairs(lua_State *L) {
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
return magnet_array_pairs(L, &r->rqst_headers);
|
2009-10-12 19:49:43 +00:00
|
|
|
}
|
|
|
|
|
2006-09-14 12:00:32 +00:00
|
|
|
static int magnet_status_get(lua_State *L) {
|
2016-01-03 14:48:09 +00:00
|
|
|
/* __index: param 1 is the (empty) table the value was not found in */
|
|
|
|
const_buffer key = magnet_checkconstbuffer(L, 2);
|
2019-11-28 15:27:01 +00:00
|
|
|
int *i = status_counter_get_counter(key.ptr, key.len);
|
2018-09-08 18:23:23 +00:00
|
|
|
lua_pushinteger(L, (lua_Integer)*i);
|
2006-09-14 12:00:32 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int magnet_status_set(lua_State *L) {
|
2016-01-03 14:48:09 +00:00
|
|
|
/* __newindex: param 1 is the (empty) table the value is supposed to be set in */
|
|
|
|
const_buffer key = magnet_checkconstbuffer(L, 2);
|
|
|
|
int counter = (int) luaL_checkinteger(L, 3);
|
2006-09-14 12:00:32 +00:00
|
|
|
|
2019-11-28 15:27:01 +00:00
|
|
|
status_counter_set(key.ptr, key.len, counter);
|
2006-09-14 12:00:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
static int magnet_status_pairs(lua_State *L) {
|
2019-11-28 15:27:01 +00:00
|
|
|
return magnet_array_pairs(L, &plugin_stats);
|
2009-10-12 19:49:43 +00:00
|
|
|
}
|
|
|
|
|
2006-09-13 13:13:27 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
2006-10-04 13:26:23 +00:00
|
|
|
enum {
|
2006-09-13 13:13:27 +00:00
|
|
|
MAGNET_ENV_UNSET,
|
|
|
|
|
2020-01-20 15:34:40 +00:00
|
|
|
MAGNET_ENV_PHYSICAL_PATH,
|
|
|
|
MAGNET_ENV_PHYSICAL_REL_PATH,
|
|
|
|
MAGNET_ENV_PHYSICAL_DOC_ROOT,
|
|
|
|
MAGNET_ENV_PHYSICAL_BASEDIR,
|
2006-09-13 13:13:27 +00:00
|
|
|
|
|
|
|
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,
|
2006-10-06 07:14:10 +00:00
|
|
|
MAGNET_ENV_REQUEST_ORIG_URI,
|
2009-04-15 22:33:36 +00:00
|
|
|
MAGNET_ENV_REQUEST_PATH_INFO,
|
2009-04-15 22:33:34 +00:00
|
|
|
MAGNET_ENV_REQUEST_REMOTE_IP,
|
2019-05-14 05:00:29 +00:00
|
|
|
MAGNET_ENV_REQUEST_SERVER_ADDR,
|
2006-09-13 13:13:27 +00:00
|
|
|
MAGNET_ENV_REQUEST_PROTOCOL
|
2009-10-12 19:49:43 +00:00
|
|
|
} type;
|
2006-09-13 13:13:27 +00:00
|
|
|
} magnet_env_t;
|
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
static const magnet_env_t magnet_env[] = {
|
2020-01-20 15:34:40 +00:00
|
|
|
{ "physical.path", MAGNET_ENV_PHYSICAL_PATH },
|
|
|
|
{ "physical.rel-path", MAGNET_ENV_PHYSICAL_REL_PATH },
|
|
|
|
{ "physical.doc-root", MAGNET_ENV_PHYSICAL_DOC_ROOT },
|
|
|
|
{ "physical.basedir", MAGNET_ENV_PHYSICAL_BASEDIR },
|
2006-09-10 22:01:43 +00:00
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
{ "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 },
|
2019-05-14 05:00:29 +00:00
|
|
|
{ "request.remote-addr", MAGNET_ENV_REQUEST_REMOTE_IP },
|
|
|
|
{ "request.server-addr", MAGNET_ENV_REQUEST_SERVER_ADDR },
|
2009-10-12 19:49:43 +00:00
|
|
|
{ "request.protocol", MAGNET_ENV_REQUEST_PROTOCOL },
|
|
|
|
|
|
|
|
{ NULL, MAGNET_ENV_UNSET }
|
|
|
|
};
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static buffer *magnet_env_get_buffer_by_id(request_st * const r, int id) {
|
2009-10-12 19:49:43 +00:00
|
|
|
buffer *dest = NULL;
|
2006-09-10 22:01:43 +00:00
|
|
|
|
2006-09-13 13:13:27 +00:00
|
|
|
/**
|
2006-10-04 13:26:23 +00:00
|
|
|
* map all internal variables to lua
|
2006-09-13 13:13:27 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
switch (id) {
|
2020-01-20 15:34:40 +00:00
|
|
|
case MAGNET_ENV_PHYSICAL_PATH: dest = &r->physical.path; break;
|
|
|
|
case MAGNET_ENV_PHYSICAL_REL_PATH: dest = &r->physical.rel_path; break;
|
|
|
|
case MAGNET_ENV_PHYSICAL_DOC_ROOT: dest = &r->physical.doc_root; break;
|
|
|
|
case MAGNET_ENV_PHYSICAL_BASEDIR: dest = &r->physical.basedir; break;
|
2006-09-13 13:13:27 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
case MAGNET_ENV_URI_PATH: dest = &r->uri.path; break;
|
2020-01-20 15:34:40 +00:00
|
|
|
case MAGNET_ENV_URI_PATH_RAW:
|
|
|
|
{
|
|
|
|
dest = r->tmp_buf;
|
|
|
|
buffer_clear(dest);
|
|
|
|
uint32_t len = buffer_string_length(&r->target);
|
|
|
|
char *qmark = memchr(r->target.ptr, '?', len);
|
|
|
|
buffer_copy_string_len(dest, r->target.ptr, qmark ? (uint32_t)(qmark - r->target.ptr) : len);
|
|
|
|
break;
|
|
|
|
}
|
2020-01-13 02:51:12 +00:00
|
|
|
case MAGNET_ENV_URI_SCHEME: dest = &r->uri.scheme; break;
|
|
|
|
case MAGNET_ENV_URI_AUTHORITY: dest = &r->uri.authority; break;
|
|
|
|
case MAGNET_ENV_URI_QUERY: dest = &r->uri.query; break;
|
2006-09-13 13:13:27 +00:00
|
|
|
|
2008-03-26 13:51:33 +00:00
|
|
|
case MAGNET_ENV_REQUEST_METHOD:
|
2020-01-13 02:51:12 +00:00
|
|
|
dest = r->tmp_buf;
|
2019-11-26 07:13:05 +00:00
|
|
|
buffer_clear(dest);
|
2020-01-13 02:51:12 +00:00
|
|
|
http_method_append(dest, r->http_method);
|
2008-03-26 13:51:33 +00:00
|
|
|
break;
|
2020-01-13 02:51:12 +00:00
|
|
|
case MAGNET_ENV_REQUEST_URI: dest = &r->target; break;
|
|
|
|
case MAGNET_ENV_REQUEST_ORIG_URI: dest = &r->target_orig; break;
|
|
|
|
case MAGNET_ENV_REQUEST_PATH_INFO: dest = &r->pathinfo; break;
|
|
|
|
case MAGNET_ENV_REQUEST_REMOTE_IP: dest = r->con->dst_addr_buf; break;
|
2019-05-14 05:00:29 +00:00
|
|
|
case MAGNET_ENV_REQUEST_SERVER_ADDR:
|
2020-01-13 02:51:12 +00:00
|
|
|
{
|
|
|
|
const server_socket * const srv_socket = r->con->srv_socket;
|
|
|
|
dest = r->tmp_buf;
|
2019-05-14 05:00:29 +00:00
|
|
|
buffer_clear(dest);
|
2020-01-13 02:51:12 +00:00
|
|
|
switch (srv_socket->addr.plain.sa_family) {
|
2019-05-14 05:00:29 +00:00
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6:
|
2020-01-13 02:51:12 +00:00
|
|
|
if (sock_addr_is_addr_wildcard(&srv_socket->addr)) {
|
2019-05-14 05:00:29 +00:00
|
|
|
sock_addr addrbuf;
|
|
|
|
socklen_t addrlen = sizeof(addrbuf);
|
2020-01-13 02:51:12 +00:00
|
|
|
const int fd = r->con->fd;
|
|
|
|
if (0 == getsockname(fd,(struct sockaddr *)&addrbuf,&addrlen)) {
|
2019-05-14 05:00:29 +00:00
|
|
|
char buf[INET6_ADDRSTRLEN + 1];
|
|
|
|
const char *s = sock_addr_inet_ntop(&addrbuf, buf, sizeof(buf)-1);
|
|
|
|
if (NULL != s)
|
|
|
|
buffer_copy_string_len(dest, s, strlen(s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_buffer(dest, srv_socket->srv_token);
|
2019-05-14 05:00:29 +00:00
|
|
|
if (dest->ptr[0] != '[' || dest->ptr[buffer_string_length(dest)-1] != ']') {
|
|
|
|
char *s = strrchr(dest->ptr, ':');
|
|
|
|
if (s != NULL) /* local IP without port */
|
|
|
|
buffer_string_set_length(dest, s - dest->ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2020-01-13 02:51:12 +00:00
|
|
|
}
|
2008-03-26 13:51:33 +00:00
|
|
|
case MAGNET_ENV_REQUEST_PROTOCOL:
|
2020-01-13 02:51:12 +00:00
|
|
|
dest = r->tmp_buf;
|
|
|
|
buffer_copy_string(dest, get_http_version_name(r->http_version));
|
2008-03-26 13:51:33 +00:00
|
|
|
break;
|
2006-09-13 13:13:27 +00:00
|
|
|
|
|
|
|
case MAGNET_ENV_UNSET: break;
|
|
|
|
}
|
|
|
|
|
2006-09-15 10:00:46 +00:00
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2020-01-20 15:34:40 +00:00
|
|
|
static int magnet_env_get_id(const char * const key) {
|
2020-01-13 02:51:12 +00:00
|
|
|
for (int i = 0; magnet_env[i].name; ++i) {
|
|
|
|
if (0 == strcmp(key, magnet_env[i].name))
|
2020-01-20 15:34:40 +00:00
|
|
|
return magnet_env[i].type;
|
2009-10-12 19:49:43 +00:00
|
|
|
}
|
2020-01-20 15:34:40 +00:00
|
|
|
return MAGNET_ENV_UNSET;
|
|
|
|
}
|
|
|
|
|
|
|
|
static buffer *magnet_env_get_buffer(request_st * const r, const char * const key) {
|
|
|
|
return magnet_env_get_buffer_by_id(r, magnet_env_get_id(key));
|
2009-10-12 19:49:43 +00:00
|
|
|
}
|
|
|
|
|
2006-09-15 10:00:46 +00:00
|
|
|
static int magnet_env_get(lua_State *L) {
|
2016-01-03 14:48:09 +00:00
|
|
|
/* __index: param 1 is the (empty) table the value was not found in */
|
2006-09-15 10:00:46 +00:00
|
|
|
const char *key = luaL_checkstring(L, 2);
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
magnet_push_buffer(L, magnet_env_get_buffer(r, key));
|
2006-09-13 13:13:27 +00:00
|
|
|
return 1;
|
2006-09-10 22:01:43 +00:00
|
|
|
}
|
2006-09-13 13:13:27 +00:00
|
|
|
|
2006-09-15 10:00:46 +00:00
|
|
|
static int magnet_env_set(lua_State *L) {
|
2020-01-20 15:34:40 +00:00
|
|
|
/* __newindex: param 1 is the (empty) table the value is supposed to be set in */
|
|
|
|
const char * const key = luaL_checkstring(L, 2);
|
|
|
|
luaL_checkany(L, 3); /* nil or a string */
|
2006-09-15 10:00:46 +00:00
|
|
|
|
2020-01-20 15:34:40 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
const int env_id = magnet_env_get_id(key);
|
|
|
|
|
|
|
|
if (env_id == MAGNET_ENV_URI_PATH_RAW) {
|
|
|
|
/* modify uri-path of r->target; preserve query-part, if present */
|
|
|
|
/* XXX: should we require that resulting path begin with '/' or %2F ? */
|
|
|
|
const uint32_t len = buffer_string_length(&r->target);
|
|
|
|
const char * const qmark = memchr(r->target.ptr, '?', len);
|
|
|
|
const_buffer val = { NULL, 0 };
|
|
|
|
if (!lua_isnil(L, 3))
|
|
|
|
val = magnet_checkconstbuffer(L, 3);
|
|
|
|
if (NULL != qmark)
|
|
|
|
buffer_copy_string_len(r->tmp_buf, qmark,
|
|
|
|
len - (uint32_t)(qmark - r->target.ptr));
|
|
|
|
buffer_copy_string_len(&r->target, val.ptr, val.len);
|
|
|
|
if (NULL != qmark)
|
|
|
|
buffer_append_string_buffer(&r->target, r->tmp_buf);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-09-15 10:00:46 +00:00
|
|
|
|
2020-01-20 15:34:40 +00:00
|
|
|
buffer * const dest = magnet_env_get_buffer_by_id(r, env_id);
|
|
|
|
if (NULL == dest)
|
|
|
|
return luaL_error(L, "couldn't store '%s' in lighty.env[]", key);
|
2006-09-15 10:00:46 +00:00
|
|
|
|
2020-01-20 15:34:40 +00:00
|
|
|
if (lua_isnil(L, 3)) {
|
|
|
|
if (env_id==MAGNET_ENV_URI_QUERY || env_id==MAGNET_ENV_PHYSICAL_PATH)
|
|
|
|
buffer_clear(dest);
|
|
|
|
else
|
|
|
|
buffer_string_set_length(dest, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const_buffer val = magnet_checkconstbuffer(L, 3);
|
|
|
|
buffer_copy_string_len(dest, val.ptr, val.len);
|
|
|
|
/* NB: setting r->uri.query does not modify query-part in r->target
|
|
|
|
* (r->uri.query is uri-decoded; r->target is not) */
|
|
|
|
}
|
2006-09-15 10:00:46 +00:00
|
|
|
|
2020-01-20 15:34:40 +00:00
|
|
|
switch (env_id) {
|
|
|
|
case MAGNET_ENV_URI_SCHEME:
|
|
|
|
case MAGNET_ENV_URI_AUTHORITY:
|
|
|
|
buffer_to_lower(dest);
|
|
|
|
if (env_id == MAGNET_ENV_URI_AUTHORITY)
|
|
|
|
r->server_name = dest;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2006-09-15 10:00:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
static int magnet_env_next(lua_State *L) {
|
2016-01-03 14:48:09 +00:00
|
|
|
const int pos = lua_tointeger(L, lua_upvalueindex(1));
|
2009-10-12 19:49:43 +00:00
|
|
|
|
2016-01-03 14:48:09 +00:00
|
|
|
/* ignore previous key: use upvalue for current pos */
|
2009-10-12 19:49:43 +00:00
|
|
|
lua_settop(L, 0);
|
|
|
|
|
|
|
|
if (NULL == magnet_env[pos].name) return 0; /* end of list */
|
2016-01-03 14:48:09 +00:00
|
|
|
/* Update our positional upval to reflect our new current position */
|
|
|
|
lua_pushinteger(L, pos + 1);
|
|
|
|
lua_replace(L, lua_upvalueindex(1));
|
2009-10-12 19:49:43 +00:00
|
|
|
|
2016-01-03 14:48:09 +00:00
|
|
|
/* key to return */
|
2009-10-12 19:49:43 +00:00
|
|
|
lua_pushstring(L, magnet_env[pos].name);
|
|
|
|
|
2016-01-03 14:48:09 +00:00
|
|
|
/* get value */
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
magnet_push_buffer(L, magnet_env_get_buffer_by_id(r, magnet_env[pos].type));
|
2009-10-12 19:49:43 +00:00
|
|
|
|
2016-01-03 14:48:09 +00:00
|
|
|
/* return 2 items on the stack (key, value) */
|
2009-10-12 19:49:43 +00:00
|
|
|
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;
|
|
|
|
}
|
2006-09-15 10:00:46 +00:00
|
|
|
|
2009-06-10 13:08:15 +00:00
|
|
|
static int magnet_cgi_get(lua_State *L) {
|
2018-09-09 05:50:33 +00:00
|
|
|
/* __index: param 1 is the (empty) table the value was not found in */
|
|
|
|
size_t klen;
|
|
|
|
const char * const k = luaL_checklstring(L, 2, &klen);
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
const buffer * const vb = http_header_env_get(r, k, klen);
|
2018-09-09 05:50:33 +00:00
|
|
|
magnet_push_buffer(L, NULL != vb ? vb : NULL);
|
|
|
|
return 1;
|
2009-06-10 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int magnet_cgi_set(lua_State *L) {
|
2018-09-09 05:50:33 +00:00
|
|
|
/* __newindex: param 1 is the (empty) table the value is supposed to be set in */
|
|
|
|
const_buffer key = magnet_checkconstbuffer(L, 2);
|
|
|
|
const_buffer val = magnet_checkconstbuffer(L, 3);
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
http_header_env_set(r, key.ptr, key.len, val.ptr, val.len);
|
2018-09-09 05:50:33 +00:00
|
|
|
return 0;
|
2009-06-10 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 19:49:43 +00:00
|
|
|
static int magnet_cgi_pairs(lua_State *L) {
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = magnet_get_request(L);
|
|
|
|
return magnet_array_pairs(L, &r->env);
|
2009-10-12 19:49:43 +00:00
|
|
|
}
|
|
|
|
|
2009-06-10 13:08:15 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static int magnet_copy_response_header(request_st * const r, lua_State * const L, int lighty_table_ndx) {
|
2016-01-03 14:48:09 +00:00
|
|
|
force_assert(lua_istable(L, lighty_table_ndx));
|
2006-09-13 13:13:27 +00:00
|