]>
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 | ||
19 | // Module Config Definitions | |
20 | ||
21 | typedef struct { | |
af4b488f BB |
22 | ngx_array_t *office_hours; |
23 | } ngx_http_office_hours_conf_t; | |
e4db4a20 | 24 | |
af4b488f BB |
25 | static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf); |
26 | static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child); | |
e4db4a20 BB |
27 | static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf); |
28 | ||
af4b488f | 29 | static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf); |
e4db4a20 BB |
30 | |
31 | //Module Directive Definitions | |
32 | ||
33 | static ngx_command_t ngx_http_office_hours_commands[] = { | |
34 | { | |
af4b488f BB |
35 | ngx_string("office_hours"), |
36 | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_1MORE, | |
37 | ngx_http_office_hours, | |
38 | NGX_HTTP_LOC_CONF_OFFSET, | |
39 | offsetof(ngx_http_office_hours_conf_t, office_hours), | |
40 | NULL | |
e4db4a20 BB |
41 | }, |
42 | ||
43 | ngx_null_command | |
44 | }; | |
45 | ||
46 | //Define the module context | |
47 | ||
48 | static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = { | |
49 | NULL, //Preconfiguration | |
50 | ngx_http_office_hours_init, //Postconfiguration | |
51 | ||
52 | NULL, //Create main configuration | |
53 | NULL, //Initialize main configuration | |
54 | NULL, //Create server configuration | |
55 | NULL, //Merge server configuration | |
56 | ||
57 | ngx_http_office_hours_create_conf, //Create location configuration | |
58 | ngx_http_office_hours_merge_conf // Merge location configuration | |
59 | }; | |
60 | ||
61 | //Define the module | |
62 | ||
63 | ngx_module_t ngx_http_office_hours_filter_module = { | |
64 | NGX_MODULE_V1, //Module Version | |
af4b488f BB |
65 | & ngx_http_office_hours_filter_module_ctx, //Module context |
66 | ngx_http_office_hours_commands, //Module commands | |
67 | NGX_HTTP_MODULE, //Module Type | |
68 | NULL, //Initialize Master | |
69 | NULL, //Initialize Module | |
70 | NULL, //Initialize Process | |
71 | NULL, //Initialize Thread | |
72 | NULL, //Exit Thread | |
73 | NULL, //Exit Process | |
74 | NULL, //Exit Master | |
75 | NGX_MODULE_V1_PADDING | |
e4db4a20 BB |
76 | }; |
77 | ||
78 | ||
79 | //Main Body Filter | |
80 | ||
81 | static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
82 | ||
af4b488f BB |
83 | static ngx_int_t |
84 | ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in) | |
85 | { | |
e4db4a20 | 86 | |
af4b488f BB |
87 | ngx_http_office_hours_conf_t *conf; |
88 | ngx_uint_t i; | |
89 | ngx_str_t *hours; | |
e4db4a20 BB |
90 | |
91 | conf = ngx_http_get_module_loc_conf(r, ngx_http_office_hours_filter_module); | |
92 | ||
93 | if (conf->office_hours == NULL) { | |
94 | ||
af4b488f | 95 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Within office hours"); |
e4db4a20 | 96 | |
af4b488f | 97 | return ngx_http_next_body_filter(r, in); |
e4db4a20 | 98 | } |
e4db4a20 BB |
99 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Outside office hours"); |
100 | ||
101 | hours = conf->office_hours->elts; | |
102 | ||
103 | for (i = 0; i < conf->office_hours->nelts; ++i) { | |
af4b488f | 104 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, (const char *)hours[i].data); |
e4db4a20 BB |
105 | } |
106 | ||
107 | r->keepalive = 0; | |
108 | return NGX_HTTP_FORBIDDEN; | |
109 | } | |
110 | ||
111 | //Read Arguments from config file | |
112 | ||
af4b488f BB |
113 | static char * |
114 | ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf) | |
115 | { | |
e4db4a20 | 116 | |
af4b488f | 117 | char *conf_structure = conf; |
e4db4a20 | 118 | |
af4b488f BB |
119 | ngx_array_t **office_hours; |
120 | ngx_str_t *hours; | |
121 | ngx_str_t *value; | |
122 | ngx_uint_t i; | |
e4db4a20 BB |
123 | |
124 | //Gets the array from the config structure using the defined | |
af4b488f BB |
125 | // offset, and if the |
126 | pointer is unset it creates a new one. | |
127 | // | |
128 | //(The first element is the directive itself, so we should be | |
129 | // offset by 1) | |
130 | office_hours = (ngx_array_t **) (conf_structure + cmd->offset); | |
e4db4a20 BB |
131 | |
132 | if (*office_hours == NGX_CONF_UNSET_PTR) { | |
af4b488f BB |
133 | *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1, |
134 | sizeof(ngx_str_t)); | |
e4db4a20 | 135 | |
af4b488f BB |
136 | if (*office_hours == NULL) { |
137 | return NGX_CONF_ERROR; | |
138 | } | |
e4db4a20 | 139 | } |
e4db4a20 BB |
140 | value = cf->args->elts; |
141 | ||
142 | for (i = 1; i < cf->args->nelts; ++i) { | |
143 | ||
af4b488f BB |
144 | hours = ngx_array_push(*office_hours); |
145 | if (hours == NULL) { | |
146 | return NGX_CONF_ERROR; | |
147 | } | |
148 | *hours = value[i]; | |
e4db4a20 BB |
149 | } |
150 | ||
151 | return NGX_CONF_OK; | |
152 | } | |
153 | ||
154 | ||
155 | //Initialize the Configuration Object | |
156 | ||
af4b488f BB |
157 | static void * |
158 | ngx_http_office_hours_create_conf(ngx_conf_t * cf) | |
159 | { | |
e4db4a20 | 160 | |
af4b488f | 161 | ngx_http_office_hours_conf_t *conf; |
e4db4a20 BB |
162 | |
163 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t)); | |
164 | ||
165 | if (conf == NULL) { | |
af4b488f | 166 | return NULL; |
e4db4a20 | 167 | } |
e4db4a20 BB |
168 | conf->office_hours = NGX_CONF_UNSET_PTR; |
169 | ||
170 | return conf; | |
171 | } | |
172 | ||
173 | //Merge Config Values | |
174 | ||
af4b488f BB |
175 | static char * |
176 | ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child) | |
177 | { | |
e4db4a20 | 178 | |
af4b488f BB |
179 | ngx_http_office_hours_conf_t *prev = parent; |
180 | ngx_http_office_hours_conf_t *conf = child; | |
e4db4a20 BB |
181 | |
182 | ngx_conf_merge_ptr_value(conf->office_hours, prev->office_hours, NULL); | |
183 | ||
184 | return NGX_CONF_OK; | |
185 | } | |
186 | ||
187 | //Postconfig Initialization Handler | |
188 | // Sets the request filter at the top of the chain | |
189 | ||
af4b488f BB |
190 | static ngx_int_t |
191 | ngx_http_office_hours_init(ngx_conf_t * cf) | |
192 | { | |
e4db4a20 BB |
193 | |
194 | ngx_http_next_body_filter = ngx_http_top_body_filter; | |
195 | ngx_http_top_body_filter = ngx_http_office_hours_body_filter; | |
196 | ||
197 | return NGX_OK; | |
198 | } |