]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/blame - ngx_http_office_hours_filter_module.c
Use astyle instead of indent
[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
23/* Main Configuration Structure */
e4db4a20
BB
24
25typedef struct {
af4b488f
BB
26 ngx_array_t *office_hours;
27} ngx_http_office_hours_conf_t;
e4db4a20 28
fa2b280d
BB
29/* Lifecycle Functions For Module Context */
30
af4b488f 31static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf);
fa2b280d
BB
32static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
33 void *parent, void *child);
e4db4a20
BB
34static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf);
35
fa2b280d
BB
36/* Configuration Handler */
37
38static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
39 void *conf);
40
41/* Body Filter Storage */
42
43static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
44
45/*
46 * Module Definitions
47 */
e4db4a20 48
fa2b280d 49/* Module Directives */
e4db4a20
BB
50
51static ngx_command_t ngx_http_office_hours_commands[] = {
52 {
fa2b280d
BB
53 ngx_string("office_hours"),
54 NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_1MORE,
55 ngx_http_office_hours,
56 NGX_HTTP_LOC_CONF_OFFSET,
57 offsetof(ngx_http_office_hours_conf_t, office_hours),
58 NULL
e4db4a20
BB
59 },
60
61 ngx_null_command
62};
63
fa2b280d
BB
64
65/* Module Context */
e4db4a20
BB
66
67static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = {
fa2b280d
BB
68 NULL, /* Preconfiguration */
69 ngx_http_office_hours_init, /* Postconfiguration */
e4db4a20 70
fa2b280d
BB
71 NULL, /* Create main configuration */
72 NULL, /* Initialize main configuration */
e4db4a20 73
fa2b280d
BB
74 NULL, /* Create server configuration */
75 NULL, /* Merge server configuration */
76
77 ngx_http_office_hours_create_conf, /* Create location configuration */
78 ngx_http_office_hours_merge_conf /* Merge location configuration */
e4db4a20
BB
79};
80
fa2b280d
BB
81
82/* Module Definition */
e4db4a20
BB
83
84ngx_module_t ngx_http_office_hours_filter_module = {
fa2b280d
BB
85 NGX_MODULE_V1, //Module Version
86 &ngx_http_office_hours_filter_module_ctx, //Module context
87 ngx_http_office_hours_commands, //Module commands
88 NGX_HTTP_MODULE, //Module Type
89 NULL, //Initialize Master
90 NULL, //Initialize Module
91 NULL, //Initialize Process
92 NULL, //Initialize Thread
93 NULL, //Exit Thread
94 NULL, //Exit Process
95 NULL, //Exit Master
96 NGX_MODULE_V1_PADDING
e4db4a20
BB
97};
98
99
fa2b280d
BB
100/*
101 * Main Body Filter
102 * If the current time is within office hours, it goes to the next
103 * handler. Otherwise it returns 403 and the office hour listing.
104 */
e4db4a20 105
af4b488f 106static ngx_int_t
fa2b280d 107ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in)
af4b488f 108{
e4db4a20 109
af4b488f
BB
110 ngx_http_office_hours_conf_t *conf;
111 ngx_uint_t i;
112 ngx_str_t *hours;
e4db4a20 113
fa2b280d
BB
114 conf =
115 ngx_http_get_module_loc_conf(r,
116 ngx_http_office_hours_filter_module);
e4db4a20
BB
117
118 if (conf->office_hours == NULL) {
fa2b280d
BB
119 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
120 "Within office hours");
121 return ngx_http_next_body_filter(r, in);
e4db4a20 122 }
fa2b280d
BB
123
124 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
125 "Outside office hours");
e4db4a20
BB
126
127 hours = conf->office_hours->elts;
128
129 for (i = 0; i < conf->office_hours->nelts; ++i) {
fa2b280d
BB
130 ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
131 (const char *) hours[i].data);
e4db4a20
BB
132 }
133
134 r->keepalive = 0;
135 return NGX_HTTP_FORBIDDEN;
136}
137
fa2b280d
BB
138/*
139 * Callback for `office_hours ` directive
140 * Reads the configuration loaded from the config file(cf)
141 * And writes it to the right place in the module configuration(conf)
142 */
e4db4a20 143
fa2b280d
BB
144static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
145 void *conf)
af4b488f 146{
e4db4a20 147
af4b488f 148 char *conf_structure = conf;
e4db4a20 149
af4b488f
BB
150 ngx_array_t **office_hours;
151 ngx_str_t *hours;
152 ngx_str_t *value;
153 ngx_uint_t i;
e4db4a20 154
fa2b280d
BB
155 /* Gets the array from the config structure using the defined
156 * offset, and if the pointer is unset it creates a new one.
157 * (The first element is the directive itself, so we should be
158 * offset by 1)
159 */
160 office_hours = (ngx_array_t **) (conf_structure + cmd->offset);
e4db4a20
BB
161
162 if (*office_hours == NGX_CONF_UNSET_PTR) {
fa2b280d
BB
163 *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1,
164 sizeof(ngx_str_t));
e4db4a20 165
fa2b280d
BB
166 if (*office_hours == NULL) {
167 return NGX_CONF_ERROR;
168 }
e4db4a20 169 }
e4db4a20
BB
170 value = cf->args->elts;
171
172 for (i = 1; i < cf->args->nelts; ++i) {
fa2b280d
BB
173 hours = ngx_array_push(*office_hours);
174 if (hours == NULL) {
175 return NGX_CONF_ERROR;
176 }
177 *hours = value[i];
e4db4a20
BB
178 }
179
180 return NGX_CONF_OK;
181}
182
183
fa2b280d
BB
184/*
185 * Config Creator
186 * Initializes the configuration structure
187 */
e4db4a20 188
fa2b280d 189static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf)
af4b488f 190{
e4db4a20 191
af4b488f 192 ngx_http_office_hours_conf_t *conf;
e4db4a20
BB
193
194 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t));
195
196 if (conf == NULL) {
fa2b280d 197 return NULL;
e4db4a20 198 }
e4db4a20
BB
199 conf->office_hours = NGX_CONF_UNSET_PTR;
200
201 return conf;
202}
203
fa2b280d
BB
204/*
205 * Merge Config Values
206 * Sets the defaults for the configuration and merges
207 * with other configurations
208 */
e4db4a20 209
fa2b280d
BB
210static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
211 void *parent, void *child)
af4b488f 212{
e4db4a20 213
af4b488f
BB
214 ngx_http_office_hours_conf_t *prev = parent;
215 ngx_http_office_hours_conf_t *conf = child;
e4db4a20
BB
216
217 ngx_conf_merge_ptr_value(conf->office_hours, prev->office_hours, NULL);
218
219 return NGX_CONF_OK;
220}
221
fa2b280d
BB
222/*
223 * Postconfig Initialization Handler
224 * Sets the request filter at the top of the chain
225 */
e4db4a20 226
fa2b280d 227static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf)
af4b488f 228{
e4db4a20
BB
229
230 ngx_http_next_body_filter = ngx_http_top_body_filter;
231 ngx_http_top_body_filter = ngx_http_office_hours_body_filter;
232
233 return NGX_OK;
234}