]>
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"; | |
27 | ||
fa2b280d | 28 | /* Main Configuration Structure */ |
e4db4a20 BB |
29 | |
30 | typedef struct { | |
af4b488f BB |
31 | ngx_array_t *office_hours; |
32 | } ngx_http_office_hours_conf_t; | |
e4db4a20 | 33 | |
fa2b280d BB |
34 | /* Lifecycle Functions For Module Context */ |
35 | ||
af4b488f | 36 | static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf); |
fa2b280d BB |
37 | static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, |
38 | void *parent, void *child); | |
e4db4a20 BB |
39 | static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf); |
40 | ||
fa2b280d BB |
41 | /* Configuration Handler */ |
42 | ||
43 | static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, | |
44 | void *conf); | |
45 | ||
46 | /* Body Filter Storage */ | |
47 | ||
48 | static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
49 | ||
fbdf81df BB |
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); | |
56 | ||
fa2b280d BB |
57 | /* |
58 | * Module Definitions | |
59 | */ | |
e4db4a20 | 60 | |
fa2b280d | 61 | /* Module Directives */ |
e4db4a20 BB |
62 | |
63 | static ngx_command_t ngx_http_office_hours_commands[] = { | |
64 | { | |
fa2b280d | 65 | ngx_string("office_hours"), |
fbdf81df | 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, |
fa2b280d BB |
67 | ngx_http_office_hours, |
68 | NGX_HTTP_LOC_CONF_OFFSET, | |
69 | offsetof(ngx_http_office_hours_conf_t, office_hours), | |
70 | NULL | |
e4db4a20 BB |
71 | }, |
72 | ||
73 | ngx_null_command | |
74 | }; | |
75 | ||
fa2b280d BB |
76 | |
77 | /* Module Context */ | |
e4db4a20 BB |
78 | |
79 | static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = { | |
fa2b280d BB |
80 | NULL, /* Preconfiguration */ |
81 | ngx_http_office_hours_init, /* Postconfiguration */ | |
e4db4a20 | 82 | |
fa2b280d BB |
83 | NULL, /* Create main configuration */ |
84 | NULL, /* Initialize main configuration */ | |
e4db4a20 | 85 | |
fa2b280d BB |
86 | NULL, /* Create server configuration */ |
87 | NULL, /* Merge server configuration */ | |
88 | ||
89 | ngx_http_office_hours_create_conf, /* Create location configuration */ | |
90 | ngx_http_office_hours_merge_conf /* Merge location configuration */ | |
e4db4a20 BB |
91 | }; |
92 | ||
fa2b280d BB |
93 | |
94 | /* Module Definition */ | |
e4db4a20 BB |
95 | |
96 | ngx_module_t ngx_http_office_hours_filter_module = { | |
fa2b280d BB |
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 | |
105 | NULL, //Exit Thread | |
106 | NULL, //Exit Process | |
107 | NULL, //Exit Master | |
108 | NGX_MODULE_V1_PADDING | |
e4db4a20 BB |
109 | }; |
110 | ||
111 | ||
fa2b280d BB |
112 | /* |
113 | * Main Body Filter | |
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. | |
116 | */ | |
e4db4a20 | 117 | |
af4b488f | 118 | static ngx_int_t |
fa2b280d | 119 | ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in) |
af4b488f | 120 | { |
e4db4a20 | 121 | |
fbdf81df | 122 | ngx_uint_t ** parsed_office_hours; |
af4b488f | 123 | ngx_http_office_hours_conf_t *conf; |
e4db4a20 | 124 | |
fa2b280d BB |
125 | conf = |
126 | ngx_http_get_module_loc_conf(r, | |
127 | ngx_http_office_hours_filter_module); | |
e4db4a20 | 128 | |
fbdf81df | 129 | |
e4db4a20 | 130 | if (conf->office_hours == NULL) { |
fa2b280d | 131 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, |
fbdf81df | 132 | "Office hours disabled"); |
fa2b280d | 133 | return ngx_http_next_body_filter(r, in); |
e4db4a20 | 134 | } |
fa2b280d | 135 | |
fbdf81df | 136 | parsed_office_hours = parse_office_hours(conf->office_hours); |
e4db4a20 | 137 | |
fbdf81df | 138 | if (within_office_hours(parsed_office_hours)) { |
fa2b280d | 139 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, |
fbdf81df BB |
140 | "Within office hours"); |
141 | return ngx_http_next_body_filter(r, in); | |
e4db4a20 BB |
142 | } |
143 | ||
fbdf81df BB |
144 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, |
145 | "Outside office hours"); | |
146 | ||
e4db4a20 BB |
147 | r->keepalive = 0; |
148 | return NGX_HTTP_FORBIDDEN; | |
149 | } | |
150 | ||
fa2b280d BB |
151 | /* |
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) | |
155 | */ | |
e4db4a20 | 156 | |
fa2b280d BB |
157 | static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, |
158 | void *conf) | |
af4b488f | 159 | { |
e4db4a20 | 160 | |
af4b488f | 161 | char *conf_structure = conf; |
e4db4a20 | 162 | |
fbdf81df | 163 | ngx_str_t *hours, *value; |
af4b488f | 164 | ngx_array_t **office_hours; |
af4b488f | 165 | ngx_uint_t i; |
e4db4a20 | 166 | |
fa2b280d BB |
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 | |
170 | * offset by 1) | |
171 | */ | |
172 | office_hours = (ngx_array_t **) (conf_structure + cmd->offset); | |
e4db4a20 BB |
173 | |
174 | if (*office_hours == NGX_CONF_UNSET_PTR) { | |
fa2b280d BB |
175 | *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1, |
176 | sizeof(ngx_str_t)); | |
e4db4a20 | 177 | |
fa2b280d BB |
178 | if (*office_hours == NULL) { |
179 | return NGX_CONF_ERROR; | |
180 | } | |
e4db4a20 | 181 | } |
e4db4a20 BB |
182 | value = cf->args->elts; |
183 | ||
184 | for (i = 1; i < cf->args->nelts; ++i) { | |
fa2b280d BB |
185 | hours = ngx_array_push(*office_hours); |
186 | if (hours == NULL) { | |
187 | return NGX_CONF_ERROR; | |
188 | } | |
189 | *hours = value[i]; | |
e4db4a20 BB |
190 | } |
191 | ||
192 | return NGX_CONF_OK; | |
193 | } | |
194 | ||
195 | ||
fa2b280d BB |
196 | /* |
197 | * Config Creator | |
198 | * Initializes the configuration structure | |
199 | */ | |
e4db4a20 | 200 | |
fa2b280d | 201 | static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf) |
af4b488f | 202 | { |
e4db4a20 | 203 | |
af4b488f | 204 | ngx_http_office_hours_conf_t *conf; |
e4db4a20 BB |
205 | |
206 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t)); | |
207 | ||
208 | if (conf == NULL) { | |
fa2b280d | 209 | return NULL; |
e4db4a20 | 210 | } |
e4db4a20 BB |
211 | conf->office_hours = NGX_CONF_UNSET_PTR; |
212 | ||
213 | return conf; | |
214 | } | |
215 | ||
fa2b280d BB |
216 | /* |
217 | * Merge Config Values | |
218 | * Sets the defaults for the configuration and merges | |
219 | * with other configurations | |
220 | */ | |
e4db4a20 | 221 | |
fa2b280d BB |
222 | static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, |
223 | void *parent, void *child) | |
af4b488f | 224 | { |
e4db4a20 | 225 | |
af4b488f BB |
226 | ngx_http_office_hours_conf_t *prev = parent; |
227 | ngx_http_office_hours_conf_t *conf = child; | |
e4db4a20 BB |
228 | |
229 | ngx_conf_merge_ptr_value(conf->office_hours, prev->office_hours, NULL); | |
230 | ||
231 | return NGX_CONF_OK; | |
232 | } | |
233 | ||
fa2b280d BB |
234 | /* |
235 | * Postconfig Initialization Handler | |
236 | * Sets the request filter at the top of the chain | |
237 | */ | |
e4db4a20 | 238 | |
fa2b280d | 239 | static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf) |
af4b488f | 240 | { |
e4db4a20 BB |
241 | |
242 | ngx_http_next_body_filter = ngx_http_top_body_filter; | |
243 | ngx_http_top_body_filter = ngx_http_office_hours_body_filter; | |
244 | ||
245 | return NGX_OK; | |
246 | } | |
fbdf81df BB |
247 | |
248 | /* | |
249 | * Parse the office hour strings in the configuration file | |
250 | * to fill out the hours array (in seconds) | |
251 | */ | |
252 | ||
253 | static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours) | |
254 | { | |
255 | ||
256 | ngx_str_t *hours; | |
257 | ngx_uint_t ** parsed_office_hours; | |
258 | ngx_uint_t i, j; | |
259 | ||
260 | parsed_office_hours = malloc(7 * sizeof(ngx_uint_t *)); | |
261 | ||
262 | hours = office_hours->elts; | |
263 | ||
264 | /* | |
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 | |
270 | */ | |
271 | ||
272 | for (i = 0; i < WEEK_LENGTH + 1 - office_hours->nelts; ++i) { | |
273 | parsed_office_hours[i] = parse_office_hours_string(hours[0]); | |
274 | } | |
275 | ||
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]); | |
279 | } | |
280 | ||
281 | return parsed_office_hours; | |
282 | } | |
283 | ||
284 | /* | |
285 | * Given a time string or the closed token, return a tuple | |
286 | * of numbers representing opening and closing hours | |
287 | */ | |
288 | ||
289 | static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours) | |
290 | { | |
291 | ||
292 | ngx_uint_t * parsed_hours; | |
293 | ||
294 | parsed_hours = malloc(2 * sizeof(ngx_uint_t)); | |
295 | ||
296 | if(ngx_strcmp(office_hours.data, CLOSED_TOKEN) == 0) { | |
297 | parsed_hours[0] = 0; | |
298 | parsed_hours[1] = 0; | |
299 | return parsed_hours; | |
300 | } | |
301 | ||
302 | parsed_hours[0] = 0; | |
303 | parsed_hours[1] = 86400; | |
304 | return parsed_hours; | |
305 | } | |
306 | ||
307 | /* | |
308 | * Given an office hours array, it returns whether or not | |
309 | * it is currently within office hours. | |
310 | */ | |
311 | ||
312 | static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours) | |
313 | { | |
314 | ||
315 | time_t now; | |
316 | ngx_uint_t day_of_week, seconds_of_day; | |
317 | ngx_uint_t * current_hours; | |
318 | ||
319 | ngx_time_update(); | |
320 | now = ngx_time(); | |
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]; | |
324 | ||
325 | return seconds_of_day >= current_hours[0] && seconds_of_day <= current_hours[1]; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Calculate the day of the week given a timestamp | |
330 | */ | |
331 | static ngx_uint_t get_day_of_week(time_t time) | |
332 | { | |
333 | ||
334 | /* Epoch was thursday, so add 3 so we start on monday */ | |
335 | return (time / 86400 + 3) % 7; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Calculate the number of seconds elapsed today | |
340 | */ | |
341 | static ngx_uint_t get_seconds_of_day(time_t time) | |
342 | { | |
343 | ||
344 | return time - (time / 86400) * 86400; | |
345 | } |