[autobuild] rm module stub code for missing deps
[lighttpd.git] / src / mod_cml_funcs.c
blob036c48561a451d7c1f148a27a4437fb8900ef8c5
1 #include "first.h"
3 #include "buffer.h"
4 #include "server.h"
5 #include "log.h"
6 #include "plugin.h"
7 #include "response.h"
9 #include "mod_cml.h"
10 #include "mod_cml_funcs.h"
12 #include <sys/stat.h>
13 #include <time.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <dirent.h>
20 #include <stdio.h>
22 #include "md5.h"
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
35 #include <lauxlib.h>
37 int f_crypto_md5(lua_State *L) {
38 li_MD5_CTX Md5Ctx;
39 HASH HA1;
40 char hex[33];
41 int n = lua_gettop(L);
42 size_t s_len;
43 const char *s;
45 if (n != 1) {
46 lua_pushstring(L, "md5: expected one argument");
47 lua_error(L);
50 if (!lua_isstring(L, 1)) {
51 lua_pushstring(L, "md5: argument has to be a string");
52 lua_error(L);
55 s = lua_tolstring(L, 1, &s_len);
57 li_MD5_Init(&Md5Ctx);
58 li_MD5_Update(&Md5Ctx, (unsigned char *) s, (unsigned int) s_len);
59 li_MD5_Final(HA1, &Md5Ctx);
61 li_tohex(hex, sizeof(hex), (const char*) HA1, 16);
63 lua_pushstring(L, hex);
65 return 1;
69 int f_file_mtime(lua_State *L) {
70 struct stat st;
71 int n = lua_gettop(L);
73 if (n != 1) {
74 lua_pushstring(L, "file_mtime: expected one argument");
75 lua_error(L);
78 if (!lua_isstring(L, 1)) {
79 lua_pushstring(L, "file_mtime: argument has to be a string");
80 lua_error(L);
83 if (-1 == stat(lua_tostring(L, 1), &st)) {
84 lua_pushnil(L);
85 return 1;
88 lua_pushinteger(L, st.st_mtime);
90 return 1;
93 static int f_dir_files_iter(lua_State *L) {
94 DIR *d;
95 struct dirent *de;
97 d = lua_touserdata(L, lua_upvalueindex(1));
99 if (NULL == (de = readdir(d))) {
100 /* EOF */
101 closedir(d);
103 return 0;
104 } else {
105 lua_pushstring(L, de->d_name);
106 return 1;
110 int f_dir_files(lua_State *L) {
111 DIR *d;
112 int n = lua_gettop(L);
114 if (n != 1) {
115 lua_pushstring(L, "dir_files: expected one argument");
116 lua_error(L);
119 if (!lua_isstring(L, 1)) {
120 lua_pushstring(L, "dir_files: argument has to be a string");
121 lua_error(L);
124 /* check if there is a valid DIR handle on the stack */
125 if (NULL == (d = opendir(lua_tostring(L, 1)))) {
126 lua_pushnil(L);
127 return 1;
130 /* push d into userdata */
131 lua_pushlightuserdata(L, d);
132 lua_pushcclosure(L, f_dir_files_iter, 1);
134 return 1;
137 int f_file_isreg(lua_State *L) {
138 struct stat st;
139 int n = lua_gettop(L);
141 if (n != 1) {
142 lua_pushstring(L, "file_isreg: expected one argument");
143 lua_error(L);
146 if (!lua_isstring(L, 1)) {
147 lua_pushstring(L, "file_isreg: argument has to be a string");
148 lua_error(L);
151 if (-1 == stat(lua_tostring(L, 1), &st)) {
152 lua_pushnil(L);
153 return 1;
156 lua_pushinteger(L, S_ISREG(st.st_mode));
158 return 1;
161 int f_file_isdir(lua_State *L) {
162 struct stat st;
163 int n = lua_gettop(L);
165 if (n != 1) {
166 lua_pushstring(L, "file_isreg: expected one argument");
167 lua_error(L);
170 if (!lua_isstring(L, 1)) {
171 lua_pushstring(L, "file_isreg: argument has to be a string");
172 lua_error(L);
175 if (-1 == stat(lua_tostring(L, 1), &st)) {
176 lua_pushnil(L);
177 return 1;
180 lua_pushinteger(L, S_ISDIR(st.st_mode));
182 return 1;
187 #ifdef USE_MEMCACHED
188 int f_memcache_exists(lua_State *L) {
189 size_t key_len;
190 const char *key;
191 memcached_st *memc;
193 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
194 lua_pushstring(L, "where is my userdata ?");
195 lua_error(L);
198 memc = lua_touserdata(L, lua_upvalueindex(1));
200 if (1 != lua_gettop(L)) {
201 lua_pushstring(L, "expected one argument");
202 lua_error(L);
205 key = luaL_checklstring(L, 1, &key_len);
206 lua_pushboolean(L, (MEMCACHED_SUCCESS == memcached_exist(memc, key, key_len)));
207 return 1;
210 int f_memcache_get_string(lua_State *L) {
211 size_t key_len, value_len;
212 char *value;
213 const char *key;
214 memcached_st *memc;
216 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
217 lua_pushstring(L, "where is my userdata ?");
218 lua_error(L);
221 memc = lua_touserdata(L, lua_upvalueindex(1));
223 if (1 != lua_gettop(L)) {
224 lua_pushstring(L, "expected one argument");
225 lua_error(L);
228 key = luaL_checklstring(L, 1, &key_len);
229 if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) {
230 lua_pushnil(L);
231 return 1;
234 lua_pushlstring(L, value, value_len);
236 free(value);
238 return 1;
241 int f_memcache_get_long(lua_State *L) {
242 size_t key_len, value_len;
243 char *value;
244 const char *key;
245 memcached_st *memc;
246 char *endptr;
247 long v;
249 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
250 lua_pushstring(L, "where is my userdata ?");
251 lua_error(L);
254 memc = lua_touserdata(L, lua_upvalueindex(1));
256 if (1 != lua_gettop(L)) {
257 lua_pushstring(L, "expected one argument");
258 lua_error(L);
261 key = luaL_checklstring(L, 1, &key_len);
262 if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) {
263 lua_pushnil(L);
264 return 1;
267 errno = 0;
268 v = strtol(value, &endptr, 10);
269 if (0 == errno && *endptr == '\0') {
270 lua_pushinteger(L, v);
271 } else {
272 lua_pushnil(L);
275 free(value);
277 return 1;
279 #endif