From 4bb0200528e76d5b052c7aefeac75610a17ff092 Mon Sep 17 00:00:00 2001 From: Maurits van der Schee Date: Fri, 14 Mar 2014 15:41:44 +0100 Subject: [PATCH] Added set_expired directive --- config | 4 +- doc/HttpSetMiscModule.wiki | 14 +++++++ src/ngx_http_set_expired.c | 32 ++++++++++++++++ src/ngx_http_set_expired.h | 11 ++++++ src/ngx_http_set_misc_module.c | 16 ++++++++ t/expired.t | 67 ++++++++++++++++++++++++++++++++++ 6 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 src/ngx_http_set_expired.c create mode 100644 src/ngx_http_set_expired.h create mode 100644 t/expired.t diff --git a/config b/config index 4f046aa..3f36567 100755 --- a/config +++ b/config @@ -7,8 +7,8 @@ fi ngx_addon_name=ngx_http_set_misc_module HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_set_misc_module" -NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_set_base32.c $ngx_addon_dir/src/ngx_http_set_default_value.c $ngx_addon_dir/src/ngx_http_set_hashed_upstream.c $ngx_addon_dir/src/ngx_http_set_quote_sql.c $ngx_addon_dir/src/ngx_http_set_quote_json.c $ngx_addon_dir/src/ngx_http_set_unescape_uri.c $ngx_addon_dir/src/ngx_http_set_misc_module.c $ngx_addon_dir/src/ngx_http_set_escape_uri.c $ngx_addon_dir/src/ngx_http_set_hash.c $ngx_addon_dir/src/ngx_http_set_local_today.c $ngx_addon_dir/src/ngx_http_set_hex.c $ngx_addon_dir/src/ngx_http_set_base64.c $ngx_addon_dir/src/ngx_http_set_random.c $ngx_addon_dir/src/ngx_http_set_secure_random.c $ngx_addon_dir/src/ngx_http_set_rotate.c" -NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_set_default_value.h $ngx_addon_dir/src/ngx_http_set_hashed_upstream.h $ngx_addon_dir/src/ngx_http_set_quote_sql.h $ngx_addon_dir/src/ngx_http_set_quote_json.h $ngx_addon_dir/src/ngx_http_set_unescape_uri.h $ngx_addon_dir/src/ngx_http_set_escape_uri.h $ngx_addon_dir/src/ngx_http_set_hash.h $ngx_addon_dir/src/ngx_http_set_local_today.h $ngx_addon_dir/src/ngx_http_set_hex.h $ngx_addon_dir/src/ngx_http_set_base64.h $ngx_addon_dir/src/ngx_http_set_random.h $ngx_addon_dir/src/ngx_http_set_rotate.h $ngx_addon_dir/src/ngx_http_set_secure_random.h $ngx_addon_dir/src/ngx_http_set_misc_module.h" +NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_set_base32.c $ngx_addon_dir/src/ngx_http_set_default_value.c $ngx_addon_dir/src/ngx_http_set_hashed_upstream.c $ngx_addon_dir/src/ngx_http_set_quote_sql.c $ngx_addon_dir/src/ngx_http_set_quote_json.c $ngx_addon_dir/src/ngx_http_set_unescape_uri.c $ngx_addon_dir/src/ngx_http_set_misc_module.c $ngx_addon_dir/src/ngx_http_set_escape_uri.c $ngx_addon_dir/src/ngx_http_set_expired.c $ngx_addon_dir/src/ngx_http_set_hash.c $ngx_addon_dir/src/ngx_http_set_local_today.c $ngx_addon_dir/src/ngx_http_set_hex.c $ngx_addon_dir/src/ngx_http_set_base64.c $ngx_addon_dir/src/ngx_http_set_random.c $ngx_addon_dir/src/ngx_http_set_secure_random.c $ngx_addon_dir/src/ngx_http_set_rotate.c" +NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_set_default_value.h $ngx_addon_dir/src/ngx_http_set_hashed_upstream.h $ngx_addon_dir/src/ngx_http_set_quote_sql.h $ngx_addon_dir/src/ngx_http_set_quote_json.h $ngx_addon_dir/src/ngx_http_set_unescape_uri.h $ngx_addon_dir/src/ngx_http_set_escape_uri.h $ngx_addon_dir/src/ngx_http_set_expired.h $ngx_addon_dir/src/ngx_http_set_hash.h $ngx_addon_dir/src/ngx_http_set_local_today.h $ngx_addon_dir/src/ngx_http_set_hex.h $ngx_addon_dir/src/ngx_http_set_base64.h $ngx_addon_dir/src/ngx_http_set_random.h $ngx_addon_dir/src/ngx_http_set_rotate.h $ngx_addon_dir/src/ngx_http_set_secure_random.h $ngx_addon_dir/src/ngx_http_set_misc_module.h" if [ $USE_OPENSSL = YES ]; then NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_http_set_hmac.h" diff --git a/doc/HttpSetMiscModule.wiki b/doc/HttpSetMiscModule.wiki index 59a5f76..7031b2b 100644 --- a/doc/HttpSetMiscModule.wiki +++ b/doc/HttpSetMiscModule.wiki @@ -862,6 +862,20 @@ And accessing /rotate will also output integer sequence 0, 1, 2, 3, This directive was first introduced in the v0.22rc7 release. +== set_expired == +'''syntax:''' ''set_expired $dst '' + +'''default:''' ''no'' + +'''context:''' ''location, location if'' + +'''phase:''' ''rewrite'' + +Sets $dst to either 1 or 0, dependent on whether or not the +timestamp as defined in timestamp (seconds since 1970-01-01 00:00:00) is or is not in the past. + +Behind the scene, this directive utilizes the ngx_time API in the Nginx core, so usually no syscall is involved due to the time caching mechanism in the Nginx core. + == set_local_today == '''syntax:''' ''set_local_today $dst'' diff --git a/src/ngx_http_set_expired.c b/src/ngx_http_set_expired.c new file mode 100644 index 0000000..17db8a9 --- /dev/null +++ b/src/ngx_http_set_expired.c @@ -0,0 +1,32 @@ +#ifndef DDEBUG +#define DDEBUG 0 +#endif +#include "ddebug.h" + +#include + +#include "ngx_http_set_expired.h" + +ngx_int_t +ngx_http_set_misc_set_expired(ngx_http_request_t *r, ngx_str_t *res, + ngx_http_variable_value_t *v) +{ + ngx_http_variable_value_t *expires; + time_t now, deadline; + + expires = v; + dd("expires=%.*s",(int) expires->len, expires->data); + + now = ngx_time(); + deadline = ngx_atotm(expires->data, expires->len); + dd("now=%lld, deadline=%lld", (long long) now, (long long) deadline); + + res->len = 1; + res->data = ngx_palloc(r->pool, res->len); + if (res->data == NULL) { + return NGX_ERROR; + } + res->data[0] = (u_char) ((deadline && now < deadline) ? '0' : '1'); + + return NGX_OK; + } diff --git a/src/ngx_http_set_expired.h b/src/ngx_http_set_expired.h new file mode 100644 index 0000000..d6d67ca --- /dev/null +++ b/src/ngx_http_set_expired.h @@ -0,0 +1,11 @@ +#ifndef NGX_HTTP_SET_EXPIRED +#define NGX_HTTP_SET_EXPIRED + +#include +#include +#include + +ngx_int_t ngx_http_set_misc_set_expired(ngx_http_request_t *r, + ngx_str_t *res, ngx_http_variable_value_t *v); + +#endif /* NGX_HTTP_SET_EXPIRED */ diff --git a/src/ngx_http_set_misc_module.c b/src/ngx_http_set_misc_module.c index c57aac7..8ec9ced 100755 --- a/src/ngx_http_set_misc_module.c +++ b/src/ngx_http_set_misc_module.c @@ -12,6 +12,7 @@ #include "ngx_http_set_quote_sql.h" #include "ngx_http_set_quote_json.h" #include "ngx_http_set_escape_uri.h" +#include "ngx_http_set_expired.h" #include "ngx_http_set_local_today.h" #include "ngx_http_set_hash.h" #include "ngx_http_set_hex.h" @@ -108,6 +109,12 @@ static ndk_set_var_t ngx_http_set_misc_escape_uri_filter = { NULL }; +static ndk_set_var_t ngx_http_set_misc_set_expired_filter = { + NDK_SET_VAR_VALUE, + (void *) ngx_http_set_misc_set_expired, + 1, + NULL +}; static ndk_set_var_t ngx_http_set_misc_decode_base32_filter = { NDK_SET_VAR_VALUE, @@ -279,6 +286,15 @@ static ngx_command_t ngx_http_set_misc_commands[] = { 0, &ngx_http_set_misc_escape_uri_filter }, + { + ngx_string ("set_expired"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF + |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, + ndk_set_var_value, + 0, + 0, + &ngx_http_set_misc_set_expired_filter + }, { ngx_string ("set_quote_sql_str"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF diff --git a/t/expired.t b/t/expired.t new file mode 100644 index 0000000..585bf2c --- /dev/null +++ b/t/expired.t @@ -0,0 +1,67 @@ +# vi:filetype= + +use lib 'lib'; +use Test::Nginx::Socket; + +#repeat_each(3); + +plan tests => repeat_each() * 2 * blocks(); + +no_long_string(); + +run_tests(); + +#no_diff(); + +__DATA__ + +=== TEST 1: expired for 1970-01-01 00:00:00 +--- config + location /bar { + set_expired $expired 0; + echo $expired; + } +--- request + GET /bar +--- response_body +1 + + + +=== TEST 2: expired for 1970-01-01 00:00:01 +--- config + location /bar { + set_expired $expired 1; + echo $expired; + } +--- request + GET /bar +--- response_body +1 + + + +=== TEST 3: expired for 2020-01-01 00:00:00 +--- config + location /bar { + set_expired $expired 1577836800; + echo $expired; + } +--- request + GET /bar +--- response_body +0 + + + +=== TEST 4: expired for 9999-01-01 00:00:00 +--- config + location /bar { + set_expired $expired 253370764800; + echo $expired; + } +--- request + GET /bar +--- response_body +0 +