-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_http_txtset_module.c
272 lines (216 loc) · 7.47 KB
/
ngx_http_txtset_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* txtset directive
*
* @author Jason Kleban <[email protected]>
* @copyright 2012 Wayfair, LLC - All rights reserved
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define TXTSET_CACHE_LIFE 10
#define TXTSET_MAX_SIZE 20
typedef struct {
u_char *text_file;
u_char *default_value;
u_char *cached_value;
time_t cached_time;
ngx_int_t cache_life;
} txtvar_info;
static char *ngx_http_txtset(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_txtlower(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
int check_string(u_char *string);
static ngx_command_t ngx_http_txtset_commands[] = {
{ ngx_string("txtset"),
NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE3|NGX_CONF_TAKE4,
ngx_http_txtset,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("txtlower"),
NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE2,
ngx_http_txtlower,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_txtset_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL, /* merge location configuration */
};
ngx_module_t ngx_http_txtset_module = {
NGX_MODULE_V1,
&ngx_http_txtset_module_ctx, /* module context */
ngx_http_txtset_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
ngx_int_t
ngx_http_txtset_lowervariable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_uint_t *input_var_index = (ngx_uint_t *) data;
ngx_http_variable_value_t *input_var;
int i, input_len;
input_var = ngx_http_get_indexed_variable(r, *input_var_index);
input_len = input_var->len;
if ((v->data = ngx_pnalloc(r->pool, input_len + 1)) == NULL) {
return NGX_ERROR;
}
v->data[input_len] = 0;
for (i = 0; i < input_len; i++) {
v->data[i] = tolower(input_var->data[i]);
}
v->len = ngx_strlen(v->data);
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
ngx_int_t
ngx_http_txtset_txtvariable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
FILE *fp;
time_t now = time(NULL);
txtvar_info *element = (txtvar_info *) data;
int valid = 1;
if (element->cached_time == 0 || now - element->cached_time > element->cache_life) {
if ((fp = fopen(element->text_file, "r")) == NULL) {
valid = 0;
}
if ((v->data = ngx_pnalloc(r->pool, TXTSET_MAX_SIZE + 1)) == NULL) {
return NGX_ERROR;
}
v->data[TXTSET_MAX_SIZE] = '\0';
if (valid == 1) {
fgets(v->data, TXTSET_MAX_SIZE, fp);
fclose(fp);
valid = check_string(v->data);
}
if (valid == 0) {
memcpy(v->data, element->default_value, TXTSET_MAX_SIZE + 1);
}
memcpy(element->cached_value, v->data, TXTSET_MAX_SIZE + 1);
element->cached_time = now;
} else {
v->data = element->cached_value;
}
v->len = ngx_strlen(v->data);
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
static char *
ngx_http_txtset(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_http_variable_t *v;
txtvar_info *element;
value = cf->args->elts;
if (value[1].data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
value[1].len--;
value[1].data++;
v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
if (v == NULL) {
return NGX_CONF_ERROR;
}
v->get_handler = ngx_http_txtset_txtvariable;
if ((element = ngx_pcalloc(cf->pool, sizeof(txtvar_info))) == NULL) {
return NGX_CONF_ERROR;
}
if ((element->text_file = ngx_pcalloc(cf->pool, value[2].len + 1)) == NULL) {
return NGX_CONF_ERROR;
}
memcpy(element->text_file, value[2].data, value[2].len);
if ((element->default_value = ngx_pcalloc(cf->pool, value[3].len + 1)) == NULL) {
return NGX_CONF_ERROR;
}
memcpy(element->default_value, value[3].data, value[3].len + 1);
if (cf->args->nelts > 4) {
element->cache_life = (ngx_int_t) atoi(value[4].data);
if (element->cache_life == 0) {
element->cache_life = TXTSET_CACHE_LIFE;
}
} else {
element->cache_life = TXTSET_CACHE_LIFE;
}
if ((element->cached_value = ngx_pcalloc(cf->pool, TXTSET_MAX_SIZE + 1)) == NULL) {
return NGX_CONF_ERROR;
}
element->cached_time == 0;
v->data = (uintptr_t) element;
return NGX_CONF_OK;
}
static char *
ngx_http_txtlower(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_http_variable_t *v;
ngx_int_t *input_var_index;
value = cf->args->elts;
if (value[1].data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
if (value[2].data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &value[2]);
return NGX_CONF_ERROR;
}
value[1].len--;
value[1].data++;
value[2].len--;
value[2].data++;
v = ngx_http_add_variable(cf, &value[2], NGX_HTTP_VAR_CHANGEABLE);
if (v == NULL) {
return NGX_CONF_ERROR;
}
v->get_handler = ngx_http_txtset_lowervariable;
input_var_index = ngx_pcalloc(cf->pool, sizeof(ngx_int_t));
*input_var_index = ngx_http_get_variable_index(cf, &value[1]);
v->data = (uintptr_t) input_var_index;
return NGX_CONF_OK;
}
int
check_string(u_char *string)
{
int i;
if (string == NULL || string[0] == '\0' || string[0] == '\n') {
return 0;
}
for (i=0; string[i] != '\0'; i++) {
if (!(string[i] >= '0' && string[i] <= '9') &&
!(string[i] >= 'A' && string[i] <= 'Z') &&
!(string[i] >= 'a' && string[i] <= 'z') &&
!(string[i] == '-' || string[i] == '_')) {
if (string[i] == '\n') {
string[i] = 0;
return 1;
}
return 0;
}
}
return 1;
}