Permalink
Browse files

polished the implementation of ngx.cookie_time, ngx.http_time, and ng…

…x.parse_http_time a bit: removed request object fetching code because we never use it and also added code to test NGX_ERROR returned by ngx_http_parse_time (we return nil in that case).
  • Loading branch information...
1 parent 2fbc16d commit 35928187691aacf121c4bb6e2dcb362e548dd36a @agentzh agentzh committed Jul 21, 2011
Showing with 49 additions and 26 deletions.
  1. +6 −26 src/ngx_http_lua_hook.c
  2. +43 −0 t/029-http-time.t
View
@@ -2619,7 +2619,6 @@ ngx_http_lua_set_content_length_header(ngx_http_request_t *r, off_t len)
int
ngx_http_lua_ngx_cookie_time(lua_State *L)
{
- ngx_http_request_t *r;
time_t t;
u_char *p;
@@ -2631,14 +2630,6 @@ ngx_http_lua_ngx_cookie_time(lua_State *L)
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_cookie_time(p, t);
@@ -2651,7 +2642,6 @@ 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;
@@ -2663,14 +2653,6 @@ ngx_http_lua_ngx_http_time(lua_State *L)
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);
@@ -2683,25 +2665,23 @@ ngx_http_lua_ngx_http_time(lua_State *L)
int
ngx_http_lua_ngx_parse_http_time(lua_State *L)
{
- ngx_http_request_t *r;
u_char *p;
size_t len;
+ time_t time;
if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting one argument");
}
p = (u_char *) luaL_checklstring(L, 1, &len);
- 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");
+ time = ngx_http_parse_time(p, len);
+ if (time == NGX_ERROR) {
+ lua_pushnil(L);
+ return 1;
}
- lua_pushnumber(L, (lua_Number) ngx_http_parse_time(p, len));
+ lua_pushnumber(L, (lua_Number) time);
return 1;
}
View
@@ -44,3 +44,46 @@ GET /lua
--- response_body
Thu, 18 Nov 2010 11:27:35 GMT
+
+
+=== TEST 3: parse_http_time in set_by_lua
+--- config
+ location /lua {
+ set_by_lua $a '
+ return ngx.parse_http_time("Thu, 18 Nov 2010 11:27:35 GMT")
+ ';
+ echo $a;
+ }
+--- request
+GET /lua
+--- response_body
+1290079655
+
+
+
+=== TEST 4: parse_http_time in content_by_lua
+--- config
+ location /lua {
+ content_by_lua '
+ ngx.say(ngx.parse_http_time("Thu, 18 Nov 2010 11:27:35 GMT"))
+ ';
+ }
+--- request
+GET /lua
+--- response_body
+1290079655
+
+
+
+=== TEST 5: bad arg for parse_http_time in content_by_lua
+--- config
+ location /lua {
+ content_by_lua '
+ ngx.say(ngx.parse_http_time("abc") or "nil")
+ ';
+ }
+--- request
+GET /lua
+--- response_body
+nil
+

0 comments on commit 3592818

Please sign in to comment.