-
Notifications
You must be signed in to change notification settings - Fork 26
/
ngx_http_qrcode_utils.c
354 lines (279 loc) · 8.2 KB
/
ngx_http_qrcode_utils.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*===============================================================
* Copyright (C) 2013 All rights reserved.
*
* Filename:ngx_http_qrcode_utils.c
* Author :dcshi
* Created:2013-02-02
================================================================*/
#include "ngx_http_qrcode_utils.h"
ngx_int_t
ngx_http_qrcode_set_fg_color(ngx_http_request_t *r, ngx_int_t *color, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_bg_color(ngx_http_request_t *r, ngx_int_t *color, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_level(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_hint(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_size(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_margin(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_version(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_casesensitive(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_content(ngx_http_request_t *r, ngx_str_t *val, ngx_array_t *compiled_args, ngx_int_t urlencode);
ngx_int_t
ngx_http_qrcode_eval_cmd_args(ngx_http_request_t *r,
ngx_http_qrcode_cmd_t *cmd, ngx_array_t *compiled_args);
ngx_int_t
ngx_http_qrcode_set_fg_color(ngx_http_request_t *r, ngx_int_t *color, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t i;
arg = compiled_args->elts;
if (arg[0].len != 6) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"fg_color format error, it should be like FF00FF. error value %V", arg);
return NGX_ERROR;
}
for (i = 0; i < 3; i++) {
*(color + i) = ngx_hextoi(arg[0].data + i*2, 2);
}
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_bg_color(ngx_http_request_t *r, ngx_int_t *color, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t i;
arg = compiled_args->elts;
if (arg[0].len != 6) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"bg_color format error, it should be like FF00FF. error value %V", arg);
return NGX_ERROR;
}
for (i = 0; i < 3; i++) {
*(color + i) = ngx_hextoi(arg[0].data + i*2, 2);
}
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_level(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t rc;
arg = compiled_args->elts;
rc = ngx_atoi(arg[0].data, arg[0].len);
if (rc < 0 || rc > 3) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"error level value %V, level should be 0, 1, 2 or 3.", arg);
return rc;
}
*val = rc;
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_hint(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t rc;
arg = compiled_args->elts;
rc = ngx_atoi(arg[0].data, arg[0].len);
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"error hint value %V", arg);
return rc;
}
*val = rc;
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_size(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t rc;
arg = compiled_args->elts;
rc = ngx_atoi(arg[0].data, arg[0].len);
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"error size value %V", arg);
return rc;
}
*val = rc;
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_margin(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t rc;
arg = compiled_args->elts;
rc = ngx_atoi(arg[0].data, arg[0].len);
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"error margin value %V.", arg);
return rc;
}
*val = rc;
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_version(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t rc;
arg = compiled_args->elts;
rc = ngx_atoi(arg[0].data, arg[0].len);
if (rc < 0 || rc > 10) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"error version value %V, it should be [0,10].", arg);
return rc;
}
*val = rc;
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_casesensitive(ngx_http_request_t *r, ngx_int_t *val, ngx_array_t *compiled_args)
{
ngx_str_t *arg;
ngx_int_t rc;
arg = compiled_args->elts;
rc = ngx_atoi(arg[0].data, arg[0].len);
if (rc < 0 || rc > 1) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"error casesensitive value %V, it should be 0 or 1.", arg);
return rc;
}
*val = rc;
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_set_content(ngx_http_request_t *r, ngx_str_t *val, ngx_array_t *compiled_args, ngx_int_t urlencode)
{
ngx_str_t *arg;
u_char *p;
arg = compiled_args->elts;
*val = arg[0];
if (urlencode == 1) {
p = ngx_pcalloc(r->pool, arg->len);
val->data = p;
ngx_unescape_uri(&p, &arg->data, arg->len, 0);
val->len = p - val->data;
}
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_eval_cmd_args(ngx_http_request_t *r,
ngx_http_qrcode_cmd_t *cmd, ngx_array_t *compiled_args)
{
ngx_http_qrcode_arg_template_t *value;
ngx_str_t *arg;
ngx_uint_t i;
value = cmd->args->elts;
for (i = 0; i < cmd->args->nelts; i++)
{
arg = ngx_array_push(compiled_args);
if (arg == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (value[i].lengths == NULL) { /* does not contain vars */
*arg = value[i].raw_value;
}
else {
if (ngx_http_script_run(r, arg, value[i].lengths->elts,
0, value[i].values->elts) == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
}
return NGX_OK;
}
ngx_int_t
ngx_http_qrcode_compile_args(ngx_http_request_t *r, ngx_http_qrcode_loc_conf_t *qlcf)
{
ngx_array_t *cmds;
ngx_http_qrcode_cmd_t *cmd;
ngx_array_t *compiled_args;
ngx_int_t rc;
ngx_uint_t i;
compiled_args = NULL;
rc = NGX_OK;
cmds = qlcf->cmds;
if (cmds == NULL)
return NGX_DECLINED;
cmd = cmds->elts;
for (i = 0; i < cmds->nelts; i++)
{
if (cmd[i].args)
{
compiled_args = ngx_array_create(r->pool,
cmd[i].args->nelts, sizeof(ngx_str_t));
if (compiled_args == NULL)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
rc = ngx_http_qrcode_eval_cmd_args(r, &cmd[i], compiled_args);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Failed to evaluate arguments for the directive.");
return rc;
}
}
/* switch cmd*/
switch (cmd[i].cfg_code)
{
case qrcode_cfg_fg_color:
rc = ngx_http_qrcode_set_fg_color(r, qlcf->fg_color, compiled_args);
break;
case qrcode_cfg_bg_color:
rc = ngx_http_qrcode_set_bg_color(r, qlcf->bg_color, compiled_args);
break;
case qrcode_cfg_level:
rc = ngx_http_qrcode_set_level(r, &qlcf->level, compiled_args);
break;
case qrcode_cfg_hint:
rc = ngx_http_qrcode_set_hint(r, &qlcf->hint, compiled_args);
break;
case qrcode_cfg_size:
rc = ngx_http_qrcode_set_size(r, &qlcf->size, compiled_args);
break;
case qrcode_cfg_margin:
rc = ngx_http_qrcode_set_margin(r, &qlcf->margin, compiled_args);
break;
case qrcode_cfg_version:
rc = ngx_http_qrcode_set_version(r, &qlcf->version, compiled_args);
break;
case qrcode_cfg_txt:
rc = ngx_http_qrcode_set_content(r, &qlcf->txt, compiled_args, 0);
break;
case qrcode_cfg_urlencode_txt:
rc = ngx_http_qrcode_set_content(r, &qlcf->txt, compiled_args, 1);
break;
case qrcode_cfg_casesensitive:
rc = ngx_http_qrcode_set_casesensitive(r, &qlcf->casesensitive, compiled_args);
break;
default:
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"Unknown cfg_code: %d", cmd[i].cfg_code);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (rc != NGX_OK)
return rc;
}
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
"fg_color %xd%xd%xd%xd, "
"bg_color %xd%xd%xd%xd, "
"level %d, "
"hint %d, "
"size %d, "
"margin %d, "
"version %d, "
"casesensitive %d, "
"txt %V",
qlcf->fg_color[0], qlcf->fg_color[1], qlcf->fg_color[2], qlcf->fg_color[3],
qlcf->bg_color[0], qlcf->bg_color[1], qlcf->bg_color[2], qlcf->bg_color[3],
qlcf->level, qlcf->hint, qlcf->size, qlcf->margin, qlcf->version,
qlcf->casesensitive,
&qlcf->txt);
return NGX_OK;
}