]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/blob - ngx_http_office_hours_filter_module.c
Update contributing guide
[rbdr/ngx_http_office_hours_filter_module] / ngx_http_office_hours_filter_module.c
1 /*
2 * Copyright 2018 Rubén Beltrán del Río
3 *
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
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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
19 /*
20 * Declarations
21 */
22
23 /* Constants */
24
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}))?");
28
29 /* Main Configuration Structure */
30
31 typedef struct {
32 ngx_array_t *office_hours;
33 } ngx_http_office_hours_conf_t;
34
35 /* Lifecycle Functions For Module Context */
36
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);
41
42 /* Configuration Handler */
43
44 static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
45 void *conf);
46
47 /* Body Filter Storage */
48
49 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
50
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);
57
58 /* Compiled Regex */
59 ngx_regex_compile_t rc;
60
61 /*
62 * Module Definitions
63 */
64
65 /* Module Directives */
66
67 static ngx_command_t ngx_http_office_hours_commands[] = {
68 {
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),
74 NULL
75 },
76
77 ngx_null_command
78 };
79
80
81 /* Module Context */
82
83 static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = {
84 NULL, /* Preconfiguration */
85 ngx_http_office_hours_init, /* Postconfiguration */
86
87 NULL, /* Create main configuration */
88 NULL, /* Initialize main configuration */
89
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 */
95 };
96
97
98 /* Module Definition */
99
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
109 NULL, //Exit Thread
110 NULL, //Exit Process
111 NULL, //Exit Master
112 NGX_MODULE_V1_PADDING
113 };
114
115
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 */
121
122 static ngx_int_t
123 ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in)
124 {
125
126 ngx_uint_t ** parsed_office_hours;
127 ngx_http_office_hours_conf_t *conf;
128
129 conf =
130 ngx_http_get_module_loc_conf(r,
131 ngx_http_office_hours_filter_module);
132
133
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);
138 }
139
140 parsed_office_hours = parse_office_hours(conf->office_hours);
141
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);
146 }
147
148 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
149 "Outside office hours");
150
151 r->keepalive = 0;
152 return NGX_HTTP_FORBIDDEN;
153 }
154
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 */
160
161 static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
162 void *conf)
163 {
164
165 char *conf_structure = conf;
166
167 ngx_str_t *hours, *value;
168 ngx_array_t **office_hours;
169 ngx_uint_t i;
170
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);
177
178 if (*office_hours == NGX_CONF_UNSET_PTR) {
179 *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1,
180 sizeof(ngx_str_t));
181
182 if (*office_hours == NULL) {
183 return NGX_CONF_ERROR;
184 }
185 }
186 value = cf->args->elts;
187
188 for (i = 1; i < cf->args->nelts; ++i) {
189 hours = ngx_array_push(*office_hours);
190 if (hours == NULL) {
191 return NGX_CONF_ERROR;
192 }
193 *hours = value[i];
194 }
195
196 return NGX_CONF_OK;
197 }
198
199
200 /*
201 * Config Creator
202 * Initializes the configuration structure
203 */
204
205 static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf)
206 {
207
208 ngx_http_office_hours_conf_t *conf;
209
210 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t));
211
212 if (conf == NULL) {
213 return NULL;
214 }
215 conf->office_hours = NGX_CONF_UNSET_PTR;
216
217 return conf;
218 }
219
220 /*
221 * Merge Config Values
222 * Sets the defaults for the configuration and merges
223 * with other configurations
224 */
225
226 static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
227 void *parent, void *child)
228 {
229
230 ngx_http_office_hours_conf_t *prev = parent;
231 ngx_http_office_hours_conf_t *conf = child;
232
233 ngx_conf_merge_ptr_value(conf->office_hours, prev->office_hours, NULL);
234
235 return NGX_CONF_OK;
236 }
237
238 /*
239 * Postconfig Initialization Handler
240 * Sets the request filter at the top of the chain
241 */
242
243 static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf)
244 {
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
249 rc.pattern = TIME_REGEX;
250 rc.pool = cf->pool;
251 if (ngx_regex_compile(&rc) != NGX_OK) {
252 return NGX_ERROR;
253 }
254
255 return NGX_OK;
256 }
257
258 /*
259 * Parse the office hour strings in the configuration file
260 * to fill out the hours array (in seconds)
261 */
262
263 static 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
299 static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours)
300 {
301
302 int captures[(1 + 4) * 3];
303 ngx_int_t n;
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
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
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
340 static 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 */
359 static 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 */
369 static ngx_uint_t get_seconds_of_day(time_t time)
370 {
371
372 return time - (time / 86400) * 86400;
373 }