]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | const ngx_uint_t WEEK_LENGTH = 7; | |
26 | const char * CLOSED_TOKEN = "closed"; | |
80c4c3cc | 27 | const ngx_str_t TIME_REGEX = ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?"); |
67206816 | 28 | const ngx_str_t OUTPUT_HTML = ngx_string("<!doctype html><html><head><title>This website is currently closed!</title></head><body><h1>This website is currently closed!</h1><p>This website has closed for the day, please check our office hours below</p></body></html>"); |
fbdf81df | 29 | |
fa2b280d | 30 | /* Main Configuration Structure */ |
e4db4a20 BB |
31 | |
32 | typedef struct { | |
af4b488f BB |
33 | ngx_array_t *office_hours; |
34 | } ngx_http_office_hours_conf_t; | |
e4db4a20 | 35 | |
fa2b280d BB |
36 | /* Lifecycle Functions For Module Context */ |
37 | ||
af4b488f | 38 | static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf); |
fa2b280d BB |
39 | static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, |
40 | void *parent, void *child); | |
e4db4a20 BB |
41 | static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf); |
42 | ||
fa2b280d BB |
43 | /* Configuration Handler */ |
44 | ||
45 | static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, | |
46 | void *conf); | |
47 | ||
67206816 | 48 | /* Filter Storage */ |
fa2b280d | 49 | |
67206816 | 50 | static ngx_http_output_header_filter_pt ngx_http_next_header_filter; |
fa2b280d BB |
51 | static ngx_http_output_body_filter_pt ngx_http_next_body_filter; |
52 | ||
fbdf81df BB |
53 | /* Utility Functions */ |
54 | static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours); | |
55 | static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours); | |
56 | static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours); | |
57 | static ngx_uint_t get_day_of_week(time_t time); | |
58 | static ngx_uint_t get_seconds_of_day(time_t time); | |
bbe15175 | 59 | static ngx_uint_t parse_number(ngx_str_t string, ngx_uint_t start, ngx_uint_t end); |
fbdf81df | 60 | |
80c4c3cc BB |
61 | /* Compiled Regex */ |
62 | ngx_regex_compile_t rc; | |
63 | ||
fa2b280d BB |
64 | /* |
65 | * Module Definitions | |
66 | */ | |
e4db4a20 | 67 | |
fa2b280d | 68 | /* Module Directives */ |
e4db4a20 BB |
69 | |
70 | static ngx_command_t ngx_http_office_hours_commands[] = { | |
71 | { | |
fa2b280d | 72 | ngx_string("office_hours"), |
fbdf81df | 73 | 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 |
74 | ngx_http_office_hours, |
75 | NGX_HTTP_LOC_CONF_OFFSET, | |
76 | offsetof(ngx_http_office_hours_conf_t, office_hours), | |
77 | NULL | |
e4db4a20 BB |
78 | }, |
79 | ||
80 | ngx_null_command | |
81 | }; | |
82 | ||
fa2b280d BB |
83 | |
84 | /* Module Context */ | |
e4db4a20 BB |
85 | |
86 | static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = { | |
fa2b280d BB |
87 | NULL, /* Preconfiguration */ |
88 | ngx_http_office_hours_init, /* Postconfiguration */ | |
e4db4a20 | 89 | |
fa2b280d BB |
90 | NULL, /* Create main configuration */ |
91 | NULL, /* Initialize main configuration */ | |
e4db4a20 | 92 | |
fa2b280d BB |
93 | NULL, /* Create server configuration */ |
94 | NULL, /* Merge server configuration */ | |
95 | ||
96 | ngx_http_office_hours_create_conf, /* Create location configuration */ | |
97 | ngx_http_office_hours_merge_conf /* Merge location configuration */ | |
e4db4a20 BB |
98 | }; |
99 | ||
fa2b280d BB |
100 | |
101 | /* Module Definition */ | |
e4db4a20 BB |
102 | |
103 | ngx_module_t ngx_http_office_hours_filter_module = { | |
fa2b280d BB |
104 | NGX_MODULE_V1, //Module Version |
105 | &ngx_http_office_hours_filter_module_ctx, //Module context | |
106 | ngx_http_office_hours_commands, //Module commands | |
107 | NGX_HTTP_MODULE, //Module Type | |
108 | NULL, //Initialize Master | |
109 | NULL, //Initialize Module | |
110 | NULL, //Initialize Process | |
111 | NULL, //Initialize Thread | |
112 | NULL, //Exit Thread | |
113 | NULL, //Exit Process | |
114 | NULL, //Exit Master | |
115 | NGX_MODULE_V1_PADDING | |
e4db4a20 BB |
116 | }; |
117 | ||
118 | ||
67206816 BB |
119 | /* |
120 | * Main Header Filter | |
121 | * If the current time is within office hours, it goes to the next | |
122 | * handler. Otherwise it sets the headers to 403 | |
123 | */ | |
124 | ||
125 | static ngx_int_t | |
126 | ngx_http_office_hours_header_filter(ngx_http_request_t * r) | |
127 | { | |
128 | ||
129 | ngx_uint_t ** parsed_office_hours; | |
130 | ngx_http_office_hours_conf_t *conf; | |
131 | ||
132 | conf = | |
133 | ngx_http_get_module_loc_conf(r, | |
134 | ngx_http_office_hours_filter_module); | |
135 | ||
136 | ||
137 | if (conf->office_hours == NULL) { | |
138 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, | |
139 | "Office hours disabled"); | |
140 | return ngx_http_next_header_filter(r); | |
141 | } | |
142 | ||
143 | parsed_office_hours = parse_office_hours(conf->office_hours); | |
144 | ||
145 | if (within_office_hours(parsed_office_hours)) { | |
146 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, | |
147 | "Within office hours"); | |
148 | return ngx_http_next_header_filter(r); | |
149 | } | |
150 | ||
151 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, | |
152 | "Outside office hours"); | |
153 | ||
154 | r->headers_out.status = NGX_HTTP_FORBIDDEN; | |
155 | r->headers_out.content_length_n = OUTPUT_HTML.len; | |
156 | ||
157 | return ngx_http_next_header_filter(r); | |
158 | } | |
159 | ||
fa2b280d BB |
160 | /* |
161 | * Main Body Filter | |
162 | * If the current time is within office hours, it goes to the next | |
67206816 | 163 | * handler. Otherwise it replaces the body with the office hours. |
fa2b280d | 164 | */ |
e4db4a20 | 165 | |
af4b488f | 166 | static ngx_int_t |
67206816 | 167 | ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t *in) |
af4b488f | 168 | { |
e4db4a20 | 169 | |
67206816 | 170 | ngx_buf_t *b; |
fbdf81df | 171 | ngx_uint_t ** parsed_office_hours; |
67206816 | 172 | ngx_chain_t out; |
af4b488f | 173 | ngx_http_office_hours_conf_t *conf; |
e4db4a20 | 174 | |
fa2b280d BB |
175 | conf = |
176 | ngx_http_get_module_loc_conf(r, | |
177 | ngx_http_office_hours_filter_module); | |
e4db4a20 | 178 | |
fbdf81df | 179 | |
e4db4a20 | 180 | if (conf->office_hours == NULL) { |
fa2b280d | 181 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, |
fbdf81df | 182 | "Office hours disabled"); |
fa2b280d | 183 | return ngx_http_next_body_filter(r, in); |
e4db4a20 | 184 | } |
fa2b280d | 185 | |
fbdf81df | 186 | parsed_office_hours = parse_office_hours(conf->office_hours); |
e4db4a20 | 187 | |
fbdf81df | 188 | if (within_office_hours(parsed_office_hours)) { |
fa2b280d | 189 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, |
fbdf81df BB |
190 | "Within office hours"); |
191 | return ngx_http_next_body_filter(r, in); | |
e4db4a20 BB |
192 | } |
193 | ||
fbdf81df BB |
194 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, |
195 | "Outside office hours"); | |
196 | ||
67206816 BB |
197 | b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); |
198 | if (b == NULL) { | |
199 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer."); | |
200 | return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
201 | } | |
202 | ||
203 | out.buf = b; | |
204 | out.next = NULL; | |
205 | ||
206 | b->start = OUTPUT_HTML.data; | |
207 | b->pos = b->start; | |
208 | b->end = OUTPUT_HTML.data + OUTPUT_HTML.len; | |
209 | b->last = b->end; | |
210 | ||
211 | b->memory = 1; | |
212 | b->last_buf = 1; | |
213 | ||
214 | return ngx_http_next_body_filter(r, &out); | |
e4db4a20 BB |
215 | } |
216 | ||
fa2b280d BB |
217 | /* |
218 | * Callback for `office_hours ` directive | |
219 | * Reads the configuration loaded from the config file(cf) | |
220 | * And writes it to the right place in the module configuration(conf) | |
221 | */ | |
e4db4a20 | 222 | |
fa2b280d BB |
223 | static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, |
224 | void *conf) | |
af4b488f | 225 | { |
e4db4a20 | 226 | |
af4b488f | 227 | char *conf_structure = conf; |
e4db4a20 | 228 | |
fbdf81df | 229 | ngx_str_t *hours, *value; |
af4b488f | 230 | ngx_array_t **office_hours; |
af4b488f | 231 | ngx_uint_t i; |
e4db4a20 | 232 | |
fa2b280d BB |
233 | /* Gets the array from the config structure using the defined |
234 | * offset, and if the pointer is unset it creates a new one. | |
235 | * (The first element is the directive itself, so we should be | |
236 | * offset by 1) | |
237 | */ | |
238 | office_hours = (ngx_array_t **) (conf_structure + cmd->offset); | |
e4db4a20 BB |
239 | |
240 | if (*office_hours == NGX_CONF_UNSET_PTR) { | |
fa2b280d BB |
241 | *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1, |
242 | sizeof(ngx_str_t)); | |
e4db4a20 | 243 | |
fa2b280d BB |
244 | if (*office_hours == NULL) { |
245 | return NGX_CONF_ERROR; | |
246 | } | |
e4db4a20 | 247 | } |
e4db4a20 BB |
248 | value = cf->args->elts; |
249 | ||
250 | for (i = 1; i < cf->args->nelts; ++i) { | |
fa2b280d BB |
251 | hours = ngx_array_push(*office_hours); |
252 | if (hours == NULL) { | |
253 | return NGX_CONF_ERROR; | |
254 | } | |
255 | *hours = value[i]; | |
e4db4a20 BB |
256 | } |
257 | ||
258 | return NGX_CONF_OK; | |
259 | } | |
260 | ||
261 | ||
fa2b280d BB |
262 | /* |
263 | * Config Creator | |
264 | * Initializes the configuration structure | |
265 | */ | |
e4db4a20 | 266 | |
fa2b280d | 267 | static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf) |
af4b488f | 268 | { |
e4db4a20 | 269 | |
af4b488f | 270 | ngx_http_office_hours_conf_t *conf; |
e4db4a20 BB |
271 | |
272 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t)); | |
273 | ||
274 | if (conf == NULL) { | |
fa2b280d | 275 | return NULL; |
e4db4a20 | 276 | } |
e4db4a20 BB |
277 | conf->office_hours = NGX_CONF_UNSET_PTR; |
278 | ||
279 | return conf; | |
280 | } | |
281 | ||
fa2b280d BB |
282 | /* |
283 | * Merge Config Values | |
284 | * Sets the defaults for the configuration and merges | |
285 | * with other configurations | |
286 | */ | |
e4db4a20 | 287 | |
fa2b280d BB |
288 | static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, |
289 | void *parent, void *child) | |
af4b488f | 290 | { |
e4db4a20 | 291 | |
af4b488f BB |
292 | ngx_http_office_hours_conf_t *prev = parent; |
293 | ngx_http_office_hours_conf_t *conf = child; | |
e4db4a20 BB |
294 | |
295 | ngx_conf_merge_ptr_value(conf->office_hours, prev->office_hours, NULL); | |
296 | ||
297 | return NGX_CONF_OK; | |
298 | } | |
299 | ||
fbdf81df BB |
300 | /* |
301 | * Parse the office hour strings in the configuration file | |
302 | * to fill out the hours array (in seconds) | |
303 | */ | |
304 | ||
305 | static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours) | |
306 | { | |
307 | ||
308 | ngx_str_t *hours; | |
309 | ngx_uint_t ** parsed_office_hours; | |
310 | ngx_uint_t i, j; | |
311 | ||
312 | parsed_office_hours = malloc(7 * sizeof(ngx_uint_t *)); | |
313 | ||
314 | hours = office_hours->elts; | |
315 | ||
316 | /* | |
317 | * On the configuration file, the leftmost element | |
318 | * always applies to all remaining days, all others | |
319 | * are read from right to left. So first we will apply | |
320 | * the initial override, and then iterate based on the | |
321 | * number of overrides | |
322 | */ | |
323 | ||
324 | for (i = 0; i < WEEK_LENGTH + 1 - office_hours->nelts; ++i) { | |
325 | parsed_office_hours[i] = parse_office_hours_string(hours[0]); | |
326 | } | |
327 | ||
328 | for (i = 1; i < office_hours->nelts; ++i) { | |
329 | j = WEEK_LENGTH - office_hours->nelts + i; | |
330 | parsed_office_hours[j] = parse_office_hours_string(hours[i]); | |
331 | } | |
332 | ||
333 | return parsed_office_hours; | |
334 | } | |
335 | ||
336 | /* | |
337 | * Given a time string or the closed token, return a tuple | |
338 | * of numbers representing opening and closing hours | |
339 | */ | |
340 | ||
341 | static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours) | |
342 | { | |
343 | ||
80c4c3cc BB |
344 | int captures[(1 + 4) * 3]; |
345 | ngx_int_t n; | |
fbdf81df BB |
346 | ngx_uint_t * parsed_hours; |
347 | ||
348 | parsed_hours = malloc(2 * sizeof(ngx_uint_t)); | |
349 | ||
350 | if(ngx_strcmp(office_hours.data, CLOSED_TOKEN) == 0) { | |
351 | parsed_hours[0] = 0; | |
352 | parsed_hours[1] = 0; | |
353 | return parsed_hours; | |
354 | } | |
355 | ||
80c4c3cc BB |
356 | n = ngx_regex_exec(rc.regex, &office_hours, captures, (1 + 4) * 3); |
357 | ||
358 | if (n >= 0) { | |
359 | /* Opening Hours */ | |
360 | ||
bbe15175 BB |
361 | parsed_hours[0] = 60 * 60 * parse_number(office_hours, captures[2], captures[3]); |
362 | parsed_hours[0] = parsed_hours[0] + 60 * parse_number(office_hours, captures[4], captures[5]); | |
80c4c3cc | 363 | |
bbe15175 BB |
364 | parsed_hours[1] = 60 * 60 * parse_number(office_hours, captures[6], captures[7]); |
365 | parsed_hours[1] = parsed_hours[1] + 60 * parse_number(office_hours, captures[8], captures[9]); | |
80c4c3cc BB |
366 | |
367 | return parsed_hours; | |
368 | } | |
369 | ||
370 | /* Non-matching strings count as open */ | |
371 | ||
fbdf81df BB |
372 | parsed_hours[0] = 0; |
373 | parsed_hours[1] = 86400; | |
374 | return parsed_hours; | |
375 | } | |
376 | ||
377 | /* | |
378 | * Given an office hours array, it returns whether or not | |
379 | * it is currently within office hours. | |
380 | */ | |
381 | ||
382 | static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours) | |
383 | { | |
384 | ||
385 | time_t now; | |
386 | ngx_uint_t day_of_week, seconds_of_day; | |
387 | ngx_uint_t * current_hours; | |
388 | ||
389 | ngx_time_update(); | |
390 | now = ngx_time(); | |
391 | day_of_week = get_day_of_week(now); | |
392 | seconds_of_day = get_seconds_of_day(now); | |
393 | current_hours = office_hours[day_of_week]; | |
394 | ||
395 | return seconds_of_day >= current_hours[0] && seconds_of_day <= current_hours[1]; | |
396 | } | |
397 | ||
398 | /* | |
399 | * Calculate the day of the week given a timestamp | |
400 | */ | |
401 | static ngx_uint_t get_day_of_week(time_t time) | |
402 | { | |
403 | ||
404 | /* Epoch was thursday, so add 3 so we start on monday */ | |
405 | return (time / 86400 + 3) % 7; | |
406 | } | |
407 | ||
408 | /* | |
409 | * Calculate the number of seconds elapsed today | |
410 | */ | |
411 | static ngx_uint_t get_seconds_of_day(time_t time) | |
412 | { | |
413 | ||
414 | return time - (time / 86400) * 86400; | |
415 | } | |
bbe15175 BB |
416 | |
417 | /* | |
418 | * Parses a string, returns 0 if match was not found | |
419 | */ | |
420 | static ngx_uint_t parse_number(ngx_str_t string, ngx_uint_t start, ngx_uint_t end) | |
421 | { | |
422 | ||
423 | if (end - start == 0) { | |
424 | return 0; | |
425 | } | |
426 | ||
427 | return ngx_atoi(&string.data[start], end - start); | |
428 | } | |
429 | ||
430 | /* | |
431 | * Postconfig Initialization Handler | |
432 | * Sets the request filter at the top of the chain | |
433 | */ | |
434 | ||
435 | static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf) | |
436 | { | |
437 | ||
67206816 BB |
438 | ngx_http_next_header_filter = ngx_http_top_header_filter; |
439 | ngx_http_top_header_filter = ngx_http_office_hours_header_filter; | |
440 | ||
bbe15175 BB |
441 | ngx_http_next_body_filter = ngx_http_top_body_filter; |
442 | ngx_http_top_body_filter = ngx_http_office_hours_body_filter; | |
443 | ||
444 | rc.pattern = TIME_REGEX; | |
445 | rc.pool = cf->pool; | |
446 | if (ngx_regex_compile(&rc) != NGX_OK) { | |
447 | return NGX_ERROR; | |
448 | } | |
449 | ||
450 | return NGX_OK; | |
451 | } |