Esempio n. 1
0
static int magnet_env_set(lua_State *L) {
	server *srv = magnet_get_server(L);
	connection *con = magnet_get_connection(L);

	/* __newindex: param 1 is the (empty) table the value is supposed to be set in */
	const char *key = luaL_checkstring(L, 2);
	buffer *dest = NULL;

	luaL_checkany(L, 3); /* nil or a string */

	if (NULL != (dest = magnet_env_get_buffer(srv, con, key))) {
		if (lua_isnil(L, 3)) {
			buffer_reset(dest);
		} else {
			const_buffer val = magnet_checkconstbuffer(L, 3);
			buffer_copy_string_len(dest, val.ptr, val.len);
		}
	} else {
		/* couldn't save */

		return luaL_error(L, "couldn't store '%s' in lighty.env[]", key);
	}

	return 0;
}
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_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 (dest && dest->used) {
		lua_pushlstring(L, dest->ptr, dest->used - 1);
	} else {
		lua_pushnil(L);
	}

	return 1;
}
Esempio n. 4
0
static int magnet_env_get(lua_State *L) {
	server *srv = magnet_get_server(L);
	connection *con = magnet_get_connection(L);

	/* __index: param 1 is the (empty) table the value was not found in */
	const char *key = luaL_checkstring(L, 2);
	buffer *dest = NULL;

	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;
}