--- embedaddon/lighttpd/src/mod_cml_funcs.c 2013/10/14 10:32:47 1.1.1.1 +++ embedaddon/lighttpd/src/mod_cml_funcs.c 2016/11/02 10:35:00 1.1.1.2 @@ -1,3 +1,5 @@ +#include "first.h" + #include "buffer.h" #include "server.h" #include "log.h" @@ -32,17 +34,16 @@ typedef char HASHHEX[HASHHEXLEN+1]; #ifdef HAVE_LUA_H +#include + int f_crypto_md5(lua_State *L) { li_MD5_CTX Md5Ctx; HASH HA1; - buffer b; char hex[33]; int n = lua_gettop(L); + size_t s_len; + const char *s; - b.ptr = hex; - b.used = 0; - b.size = sizeof(hex); - if (n != 1) { lua_pushstring(L, "md5: expected one argument"); lua_error(L); @@ -53,13 +54,15 @@ int f_crypto_md5(lua_State *L) { lua_error(L); } + s = lua_tolstring(L, 1, &s_len); + li_MD5_Init(&Md5Ctx); - li_MD5_Update(&Md5Ctx, (unsigned char *)lua_tostring(L, 1), lua_strlen(L, 1)); + li_MD5_Update(&Md5Ctx, (unsigned char *) s, (unsigned int) s_len); li_MD5_Final(HA1, &Md5Ctx); - buffer_copy_string_hex(&b, (char *)HA1, 16); + li_tohex(hex, sizeof(hex), (const char*) HA1, 16); - lua_pushstring(L, b.ptr); + lua_pushstring(L, hex); return 1; } @@ -84,7 +87,7 @@ int f_file_mtime(lua_State *L) { return 1; } - lua_pushnumber(L, st.st_mtime); + lua_pushinteger(L, st.st_mtime); return 1; } @@ -126,7 +129,7 @@ int f_dir_files(lua_State *L) { return 1; } - /* push d into registry */ + /* push d into userdata */ lua_pushlightuserdata(L, d); lua_pushcclosure(L, f_dir_files_iter, 1); @@ -152,7 +155,7 @@ int f_file_isreg(lua_State *L) { return 1; } - lua_pushnumber(L, S_ISREG(st.st_mode)); + lua_pushinteger(L, S_ISREG(st.st_mode)); return 1; } @@ -176,119 +179,102 @@ int f_file_isdir(lua_State *L) { return 1; } - lua_pushnumber(L, S_ISDIR(st.st_mode)); + lua_pushinteger(L, S_ISDIR(st.st_mode)); return 1; } -#ifdef HAVE_MEMCACHE_H +#ifdef USE_MEMCACHED int f_memcache_exists(lua_State *L) { - char *r; - int n = lua_gettop(L); - struct memcache *mc; + size_t key_len; + const char *key; + memcached_st *memc; if (!lua_islightuserdata(L, lua_upvalueindex(1))) { lua_pushstring(L, "where is my userdata ?"); lua_error(L); } - mc = lua_touserdata(L, lua_upvalueindex(1)); + memc = lua_touserdata(L, lua_upvalueindex(1)); - if (n != 1) { + if (1 != lua_gettop(L)) { lua_pushstring(L, "expected one argument"); lua_error(L); } - if (!lua_isstring(L, 1)) { - lua_pushstring(L, "argument has to be a string"); - lua_error(L); - } - - if (NULL == (r = mc_aget(mc, - (char*) lua_tostring(L, 1), lua_strlen(L, 1)))) { - - lua_pushboolean(L, 0); - return 1; - } - - free(r); - - lua_pushboolean(L, 1); + key = luaL_checklstring(L, 1, &key_len); + lua_pushboolean(L, (MEMCACHED_SUCCESS == memcached_exist(memc, key, key_len))); return 1; } int f_memcache_get_string(lua_State *L) { - char *r; - int n = lua_gettop(L); + size_t key_len, value_len; + char *value; + const char *key; + memcached_st *memc; - struct memcache *mc; - if (!lua_islightuserdata(L, lua_upvalueindex(1))) { lua_pushstring(L, "where is my userdata ?"); lua_error(L); } - mc = lua_touserdata(L, lua_upvalueindex(1)); + memc = lua_touserdata(L, lua_upvalueindex(1)); - - if (n != 1) { + if (1 != lua_gettop(L)) { lua_pushstring(L, "expected one argument"); lua_error(L); } - if (!lua_isstring(L, 1)) { - lua_pushstring(L, "argument has to be a string"); - lua_error(L); - } - - if (NULL == (r = mc_aget(mc, - (char*) lua_tostring(L, 1), lua_strlen(L, 1)))) { + key = luaL_checklstring(L, 1, &key_len); + if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) { lua_pushnil(L); return 1; } - lua_pushstring(L, r); + lua_pushlstring(L, value, value_len); - free(r); + free(value); return 1; } int f_memcache_get_long(lua_State *L) { - char *r; - int n = lua_gettop(L); + size_t key_len, value_len; + char *value; + const char *key; + memcached_st *memc; + char *endptr; + long v; - struct memcache *mc; - if (!lua_islightuserdata(L, lua_upvalueindex(1))) { lua_pushstring(L, "where is my userdata ?"); lua_error(L); } - mc = lua_touserdata(L, lua_upvalueindex(1)); + memc = lua_touserdata(L, lua_upvalueindex(1)); - - if (n != 1) { + if (1 != lua_gettop(L)) { lua_pushstring(L, "expected one argument"); lua_error(L); } - if (!lua_isstring(L, 1)) { - lua_pushstring(L, "argument has to be a string"); - lua_error(L); + key = luaL_checklstring(L, 1, &key_len); + if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) { + lua_pushnil(L); + return 1; } - if (NULL == (r = mc_aget(mc, - (char*) lua_tostring(L, 1), lua_strlen(L, 1)))) { + errno = 0; + v = strtol(value, &endptr, 10); + if (0 == errno && *endptr == '\0') { + lua_pushinteger(L, v); + } else { lua_pushnil(L); - return 1; } - lua_pushnumber(L, strtol(r, NULL, 10)); - - free(r); + free(value); return 1; }