mirror of
http://git.haproxy.org/git/haproxy.git/
synced 2025-05-08 10:48:03 +00:00
MINOR: lua: Add date functions
This patch add date parsing function in Lua API. These function are usefull for parsing standard HTTP date provided in headers.
This commit is contained in:
parent
9312794ed7
commit
1550d5d035
@ -22,6 +22,56 @@ int hlua_now(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* This functions expects a Lua string as HTTP date, parse it and
|
||||
* returns an integer containing the epoch format of the date, or
|
||||
* nil if the parsing fails.
|
||||
*/
|
||||
static int hlua_parse_date(lua_State *L, int (*fcn)(const char *, int, struct tm*))
|
||||
{
|
||||
const char *str;
|
||||
size_t len;
|
||||
struct tm tm;
|
||||
time_t time;
|
||||
|
||||
str = luaL_checklstring(L, 1, &len);
|
||||
|
||||
if (!fcn(str, len, &tm)) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* This function considers the content of the broken-down time
|
||||
* is exprimed in the UTC timezone. timegm don't care about
|
||||
* the gnu variable tm_gmtoff. If gmtoff is set, or if you know
|
||||
* the timezone from the broken-down time, it must be fixed
|
||||
* after the conversion.
|
||||
*/
|
||||
time = timegm(&tm);
|
||||
if (time == -1) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
lua_pushinteger(L, (int)time);
|
||||
return 1;
|
||||
}
|
||||
static int hlua_http_date(lua_State *L)
|
||||
{
|
||||
return hlua_parse_date(L, parse_http_date);
|
||||
}
|
||||
static int hlua_imf_date(lua_State *L)
|
||||
{
|
||||
return hlua_parse_date(L, parse_imf_date);
|
||||
}
|
||||
static int hlua_rfc850_date(lua_State *L)
|
||||
{
|
||||
return hlua_parse_date(L, parse_rfc850_date);
|
||||
}
|
||||
static int hlua_asctime_date(lua_State *L)
|
||||
{
|
||||
return hlua_parse_date(L, parse_asctime_date);
|
||||
}
|
||||
|
||||
static void hlua_array_add_fcn(lua_State *L, const char *name,
|
||||
int (*function)(lua_State *L))
|
||||
{
|
||||
@ -33,5 +83,9 @@ static void hlua_array_add_fcn(lua_State *L, const char *name,
|
||||
int hlua_fcn_reg_core_fcn(lua_State *L)
|
||||
{
|
||||
hlua_array_add_fcn(L, "now", hlua_now);
|
||||
return 1;
|
||||
hlua_array_add_fcn(L, "http_date", hlua_http_date);
|
||||
hlua_array_add_fcn(L, "imf_date", hlua_imf_date);
|
||||
hlua_array_add_fcn(L, "rfc850_date", hlua_rfc850_date);
|
||||
hlua_array_add_fcn(L, "asctime_date", hlua_asctime_date);
|
||||
return 5;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user