2 * Copyright 2018 Rubén Beltrán del Río
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5 * License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
10 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
15 #include <ngx_config.h>
25 const ngx_uint_t WEEK_LENGTH
= 7;
26 const char * CLOSED_TOKEN
= "closed";
27 const ngx_str_t TIME_REGEX
= ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?");
29 /* Main Configuration Structure */
32 ngx_array_t
*office_hours
;
33 } ngx_http_office_hours_conf_t
;
35 /* Lifecycle Functions For Module Context */
37 static void *ngx_http_office_hours_create_conf(ngx_conf_t
* cf
);
38 static char *ngx_http_office_hours_merge_conf(ngx_conf_t
* cf
,
39 void *parent
, void *child
);
40 static ngx_int_t
ngx_http_office_hours_init(ngx_conf_t
* cf
);
42 /* Configuration Handler */
44 static char *ngx_http_office_hours(ngx_conf_t
* cf
, ngx_command_t
* cmd
,
47 /* Body Filter Storage */
49 static ngx_http_output_body_filter_pt ngx_http_next_body_filter
;
51 /* Utility Functions */
52 static ngx_uint_t
** parse_office_hours(ngx_array_t
* office_hours
);
53 static ngx_uint_t
* parse_office_hours_string(ngx_str_t office_hours
);
54 static ngx_flag_t
within_office_hours(ngx_uint_t
** office_hours
);
55 static ngx_uint_t
get_day_of_week(time_t time
);
56 static ngx_uint_t
get_seconds_of_day(time_t time
);
59 ngx_regex_compile_t rc
;
65 /* Module Directives */
67 static ngx_command_t ngx_http_office_hours_commands
[] = {
69 ngx_string("office_hours"),
70 NGX_HTTP_MAIN_CONF
| NGX_HTTP_SRV_CONF
| NGX_HTTP_LOC_CONF
| NGX_CONF_TAKE1
| NGX_CONF_TAKE2
| NGX_CONF_TAKE3
| NGX_CONF_TAKE4
| NGX_CONF_TAKE5
| NGX_CONF_TAKE6
| NGX_CONF_TAKE7
,
71 ngx_http_office_hours
,
72 NGX_HTTP_LOC_CONF_OFFSET
,
73 offsetof(ngx_http_office_hours_conf_t
, office_hours
),
83 static ngx_http_module_t ngx_http_office_hours_filter_module_ctx
= {
84 NULL
, /* Preconfiguration */
85 ngx_http_office_hours_init
, /* Postconfiguration */
87 NULL
, /* Create main configuration */
88 NULL
, /* Initialize main configuration */
90 NULL
, /* Create server configuration */
91 NULL
, /* Merge server configuration */
93 ngx_http_office_hours_create_conf
, /* Create location configuration */
94 ngx_http_office_hours_merge_conf
/* Merge location configuration */
98 /* Module Definition */
100 ngx_module_t ngx_http_office_hours_filter_module
= {
101 NGX_MODULE_V1
, //Module Version
102 &ngx_http_office_hours_filter_module_ctx
, //Module context
103 ngx_http_office_hours_commands
, //Module commands
104 NGX_HTTP_MODULE
, //Module Type
105 NULL
, //Initialize Master
106 NULL
, //Initialize Module
107 NULL
, //Initialize Process
108 NULL
, //Initialize Thread
112 NGX_MODULE_V1_PADDING
118 * If the current time is within office hours, it goes to the next
119 * handler. Otherwise it returns 403 and the office hour listing.
123 ngx_http_office_hours_body_filter(ngx_http_request_t
* r
, ngx_chain_t
* in
)
126 ngx_uint_t
** parsed_office_hours
;
127 ngx_http_office_hours_conf_t
*conf
;
130 ngx_http_get_module_loc_conf(r
,
131 ngx_http_office_hours_filter_module
);
134 if (conf
->office_hours
== NULL
) {
135 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0,
136 "Office hours disabled");
137 return ngx_http_next_body_filter(r
, in
);
140 parsed_office_hours
= parse_office_hours(conf
->office_hours
);
142 if (within_office_hours(parsed_office_hours
)) {
143 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0,
144 "Within office hours");
145 return ngx_http_next_body_filter(r
, in
);
148 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0,
149 "Outside office hours");
152 return NGX_HTTP_FORBIDDEN
;
156 * Callback for `office_hours ` directive
157 * Reads the configuration loaded from the config file(cf)
158 * And writes it to the right place in the module configuration(conf)
161 static char *ngx_http_office_hours(ngx_conf_t
* cf
, ngx_command_t
* cmd
,
165 char *conf_structure
= conf
;
167 ngx_str_t
*hours
, *value
;
168 ngx_array_t
**office_hours
;
171 /* Gets the array from the config structure using the defined
172 * offset, and if the pointer is unset it creates a new one.
173 * (The first element is the directive itself, so we should be
176 office_hours
= (ngx_array_t
**) (conf_structure
+ cmd
->offset
);
178 if (*office_hours
== NGX_CONF_UNSET_PTR
) {
179 *office_hours
= ngx_array_create(cf
->pool
, cf
->args
->nelts
- 1,
182 if (*office_hours
== NULL
) {
183 return NGX_CONF_ERROR
;
186 value
= cf
->args
->elts
;
188 for (i
= 1; i
< cf
->args
->nelts
; ++i
) {
189 hours
= ngx_array_push(*office_hours
);
191 return NGX_CONF_ERROR
;
202 * Initializes the configuration structure
205 static void *ngx_http_office_hours_create_conf(ngx_conf_t
* cf
)
208 ngx_http_office_hours_conf_t
*conf
;
210 conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_office_hours_conf_t
));
215 conf
->office_hours
= NGX_CONF_UNSET_PTR
;
221 * Merge Config Values
222 * Sets the defaults for the configuration and merges
223 * with other configurations
226 static char *ngx_http_office_hours_merge_conf(ngx_conf_t
* cf
,
227 void *parent
, void *child
)
230 ngx_http_office_hours_conf_t
*prev
= parent
;
231 ngx_http_office_hours_conf_t
*conf
= child
;
233 ngx_conf_merge_ptr_value(conf
->office_hours
, prev
->office_hours
, NULL
);
239 * Postconfig Initialization Handler
240 * Sets the request filter at the top of the chain
243 static ngx_int_t
ngx_http_office_hours_init(ngx_conf_t
* cf
)
246 ngx_http_next_body_filter
= ngx_http_top_body_filter
;
247 ngx_http_top_body_filter
= ngx_http_office_hours_body_filter
;
249 rc
.pattern
= TIME_REGEX
;
251 if (ngx_regex_compile(&rc
) != NGX_OK
) {
259 * Parse the office hour strings in the configuration file
260 * to fill out the hours array (in seconds)
263 static ngx_uint_t
** parse_office_hours(ngx_array_t
* office_hours
)
267 ngx_uint_t
** parsed_office_hours
;
270 parsed_office_hours
= malloc(7 * sizeof(ngx_uint_t
*));
272 hours
= office_hours
->elts
;
275 * On the configuration file, the leftmost element
276 * always applies to all remaining days, all others
277 * are read from right to left. So first we will apply
278 * the initial override, and then iterate based on the
279 * number of overrides
282 for (i
= 0; i
< WEEK_LENGTH
+ 1 - office_hours
->nelts
; ++i
) {
283 parsed_office_hours
[i
] = parse_office_hours_string(hours
[0]);
286 for (i
= 1; i
< office_hours
->nelts
; ++i
) {
287 j
= WEEK_LENGTH
- office_hours
->nelts
+ i
;
288 parsed_office_hours
[j
] = parse_office_hours_string(hours
[i
]);
291 return parsed_office_hours
;
295 * Given a time string or the closed token, return a tuple
296 * of numbers representing opening and closing hours
299 static ngx_uint_t
* parse_office_hours_string(ngx_str_t office_hours
)
302 int captures
[(1 + 4) * 3];
304 ngx_uint_t
* parsed_hours
;
306 parsed_hours
= malloc(2 * sizeof(ngx_uint_t
));
308 if(ngx_strcmp(office_hours
.data
, CLOSED_TOKEN
) == 0) {
314 n
= ngx_regex_exec(rc
.regex
, &office_hours
, captures
, (1 + 4) * 3);
319 parsed_hours
[0] = 60 * 60 * ngx_atoi(&office_hours
.data
[captures
[2]], captures
[3] - captures
[2]);
320 parsed_hours
[0] = parsed_hours
[0] + 60 * ngx_atoi(&office_hours
.data
[captures
[4]], captures
[5] - captures
[4]);
322 parsed_hours
[1] = 60 * 60 * ngx_atoi(&office_hours
.data
[captures
[6]], captures
[7] - captures
[6]);
323 parsed_hours
[1] = parsed_hours
[1] + 60 * ngx_atoi(&office_hours
.data
[captures
[8]], captures
[9] - captures
[8]);
328 /* Non-matching strings count as open */
331 parsed_hours
[1] = 86400;
336 * Given an office hours array, it returns whether or not
337 * it is currently within office hours.
340 static ngx_flag_t
within_office_hours(ngx_uint_t
** office_hours
)
344 ngx_uint_t day_of_week
, seconds_of_day
;
345 ngx_uint_t
* current_hours
;
349 day_of_week
= get_day_of_week(now
);
350 seconds_of_day
= get_seconds_of_day(now
);
351 current_hours
= office_hours
[day_of_week
];
353 return seconds_of_day
>= current_hours
[0] && seconds_of_day
<= current_hours
[1];
357 * Calculate the day of the week given a timestamp
359 static ngx_uint_t
get_day_of_week(time_t time
)
362 /* Epoch was thursday, so add 3 so we start on monday */
363 return (time
/ 86400 + 3) % 7;
367 * Calculate the number of seconds elapsed today
369 static ngx_uint_t
get_seconds_of_day(time_t time
)
372 return time
- (time
/ 86400) * 86400;