1
1
/*===============================================================
2
2
* Copyright (C) 2012 All rights reserved.
3
- *
3
+ *
4
4
* Filename:ngx_http_qrcode_module.c
5
5
* Author :dcshi
6
6
* Created:2012-12-09
@@ -45,92 +45,92 @@ ngx_http_qrcode_urlencode_txt(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
45
45
static char *
46
46
ngx_http_qrcode_gen (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
47
47
48
- static ngx_int_t
48
+ static ngx_int_t
49
49
ngx_http_qrcode_handler (ngx_http_request_t * r );
50
50
51
51
static void *
52
52
ngx_http_qrcode_create_loc_conf (ngx_conf_t * cf );
53
53
54
- static char *
54
+ static char *
55
55
ngx_http_qrcode_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child );
56
56
57
57
static char *
58
- ngx_http_qrcode_cmder (ngx_http_qrcode_cfg_t cfg_code ,
58
+ ngx_http_qrcode_cmder (ngx_http_qrcode_cfg_t cfg_code ,
59
59
ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
60
60
61
61
static ngx_command_t ngx_http_qrcode_commands [] = {
62
- {
62
+ {
63
63
ngx_string ("qrcode_fg_color" ),
64
64
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
65
65
ngx_http_qrcode_fg_color ,
66
66
NGX_HTTP_LOC_CONF_OFFSET ,
67
67
offsetof(ngx_http_qrcode_loc_conf_t , fg_color ),
68
68
NULL },
69
- {
69
+ {
70
70
ngx_string ("qrcode_bg_color" ),
71
71
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
72
72
ngx_http_qrcode_bg_color ,
73
73
NGX_HTTP_LOC_CONF_OFFSET ,
74
74
offsetof(ngx_http_qrcode_loc_conf_t , bg_color ),
75
75
NULL },
76
- {
76
+ {
77
77
ngx_string ("qrcode_level" ),
78
78
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
79
79
ngx_http_qrcode_level ,
80
80
NGX_HTTP_LOC_CONF_OFFSET ,
81
81
offsetof(ngx_http_qrcode_loc_conf_t , level ),
82
82
NULL },
83
- {
83
+ {
84
84
ngx_string ("qrcode_hint" ),
85
85
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
86
86
ngx_http_qrcode_hint ,
87
87
NGX_HTTP_LOC_CONF_OFFSET ,
88
88
offsetof(ngx_http_qrcode_loc_conf_t , hint ),
89
89
NULL },
90
- {
90
+ {
91
91
ngx_string ("qrcode_size" ),
92
92
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
93
93
ngx_http_qrcode_size ,
94
94
NGX_HTTP_LOC_CONF_OFFSET ,
95
95
offsetof(ngx_http_qrcode_loc_conf_t , size ),
96
96
NULL },
97
- {
97
+ {
98
98
ngx_string ("qrcode_margin" ),
99
99
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
100
100
ngx_http_qrcode_margin ,
101
101
NGX_HTTP_LOC_CONF_OFFSET ,
102
102
offsetof(ngx_http_qrcode_loc_conf_t , margin ),
103
103
NULL },
104
- {
104
+ {
105
105
ngx_string ("qrcode_version" ),
106
106
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
107
107
ngx_http_qrcode_version ,
108
108
NGX_HTTP_LOC_CONF_OFFSET ,
109
109
offsetof(ngx_http_qrcode_loc_conf_t , version ),
110
110
NULL },
111
- {
111
+ {
112
112
ngx_string ("qrcode_casesensitive" ),
113
113
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
114
114
ngx_http_qrcode_casesensitive ,
115
115
NGX_HTTP_LOC_CONF_OFFSET ,
116
116
offsetof(ngx_http_qrcode_loc_conf_t , casesensitive ),
117
117
NULL },
118
- {
118
+ {
119
119
ngx_string ("qrcode_txt" ),
120
120
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
121
121
ngx_http_qrcode_txt ,
122
122
NGX_HTTP_LOC_CONF_OFFSET ,
123
123
offsetof(ngx_http_qrcode_loc_conf_t , txt ),
124
124
NULL },
125
- {
125
+ {
126
126
ngx_string ("qrcode_urlencode_txt" ),
127
127
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
128
128
ngx_http_qrcode_urlencode_txt ,
129
129
NGX_HTTP_LOC_CONF_OFFSET ,
130
130
offsetof(ngx_http_qrcode_loc_conf_t , txt ),
131
131
NULL },
132
132
133
- {
133
+ {
134
134
ngx_string ("qrcode_gen" ),
135
135
NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 |NGX_CONF_NOARGS ,
136
136
ngx_http_qrcode_gen ,
@@ -173,7 +173,7 @@ ngx_http_qrcode_create_loc_conf(ngx_conf_t *cf)
173
173
174
174
qlcf = ngx_pcalloc (cf -> pool , sizeof (ngx_http_qrcode_loc_conf_t ));
175
175
if (qlcf == NULL ) {
176
- ngx_log_error (NGX_LOG_ERR , cf -> log , 0 ,
176
+ ngx_log_error (NGX_LOG_ERR , cf -> log , 0 ,
177
177
"Mem calloc ngx_http_qrcode_loc_conf_t fail" );
178
178
return NGX_CONF_ERROR ;
179
179
}
@@ -199,7 +199,7 @@ ngx_http_qrcode_create_loc_conf(ngx_conf_t *cf)
199
199
return qlcf ;
200
200
}
201
201
202
- static char *
202
+ static char *
203
203
ngx_http_qrcode_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child ) {
204
204
ngx_http_qrcode_loc_conf_t * pre = parent ;
205
205
ngx_http_qrcode_loc_conf_t * qlcf = child ;
@@ -290,16 +290,16 @@ ngx_http_qrcode_urlencode_txt(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
290
290
static char *
291
291
ngx_http_qrcode_gen (ngx_conf_t * cf , ngx_command_t * cmd , void * conf )
292
292
{
293
- ngx_http_core_loc_conf_t * core_conf =
293
+ ngx_http_core_loc_conf_t * core_conf =
294
294
ngx_http_conf_get_module_loc_conf (cf , ngx_http_core_module );
295
295
296
296
core_conf -> handler = ngx_http_qrcode_handler ;
297
-
297
+
298
298
return NGX_CONF_OK ;
299
299
}
300
300
301
- static ngx_int_t
302
- ngx_http_qrcode_handler (ngx_http_request_t * r )
301
+ static ngx_int_t
302
+ ngx_http_qrcode_handler (ngx_http_request_t * r )
303
303
{
304
304
ngx_http_qrcode_loc_conf_t * qlcf ;
305
305
ngx_int_t code_size ;
@@ -309,7 +309,7 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
309
309
int img_stream_len ;
310
310
u_char * encoded_txt ;
311
311
ngx_int_t rc ;
312
-
312
+
313
313
qlcf = ngx_http_get_module_loc_conf (r , ngx_http_qrcode_module );
314
314
315
315
/* compile args */
@@ -320,27 +320,27 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
320
320
}
321
321
322
322
encoded_txt = ngx_pcalloc (r -> pool , qlcf -> txt .len + 1 );
323
- ngx_sprintf (encoded_txt , "%V" , & qlcf -> txt );
323
+ ngx_sprintf (encoded_txt , "%V" , & qlcf -> txt );
324
324
325
325
QRcode * code ;
326
- code = QRcode_encodeString ((char * )encoded_txt ,
326
+ code = QRcode_encodeString ((char * )encoded_txt ,
327
327
qlcf -> version , qlcf -> level , qlcf -> hint , qlcf -> casesensitive );
328
328
329
329
if (code == NULL ) {
330
- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
330
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
331
331
"Failed to encode content.exception raised by libqrencode: %s" , strerror (errno ));
332
332
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
333
333
}
334
334
335
- code_size = qlcf -> size / code -> width ;
335
+ code_size = (( qlcf -> size > 2048 ) ? 1024 : qlcf -> size ) / code -> width ;
336
336
code_size = (code_size == 0 ) ? 1 : code_size ;
337
337
img_margin = qlcf -> margin ;
338
338
img_width = code -> width * code_size + 2 * img_margin ;
339
339
340
340
gdImagePtr img ;
341
341
img = gdImageCreate (img_width , img_width );
342
342
343
- fg_color = gdImageColorAllocate (img ,
343
+ fg_color = gdImageColorAllocate (img ,
344
344
qlcf -> fg_color [0 ], qlcf -> fg_color [1 ], qlcf -> fg_color [2 ]);
345
345
346
346
bg_color = gdImageColorAllocate (img ,
@@ -349,18 +349,18 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
349
349
gdImageFill (img , 0 , 0 , bg_color );
350
350
351
351
u_char * p = code -> data ;
352
- for (y = 0 ; y < code -> width ; y ++ )
352
+ for (y = 0 ; y < code -> width ; y ++ )
353
353
{
354
- for (x = 0 ; x < code -> width ; x ++ )
354
+ for (x = 0 ; x < code -> width ; x ++ )
355
355
{
356
356
if (* p & 1 ) {
357
357
posx = x * code_size + img_margin ;
358
358
posy = y * code_size + img_margin ;
359
359
360
- gdImageFilledRectangle (img , posx , posy ,
360
+ gdImageFilledRectangle (img , posx , posy ,
361
361
posx + code_size , posy + code_size , fg_color );
362
362
}
363
- p ++ ;
363
+ p ++ ;
364
364
}
365
365
}
366
366
@@ -380,15 +380,15 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
380
380
buffer = ngx_pcalloc (r -> pool , sizeof (ngx_buf_t ));
381
381
382
382
if (buffer == NULL ) {
383
- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
383
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
384
384
"Failed to allocate response buffer" );
385
385
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
386
386
}
387
387
388
388
//set up the buffer chain
389
389
ngx_chain_t out ;
390
390
buffer -> pos = img_stream ;
391
- buffer -> last = img_stream + img_stream_len ;
391
+ buffer -> last = img_stream + img_stream_len ;
392
392
buffer -> memory = 1 ;
393
393
buffer -> last_buf = 1 ;
394
394
out .buf = buffer ;
@@ -398,22 +398,22 @@ ngx_http_qrcode_handler(ngx_http_request_t* r)
398
398
}
399
399
400
400
static char *
401
- ngx_http_qrcode_cmder (ngx_http_qrcode_cfg_t cfg_code ,
401
+ ngx_http_qrcode_cmder (ngx_http_qrcode_cfg_t cfg_code ,
402
402
ngx_conf_t * cf , ngx_command_t * cmd , void * conf )
403
403
{
404
404
ngx_http_qrcode_loc_conf_t * qlcf ;
405
405
ngx_http_qrcode_cmd_t * qr_cmd ;
406
406
ngx_str_t * raw_args ;
407
- ngx_array_t * * cmds_ptr ;
407
+ ngx_array_t * * cmds_ptr ;
408
408
ngx_array_t * * args_ptr ;
409
409
ngx_http_qrcode_arg_template_t * arg ;
410
410
ngx_http_script_compile_t sc ;
411
411
ngx_uint_t i , n ;
412
412
413
413
qlcf = (ngx_http_qrcode_loc_conf_t * )conf ;
414
414
cmds_ptr = & qlcf -> cmds ;
415
-
416
- if (* cmds_ptr == NULL )
415
+
416
+ if (* cmds_ptr == NULL )
417
417
{
418
418
* cmds_ptr = ngx_array_create (cf -> pool , 1 , sizeof (ngx_http_qrcode_cmd_t ));
419
419
@@ -440,7 +440,7 @@ ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
440
440
raw_args = cf -> args -> elts ;
441
441
442
442
// we skip the first arg and start from the second
443
- for (i = 1 ; i < cf -> args -> nelts ; i ++ )
443
+ for (i = 1 ; i < cf -> args -> nelts ; i ++ )
444
444
{
445
445
arg = ngx_array_push (* args_ptr );
446
446
@@ -471,4 +471,4 @@ ngx_http_qrcode_cmder(ngx_http_qrcode_cfg_t cfg_code,
471
471
} /* end for */
472
472
473
473
return NGX_CONF_OK ;
474
- }
474
+ }
0 commit comments