[mod_cgi] fix pipe_cloexec() when no O_CLOEXEC
[lighttpd.git] / src / mod_cml_funcs.c
blob8e809817e63e43f412d0dd90463f3bed11b5dc2d
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 #ifdef HAVE_LUA_H
37 #include <lauxlib.h>
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;
47 if (n != 1) {
48 lua_pushstring(L, "md5: expected one argument");
49 lua_error(L);
52 if (!lua_isstring(L, 1)) {
53 lua_pushstring(L, "md5: argument has to be a string");
54 lua_error(L);
57 s = lua_tolstring(L, 1, &s_len);
59 li_MD5_Init(&Md5Ctx);
60 li_MD5_Update(&Md5Ctx, (unsigned char *) s, (unsigned int) s_len);
61 li_MD5_Final(HA1, &Md5Ctx);
63 li_tohex(hex, sizeof(hex), (const char*) HA1, 16);
65 lua_pushstring(L, hex);
67 return 1;
71 int f_file_mtime(lua_State *L) {
72 struct stat st;
73 int n = lua_gettop(L);
75 if (n != 1) {
76 lua_pushstring(L, "file_mtime: expected one argument");
77 lua_error(L);
80 if (!lua_isstring(L, 1)) {
81 lua_pushstring(L, "file_mtime: argument has to be a string");
82 lua_error(L);
85 if (-1 == stat(lua_tostring(L, 1), &st)) {
86 lua_pushnil(L);
87 return 1;
90 lua_pushinteger(L, st.st_mtime);
92 return 1;
95 static int f_dir_files_iter(lua_State *L) {
96 DIR *d;
97 struct dirent *de;
99 d = lua_touserdata(L, lua_upvalueindex(1));
101 if (NULL == (de = readdir(d))) {
102 /* EOF */
103 closedir(d);
105 return 0;
106 } else {
107 lua_pushstring(L, de->d_name);
108 return 1;
112 int f_dir_files(lua_State *L) {
113 DIR *d;
114 int n = lua_gettop(L);
116 if (n != 1) {
117 lua_pushstring(L, "dir_files: expected one argument");
118 lua_error(L);
121 if (!lua_isstring(L, 1)) {
122 lua_pushstring(L, "dir_files: argument has to be a string");
123 lua_error(L);
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;
132 /* push d into userdata */
133 lua_pushlightuserdata(L, d);
134 lua_pushcclosure(L, f_dir_files_iter, 1);
136 return 1;
139 int f_file_isreg(lua_State *L) {
140 struct stat st;
141 int n = lua_gettop(L);
143 if (n != 1) {
144 lua_pushstring(L, "file_isreg: expected one argument");
145 lua_error(L);
148 if (!lua_isstring(L, 1)) {
149 lua_pushstring(L, "file_isreg: argument has to be a string");
150 lua_error(L);
153 if (-1 == stat(lua_tostring(L, 1), &st)) {
154 lua_pushnil(L);
155 return 1;
158 lua_pushinteger(L, S_ISREG(st.st_mode));
160 return 1;
163 int f_file_isdir(lua_State *L) {
164 struct stat st;
165 int n = lua_gettop(L);
167 if (n != 1) {
168 lua_pushstring(L, "file_isreg: expected one argument");
169 lua_error(L);
172 if (!lua_isstring(L, 1)) {
173 lua_pushstring(L, "file_isreg: argument has to be a string");
174 lua_error(L);
177 if (-1 == stat(lua_tostring(L, 1), &st)) {
178 lua_pushnil(L);
179 return 1;
182 lua_pushinteger(L, S_ISDIR(st.st_mode));
184 return 1;
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;
195 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
196 lua_pushstring(L, "where is my userdata ?");
197 lua_error(L);
200 memc = lua_touserdata(L, lua_upvalueindex(1));
202 if (1 != lua_gettop(L)) {
203 lua_pushstring(L, "expected one argument");
204 lua_error(L);
207 key = luaL_checklstring(L, 1, &key_len);
208 lua_pushboolean(L, (MEMCACHED_SUCCESS == memcached_exist(memc, key, key_len)));
209 return 1;
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;
218 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
219 lua_pushstring(L, "where is my userdata ?");
220 lua_error(L);
223 memc = lua_touserdata(L, lua_upvalueindex(1));
225 if (1 != lua_gettop(L)) {
226 lua_pushstring(L, "expected one argument");
227 lua_error(L);
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;
236 lua_pushlstring(L, value, value_len);
238 free(value);
240 return 1;
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;
251 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
252 lua_pushstring(L, "where is my userdata ?");
253 lua_error(L);
256 memc = lua_touserdata(L, lua_upvalueindex(1));
258 if (1 != lua_gettop(L)) {
259 lua_pushstring(L, "expected one argument");
260 lua_error(L);
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;
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);
277 free(value);
279 return 1;
281 #endif
283 #endif