0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
|
13 char **tables;
|
|
14 ngx_str_t name;
|
4
|
15 ngx_uint_t server; /* unsigned server:1; */
|
0
|
16 } ngx_http_charset_t;
|
|
17
|
|
18
|
|
19 typedef struct {
|
|
20 ngx_int_t src;
|
|
21 ngx_int_t dst;
|
|
22 char *src2dst;
|
|
23 char *dst2src;
|
|
24 } ngx_http_charset_tables_t;
|
|
25
|
|
26
|
|
27 typedef struct {
|
|
28 ngx_array_t charsets; /* ngx_http_charset_t */
|
|
29 ngx_array_t tables; /* ngx_http_charset_tables_t */
|
|
30 } ngx_http_charset_main_conf_t;
|
|
31
|
|
32
|
|
33 typedef struct {
|
|
34 ngx_flag_t enable;
|
|
35 ngx_flag_t autodetect;
|
|
36
|
|
37 ngx_int_t default_charset;
|
|
38 ngx_int_t source_charset;
|
|
39 } ngx_http_charset_loc_conf_t;
|
|
40
|
|
41
|
|
42 typedef struct {
|
|
43 ngx_int_t server;
|
|
44 ngx_int_t client;
|
|
45 } ngx_http_charset_ctx_t;
|
|
46
|
|
47
|
4
|
48 static ngx_uint_t ngx_charset_recode(ngx_buf_t *b, char *table);
|
0
|
49
|
|
50 static char *ngx_charset_map_block(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
51 void *conf);
|
|
52 static char *ngx_charset_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf);
|
|
53
|
|
54 static char *ngx_http_set_charset_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
55 void *conf);
|
|
56 static ngx_int_t ngx_http_add_charset(ngx_array_t *charsets, ngx_str_t *name);
|
|
57
|
|
58 static ngx_int_t ngx_http_charset_filter_init(ngx_cycle_t *cycle);
|
|
59
|
|
60 static void *ngx_http_charset_create_main_conf(ngx_conf_t *cf);
|
|
61 static char *ngx_http_charset_init_main_conf(ngx_conf_t *cf, void *conf);
|
|
62 static void *ngx_http_charset_create_loc_conf(ngx_conf_t *cf);
|
|
63 static char *ngx_http_charset_merge_loc_conf(ngx_conf_t *cf,
|
|
64 void *parent, void *child);
|
|
65
|
|
66
|
|
67 static ngx_command_t ngx_http_charset_filter_commands[] = {
|
|
68
|
|
69 { ngx_string("charset_map"),
|
|
70 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
|
|
71 ngx_charset_map_block,
|
|
72 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
73 0,
|
|
74 NULL },
|
|
75
|
|
76 { ngx_string("default_charset"),
|
|
77 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
78 ngx_http_set_charset_slot,
|
|
79 NGX_HTTP_LOC_CONF_OFFSET,
|
|
80 offsetof(ngx_http_charset_loc_conf_t, default_charset),
|
|
81 NULL },
|
|
82
|
|
83 { ngx_string("source_charset"),
|
|
84 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
85 ngx_http_set_charset_slot,
|
|
86 NGX_HTTP_LOC_CONF_OFFSET,
|
|
87 offsetof(ngx_http_charset_loc_conf_t, source_charset),
|
|
88 NULL },
|
|
89
|
|
90 { ngx_string("charset"),
|
|
91 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
92 ngx_conf_set_flag_slot,
|
|
93 NGX_HTTP_LOC_CONF_OFFSET,
|
|
94 offsetof(ngx_http_charset_loc_conf_t, enable),
|
|
95 NULL },
|
|
96
|
|
97 { ngx_string("autodetect_charset"),
|
|
98 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
99 ngx_conf_set_flag_slot,
|
|
100 NGX_HTTP_LOC_CONF_OFFSET,
|
|
101 offsetof(ngx_http_charset_loc_conf_t, autodetect),
|
|
102 NULL },
|
|
103
|
|
104 ngx_null_command
|
|
105 };
|
|
106
|
|
107
|
|
108 static ngx_http_module_t ngx_http_charset_filter_module_ctx = {
|
|
109 NULL, /* pre conf */
|
|
110
|
|
111 ngx_http_charset_create_main_conf, /* create main configuration */
|
|
112 ngx_http_charset_init_main_conf, /* init main configuration */
|
|
113
|
|
114 NULL, /* create server configuration */
|
|
115 NULL, /* merge server configuration */
|
|
116
|
|
117 ngx_http_charset_create_loc_conf, /* create location configuration */
|
|
118 ngx_http_charset_merge_loc_conf /* merge location configuration */
|
|
119 };
|
|
120
|
|
121
|
|
122 ngx_module_t ngx_http_charset_filter_module = {
|
|
123 NGX_MODULE,
|
|
124 &ngx_http_charset_filter_module_ctx, /* module context */
|
|
125 ngx_http_charset_filter_commands, /* module directives */
|
|
126 NGX_HTTP_MODULE, /* module type */
|
|
127 ngx_http_charset_filter_init, /* init module */
|
10
|
128 NULL /* init process */
|
0
|
129 };
|
|
130
|
|
131
|
|
132 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
133 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
134
|
|
135
|
|
136 static ngx_int_t ngx_http_charset_header_filter(ngx_http_request_t *r)
|
|
137 {
|
|
138 ngx_http_charset_t *charsets;
|
|
139 ngx_http_charset_ctx_t *ctx;
|
|
140 ngx_http_charset_loc_conf_t *lcf;
|
|
141 ngx_http_charset_main_conf_t *mcf;
|
|
142
|
|
143 mcf = ngx_http_get_module_main_conf(r, ngx_http_charset_filter_module);
|
|
144 lcf = ngx_http_get_module_loc_conf(r, ngx_http_charset_filter_module);
|
|
145
|
|
146 if (lcf->enable == 0) {
|
|
147 return ngx_http_next_header_filter(r);
|
|
148 }
|
|
149
|
|
150 #if 0
|
|
151 if (lcf->default_charset.len == 0) {
|
|
152 return ngx_http_next_header_filter(r);
|
|
153 }
|
|
154 #endif
|
|
155
|
|
156 if (r->headers_out.content_type == NULL) {
|
|
157 return ngx_http_next_header_filter(r);
|
|
158 }
|
|
159
|
|
160 if (ngx_strncasecmp(r->headers_out.content_type->value.data,
|
|
161 "text/", 5) != 0
|
|
162 && ngx_strncasecmp(r->headers_out.content_type->value.data,
|
|
163 "application/x-javascript", 24) != 0)
|
|
164 {
|
|
165 return ngx_http_next_header_filter(r);
|
|
166 }
|
|
167
|
|
168 if (ngx_strstr(r->headers_out.content_type->value.data, "charset") != NULL)
|
|
169 {
|
|
170 return ngx_http_next_header_filter(r);
|
|
171 }
|
|
172
|
|
173 if (r->headers_out.status == NGX_HTTP_MOVED_PERMANENTLY
|
|
174 && r->headers_out.status == NGX_HTTP_MOVED_TEMPORARILY)
|
|
175 {
|
|
176 /*
|
|
177 * do not set charset for the redirect because NN 4.x uses this
|
|
178 * charset instead of the next page charset
|
|
179 */
|
|
180
|
|
181 r->headers_out.charset.len = 0;
|
|
182 return ngx_http_next_header_filter(r);
|
|
183 }
|
|
184
|
|
185 if (r->headers_out.charset.len) {
|
|
186 return ngx_http_next_header_filter(r);
|
|
187 }
|
|
188
|
|
189 charsets = mcf->charsets.elts;
|
|
190 r->headers_out.charset = charsets[lcf->default_charset].name;
|
|
191
|
|
192 if (lcf->default_charset == lcf->source_charset) {
|
|
193 return ngx_http_next_header_filter(r);
|
|
194 }
|
|
195
|
|
196 ngx_http_create_ctx(r, ctx, ngx_http_charset_filter_module,
|
|
197 sizeof(ngx_http_charset_ctx_t), NGX_ERROR);
|
|
198
|
|
199 r->filter_need_in_memory = 1;
|
|
200
|
|
201 return ngx_http_next_header_filter(r);
|
|
202 }
|
|
203
|
|
204
|
|
205 static ngx_int_t ngx_http_charset_body_filter(ngx_http_request_t *r,
|
|
206 ngx_chain_t *in)
|
|
207 {
|
|
208 char *table;
|
|
209 ngx_chain_t *cl;
|
|
210 ngx_http_charset_t *charsets;
|
|
211 ngx_http_charset_ctx_t *ctx;
|
|
212 ngx_http_charset_loc_conf_t *lcf;
|
|
213 ngx_http_charset_main_conf_t *mcf;
|
|
214
|
|
215 ctx = ngx_http_get_module_ctx(r, ngx_http_charset_filter_module);
|
|
216
|
|
217 if (ctx == NULL) {
|
|
218 return ngx_http_next_body_filter(r, in);
|
|
219 }
|
|
220
|
|
221 mcf = ngx_http_get_module_main_conf(r, ngx_http_charset_filter_module);
|
|
222 lcf = ngx_http_get_module_loc_conf(r, ngx_http_charset_filter_module);
|
|
223
|
|
224 charsets = mcf->charsets.elts;
|
|
225 table = charsets[lcf->source_charset].tables[lcf->default_charset];
|
|
226
|
|
227 for (cl = in; cl; cl = cl->next) {
|
|
228 ngx_charset_recode(cl->buf, table);
|
|
229 }
|
|
230
|
|
231 return ngx_http_next_body_filter(r, in);
|
|
232 }
|
|
233
|
|
234
|
4
|
235 static ngx_uint_t ngx_charset_recode(ngx_buf_t *b, char *table)
|
0
|
236 {
|
4
|
237 u_char *p;
|
|
238 ngx_uint_t change;
|
|
239
|
|
240 change = 0;
|
0
|
241
|
|
242 for (p = b->pos; p < b->last; p++) {
|
4
|
243 if (*p != table[*p]) {
|
|
244 change = 1;
|
|
245 break;
|
|
246 }
|
0
|
247 }
|
4
|
248
|
|
249 if (change) {
|
|
250
|
|
251 while (p < b->last) {
|
|
252 *p = table[*p];
|
|
253 p++;
|
|
254 }
|
|
255
|
|
256 b->in_file = 0;
|
|
257 }
|
|
258
|
|
259 return change;
|
0
|
260 }
|
|
261
|
|
262
|
|
263 static char *ngx_charset_map_block(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
264 void *conf)
|
|
265 {
|
|
266 ngx_http_charset_main_conf_t *mcf = conf;
|
|
267
|
|
268 char *rv;
|
|
269 ngx_int_t src, dst;
|
|
270 ngx_uint_t i;
|
|
271 ngx_str_t *value;
|
|
272 ngx_conf_t pvcf;
|
|
273 ngx_http_charset_tables_t *table;
|
|
274
|
|
275 value = cf->args->elts;
|
|
276
|
|
277 src = ngx_http_add_charset(&mcf->charsets, &value[1]);
|
|
278 if (src == NGX_ERROR) {
|
|
279 return NGX_CONF_ERROR;
|
|
280 }
|
|
281
|
|
282 dst = ngx_http_add_charset(&mcf->charsets, &value[2]);
|
|
283 if (dst == NGX_ERROR) {
|
|
284 return NGX_CONF_ERROR;
|
|
285 }
|
|
286
|
|
287 if (src == dst) {
|
|
288 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
289 "\"charset_map\" between the same charsets "
|
10
|
290 "\"%V\" and \"%V\"", &value[1], &value[2]);
|
0
|
291 return NGX_CONF_ERROR;
|
|
292 }
|
|
293
|
|
294 table = mcf->tables.elts;
|
|
295 for (i = 0; i < mcf->tables.nelts; i++) {
|
|
296 if ((src == table->src && dst == table->dst)
|
|
297 || (src == table->dst && dst == table->src))
|
|
298 {
|
|
299 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
300 "duplicate \"charset_map\" between "
|
10
|
301 "\"%V\" and \"%V\"", &value[1], &value[2]);
|
0
|
302 return NGX_CONF_ERROR;
|
|
303 }
|
|
304 }
|
|
305
|
|
306 if (!(table = ngx_push_array(&mcf->tables))) {
|
|
307 return NGX_CONF_ERROR;
|
|
308 }
|
|
309
|
|
310 table->src = src;
|
|
311 table->dst = dst;
|
|
312
|
|
313 if (!(table->src2dst = ngx_palloc(cf->pool, 256))) {
|
|
314 return NGX_CONF_ERROR;
|
|
315 }
|
|
316
|
|
317 if (!(table->dst2src = ngx_palloc(cf->pool, 256))) {
|
|
318 return NGX_CONF_ERROR;
|
|
319 }
|
|
320
|
|
321 for (i = 0; i < 128; i++) {
|
|
322 table->src2dst[i] = (char) i;
|
|
323 table->dst2src[i] = (char) i;
|
|
324 }
|
|
325
|
|
326 for (/* void */; i < 256; i++) {
|
|
327 table->src2dst[i] = '?';
|
|
328 table->dst2src[i] = '?';
|
|
329 }
|
|
330
|
|
331 pvcf = *cf;
|
|
332 cf->ctx = table;
|
|
333 cf->handler = ngx_charset_map;
|
|
334 cf->handler_conf = conf;
|
|
335 rv = ngx_conf_parse(cf, NULL);
|
|
336 *cf = pvcf;
|
|
337
|
|
338 return rv;
|
|
339 }
|
|
340
|
|
341
|
|
342 static char *ngx_charset_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
|
343 {
|
|
344 ngx_int_t src, dst;
|
|
345 ngx_str_t *value;
|
|
346 ngx_http_charset_tables_t *table;
|
|
347
|
|
348 if (cf->args->nelts != 2) {
|
|
349 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameters number");
|
|
350 return NGX_CONF_ERROR;
|
|
351 }
|
|
352
|
|
353 value = cf->args->elts;
|
|
354
|
|
355 src = ngx_hextoi(value[0].data, value[0].len);
|
|
356 if (src == NGX_ERROR || src > 255) {
|
|
357 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
358 "invalid value \"%V\"", &value[0]);
|
0
|
359 return NGX_CONF_ERROR;
|
|
360 }
|
|
361
|
|
362 dst = ngx_hextoi(value[1].data, value[1].len);
|
|
363 if (dst == NGX_ERROR || dst > 255) {
|
|
364 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
365 "invalid value \"%V\"", &value[1]);
|
0
|
366 return NGX_CONF_ERROR;
|
|
367 }
|
|
368
|
|
369 table = cf->ctx;
|
|
370
|
|
371 table->src2dst[src] = (char) dst;
|
|
372 table->dst2src[dst] = (char) src;
|
|
373
|
|
374 return NGX_CONF_OK;
|
|
375 }
|
|
376
|
|
377
|
|
378 static char *ngx_http_set_charset_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
379 void *conf)
|
|
380 {
|
|
381 char *p = conf;
|
|
382
|
|
383 ngx_int_t *cp;
|
|
384 ngx_str_t *value;
|
|
385 ngx_http_charset_t *charset;
|
|
386 ngx_http_charset_main_conf_t *mcf;
|
|
387
|
|
388 cp = (ngx_int_t *) (p + cmd->offset);
|
|
389
|
|
390 if (*cp != NGX_CONF_UNSET) {
|
|
391 return "is duplicate";
|
|
392 }
|
|
393
|
|
394 mcf = ngx_http_conf_get_module_main_conf(cf,
|
|
395 ngx_http_charset_filter_module);
|
|
396
|
|
397 value = cf->args->elts;
|
|
398
|
|
399 *cp = ngx_http_add_charset(&mcf->charsets, &value[1]);
|
|
400 if (*cp == NGX_ERROR) {
|
|
401 return NGX_CONF_ERROR;
|
|
402 }
|
|
403
|
|
404 if (cmd->offset == offsetof(ngx_http_charset_loc_conf_t, source_charset)) {
|
|
405 charset = mcf->charsets.elts;
|
|
406 charset[*cp].server = 1;
|
|
407 }
|
|
408
|
|
409 return NGX_CONF_OK;
|
|
410 }
|
|
411
|
|
412
|
|
413 static ngx_int_t ngx_http_add_charset(ngx_array_t *charsets, ngx_str_t *name)
|
|
414 {
|
|
415 ngx_uint_t i;
|
|
416 ngx_http_charset_t *c;
|
|
417
|
|
418 c = charsets->elts;
|
|
419 for (i = 0; i < charsets->nelts; i++) {
|
|
420 if (name->len != c[i].name.len) {
|
|
421 continue;
|
|
422 }
|
|
423
|
|
424 if (ngx_strcasecmp(name->data, c[i].name.data) == 0) {
|
|
425 break;
|
|
426 }
|
|
427 }
|
|
428
|
|
429 if (i < charsets->nelts) {
|
|
430 return i;
|
|
431 }
|
|
432
|
|
433 if (!(c = ngx_push_array(charsets))) {
|
|
434 return NGX_ERROR;
|
|
435 }
|
|
436
|
4
|
437 c->tables = NULL;
|
0
|
438 c->name = *name;
|
4
|
439 c->server = 0;
|
0
|
440
|
|
441 return i;
|
|
442 }
|
|
443
|
|
444
|
|
445 static ngx_int_t ngx_http_charset_filter_init(ngx_cycle_t *cycle)
|
|
446 {
|
|
447 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
448 ngx_http_top_header_filter = ngx_http_charset_header_filter;
|
|
449
|
|
450 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
451 ngx_http_top_body_filter = ngx_http_charset_body_filter;
|
|
452
|
|
453 return NGX_OK;
|
|
454 }
|
|
455
|
|
456
|
|
457 static void *ngx_http_charset_create_main_conf(ngx_conf_t *cf)
|
|
458 {
|
|
459 ngx_http_charset_main_conf_t *mcf;
|
|
460
|
|
461 if (!(mcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_main_conf_t)))) {
|
|
462 return NGX_CONF_ERROR;
|
|
463 }
|
|
464
|
|
465 ngx_init_array(mcf->charsets, cf->pool, 5, sizeof(ngx_http_charset_t),
|
|
466 NGX_CONF_ERROR);
|
|
467
|
|
468 ngx_init_array(mcf->tables, cf->pool, 10, sizeof(ngx_http_charset_tables_t),
|
|
469 NGX_CONF_ERROR);
|
|
470
|
|
471 return mcf;
|
|
472 }
|
|
473
|
|
474
|
|
475 static char *ngx_http_charset_init_main_conf(ngx_conf_t *cf, void *conf)
|
|
476 {
|
|
477 ngx_http_charset_main_conf_t *mcf = conf;
|
|
478
|
|
479 ngx_uint_t i, n;
|
|
480 ngx_http_charset_t *charset;
|
|
481 ngx_http_charset_tables_t *tables;
|
|
482
|
|
483 tables = mcf->tables.elts;
|
|
484 charset = mcf->charsets.elts;
|
|
485
|
|
486 for (i = 0; i < mcf->charsets.nelts; i++) {
|
|
487 if (!charset[i].server) {
|
|
488 continue;
|
|
489 }
|
|
490
|
|
491 charset[i].tables = ngx_pcalloc(cf->pool,
|
|
492 sizeof(char *) * mcf->charsets.nelts);
|
|
493
|
|
494 if (charset[i].tables == NULL) {
|
|
495 return NGX_CONF_ERROR;
|
|
496 }
|
|
497
|
|
498 for (n = 0; n < mcf->tables.nelts; n++) {
|
|
499 if ((ngx_int_t) i == tables[n].src) {
|
|
500 charset[i].tables[tables[n].dst] = tables[n].src2dst;
|
|
501 continue;
|
|
502 }
|
|
503
|
|
504 if ((ngx_int_t) i == tables[n].dst) {
|
|
505 charset[i].tables[tables[n].src] = tables[n].dst2src;
|
|
506 }
|
|
507 }
|
|
508 }
|
|
509
|
|
510 for (i = 0; i < mcf->charsets.nelts; i++) {
|
|
511 if (!charset[i].server) {
|
|
512 continue;
|
|
513 }
|
|
514
|
|
515 for (n = 0; n < mcf->charsets.nelts; n++) {
|
|
516 if (i == n) {
|
|
517 continue;
|
|
518 }
|
|
519
|
|
520 if (charset[i].tables[n]) {
|
|
521 continue;
|
|
522 }
|
|
523
|
|
524 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
525 " no \"charset_map\" between the charsets "
|
10
|
526 "\"%V\" and \"%V\"",
|
|
527 &charset[i].name, &charset[n].name);
|
0
|
528 return NGX_CONF_ERROR;
|
|
529 }
|
|
530 }
|
|
531
|
|
532 return NGX_CONF_OK;
|
|
533 }
|
|
534
|
|
535
|
|
536 static void *ngx_http_charset_create_loc_conf(ngx_conf_t *cf)
|
|
537 {
|
|
538 ngx_http_charset_loc_conf_t *lcf;
|
|
539
|
|
540 if (!(lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_loc_conf_t)))) {
|
|
541 return NGX_CONF_ERROR;
|
|
542 }
|
|
543
|
|
544 lcf->enable = NGX_CONF_UNSET;
|
|
545 lcf->autodetect = NGX_CONF_UNSET;
|
|
546 lcf->default_charset = NGX_CONF_UNSET;
|
|
547 lcf->source_charset = NGX_CONF_UNSET;
|
|
548
|
|
549 return lcf;
|
|
550 }
|
|
551
|
|
552
|
|
553 static char *ngx_http_charset_merge_loc_conf(ngx_conf_t *cf,
|
|
554 void *parent, void *child)
|
|
555 {
|
|
556 ngx_http_charset_loc_conf_t *prev = parent;
|
|
557 ngx_http_charset_loc_conf_t *conf = child;
|
|
558
|
|
559 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
560 ngx_conf_merge_value(conf->autodetect, prev->autodetect, 0);
|
|
561
|
|
562 if (conf->source_charset == NGX_CONF_UNSET) {
|
|
563 conf->source_charset = prev->source_charset;
|
|
564 }
|
|
565
|
|
566 ngx_conf_merge_value(conf->default_charset, prev->default_charset,
|
|
567 conf->source_charset);
|
|
568
|
|
569 return NGX_CONF_OK;
|
|
570 }
|