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";
28 /* Main Configuration Structure */
31 ngx_array_t
*office_hours
;
32 } ngx_http_office_hours_conf_t
;
34 /* Lifecycle Functions For Module Context */
36 static void *ngx_http_office_hours_create_conf(ngx_conf_t
* cf
);
37 static char *ngx_http_office_hours_merge_conf(ngx_conf_t
* cf
,
38 void *parent
, void *child
);
39 static ngx_int_t
ngx_http_office_hours_init(ngx_conf_t
* cf
);
41 /* Configuration Handler */
43 static char *ngx_http_office_hours(ngx_conf_t
* cf
, ngx_command_t
* cmd
,
46 /* Body Filter Storage */
48 static ngx_http_output_body_filter_pt ngx_http_next_body_filter
;
50 /* Utility Functions */
51 static ngx_uint_t
** parse_office_hours(ngx_array_t
* office_hours
);
52 static ngx_uint_t
* parse_office_hours_string(ngx_str_t office_hours
);
53 static ngx_flag_t
within_office_hours(ngx_uint_t
** office_hours
);
54 static ngx_uint_t
get_day_of_week(time_t time
);
55 static ngx_uint_t
get_seconds_of_day(time_t time
);
61 /* Module Directives */
63 static ngx_command_t ngx_http_office_hours_commands
[] = {
65 ngx_string("office_hours"),
66 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
,
67 ngx_http_office_hours
,
68 NGX_HTTP_LOC_CONF_OFFSET
,
69 offsetof(ngx_http_office_hours_conf_t
, office_hours
),
79 static ngx_http_module_t ngx_http_office_hours_filter_module_ctx
= {
80 NULL
, /* Preconfiguration */
81 ngx_http_office_hours_init
, /* Postconfiguration */
83 NULL
, /* Create main configuration */
84 NULL
, /* Initialize main configuration */
86 NULL
, /* Create server configuration */
87 NULL
, /* Merge server configuration */
89 ngx_http_office_hours_create_conf
, /* Create location configuration */
90 ngx_http_office_hours_merge_conf
/* Merge location configuration */
94 /* Module Definition */
96 ngx_module_t ngx_http_office_hours_filter_module
= {
97 NGX_MODULE_V1
, //Module Version
98 &ngx_http_office_hours_filter_module_ctx
, //Module context
99 ngx_http_office_hours_commands
, //Module commands
100 NGX_HTTP_MODULE
, //Module Type
101 NULL
, //Initialize Master
102 NULL
, //Initialize Module
103 NULL
, //Initialize Process
104 NULL
, //Initialize Thread
108 NGX_MODULE_V1_PADDING
114 * If the current time is within office hours, it goes to the next
115 * handler. Otherwise it returns 403 and the office hour listing.
119 ngx_http_office_hours_body_filter(ngx_http_request_t
* r
, ngx_chain_t
* in
)
122 ngx_uint_t
** parsed_office_hours
;
123 ngx_http_office_hours_conf_t
*conf
;
126 ngx_http_get_module_loc_conf(r
,
127 ngx_http_office_hours_filter_module
);
130 if (conf
->office_hours
== NULL
) {
131 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0,
132 "Office hours disabled");
133 return ngx_http_next_body_filter(r
, in
);
136 parsed_office_hours
= parse_office_hours(conf
->office_hours
);
138 if (within_office_hours(parsed_office_hours
)) {
139 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0,
140 "Within office hours");
141 return ngx_http_next_body_filter(r
, in
);
144 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0,
145 "Outside office hours");
148 return NGX_HTTP_FORBIDDEN
;
152 * Callback for `office_hours ` directive
153 * Reads the configuration loaded from the config file(cf)
154 * And writes it to the right place in the module configuration(conf)
157 static char *ngx_http_office_hours(ngx_conf_t
* cf
, ngx_command_t
* cmd
,
161 char *conf_structure
= conf
;
163 ngx_str_t
*hours
, *value
;
164 ngx_array_t
**office_hours
;
167 /* Gets the array from the config structure using the defined
168 * offset, and if the pointer is unset it creates a new one.
169 * (The first element is the directive itself, so we should be
172 office_hours
= (ngx_array_t
**) (conf_structure
+ cmd
->offset
);
174 if (*office_hours
== NGX_CONF_UNSET_PTR
) {
175 *office_hours
= ngx_array_create(cf
->pool
, cf
->args
->nelts
- 1,
178 if (*office_hours
== NULL
) {
179 return NGX_CONF_ERROR
;
182 value
= cf
->args
->elts
;
184 for (i
= 1; i
< cf
->args
->nelts
; ++i
) {
185 hours
= ngx_array_push(*office_hours
);
187 return NGX_CONF_ERROR
;
198 * Initializes the configuration structure
201 static void *ngx_http_office_hours_create_conf(ngx_conf_t
* cf
)
204 ngx_http_office_hours_conf_t
*conf
;
206 conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_office_hours_conf_t
));
211 conf
->office_hours
= NGX_CONF_UNSET_PTR
;
217 * Merge Config Values
218 * Sets the defaults for the configuration and merges
219 * with other configurations
222 static char *ngx_http_office_hours_merge_conf(ngx_conf_t
* cf
,
223 void *parent
, void *child
)
226 ngx_http_office_hours_conf_t
*prev
= parent
;
227 ngx_http_office_hours_conf_t
*conf
= child
;
229 ngx_conf_merge_ptr_value(conf
->office_hours
, prev
->office_hours
, NULL
);
235 * Postconfig Initialization Handler
236 * Sets the request filter at the top of the chain
239 static ngx_int_t
ngx_http_office_hours_init(ngx_conf_t
* cf
)
242 ngx_http_next_body_filter
= ngx_http_top_body_filter
;
243 ngx_http_top_body_filter
= ngx_http_office_hours_body_filter
;
249 * Parse the office hour strings in the configuration file
250 * to fill out the hours array (in seconds)
253 static ngx_uint_t
** parse_office_hours(ngx_array_t
* office_hours
)
257 ngx_uint_t
** parsed_office_hours
;
260 parsed_office_hours
= malloc(7 * sizeof(ngx_uint_t
*));
262 hours
= office_hours
->elts
;
265 * On the configuration file, the leftmost element
266 * always applies to all remaining days, all others
267 * are read from right to left. So first we will apply
268 * the initial override, and then iterate based on the
269 * number of overrides
272 for (i
= 0; i
< WEEK_LENGTH
+ 1 - office_hours
->nelts
; ++i
) {
273 parsed_office_hours
[i
] = parse_office_hours_string(hours
[0]);
276 for (i
= 1; i
< office_hours
->nelts
; ++i
) {
277 j
= WEEK_LENGTH
- office_hours
->nelts
+ i
;
278 parsed_office_hours
[j
] = parse_office_hours_string(hours
[i
]);
281 return parsed_office_hours
;
285 * Given a time string or the closed token, return a tuple
286 * of numbers representing opening and closing hours
289 static ngx_uint_t
* parse_office_hours_string(ngx_str_t office_hours
)
292 ngx_uint_t
* parsed_hours
;
294 parsed_hours
= malloc(2 * sizeof(ngx_uint_t
));
296 if(ngx_strcmp(office_hours
.data
, CLOSED_TOKEN
) == 0) {
303 parsed_hours
[1] = 86400;
308 * Given an office hours array, it returns whether or not
309 * it is currently within office hours.
312 static ngx_flag_t
within_office_hours(ngx_uint_t
** office_hours
)
316 ngx_uint_t day_of_week
, seconds_of_day
;
317 ngx_uint_t
* current_hours
;
321 day_of_week
= get_day_of_week(now
);
322 seconds_of_day
= get_seconds_of_day(now
);
323 current_hours
= office_hours
[day_of_week
];
325 return seconds_of_day
>= current_hours
[0] && seconds_of_day
<= current_hours
[1];
329 * Calculate the day of the week given a timestamp
331 static ngx_uint_t
get_day_of_week(time_t time
)
334 /* Epoch was thursday, so add 3 so we start on monday */
335 return (time
/ 86400 + 3) % 7;
339 * Calculate the number of seconds elapsed today
341 static ngx_uint_t
get_seconds_of_day(time_t time
)
344 return time
- (time
/ 86400) * 86400;