]>
Commit | Line | Data |
---|---|---|
e4db4a20 BB |
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 | // Module Config Definitions | |
20 | ||
21 | typedef struct { | |
22 | ngx_array_t *office_hours; | |
23 | } ngx_http_office_hours_conf_t; | |
24 | ||
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); | |
27 | static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf); | |
28 | ||
29 | static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf); | |
30 | ||
31 | //Module Directive Definitions | |
32 | ||
33 | static ngx_command_t ngx_http_office_hours_commands[] = { | |
34 | { | |
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 | |
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 | |
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 | |
76 | }; | |
77 | ||
78 | ||
79 | //Main Body Filter | |
80 | ||
81 | static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
82 | ||
83 | static ngx_int_t | |
84 | ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in) { | |
85 | ||
86 | ngx_http_office_hours_conf_t *conf; | |
87 | ngx_uint_t i; | |
88 | ngx_str_t *hours; | |
89 | ||
90 | conf = ngx_http_get_module_loc_conf(r, ngx_http_office_hours_filter_module); | |
91 | ||
92 | if (conf->office_hours == NULL) { | |
93 | ||
94 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Within office hours"); | |
95 | ||
96 | return ngx_http_next_body_filter(r, in); | |
97 | } | |
98 | ||
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) { | |
104 | ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, (const char *)hours[i].data); | |
105 | } | |
106 | ||
107 | r->keepalive = 0; | |
108 | return NGX_HTTP_FORBIDDEN; | |
109 | } | |
110 | ||
111 | //Read Arguments from config file | |
112 | ||
113 | static char * | |
114 | ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf) { | |
115 | ||
116 | char *conf_structure = conf; | |
117 | ||
118 | ngx_array_t **office_hours; | |
119 | ngx_str_t *hours; | |
120 | ngx_str_t *value; | |
121 | ngx_uint_t i; | |
122 | ||
123 | //Gets the array from the config structure using the defined | |
124 | // offset, and if the | |
125 | pointer is unset it creates a new one. | |
126 | // | |
127 | //(The first element is the directive itself, so we should be | |
128 | // offset by 1) | |
129 | office_hours = (ngx_array_t **) (conf_structure + cmd->offset); | |
130 | ||
131 | if (*office_hours == NGX_CONF_UNSET_PTR) { | |
132 | *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1, | |
133 | sizeof(ngx_str_t)); | |
134 | ||
135 | if (*office_hours == NULL) { | |
136 | return NGX_CONF_ERROR; | |
137 | } | |
138 | } | |
139 | ||
140 | value = cf->args->elts; | |
141 | ||
142 | for (i = 1; i < cf->args->nelts; ++i) { | |
143 | ||
144 | hours = ngx_array_push(*office_hours); | |
145 | if (hours == NULL) { | |
146 | return NGX_CONF_ERROR; | |
147 | } | |
148 | ||
149 | *hours = value[i]; | |
150 | } | |
151 | ||
152 | return NGX_CONF_OK; | |
153 | } | |
154 | ||
155 | ||
156 | //Initialize the Configuration Object | |
157 | ||
158 | static void * | |
159 | ngx_http_office_hours_create_conf(ngx_conf_t * cf) { | |
160 | ||
161 | ngx_http_office_hours_conf_t *conf; | |
162 | ||
163 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t)); | |
164 | ||
165 | if (conf == NULL) { | |
166 | return NULL; | |
167 | } | |
168 | ||
169 | conf->office_hours = NGX_CONF_UNSET_PTR; | |
170 | ||
171 | return conf; | |
172 | } | |
173 | ||
174 | //Merge Config Values | |
175 | ||
176 | static char * | |
177 | ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child) { | |
178 | ||
179 | ngx_http_office_hours_conf_t *prev = parent; | |
180 | ngx_http_office_hours_conf_t *conf = child; | |
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 | ||
956d445d | 190 | static ngx_int_t |
e4db4a20 BB |
191 | ngx_http_office_hours_init(ngx_conf_t * cf) { |
192 | ||
193 | ngx_http_next_body_filter = ngx_http_top_body_filter; | |
194 | ngx_http_top_body_filter = ngx_http_office_hours_body_filter; | |
195 | ||
196 | return NGX_OK; | |
197 | } |