]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/blame - ngx_http_office_hours_filter_module.c
Add dummy filter module.
[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
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
21typedef struct {
22 ngx_array_t *office_hours;
23} ngx_http_office_hours_conf_t;
24
25static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf);
26static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child);
27static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf);
28
29static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
30
31//Module Directive Definitions
32
33static 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
48static 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
63ngx_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
81static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
82
83static ngx_int_t
84ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in) {
85
86ngx_http_office_hours_conf_t *conf;
87ngx_uint_t i;
88ngx_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
113static char *
114ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf) {
115
116char *conf_structure = conf;
117
118ngx_array_t **office_hours;
119ngx_str_t *hours;
120ngx_str_t *value;
121ngx_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
158static void *
159ngx_http_office_hours_create_conf(ngx_conf_t * cf) {
160
161ngx_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
176static char *
177ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child) {
178
179ngx_http_office_hours_conf_t *prev = parent;
180ngx_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
190static ngx_int_t
191ngx_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}