2 * Copyright 2018 Rubén Beltrán del Río
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <ngx_config.h>
19 // Module Config Definitions
22 ngx_array_t
*office_hours
;
23 } ngx_http_office_hours_conf_t
;
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
);
29 static char *ngx_http_office_hours(ngx_conf_t
* cf
, ngx_command_t
* cmd
, void *conf
);
31 //Module Directive Definitions
33 static ngx_command_t ngx_http_office_hours_commands
[] = {
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
),
46 //Define the module context
48 static ngx_http_module_t ngx_http_office_hours_filter_module_ctx
= {
49 NULL
, //Preconfiguration
50 ngx_http_office_hours_init
, //Postconfiguration
52 NULL
, //Create main configuration
53 NULL
, //Initialize main configuration
54 NULL
, //Create server configuration
55 NULL
, //Merge server configuration
57 ngx_http_office_hours_create_conf
, //Create location configuration
58 ngx_http_office_hours_merge_conf
// Merge location configuration
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
81 static ngx_http_output_body_filter_pt ngx_http_next_body_filter
;
84 ngx_http_office_hours_body_filter(ngx_http_request_t
* r
, ngx_chain_t
* in
) {
86 ngx_http_office_hours_conf_t
*conf
;
90 conf
= ngx_http_get_module_loc_conf(r
, ngx_http_office_hours_filter_module
);
92 if (conf
->office_hours
== NULL
) {
94 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0, "Within office hours");
96 return ngx_http_next_body_filter(r
, in
);
99 ngx_log_error(NGX_LOG_DEBUG
, r
->connection
->log
, 0, "Outside office hours");
101 hours
= conf
->office_hours
->elts
;
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
);
108 return NGX_HTTP_FORBIDDEN
;
111 //Read Arguments from config file
114 ngx_http_office_hours(ngx_conf_t
* cf
, ngx_command_t
* cmd
, void *conf
) {
116 char *conf_structure
= conf
;
118 ngx_array_t
**office_hours
;
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
.
127 //(The first element is the directive itself, so we should be
129 office_hours
= (ngx_array_t
**) (conf_structure
+ cmd
->offset
);
131 if (*office_hours
== NGX_CONF_UNSET_PTR
) {
132 *office_hours
= ngx_array_create(cf
->pool
, cf
->args
->nelts
- 1,
135 if (*office_hours
== NULL
) {
136 return NGX_CONF_ERROR
;
140 value
= cf
->args
->elts
;
142 for (i
= 1; i
< cf
->args
->nelts
; ++i
) {
144 hours
= ngx_array_push(*office_hours
);
146 return NGX_CONF_ERROR
;
156 //Initialize the Configuration Object
159 ngx_http_office_hours_create_conf(ngx_conf_t
* cf
) {
161 ngx_http_office_hours_conf_t
*conf
;
163 conf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_http_office_hours_conf_t
));
169 conf
->office_hours
= NGX_CONF_UNSET_PTR
;
174 //Merge Config Values
177 ngx_http_office_hours_merge_conf(ngx_conf_t
* cf
, void *parent
, void *child
) {
179 ngx_http_office_hours_conf_t
*prev
= parent
;
180 ngx_http_office_hours_conf_t
*conf
= child
;
182 ngx_conf_merge_ptr_value(conf
->office_hours
, prev
->office_hours
, NULL
);
187 //Postconfig Initialization Handler
188 // Sets the request filter at the top of the chain
191 ngx_http_office_hours_init(ngx_conf_t
* cf
) {
193 ngx_http_next_body_filter
= ngx_http_top_body_filter
;
194 ngx_http_top_body_filter
= ngx_http_office_hours_body_filter
;