Permalink
Browse files

applied the patch from Feng Xinguo (@cyberty) to add ngx.http_time() …

…function to expose the nginx core function ngx_http_time to the Lua land.
  • Loading branch information...
1 parent dcdcf87 commit 8e76765ae72f17da835e969856f2ad06b7dc6632 @agentzh agentzh committed Jun 30, 2011
Showing with 109 additions and 0 deletions.
  1. +9 −0 README.markdown
  2. +32 −0 src/ngx_http_lua_hook.c
  3. +1 −0 src/ngx_http_lua_hook.h
  4. +3 −0 src/ngx_http_lua_setby.c
  5. +3 −0 src/ngx_http_lua_util.c
  6. +15 −0 t/021-cookie-time.t
  7. +46 −0 t/029-http-time.t
View
@@ -1219,6 +1219,15 @@ Returns a formated string can be used as the cookie expiration time. The paramet
ngx.say(ngx.cookie_time(1290079655))
-- yields "Thu, 18-Nov-10 11:27:35 GMT"
+ngx.http_time(sec)
+--------------------
+* **Context:** `set_by_lua*`, `rewrite_by_lua*`, `access_by_lua*`, `content_by_lua*`
+
+Returns a formated string can be used as the http header time (for example, being used in Last-Modified header). The parameter `sec` is the timestamp in seconds (like those returned from `ngx.time`).
+
+ ngx.say(ngx.http_time(1290079655))
+ -- yields "Thu, 18 Nov 10 11:27:35 GMT"
+
ngx.is_subrequest
-----------------
* **Context:** `set_by_lua*`, `rewrite_by_lua*`, `access_by_lua*`, `content_by_lua*`
View
@@ -2647,6 +2647,38 @@ ngx_http_lua_ngx_cookie_time(lua_State *L)
int
+ngx_http_lua_ngx_http_time(lua_State *L)
+{
+ ngx_http_request_t *r;
+ time_t t;
+ u_char *p;
+
+ u_char buf[sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1];
+
+ if (lua_gettop(L) != 1) {
+ return luaL_error(L, "expecting one argument");
+ }
+
+ t = (time_t) luaL_checknumber(L, 1);
+
+ lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
+ r = lua_touserdata(L, -1);
+ lua_pop(L, 1);
+
+ if (r == NULL) {
+ return luaL_error(L, "no request object found");
+ }
+
+ p = buf;
+ p = ngx_http_time(p, t);
+
+ lua_pushlstring(L, (char *) buf, p - buf);
+
+ return 1;
+}
+
+
+int
ngx_http_lua_ngx_redirect(lua_State *L)
{
ngx_http_lua_ctx_t *ctx;
View
@@ -41,6 +41,7 @@ int ngx_http_lua_ngx_time(lua_State *L);
int ngx_http_lua_ngx_localtime(lua_State *L);
int ngx_http_lua_ngx_utctime(lua_State *L);
int ngx_http_lua_ngx_cookie_time(lua_State *L);
+int ngx_http_lua_ngx_http_time(lua_State *L);
int ngx_http_lua_ngx_redirect(lua_State *L);
int ngx_http_lua_ngx_location_capture(lua_State *L);
View
@@ -143,6 +143,9 @@ ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r, size_t nargs,
lua_pushcfunction(L, ngx_http_lua_ngx_today);
lua_setfield(L, -2, "today");
+ lua_pushcfunction(L, ngx_http_lua_ngx_http_time);
+ lua_setfield(L, -2, "http_time");
+
lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time);
lua_setfield(L, -2, "cookie_time");
View
@@ -613,6 +613,9 @@ init_ngx_lua_globals(lua_State *L)
lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time);
lua_setfield(L, -2, "cookie_time");
+ lua_pushcfunction(L, ngx_http_lua_ngx_http_time);
+ lua_setfield(L, -2, "http_time");
+
lua_pushcfunction(L, ngx_http_lua_ngx_redirect);
lua_setfield(L, -2, "redirect");
View
@@ -29,3 +29,18 @@ GET /lua
--- response_body
Thu, 18-Nov-10 11:27:35 GMT
+
+
+=== TEST 2: cookie_time in set_by_lua
+--- config
+ location /lua {
+ set_by_lua $a '
+ return ngx.cookie_time(1290079655)
+ ';
+ echo $a;
+ }
+--- request
+GET /lua
+--- response_body
+Thu, 18-Nov-10 11:27:35 GMT
+
View
@@ -0,0 +1,46 @@
+# vim:set ft= ts=4 sw=4 et fdm=marker:
+use lib 'lib';
+use Test::Nginx::Socket;
+
+#worker_connections(1014);
+#master_process_enabled(1);
+log_level('warn');
+
+repeat_each(2);
+#repeat_each(1);
+
+plan tests => repeat_each() * (blocks() * 2);
+
+#no_diff();
+#no_long_string();
+run_tests();
+
+__DATA__
+
+=== TEST 1: http_time in content_by_lua
+--- config
+ location /lua {
+ content_by_lua '
+ ngx.say(ngx.http_time(1290079655))
+ ';
+ }
+--- request
+GET /lua
+--- response_body
+Thu, 18 Nov 2010 11:27:35 GMT
+
+
+
+=== TEST 2: http_time in set_by_lua
+--- config
+ location /lua {
+ set_by_lua $a '
+ return ngx.http_time(1290079655)
+ ';
+ echo $a;
+ }
+--- request
+GET /lua
+--- response_body
+Thu, 18 Nov 2010 11:27:35 GMT
+

0 comments on commit 8e76765

Please sign in to comment.