File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / lighttpd / src / mod_cml_funcs.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Nov 2 10:35:00 2016 UTC (7 years, 8 months ago) by misho
Branches: lighttpd, MAIN
CVS tags: v1_4_41p8, HEAD
lighttpd 1.4.41

    1: #include "first.h"
    2: 
    3: #include "buffer.h"
    4: #include "server.h"
    5: #include "log.h"
    6: #include "plugin.h"
    7: #include "response.h"
    8: 
    9: #include "mod_cml.h"
   10: #include "mod_cml_funcs.h"
   11: 
   12: #include <sys/stat.h>
   13: #include <time.h>
   14: 
   15: #include <stdlib.h>
   16: #include <string.h>
   17: #include <errno.h>
   18: #include <unistd.h>
   19: #include <dirent.h>
   20: #include <stdio.h>
   21: 
   22: #include "md5.h"
   23: 
   24: #define HASHLEN 16
   25: typedef unsigned char HASH[HASHLEN];
   26: #define HASHHEXLEN 32
   27: typedef char HASHHEX[HASHHEXLEN+1];
   28: #ifdef USE_OPENSSL
   29: #define IN const
   30: #else
   31: #define IN
   32: #endif
   33: #define OUT
   34: 
   35: #ifdef HAVE_LUA_H
   36: 
   37: #include <lauxlib.h>
   38: 
   39: int f_crypto_md5(lua_State *L) {
   40: 	li_MD5_CTX Md5Ctx;
   41: 	HASH HA1;
   42: 	char hex[33];
   43: 	int n = lua_gettop(L);
   44: 	size_t s_len;
   45: 	const char *s;
   46: 
   47: 	if (n != 1) {
   48: 		lua_pushstring(L, "md5: expected one argument");
   49: 		lua_error(L);
   50: 	}
   51: 
   52: 	if (!lua_isstring(L, 1)) {
   53: 		lua_pushstring(L, "md5: argument has to be a string");
   54: 		lua_error(L);
   55: 	}
   56: 
   57: 	s = lua_tolstring(L, 1, &s_len);
   58: 
   59: 	li_MD5_Init(&Md5Ctx);
   60: 	li_MD5_Update(&Md5Ctx, (unsigned char *) s, (unsigned int) s_len);
   61: 	li_MD5_Final(HA1, &Md5Ctx);
   62: 
   63: 	li_tohex(hex, sizeof(hex), (const char*) HA1, 16);
   64: 
   65: 	lua_pushstring(L, hex);
   66: 
   67: 	return 1;
   68: }
   69: 
   70: 
   71: int f_file_mtime(lua_State *L) {
   72: 	struct stat st;
   73: 	int n = lua_gettop(L);
   74: 
   75: 	if (n != 1) {
   76: 		lua_pushstring(L, "file_mtime: expected one argument");
   77: 		lua_error(L);
   78: 	}
   79: 
   80: 	if (!lua_isstring(L, 1)) {
   81: 		lua_pushstring(L, "file_mtime: argument has to be a string");
   82: 		lua_error(L);
   83: 	}
   84: 
   85: 	if (-1 == stat(lua_tostring(L, 1), &st)) {
   86: 		lua_pushnil(L);
   87: 		return 1;
   88: 	}
   89: 
   90: 	lua_pushinteger(L, st.st_mtime);
   91: 
   92: 	return 1;
   93: }
   94: 
   95: static int f_dir_files_iter(lua_State *L) {
   96: 	DIR *d;
   97: 	struct dirent *de;
   98: 
   99: 	d = lua_touserdata(L, lua_upvalueindex(1));
  100: 
  101: 	if (NULL == (de = readdir(d))) {
  102: 		/* EOF */
  103: 		closedir(d);
  104: 
  105: 		return 0;
  106: 	} else {
  107: 		lua_pushstring(L, de->d_name);
  108: 		return 1;
  109: 	}
  110: }
  111: 
  112: int f_dir_files(lua_State *L) {
  113: 	DIR *d;
  114: 	int n = lua_gettop(L);
  115: 
  116: 	if (n != 1) {
  117: 		lua_pushstring(L, "dir_files: expected one argument");
  118: 		lua_error(L);
  119: 	}
  120: 
  121: 	if (!lua_isstring(L, 1)) {
  122: 		lua_pushstring(L, "dir_files: argument has to be a string");
  123: 		lua_error(L);
  124: 	}
  125: 
  126: 	/* check if there is a valid DIR handle on the stack */
  127: 	if (NULL == (d = opendir(lua_tostring(L, 1)))) {
  128: 		lua_pushnil(L);
  129: 		return 1;
  130: 	}
  131: 
  132: 	/* push d into userdata */
  133: 	lua_pushlightuserdata(L, d);
  134: 	lua_pushcclosure(L, f_dir_files_iter, 1);
  135: 
  136: 	return 1;
  137: }
  138: 
  139: int f_file_isreg(lua_State *L) {
  140: 	struct stat st;
  141: 	int n = lua_gettop(L);
  142: 
  143: 	if (n != 1) {
  144: 		lua_pushstring(L, "file_isreg: expected one argument");
  145: 		lua_error(L);
  146: 	}
  147: 
  148: 	if (!lua_isstring(L, 1)) {
  149: 		lua_pushstring(L, "file_isreg: argument has to be a string");
  150: 		lua_error(L);
  151: 	}
  152: 
  153: 	if (-1 == stat(lua_tostring(L, 1), &st)) {
  154: 		lua_pushnil(L);
  155: 		return 1;
  156: 	}
  157: 
  158: 	lua_pushinteger(L, S_ISREG(st.st_mode));
  159: 
  160: 	return 1;
  161: }
  162: 
  163: int f_file_isdir(lua_State *L) {
  164: 	struct stat st;
  165: 	int n = lua_gettop(L);
  166: 
  167: 	if (n != 1) {
  168: 		lua_pushstring(L, "file_isreg: expected one argument");
  169: 		lua_error(L);
  170: 	}
  171: 
  172: 	if (!lua_isstring(L, 1)) {
  173: 		lua_pushstring(L, "file_isreg: argument has to be a string");
  174: 		lua_error(L);
  175: 	}
  176: 
  177: 	if (-1 == stat(lua_tostring(L, 1), &st)) {
  178: 		lua_pushnil(L);
  179: 		return 1;
  180: 	}
  181: 
  182: 	lua_pushinteger(L, S_ISDIR(st.st_mode));
  183: 
  184: 	return 1;
  185: }
  186: 
  187: 
  188: 
  189: #ifdef USE_MEMCACHED
  190: int f_memcache_exists(lua_State *L) {
  191: 	size_t key_len;
  192: 	const char *key;
  193: 	memcached_st *memc;
  194: 
  195: 	if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
  196: 		lua_pushstring(L, "where is my userdata ?");
  197: 		lua_error(L);
  198: 	}
  199: 
  200: 	memc = lua_touserdata(L, lua_upvalueindex(1));
  201: 
  202: 	if (1 != lua_gettop(L)) {
  203: 		lua_pushstring(L, "expected one argument");
  204: 		lua_error(L);
  205: 	}
  206: 
  207: 	key = luaL_checklstring(L, 1, &key_len);
  208: 	lua_pushboolean(L, (MEMCACHED_SUCCESS == memcached_exist(memc, key, key_len)));
  209: 	return 1;
  210: }
  211: 
  212: int f_memcache_get_string(lua_State *L) {
  213: 	size_t key_len, value_len;
  214: 	char *value;
  215: 	const char *key;
  216: 	memcached_st *memc;
  217: 
  218: 	if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
  219: 		lua_pushstring(L, "where is my userdata ?");
  220: 		lua_error(L);
  221: 	}
  222: 
  223: 	memc = lua_touserdata(L, lua_upvalueindex(1));
  224: 
  225: 	if (1 != lua_gettop(L)) {
  226: 		lua_pushstring(L, "expected one argument");
  227: 		lua_error(L);
  228: 	}
  229: 
  230: 	key = luaL_checklstring(L, 1, &key_len);
  231: 	if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) {
  232: 		lua_pushnil(L);
  233: 		return 1;
  234: 	}
  235: 
  236: 	lua_pushlstring(L, value, value_len);
  237: 
  238: 	free(value);
  239: 
  240: 	return 1;
  241: }
  242: 
  243: int f_memcache_get_long(lua_State *L) {
  244: 	size_t key_len, value_len;
  245: 	char *value;
  246: 	const char *key;
  247: 	memcached_st *memc;
  248: 	char *endptr;
  249: 	long v;
  250: 
  251: 	if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
  252: 		lua_pushstring(L, "where is my userdata ?");
  253: 		lua_error(L);
  254: 	}
  255: 
  256: 	memc = lua_touserdata(L, lua_upvalueindex(1));
  257: 
  258: 	if (1 != lua_gettop(L)) {
  259: 		lua_pushstring(L, "expected one argument");
  260: 		lua_error(L);
  261: 	}
  262: 
  263: 	key = luaL_checklstring(L, 1, &key_len);
  264: 	if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) {
  265: 		lua_pushnil(L);
  266: 		return 1;
  267: 	}
  268: 
  269: 	errno = 0;
  270: 	v = strtol(value, &endptr, 10);
  271: 	if (0 == errno && *endptr == '\0') {
  272: 		lua_pushinteger(L, v);
  273: 	} else {
  274: 		lua_pushnil(L);
  275: 	}
  276: 
  277: 	free(value);
  278: 
  279: 	return 1;
  280: }
  281: #endif
  282: 
  283: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>