]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/blame - ngx_http_office_hours_filter_module.c
Update contributing guide
[rbdr/ngx_http_office_hours_filter_module] / ngx_http_office_hours_filter_module.c
CommitLineData
e4db4a20
BB
1/*
2 * Copyright 2018 Rubén Beltrán del Río
af4b488f 3 *
e4db4a20
BB
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
af4b488f 6 *
e4db4a20 7 * http://www.apache.org/licenses/LICENSE-2.0
af4b488f 8 *
e4db4a20
BB
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.
12 */
13
14
15#include <ngx_config.h>
16#include <ngx_core.h>
17#include <ngx_http.h>
18
fa2b280d
BB
19/*
20 * Declarations
21 */
22
fbdf81df
BB
23/* Constants */
24
25const ngx_uint_t WEEK_LENGTH = 7;
26const char * CLOSED_TOKEN = "closed";
80c4c3cc 27const ngx_str_t TIME_REGEX = ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?");
fbdf81df 28
fa2b280d 29/* Main Configuration Structure */
e4db4a20
BB
30
31typedef struct {
af4b488f
BB
32 ngx_array_t *office_hours;
33} ngx_http_office_hours_conf_t;
e4db4a20 34
fa2b280d
BB
35/* Lifecycle Functions For Module Context */
36
af4b488f 37static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf);
fa2b280d
BB
38static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
39 void *parent, void *child);
e4db4a20
BB
40static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf);
41
fa2b280d
BB
42/* Configuration Handler */
43
44static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
45 void *conf);
46
47/* Body Filter Storage */
48
49static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
50
fbdf81df
BB
51/* Utility Functions */
52static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours);
53static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours);
54static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours);
55static ngx_uint_t get_day_of_week(time_t time);
56static ngx_uint_t get_seconds_of_day(time_t time);
57
80c4c3cc
BB
58/* Compiled Regex */
59ngx_regex_compile_t rc;
60
fa2b280d
BB
61/*
62 * Module Definitions
63 */
e4db4a20 64
fa2b280d 65/* Module Directives */
e4db4a20
BB
66
67static ngx_command_t ngx_http_office_hours_commands[] = {
68 {
fa2b280d 69 ngx_string("office_hours"),
fbdf81df 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,
fa2b280d
BB
71 ngx_http_office_hours,
72 NGX_HTTP_LOC_CONF_OFFSET,
73 offsetof(ngx_http_office_hours_conf_t, office_hours),
74 NULL
e4db4a20
BB
75 },
76
77 ngx_null_command
78};
79
fa2b280d
BB
80
81/* Module Context */
e4db4a20
BB
82
83static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = {
fa2b280d
BB
84 NULL, /* Preconfiguration */
85 ngx_http_office_hours_init, /* Postconfiguration */
e4db4a20 86
fa2b280d
BB
87 NULL, /* Create main configuration */
88 NULL, /* Initialize main configuration */
e4db4a20 89
fa2b280d
BB
90 NULL, /* Create server configuration */
91 NULL, /* Merge server configuration */
92
93 ngx_http_office_hours_create_conf, /* Create location configuration */
94 ngx_http_office_hours_merge_conf /* Merge location configuration */
e4db4a20
BB
95};
96
fa2b280d
BB
97
98/* Module Definition */
e4db4a20
BB
99
100ngx_module_t ngx_http_office_hours_filter_module = {
fa2b280d
BB
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
109 NULL, //Exit Thread
110 NULL, //Exit Process
111 NULL, //Exit Master
112 NGX_MODULE_V1_PADDING
e4db4a20
BB
113};
114
115
fa2b280d
BB
116/*
117 * Main Body Filter
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.
120 */
e4db4a20 121
af4b488f 122static ngx_int_t
fa2b280d 123ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in)
af4b488f 124{
e4db4a20 125
fbdf81df 126 ngx_uint_t ** parsed_office_hours;
af4b488f 127 ngx_http_office_hours_conf_t *conf;
e4db4a20 128
fa2b280d
BB
129 conf =
130 ngx_http_get_module_loc_conf(r,
131 ngx_http_office_hours_filter_module);
e4db4a20 132
fbdf81df 133
e4db4a20 134 if (conf->office_hours == NULL) {
fa2b280d 135 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
fbdf81df 136 "Office hours disabled");
fa2b280d 137 return ngx_http_next_body_filter(r, in);
e4db4a20 138 }
fa2b280d 139
fbdf81df 140 parsed_office_hours = parse_office_hours(conf->office_hours);
e4db4a20 141
fbdf81df 142 if (within_office_hours(parsed_office_hours)) {
fa2b280d 143 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
fbdf81df
BB
144 "Within office hours");
145 return ngx_http_next_body_filter(r, in);
e4db4a20
BB
146 }
147
fbdf81df
BB
148 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
149 "Outside office hours");
150
e4db4a20
BB
151 r->keepalive = 0;
152 return NGX_HTTP_FORBIDDEN;
153}
154
fa2b280d
BB
155/*
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)
159 */
e4db4a20 160
fa2b280d
BB
161static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
162 void *conf)
af4b488f 163{
e4db4a20 164
af4b488f 165 char *conf_structure = conf;
e4db4a20 166
fbdf81df 167 ngx_str_t *hours, *value;
af4b488f 168 ngx_array_t **office_hours;
af4b488f 169 ngx_uint_t i;
e4db4a20 170
fa2b280d
BB
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
174 * offset by 1)
175 */
176 office_hours = (ngx_array_t **) (conf_structure + cmd->offset);
e4db4a20
BB
177
178 if (*office_hours == NGX_CONF_UNSET_PTR) {
fa2b280d
BB
179 *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1,
180 sizeof(ngx_str_t));
e4db4a20 181
fa2b280d
BB
182 if (*office_hours == NULL) {
183 return NGX_CONF_ERROR;
184 }
e4db4a20 185 }
e4db4a20
BB
186 value = cf->args->elts;
187
188 for (i = 1; i < cf->args->nelts; ++i) {
fa2b280d
BB
189 hours = ngx_array_push(*office_hours);
190 if (hours == NULL) {
191 return NGX_CONF_ERROR;
192 }
193 *hours = value[i];
e4db4a20
BB
194 }
195
196 return NGX_CONF_OK;
197}
198
199
fa2b280d
BB
200/*
201 * Config Creator
202 * Initializes the configuration structure
203 */
e4db4a20 204
fa2b280d 205static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf)
af4b488f 206{
e4db4a20 207
af4b488f 208 ngx_http_office_hours_conf_t *conf;
e4db4a20
BB
209
210 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t));
211
212 if (conf == NULL) {
fa2b280d 213 return NULL;
e4db4a20 214 }
e4db4a20
BB
215 conf->office_hours = NGX_CONF_UNSET_PTR;
216
217 return conf;
218}
219
fa2b280d
BB
220/*
221 * Merge Config Values
222 * Sets the defaults for the configuration and merges
223 * with other configurations
224 */
e4db4a20 225
fa2b280d
BB
226static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
227 void *parent, void *child)
af4b488f 228{
e4db4a20 229
af4b488f
BB
230 ngx_http_office_hours_conf_t *prev = parent;
231 ngx_http_office_hours_conf_t *conf = child;
e4db4a20
BB
232
233 ngx_conf_merge_ptr_value(conf->office_hours, prev->office_hours, NULL);
234
235 return NGX_CONF_OK;
236}
237
fa2b280d
BB
238/*
239 * Postconfig Initialization Handler
240 * Sets the request filter at the top of the chain
241 */
e4db4a20 242
fa2b280d 243static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf)
af4b488f 244{
e4db4a20
BB
245
246 ngx_http_next_body_filter = ngx_http_top_body_filter;
247 ngx_http_top_body_filter = ngx_http_office_hours_body_filter;
248
80c4c3cc
BB
249 rc.pattern = TIME_REGEX;
250 rc.pool = cf->pool;
251 if (ngx_regex_compile(&rc) != NGX_OK) {
252 return NGX_ERROR;
253 }
254
e4db4a20
BB
255 return NGX_OK;
256}
fbdf81df
BB
257
258/*
259 * Parse the office hour strings in the configuration file
260 * to fill out the hours array (in seconds)
261 */
262
263static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours)
264{
265
266 ngx_str_t *hours;
267 ngx_uint_t ** parsed_office_hours;
268 ngx_uint_t i, j;
269
270 parsed_office_hours = malloc(7 * sizeof(ngx_uint_t *));
271
272 hours = office_hours->elts;
273
274 /*
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
280 */
281
282 for (i = 0; i < WEEK_LENGTH + 1 - office_hours->nelts; ++i) {
283 parsed_office_hours[i] = parse_office_hours_string(hours[0]);
284 }
285
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]);
289 }
290
291 return parsed_office_hours;
292}
293
294/*
295 * Given a time string or the closed token, return a tuple
296 * of numbers representing opening and closing hours
297 */
298
299static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours)
300{
301
80c4c3cc
BB
302 int captures[(1 + 4) * 3];
303 ngx_int_t n;
fbdf81df
BB
304 ngx_uint_t * parsed_hours;
305
306 parsed_hours = malloc(2 * sizeof(ngx_uint_t));
307
308 if(ngx_strcmp(office_hours.data, CLOSED_TOKEN) == 0) {
309 parsed_hours[0] = 0;
310 parsed_hours[1] = 0;
311 return parsed_hours;
312 }
313
80c4c3cc
BB
314 n = ngx_regex_exec(rc.regex, &office_hours, captures, (1 + 4) * 3);
315
316 if (n >= 0) {
317 /* Opening Hours */
318
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]);
321
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]);
324
325 return parsed_hours;
326 }
327
328 /* Non-matching strings count as open */
329
fbdf81df
BB
330 parsed_hours[0] = 0;
331 parsed_hours[1] = 86400;
332 return parsed_hours;
333}
334
335/*
336 * Given an office hours array, it returns whether or not
337 * it is currently within office hours.
338 */
339
340static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours)
341{
342
343 time_t now;
344 ngx_uint_t day_of_week, seconds_of_day;
345 ngx_uint_t * current_hours;
346
347 ngx_time_update();
348 now = ngx_time();
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];
352
353 return seconds_of_day >= current_hours[0] && seconds_of_day <= current_hours[1];
354}
355
356/*
357 * Calculate the day of the week given a timestamp
358 */
359static ngx_uint_t get_day_of_week(time_t time)
360{
361
362 /* Epoch was thursday, so add 3 so we start on monday */
363 return (time / 86400 + 3) % 7;
364}
365
366/*
367 * Calculate the number of seconds elapsed today
368 */
369static ngx_uint_t get_seconds_of_day(time_t time)
370{
371
372 return time - (time / 86400) * 86400;
373}