Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added set_expired directive #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions doc/HttpSetMiscModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,20 @@ And accessing <code>/rotate</code> will also output integer sequence 0, 1, 2, 3,

This directive was first introduced in the <code>v0.22rc7</code> release.

== set_expired ==
'''syntax:''' ''set_expired $dst <timestamp>''

'''default:''' ''no''

'''context:''' ''location, location if''

'''phase:''' ''rewrite''

Sets <code>$dst</code> to either <code>1</code> or <code>0</code>, dependent on whether or not the
timestamp as defined in <code>timestamp</code> (seconds since 1970-01-01 00:00:00) is or is not in the past.

Behind the scene, this directive utilizes the <code>ngx_time</code> 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''

Expand Down
32 changes: 32 additions & 0 deletions src/ngx_http_set_expired.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"

#include <ndk.h>

#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;
}
11 changes: 11 additions & 0 deletions src/ngx_http_set_expired.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef NGX_HTTP_SET_EXPIRED
#define NGX_HTTP_SET_EXPIRED

#include <ngx_core.h>
#include <ngx_config.h>
#include <ngx_http.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);

#endif /* NGX_HTTP_SET_EXPIRED */
16 changes: 16 additions & 0 deletions src/ngx_http_set_misc_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions t/expired.t
Original file line number Diff line number Diff line change
@@ -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