Changeset 472

Show
Ignore:
Timestamp:
07/24/2005 06:58:03 AM (3 years ago)
Author:
jan
Message:

added functions file_isreg() and dir_files() and added last-modified handling

Location:
branches/lighttpd-1.3.x/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • branches/lighttpd-1.3.x/src/mod_cml_funcs.c

    r452 r472  
    66#include <errno.h> 
    77#include <unistd.h> 
     8#include <dirent.h> 
    89#include <stdio.h> 
    910 
     
    7576         
    7677        if (n != 1) { 
    77                 lua_pushstring(L, "expected one argument"); 
    78                 lua_error(L); 
    79         } 
    80          
    81         if (!lua_isstring(L, 1)) { 
    82                 lua_pushstring(L, "argument has to be a string"); 
     78                lua_pushstring(L, "file_mtime: expected one argument"); 
     79                lua_error(L); 
     80        } 
     81         
     82        if (!lua_isstring(L, 1)) { 
     83                lua_pushstring(L, "file_mtime: argument has to be a string"); 
    8384                lua_error(L); 
    8485        } 
    8586         
    8687        if (-1 == stat(lua_tostring(L, 1), &st)) { 
    87                 lua_pushstring(L, "stat failed"); 
    88                 lua_error(L); 
     88                lua_pushnil(L); 
     89                return 1; 
    8990        } 
    9091         
     
    9394        return 1; 
    9495} 
     96 
     97int f_dir_files_iter(lua_State *L) { 
     98        DIR *d; 
     99        struct dirent *de; 
     100        int n = lua_gettop(L); 
     101         
     102        d = lua_touserdata(L, lua_upvalueindex(1)); 
     103         
     104        if (NULL == (de = readdir(d))) { 
     105                /* EOF */ 
     106                closedir(d); 
     107                 
     108                return 0; 
     109        } else { 
     110                lua_pushstring(L, de->d_name); 
     111                return 1; 
     112        } 
     113} 
     114 
     115int f_dir_files(lua_State *L) { 
     116        DIR *d; 
     117        int n = lua_gettop(L); 
     118         
     119        if (n != 1) { 
     120                lua_pushstring(L, "dir_files: expected one argument"); 
     121                lua_error(L); 
     122        } 
     123         
     124        if (!lua_isstring(L, 1)) { 
     125                lua_pushstring(L, "dir_files: argument has to be a string"); 
     126                lua_error(L); 
     127        } 
     128         
     129        /* check if there is a valid DIR handle on the stack */  
     130        if (NULL == (d = opendir(lua_tostring(L, 1)))) { 
     131                lua_pushnil(L); 
     132                return 1; 
     133        } 
     134         
     135        /* push d into registry */ 
     136        lua_pushlightuserdata(L, d); 
     137        lua_pushcclosure(L, f_dir_files_iter, 1); 
     138         
     139        return 1; 
     140} 
     141 
     142int f_file_isreg(lua_State *L) { 
     143        struct stat st; 
     144        int n = lua_gettop(L); 
     145         
     146        if (n != 1) { 
     147                lua_pushstring(L, "file_isreg: expected one argument"); 
     148                lua_error(L); 
     149        } 
     150         
     151        if (!lua_isstring(L, 1)) { 
     152                lua_pushstring(L, "file_isreg: argument has to be a string"); 
     153                lua_error(L); 
     154        } 
     155         
     156        if (-1 == stat(lua_tostring(L, 1), &st)) { 
     157                lua_pushnil(L); 
     158                return 1; 
     159        } 
     160         
     161        lua_pushnumber(L, S_ISREG(st.st_mode)); 
     162         
     163        return 1; 
     164} 
     165 
    95166 
    96167#ifdef HAVE_MEMCACHE_H 
  • branches/lighttpd-1.3.x/src/mod_cml_funcs.h

    r463 r472  
    99int f_crypto_md5(lua_State *L); 
    1010int f_file_mtime(lua_State *L); 
     11int f_file_isreg(lua_State *L); 
     12int f_dir_files(lua_State *L); 
     13 
    1114int f_memcache_exists(lua_State *L); 
    1215int f_memcache_get_string(lua_State *L); 
  • branches/lighttpd-1.3.x/src/mod_cml_lua.c

    r469 r472  
    22#include <stdio.h> 
    33#include <errno.h> 
     4#include <time.h> 
    45 
    56#include "mod_cml.h" 
     
    165166        lua_register(L, "md5", f_crypto_md5); 
    166167        lua_register(L, "file_mtime", f_file_mtime); 
     168        lua_register(L, "file_isreg", f_file_isreg); 
     169        lua_register(L, "dir_files", f_dir_files); 
    167170         
    168171#ifdef HAVE_MEMCACHE_H 
     
    313316                if (ret == 0) { 
    314317                        con->file_finished = 1; 
    315  
     318                         
     319                        /* no Last-Modified specified */ 
     320                        if (mtime && NULL == array_get_element(con->response.headers, "Last-Modified")) { 
     321                                char timebuf[sizeof("Sat, 23 Jul 2005 21:20:01 GMT")]; 
     322                 
     323                                strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&mtime)); 
     324                                 
     325                                response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), timebuf, sizeof(timebuf) - 1); 
     326                        } 
     327                         
    316328                        if (http_response_handle_cachable(srv, con, mtime)) { 
    317329                                /* ok, the client already has our content,