[core] consolidate duplicated read-to-close code
[lighttpd.git] / src / mod_cml_funcs.c
bloba62ec5e46a33a997d60342746a07bdc16cb7e2d7
1 #include "first.h"
3 #include <sys/stat.h>
4 #include <time.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <dirent.h>
11 #include <stdio.h>
13 #include <lauxlib.h>
15 #include "mod_cml_funcs.h"
16 #include "mod_cml.h"
18 #include "buffer.h"
19 #include "server.h"
20 #include "log.h"
21 #include "plugin.h"
22 #include "response.h"
24 #include "md5.h"
26 #define HASHLEN 16
27 typedef unsigned char HASH[HASHLEN];
28 #define HASHHEXLEN 32
29 typedef char HASHHEX[HASHHEXLEN+1];
31 int f_crypto_md5(lua_State *L) {
32 li_MD5_CTX Md5Ctx;
33 HASH HA1;
34 char hex[33];
35 int n = lua_gettop(L);
36 size_t s_len;
37 const char *s;
39 if (n != 1) {
40 lua_pushstring(L, "md5: expected one argument");
41 lua_error(L);
44 if (!lua_isstring(L, 1)) {
45 lua_pushstring(L, "md5: argument has to be a string");
46 lua_error(L);
49 s = lua_tolstring(L, 1, &s_len);
51 li_MD5_Init(&Md5Ctx);
52 li_MD5_Update(&Md5Ctx, (unsigned char *) s, (unsigned int) s_len);
53 li_MD5_Final(HA1, &Md5Ctx);
55 li_tohex(hex, sizeof(hex), (const char*) HA1, 16);
57 lua_pushstring(L, hex);
59 return 1;
63 int f_file_mtime(lua_State *L) {
64 struct stat st;
65 int n = lua_gettop(L);
67 if (n != 1) {
68 lua_pushstring(L, "file_mtime: expected one argument");
69 lua_error(L);
72 if (!lua_isstring(L, 1)) {
73 lua_pushstring(L, "file_mtime: argument has to be a string");
74 lua_error(L);
77 if (-1 == stat(lua_tostring(L, 1), &st)) {
78 lua_pushnil(L);
79 return 1;
82 lua_pushinteger(L, st.st_mtime);
84 return 1;
87 static int f_dir_files_iter(lua_State *L) {
88 DIR *d;
89 struct dirent *de;
91 d = lua_touserdata(L, lua_upvalueindex(1));
93 if (NULL == (de = readdir(d))) {
94 /* EOF */
95 closedir(d);
97 return 0;
98 } else {
99 lua_pushstring(L, de->d_name);
100 return 1;
104 int f_dir_files(lua_State *L) {
105 DIR *d;
106 int n = lua_gettop(L);
108 if (n != 1) {
109 lua_pushstring(L, "dir_files: expected one argument");
110 lua_error(L);
113 if (!lua_isstring(L, 1)) {
114 lua_pushstring(L, "dir_files: argument has to be a string");
115 lua_error(L);
118 /* check if there is a valid DIR handle on the stack */
119 if (NULL == (d = opendir(lua_tostring(L, 1)))) {
120 lua_pushnil(L);
121 return 1;
124 /* push d into userdata */
125 lua_pushlightuserdata(L, d);
126 lua_pushcclosure(L, f_dir_files_iter, 1);
128 return 1;
131 int f_file_isreg(lua_State *L) {
132 struct stat st;
133 int n = lua_gettop(L);
135 if (n != 1) {
136 lua_pushstring(L, "file_isreg: expected one argument");
137 lua_error(L);
140 if (!lua_isstring(L, 1)) {
141 lua_pushstring(L, "file_isreg: argument has to be a string");
142 lua_error(L);
145 if (-1 == stat(lua_tostring(L, 1), &st)) {
146 lua_pushnil(L);
147 return 1;
150 lua_pushinteger(L, S_ISREG(st.st_mode));
152 return 1;
155 int f_file_isdir(lua_State *L) {
156 struct stat st;
157 int n = lua_gettop(L);
159 if (n != 1) {
160 lua_pushstring(L, "file_isreg: expected one argument");
161 lua_error(L);
164 if (!lua_isstring(L, 1)) {
165 lua_pushstring(L, "file_isreg: argument has to be a string");
166 lua_error(L);
169 if (-1 == stat(lua_tostring(L, 1), &st)) {
170 lua_pushnil(L);
171 return 1;
174 lua_pushinteger(L, S_ISDIR(st.st_mode));
176 return 1;
181 #ifdef USE_MEMCACHED
182 int f_memcache_exists(lua_State *L) {
183 size_t key_len;
184 const char *key;
185 memcached_st *memc;
187 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
188 lua_pushstring(L, "where is my userdata ?");
189 lua_error(L);
192 memc = lua_touserdata(L, lua_upvalueindex(1));
194 if (1 != lua_gettop(L)) {
195 lua_pushstring(L, "expected one argument");
196 lua_error(L);
199 key = luaL_checklstring(L, 1, &key_len);
200 lua_pushboolean(L, (MEMCACHED_SUCCESS == memcached_exist(memc, key, key_len)));
201 return 1;
204 int f_memcache_get_string(lua_State *L) {
205 size_t key_len, value_len;
206 char *value;
207 const char *key;
208 memcached_st *memc;
210 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
211 lua_pushstring(L, "where is my userdata ?");
212 lua_error(L);
215 memc = lua_touserdata(L, lua_upvalueindex(1));
217 if (1 != lua_gettop(L)) {
218 lua_pushstring(L, "expected one argument");
219 lua_error(L);
222 key = luaL_checklstring(L, 1, &key_len);
223 if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) {
224 lua_pushnil(L);
225 return 1;
228 lua_pushlstring(L, value, value_len);
230 free(value);
232 return 1;
235 int f_memcache_get_long(lua_State *L) {
236 size_t key_len, value_len;
237 char *value;
238 const char *key;
239 memcached_st *memc;
240 char *endptr;
241 long v;
243 if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
244 lua_pushstring(L, "where is my userdata ?");
245 lua_error(L);
248 memc = lua_touserdata(L, lua_upvalueindex(1));
250 if (1 != lua_gettop(L)) {
251 lua_pushstring(L, "expected one argument");
252 lua_error(L);
255 key = luaL_checklstring(L, 1, &key_len);
256 if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) {
257 lua_pushnil(L);
258 return 1;
261 errno = 0;
262 v = strtol(value, &endptr, 10);
263 if (0 == errno && *endptr == '\0') {
264 lua_pushinteger(L, v);
265 } else {
266 lua_pushnil(L);
269 free(value);
271 return 1;
273 #endif