50
|
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
|
206
|
12 #define NGX_HTTP_NO_CHARSET -2
|
244
|
13 #define NGX_HTTP_CHARSET_VAR 0x10000
|
206
|
14
|
|
15 /* 1 byte length and up to 3 bytes for the UTF-8 encoding of the UCS-2 */
|
|
16 #define NGX_UTF_LEN 4
|
|
17
|
|
18 #define NGX_HTML_ENTITY_LEN (sizeof("") - 1)
|
78
|
19
|
|
20
|
50
|
21 typedef struct {
|
206
|
22 u_char **tables;
|
|
23 ngx_str_t name;
|
72
|
24
|
206
|
25 unsigned length:16;
|
|
26 unsigned utf8:1;
|
50
|
27 } ngx_http_charset_t;
|
|
28
|
|
29
|
|
30 typedef struct {
|
206
|
31 ngx_int_t src;
|
|
32 ngx_int_t dst;
|
78
|
33 } ngx_http_charset_recode_t;
|
|
34
|
|
35
|
|
36 typedef struct {
|
206
|
37 ngx_int_t src;
|
|
38 ngx_int_t dst;
|
|
39 u_char *src2dst;
|
|
40 u_char *dst2src;
|
50
|
41 } ngx_http_charset_tables_t;
|
|
42
|
|
43
|
|
44 typedef struct {
|
206
|
45 ngx_array_t charsets; /* ngx_http_charset_t */
|
|
46 ngx_array_t tables; /* ngx_http_charset_tables_t */
|
|
47 ngx_array_t recodes; /* ngx_http_charset_recode_t */
|
50
|
48 } ngx_http_charset_main_conf_t;
|
|
49
|
|
50
|
|
51 typedef struct {
|
206
|
52 ngx_int_t charset;
|
|
53 ngx_int_t source_charset;
|
|
54 ngx_flag_t override_charset;
|
394
|
55
|
|
56 ngx_hash_t types;
|
|
57 ngx_array_t *types_keys;
|
50
|
58 } ngx_http_charset_loc_conf_t;
|
|
59
|
|
60
|
|
61 typedef struct {
|
206
|
62 u_char *table;
|
|
63 ngx_int_t charset;
|
|
64
|
|
65 ngx_chain_t *busy;
|
|
66 ngx_chain_t *free_bufs;
|
|
67 ngx_chain_t *free_buffers;
|
|
68
|
|
69 size_t saved_len;
|
|
70 u_char saved[NGX_UTF_LEN];
|
|
71
|
|
72 unsigned length:16;
|
|
73 unsigned from_utf8:1;
|
|
74 unsigned to_utf8:1;
|
50
|
75 } ngx_http_charset_ctx_t;
|
|
76
|
|
77
|
206
|
78 typedef struct {
|
|
79 ngx_http_charset_tables_t *table;
|
|
80 ngx_http_charset_t *charset;
|
|
81 ngx_uint_t characters;
|
|
82 } ngx_http_charset_conf_ctx_t;
|
|
83
|
50
|
84
|
206
|
85 static ngx_int_t ngx_http_charset_get_charset(ngx_http_charset_t *charsets,
|
244
|
86 ngx_uint_t n, ngx_str_t *charset);
|
206
|
87 static ngx_int_t ngx_http_charset_set_charset(ngx_http_request_t *r,
|
|
88 ngx_http_charset_t *charsets, ngx_int_t charset, ngx_int_t source_charset);
|
|
89 static ngx_uint_t ngx_http_charset_recode(ngx_buf_t *b, u_char *table);
|
|
90 static ngx_chain_t *ngx_http_charset_recode_from_utf8(ngx_pool_t *pool,
|
|
91 ngx_buf_t *buf, ngx_http_charset_ctx_t *ctx);
|
|
92 static ngx_chain_t *ngx_http_charset_recode_to_utf8(ngx_pool_t *pool,
|
|
93 ngx_buf_t *buf, ngx_http_charset_ctx_t *ctx);
|
|
94
|
|
95 static ngx_chain_t *ngx_http_charset_get_buf(ngx_pool_t *pool,
|
|
96 ngx_http_charset_ctx_t *ctx);
|
|
97 static ngx_chain_t *ngx_http_charset_get_buffer(ngx_pool_t *pool,
|
|
98 ngx_http_charset_ctx_t *ctx, size_t size);
|
|
99
|
|
100 static char *ngx_http_charset_map_block(ngx_conf_t *cf, ngx_command_t *cmd,
|
50
|
101 void *conf);
|
206
|
102 static char *ngx_http_charset_map(ngx_conf_t *cf, ngx_command_t *dummy,
|
|
103 void *conf);
|
50
|
104
|
|
105 static char *ngx_http_set_charset_slot(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
106 void *conf);
|
|
107 static ngx_int_t ngx_http_add_charset(ngx_array_t *charsets, ngx_str_t *name);
|
|
108
|
|
109 static void *ngx_http_charset_create_main_conf(ngx_conf_t *cf);
|
|
110 static void *ngx_http_charset_create_loc_conf(ngx_conf_t *cf);
|
|
111 static char *ngx_http_charset_merge_loc_conf(ngx_conf_t *cf,
|
|
112 void *parent, void *child);
|
78
|
113 static ngx_int_t ngx_http_charset_postconfiguration(ngx_conf_t *cf);
|
50
|
114
|
|
115
|
394
|
116 ngx_str_t ngx_http_charset_default_types[] = {
|
|
117 ngx_string("text/html"),
|
|
118 ngx_string("text/xml"),
|
|
119 ngx_string("text/plain"),
|
|
120 ngx_string("text/vnd.wap.wml"),
|
|
121 ngx_string("application/x-javascript"),
|
|
122 ngx_string("application/rss+xml"),
|
|
123 ngx_null_string
|
|
124 };
|
|
125
|
|
126
|
50
|
127 static ngx_command_t ngx_http_charset_filter_commands[] = {
|
|
128
|
78
|
129 { ngx_string("charset"),
|
108
|
130 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|
|
131 |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
|
78
|
132 ngx_http_set_charset_slot,
|
|
133 NGX_HTTP_LOC_CONF_OFFSET,
|
|
134 offsetof(ngx_http_charset_loc_conf_t, charset),
|
|
135 NULL },
|
|
136
|
|
137 { ngx_string("source_charset"),
|
108
|
138 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|
|
139 |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
|
78
|
140 ngx_http_set_charset_slot,
|
|
141 NGX_HTTP_LOC_CONF_OFFSET,
|
|
142 offsetof(ngx_http_charset_loc_conf_t, source_charset),
|
|
143 NULL },
|
|
144
|
184
|
145 { ngx_string("override_charset"),
|
|
146 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|
|
147 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
|
|
148 ngx_conf_set_flag_slot,
|
|
149 NGX_HTTP_LOC_CONF_OFFSET,
|
|
150 offsetof(ngx_http_charset_loc_conf_t, override_charset),
|
|
151 NULL },
|
|
152
|
394
|
153 { ngx_string("charset_types"),
|
|
154 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
155 ngx_http_types_slot,
|
|
156 NGX_HTTP_LOC_CONF_OFFSET,
|
|
157 offsetof(ngx_http_charset_loc_conf_t, types_keys),
|
|
158 &ngx_http_charset_default_types[0] },
|
|
159
|
50
|
160 { ngx_string("charset_map"),
|
|
161 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
|
206
|
162 ngx_http_charset_map_block,
|
50
|
163 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
164 0,
|
|
165 NULL },
|
|
166
|
|
167 ngx_null_command
|
|
168 };
|
|
169
|
|
170
|
|
171 static ngx_http_module_t ngx_http_charset_filter_module_ctx = {
|
58
|
172 NULL, /* preconfiguration */
|
78
|
173 ngx_http_charset_postconfiguration, /* postconfiguration */
|
50
|
174
|
|
175 ngx_http_charset_create_main_conf, /* create main configuration */
|
78
|
176 NULL, /* init main configuration */
|
50
|
177
|
|
178 NULL, /* create server configuration */
|
|
179 NULL, /* merge server configuration */
|
|
180
|
|
181 ngx_http_charset_create_loc_conf, /* create location configuration */
|
|
182 ngx_http_charset_merge_loc_conf /* merge location configuration */
|
|
183 };
|
|
184
|
|
185
|
|
186 ngx_module_t ngx_http_charset_filter_module = {
|
58
|
187 NGX_MODULE_V1,
|
50
|
188 &ngx_http_charset_filter_module_ctx, /* module context */
|
|
189 ngx_http_charset_filter_commands, /* module directives */
|
|
190 NGX_HTTP_MODULE, /* module type */
|
90
|
191 NULL, /* init master */
|
230
|
192 NULL, /* init module */
|
90
|
193 NULL, /* init process */
|
|
194 NULL, /* init thread */
|
|
195 NULL, /* exit thread */
|
|
196 NULL, /* exit process */
|
|
197 NULL, /* exit master */
|
|
198 NGX_MODULE_V1_PADDING
|
50
|
199 };
|
|
200
|
|
201
|
|
202 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
203 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
204
|
|
205
|
|
206 static ngx_int_t
|
|
207 ngx_http_charset_header_filter(ngx_http_request_t *r)
|
|
208 {
|
184
|
209 ngx_int_t charset, source_charset;
|
320
|
210 ngx_str_t *mc, *from, *to, s;
|
206
|
211 ngx_uint_t n;
|
50
|
212 ngx_http_charset_t *charsets;
|
|
213 ngx_http_charset_ctx_t *ctx;
|
244
|
214 ngx_http_variable_value_t *vv;
|
202
|
215 ngx_http_charset_loc_conf_t *lcf, *mlcf;
|
50
|
216 ngx_http_charset_main_conf_t *mcf;
|
|
217
|
|
218 mcf = ngx_http_get_module_main_conf(r, ngx_http_charset_filter_module);
|
184
|
219
|
206
|
220 charsets = mcf->charsets.elts;
|
|
221 n = mcf->charsets.nelts;
|
184
|
222
|
206
|
223 /* destination charset */
|
202
|
224
|
|
225 if (r == r->main) {
|
206
|
226
|
494
|
227 if (!r->ignore_content_encoding
|
|
228 && r->headers_out.content_encoding
|
382
|
229 && r->headers_out.content_encoding->value.len)
|
|
230 {
|
|
231 return ngx_http_next_header_filter(r);
|
|
232 }
|
|
233
|
202
|
234 if (r->headers_out.content_type.len == 0) {
|
|
235 return ngx_http_next_header_filter(r);
|
|
236 }
|
50
|
237
|
206
|
238 if (r->headers_out.override_charset
|
|
239 && r->headers_out.override_charset->len)
|
202
|
240 {
|
206
|
241 charset = ngx_http_charset_get_charset(charsets, n,
|
244
|
242 r->headers_out.override_charset);
|
206
|
243
|
|
244 if (charset == NGX_HTTP_NO_CHARSET) {
|
210
|
245 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
206
|
246 "unknown charset \"%V\" to override",
|
208
|
247 r->headers_out.override_charset);
|
206
|
248
|
|
249 return ngx_http_next_header_filter(r);
|
|
250 }
|
|
251
|
|
252 } else {
|
|
253 mlcf = ngx_http_get_module_loc_conf(r,
|
|
254 ngx_http_charset_filter_module);
|
|
255 charset = mlcf->charset;
|
|
256
|
|
257 if (charset == NGX_HTTP_NO_CHARSET) {
|
|
258 return ngx_http_next_header_filter(r);
|
|
259 }
|
|
260
|
|
261 if (r->headers_out.charset.len) {
|
|
262 if (mlcf->override_charset == 0) {
|
|
263 return ngx_http_next_header_filter(r);
|
|
264 }
|
|
265
|
|
266 } else {
|
394
|
267 if (ngx_http_test_content_type(r, &mlcf->types) == NULL) {
|
206
|
268 return ngx_http_next_header_filter(r);
|
|
269 }
|
|
270 }
|
244
|
271
|
|
272 if (charset >= NGX_HTTP_CHARSET_VAR) {
|
|
273 vv = ngx_http_get_indexed_variable(r,
|
|
274 charset - NGX_HTTP_CHARSET_VAR);
|
|
275
|
250
|
276 if (vv == NULL || vv->not_found) {
|
|
277 return NGX_ERROR;
|
|
278 }
|
|
279
|
320
|
280 s.len = vv->len;
|
|
281 s.data = vv->data;
|
|
282
|
|
283 charset = ngx_http_charset_get_charset(charsets, n, &s);
|
244
|
284 }
|
202
|
285 }
|
|
286
|
|
287 } else {
|
206
|
288 ctx = ngx_http_get_module_ctx(r->main, ngx_http_charset_filter_module);
|
|
289
|
|
290 if (ctx == NULL) {
|
|
291
|
|
292 mc = &r->main->headers_out.charset;
|
|
293
|
|
294 if (mc->len == 0) {
|
|
295 return ngx_http_next_header_filter(r);
|
|
296 }
|
|
297
|
|
298 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_charset_ctx_t));
|
|
299 if (ctx == NULL) {
|
|
300 return NGX_ERROR;
|
|
301 }
|
|
302
|
|
303 ngx_http_set_ctx(r->main, ctx, ngx_http_charset_filter_module);
|
202
|
304
|
244
|
305 charset = ngx_http_charset_get_charset(charsets, n, mc);
|
206
|
306
|
|
307 ctx->charset = charset;
|
|
308
|
210
|
309 } else {
|
|
310 charset = ctx->charset;
|
202
|
311 }
|
50
|
312 }
|
|
313
|
206
|
314 /* source charset */
|
184
|
315
|
206
|
316 if (r->headers_out.charset.len == 0) {
|
|
317 lcf = ngx_http_get_module_loc_conf(r, ngx_http_charset_filter_module);
|
184
|
318
|
244
|
319 source_charset = lcf->source_charset;
|
|
320
|
|
321 if (source_charset >= NGX_HTTP_CHARSET_VAR) {
|
|
322 vv = ngx_http_get_indexed_variable(r,
|
|
323 source_charset - NGX_HTTP_CHARSET_VAR);
|
|
324
|
250
|
325 if (vv == NULL || vv->not_found) {
|
|
326 return NGX_ERROR;
|
|
327 }
|
|
328
|
320
|
329 s.len = vv->len;
|
|
330 s.data = vv->data;
|
|
331
|
|
332 source_charset = ngx_http_charset_get_charset(charsets, n, &s);
|
244
|
333 }
|
|
334
|
210
|
335 if (charset != NGX_HTTP_NO_CHARSET) {
|
|
336 return ngx_http_charset_set_charset(r, mcf->charsets.elts, charset,
|
244
|
337 source_charset);
|
210
|
338 }
|
|
339
|
244
|
340 if (source_charset == NGX_CONF_UNSET) {
|
210
|
341 return ngx_http_next_header_filter(r);
|
|
342 }
|
|
343
|
244
|
344 from = &charsets[source_charset].name;
|
212
|
345 to = &r->main->headers_out.charset;
|
210
|
346
|
212
|
347 goto no_charset_map;
|
206
|
348 }
|
184
|
349
|
206
|
350 source_charset = ngx_http_charset_get_charset(charsets, n,
|
244
|
351 &r->headers_out.charset);
|
184
|
352
|
210
|
353 if (charset == NGX_HTTP_NO_CHARSET
|
|
354 || source_charset == NGX_HTTP_NO_CHARSET)
|
|
355 {
|
|
356 if (charset != source_charset
|
|
357 || ngx_strcasecmp(r->main->headers_out.charset.data,
|
|
358 r->headers_out.charset.data)
|
|
359 != 0)
|
|
360 {
|
212
|
361 from = &r->headers_out.charset;
|
|
362 to = (charset == NGX_HTTP_NO_CHARSET) ?
|
|
363 &r->main->headers_out.charset:
|
|
364 &charsets[charset].name;
|
|
365
|
|
366 goto no_charset_map;
|
210
|
367 }
|
184
|
368
|
206
|
369 return ngx_http_next_header_filter(r);
|
|
370 }
|
184
|
371
|
206
|
372 if (source_charset != charset
|
|
373 && (charsets[source_charset].tables == NULL
|
|
374 || charsets[source_charset].tables[charset] == NULL))
|
|
375 {
|
212
|
376 from = &charsets[source_charset].name;
|
|
377 to = &charsets[charset].name;
|
184
|
378
|
212
|
379 goto no_charset_map;
|
50
|
380 }
|
|
381
|
206
|
382 r->headers_out.content_type.len = r->headers_out.content_type_len;
|
|
383
|
|
384 return ngx_http_charset_set_charset(r, mcf->charsets.elts, charset,
|
|
385 source_charset);
|
212
|
386
|
|
387 no_charset_map:
|
|
388
|
|
389 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
342
|
390 "no \"charset_map\" between the charsets \"%V\" and \"%V\"",
|
|
391 from, to);
|
212
|
392
|
|
393 return ngx_http_next_header_filter(r);
|
206
|
394 }
|
|
395
|
|
396
|
|
397 static ngx_int_t
|
|
398 ngx_http_charset_get_charset(ngx_http_charset_t *charsets, ngx_uint_t n,
|
244
|
399 ngx_str_t *charset)
|
206
|
400 {
|
|
401 ngx_uint_t i;
|
|
402
|
|
403 for (i = 0; i < n; i++) {
|
320
|
404 if (charsets[i].name.len != charset->len) {
|
244
|
405 continue;
|
|
406 }
|
|
407
|
320
|
408 if (ngx_strncasecmp(charsets[i].name.data, charset->data, charset->len)
|
|
409 == 0)
|
|
410 {
|
206
|
411 return i;
|
|
412 }
|
|
413 }
|
|
414
|
|
415 return NGX_HTTP_NO_CHARSET;
|
|
416 }
|
|
417
|
|
418
|
|
419 static ngx_int_t
|
|
420 ngx_http_charset_set_charset(ngx_http_request_t *r,
|
|
421 ngx_http_charset_t *charsets, ngx_int_t charset, ngx_int_t source_charset)
|
|
422 {
|
|
423 ngx_http_charset_ctx_t *ctx;
|
|
424
|
50
|
425 if (r->headers_out.status == NGX_HTTP_MOVED_PERMANENTLY
|
|
426 || r->headers_out.status == NGX_HTTP_MOVED_TEMPORARILY)
|
|
427 {
|
|
428 /*
|
184
|
429 * do not set charset for the redirect because NN 4.x
|
|
430 * use this charset instead of the next page charset
|
50
|
431 */
|
|
432
|
|
433 r->headers_out.charset.len = 0;
|
206
|
434
|
50
|
435 return ngx_http_next_header_filter(r);
|
|
436 }
|
|
437
|
184
|
438 r->headers_out.charset = charsets[charset].name;
|
|
439 r->utf8 = charsets[charset].utf8;
|
|
440
|
|
441 if (source_charset == NGX_CONF_UNSET || source_charset == charset) {
|
50
|
442 return ngx_http_next_header_filter(r);
|
|
443 }
|
|
444
|
|
445 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_charset_ctx_t));
|
|
446 if (ctx == NULL) {
|
|
447 return NGX_ERROR;
|
|
448 }
|
|
449
|
|
450 ngx_http_set_ctx(r, ctx, ngx_http_charset_filter_module);
|
|
451
|
184
|
452 ctx->table = charsets[source_charset].tables[charset];
|
|
453 ctx->charset = charset;
|
206
|
454 ctx->length = charsets[charset].length;
|
|
455 ctx->from_utf8 = charsets[source_charset].utf8;
|
|
456 ctx->to_utf8 = charsets[charset].utf8;
|
|
457
|
212
|
458 r->filter_need_in_memory = 1;
|
|
459
|
206
|
460 if ((ctx->to_utf8 || ctx->from_utf8) && r == r->main) {
|
|
461 ngx_http_clear_content_length(r);
|
212
|
462
|
|
463 } else {
|
|
464 r->filter_need_temporary = 1;
|
206
|
465 }
|
50
|
466
|
|
467 return ngx_http_next_header_filter(r);
|
|
468 }
|
|
469
|
|
470
|
|
471 static ngx_int_t
|
|
472 ngx_http_charset_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
473 {
|
206
|
474 ngx_int_t rc;
|
|
475 ngx_buf_t *b;
|
|
476 ngx_chain_t *cl, *out, **ll;
|
184
|
477 ngx_http_charset_ctx_t *ctx;
|
50
|
478
|
|
479 ctx = ngx_http_get_module_ctx(r, ngx_http_charset_filter_module);
|
|
480
|
184
|
481 if (ctx == NULL || ctx->table == NULL) {
|
50
|
482 return ngx_http_next_body_filter(r, in);
|
|
483 }
|
|
484
|
206
|
485 if ((ctx->to_utf8 || ctx->from_utf8) || ctx->busy) {
|
|
486
|
|
487 out = NULL;
|
|
488 ll = &out;
|
|
489
|
|
490 for (cl = in; cl; cl = cl->next) {
|
|
491 b = cl->buf;
|
|
492
|
|
493 if (ngx_buf_size(b) == 0) {
|
214
|
494
|
|
495 *ll = ngx_alloc_chain_link(r->pool);
|
|
496 if (*ll == NULL) {
|
|
497 return NGX_ERROR;
|
|
498 }
|
|
499
|
|
500 (*ll)->buf = b;
|
|
501 (*ll)->next = NULL;
|
|
502
|
|
503 ll = &(*ll)->next;
|
|
504
|
206
|
505 continue;
|
|
506 }
|
|
507
|
|
508 if (ctx->to_utf8) {
|
|
509 *ll = ngx_http_charset_recode_to_utf8(r->pool, b, ctx);
|
|
510
|
|
511 } else {
|
|
512 *ll = ngx_http_charset_recode_from_utf8(r->pool, b, ctx);
|
|
513 }
|
|
514
|
|
515 if (*ll == NULL) {
|
|
516 return NGX_ERROR;
|
|
517 }
|
|
518
|
|
519 while (*ll) {
|
|
520 ll = &(*ll)->next;
|
|
521 }
|
|
522 }
|
|
523
|
|
524 rc = ngx_http_next_body_filter(r, out);
|
|
525
|
|
526 if (out) {
|
|
527 if (ctx->busy == NULL) {
|
|
528 ctx->busy = out;
|
|
529
|
|
530 } else {
|
|
531 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ }
|
|
532 cl->next = out;
|
|
533 }
|
|
534 }
|
|
535
|
|
536 while (ctx->busy) {
|
|
537
|
|
538 cl = ctx->busy;
|
|
539 b = cl->buf;
|
|
540
|
|
541 if (ngx_buf_size(b) != 0) {
|
|
542 break;
|
|
543 }
|
|
544
|
|
545 ctx->busy = cl->next;
|
|
546
|
|
547 if (b->tag != (ngx_buf_tag_t) &ngx_http_charset_filter_module) {
|
|
548 continue;
|
|
549 }
|
|
550
|
|
551 if (b->shadow) {
|
|
552 b->shadow->pos = b->shadow->last;
|
|
553 }
|
|
554
|
|
555 if (b->pos) {
|
|
556 cl->next = ctx->free_buffers;
|
|
557 ctx->free_buffers = cl;
|
|
558 continue;
|
|
559 }
|
|
560
|
|
561 cl->next = ctx->free_bufs;
|
|
562 ctx->free_bufs = cl;
|
|
563 }
|
|
564
|
|
565 return rc;
|
|
566 }
|
|
567
|
50
|
568 for (cl = in; cl; cl = cl->next) {
|
184
|
569 (void) ngx_http_charset_recode(cl->buf, ctx->table);
|
50
|
570 }
|
|
571
|
|
572 return ngx_http_next_body_filter(r, in);
|
|
573 }
|
|
574
|
|
575
|
|
576 static ngx_uint_t
|
184
|
577 ngx_http_charset_recode(ngx_buf_t *b, u_char *table)
|
50
|
578 {
|
370
|
579 u_char *p, *last;
|
184
|
580
|
370
|
581 last = b->last;
|
50
|
582
|
370
|
583 for (p = b->pos; p < last; p++) {
|
|
584
|
|
585 if (*p != table[*p]) {
|
|
586 goto recode;
|
50
|
587 }
|
|
588 }
|
|
589
|
184
|
590 return 0;
|
370
|
591
|
|
592 recode:
|
|
593
|
|
594 do {
|
|
595 if (*p != table[*p]) {
|
|
596 *p = table[*p];
|
|
597 }
|
|
598
|
|
599 p++;
|
|
600
|
|
601 } while (p < last);
|
|
602
|
|
603 b->in_file = 0;
|
|
604
|
|
605 return 1;
|
50
|
606 }
|
|
607
|
|
608
|
206
|
609 static ngx_chain_t *
|
|
610 ngx_http_charset_recode_from_utf8(ngx_pool_t *pool, ngx_buf_t *buf,
|
|
611 ngx_http_charset_ctx_t *ctx)
|
|
612 {
|
|
613 size_t len, size;
|
|
614 u_char c, *p, *src, *dst, *saved, **table;
|
|
615 uint32_t n;
|
|
616 ngx_buf_t *b;
|
|
617 ngx_uint_t i;
|
|
618 ngx_chain_t *out, *cl, **ll;
|
|
619
|
|
620 src = buf->pos;
|
|
621
|
|
622 if (ctx->saved_len == 0) {
|
|
623
|
|
624 for ( /* void */ ; src < buf->last; src++) {
|
|
625
|
|
626 if (*src < 0x80) {
|
|
627 continue;
|
|
628 }
|
|
629
|
|
630 len = src - buf->pos;
|
|
631
|
|
632 if (len > 512) {
|
|
633 out = ngx_http_charset_get_buf(pool, ctx);
|
|
634 if (out == NULL) {
|
|
635 return NULL;
|
|
636 }
|
|
637
|
|
638 b = out->buf;
|
|
639
|
|
640 b->temporary = buf->temporary;
|
|
641 b->memory = buf->memory;
|
|
642 b->mmap = buf->mmap;
|
|
643 b->flush = buf->flush;
|
|
644
|
|
645 b->pos = buf->pos;
|
|
646 b->last = src;
|
|
647
|
|
648 out->buf = b;
|
|
649 out->next = NULL;
|
|
650
|
|
651 size = buf->last - src;
|
|
652
|
|
653 saved = src;
|
390
|
654 n = ngx_utf8_decode(&saved, size);
|
206
|
655
|
|
656 if (n == 0xfffffffe) {
|
|
657 /* incomplete UTF-8 symbol */
|
|
658
|
|
659 ngx_memcpy(ctx->saved, src, size);
|
|
660 ctx->saved_len = size;
|
|
661
|
|
662 b->shadow = buf;
|
|
663
|
|
664 return out;
|
|
665 }
|
|
666
|
|
667 } else {
|
|
668 out = NULL;
|
|
669 size = len + buf->last - src;
|
|
670 src = buf->pos;
|
|
671 }
|
|
672
|
|
673 if (size < NGX_HTML_ENTITY_LEN) {
|
|
674 size += NGX_HTML_ENTITY_LEN;
|
|
675 }
|
|
676
|
|
677 cl = ngx_http_charset_get_buffer(pool, ctx, size);
|
|
678 if (cl == NULL) {
|
|
679 return NULL;
|
|
680 }
|
|
681
|
|
682 if (out) {
|
|
683 out->next = cl;
|
|
684
|
|
685 } else {
|
|
686 out = cl;
|
|
687 }
|
|
688
|
|
689 b = cl->buf;
|
|
690 dst = b->pos;
|
|
691
|
|
692 goto recode;
|
|
693 }
|
|
694
|
|
695 out = ngx_alloc_chain_link(pool);
|
|
696 if (out == NULL) {
|
|
697 return NULL;
|
|
698 }
|
|
699
|
|
700 out->buf = buf;
|
|
701 out->next = NULL;
|
|
702
|
|
703 return out;
|
|
704 }
|
|
705
|
|
706 /* process incomplete UTF sequence from previous buffer */
|
|
707
|
|
708 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pool->log, 0,
|
|
709 "http charset utf saved: %z", ctx->saved_len);
|
|
710
|
|
711 p = src;
|
|
712
|
|
713 for (i = ctx->saved_len; i < NGX_UTF_LEN; i++) {
|
|
714 ctx->saved[i] = *p++;
|
|
715
|
|
716 if (p == buf->last) {
|
|
717 break;
|
|
718 }
|
|
719 }
|
|
720
|
|
721 saved = ctx->saved;
|
390
|
722 n = ngx_utf8_decode(&saved, i);
|
206
|
723
|
|
724 c = '\0';
|
|
725
|
|
726 if (n < 0x10000) {
|
|
727 table = (u_char **) ctx->table;
|
|
728 p = table[n >> 8];
|
|
729
|
|
730 if (p) {
|
|
731 c = p[n & 0xff];
|
|
732 }
|
|
733
|
|
734 } else if (n == 0xfffffffe) {
|
|
735
|
|
736 /* incomplete UTF-8 symbol */
|
|
737
|
|
738 if (i < NGX_UTF_LEN) {
|
|
739 out = ngx_http_charset_get_buf(pool, ctx);
|
|
740 if (out == NULL) {
|
|
741 return NULL;
|
|
742 }
|
|
743
|
|
744 b = out->buf;
|
|
745
|
|
746 b->pos = buf->pos;
|
|
747 b->last = buf->last;
|
|
748 b->sync = 1;
|
|
749 b->shadow = buf;
|
|
750
|
|
751 ngx_memcpy(&ctx->saved[ctx->saved_len], src, i);
|
|
752 ctx->saved_len += i;
|
|
753
|
|
754 return out;
|
|
755 }
|
|
756 }
|
|
757
|
|
758 size = buf->last - buf->pos;
|
|
759
|
|
760 if (size < NGX_HTML_ENTITY_LEN) {
|
|
761 size += NGX_HTML_ENTITY_LEN;
|
|
762 }
|
|
763
|
|
764 cl = ngx_http_charset_get_buffer(pool, ctx, size);
|
|
765 if (cl == NULL) {
|
|
766 return NULL;
|
|
767 }
|
|
768
|
|
769 out = cl;
|
|
770
|
|
771 b = cl->buf;
|
|
772 dst = b->pos;
|
|
773
|
|
774 if (c) {
|
|
775 *dst++ = c;
|
|
776
|
|
777 } else if (n == 0xfffffffe) {
|
|
778 *dst++ = '?';
|
|
779
|
|
780 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pool->log, 0,
|
|
781 "http charset invalid utf 0");
|
|
782
|
|
783 saved = &ctx->saved[NGX_UTF_LEN];
|
|
784
|
|
785 } else if (n > 0x10ffff) {
|
|
786 *dst++ = '?';
|
|
787
|
|
788 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pool->log, 0,
|
|
789 "http charset invalid utf 1");
|
|
790
|
|
791 } else {
|
|
792 dst = ngx_sprintf(dst, "&#%uD;", n);
|
|
793 }
|
|
794
|
|
795 src += (saved - ctx->saved) - ctx->saved_len;
|
|
796 ctx->saved_len = 0;
|
|
797
|
|
798 recode:
|
|
799
|
|
800 ll = &cl->next;
|
|
801
|
|
802 table = (u_char **) ctx->table;
|
|
803
|
|
804 while (src < buf->last) {
|
|
805
|
|
806 if ((size_t) (b->end - dst) < NGX_HTML_ENTITY_LEN) {
|
|
807 b->last = dst;
|
|
808
|
|
809 size = buf->last - src + NGX_HTML_ENTITY_LEN;
|
|
810
|
|
811 cl = ngx_http_charset_get_buffer(pool, ctx, size);
|
|
812 if (cl == NULL) {
|
|
813 return NULL;
|
|
814 }
|
|
815
|
|
816 *ll = cl;
|
|
817 ll = &cl->next;
|
|
818
|
|
819 b = cl->buf;
|
|
820 dst = b->pos;
|
|
821 }
|
|
822
|
|
823 if (*src < 0x80) {
|
|
824 *dst++ = *src++;
|
|
825 continue;
|
|
826 }
|
|
827
|
|
828 len = buf->last - src;
|
|
829
|
390
|
830 n = ngx_utf8_decode(&src, len);
|
206
|
831
|
|
832 if (n < 0x10000) {
|
|
833
|
|
834 p = table[n >> 8];
|
|
835
|
|
836 if (p) {
|
|
837 c = p[n & 0xff];
|
|
838
|
|
839 if (c) {
|
|
840 *dst++ = c;
|
|
841 continue;
|
|
842 }
|
|
843 }
|
|
844
|
|
845 dst = ngx_sprintf(dst, "&#%uD;", n);
|
|
846
|
|
847 continue;
|
|
848 }
|
|
849
|
|
850 if (n == 0xfffffffe) {
|
|
851 /* incomplete UTF-8 symbol */
|
|
852
|
|
853 ngx_memcpy(ctx->saved, src, len);
|
|
854 ctx->saved_len = len;
|
|
855
|
|
856 if (b->pos == dst) {
|
|
857 b->sync = 1;
|
|
858 b->temporary = 0;
|
|
859 }
|
|
860
|
|
861 break;
|
|
862 }
|
|
863
|
|
864 if (n > 0x10ffff) {
|
|
865 *dst++ = '?';
|
|
866
|
|
867 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pool->log, 0,
|
|
868 "http charset invalid utf 2");
|
|
869
|
|
870 continue;
|
|
871 }
|
|
872
|
|
873 /* n > 0xffff */
|
|
874
|
|
875 dst = ngx_sprintf(dst, "&#%uD;", n);
|
|
876 }
|
|
877
|
|
878 b->last = dst;
|
|
879
|
|
880 b->last_buf = buf->last_buf;
|
|
881 b->last_in_chain = buf->last_in_chain;
|
|
882 b->flush = buf->flush;
|
|
883
|
|
884 b->shadow = buf;
|
|
885
|
|
886 return out;
|
|
887 }
|
|
888
|
|
889
|
|
890 static ngx_chain_t *
|
|
891 ngx_http_charset_recode_to_utf8(ngx_pool_t *pool, ngx_buf_t *buf,
|
|
892 ngx_http_charset_ctx_t *ctx)
|
|
893 {
|
|
894 size_t len, size;
|
|
895 u_char *p, *src, *dst, *table;
|
|
896 ngx_buf_t *b;
|
|
897 ngx_chain_t *out, *cl, **ll;
|
|
898
|
|
899 table = ctx->table;
|
|
900
|
|
901 for (src = buf->pos; src < buf->last; src++) {
|
|
902 if (table[*src * NGX_UTF_LEN] == '\1') {
|
|
903 continue;
|
|
904 }
|
|
905
|
|
906 goto recode;
|
|
907 }
|
|
908
|
|
909 out = ngx_alloc_chain_link(pool);
|
|
910 if (out == NULL) {
|
|
911 return NULL;
|
|
912 }
|
|
913
|
|
914 out->buf = buf;
|
|
915 out->next = NULL;
|
|
916
|
|
917 return out;
|
|
918
|
|
919 recode:
|
|
920
|
|
921 /*
|
|
922 * we assume that there are about half of characters to be recoded,
|
|
923 * so we preallocate "size / 2 + size / 2 * ctx->length"
|
|
924 */
|
|
925
|
|
926 len = src - buf->pos;
|
|
927
|
|
928 if (len > 512) {
|
|
929 out = ngx_http_charset_get_buf(pool, ctx);
|
|
930 if (out == NULL) {
|
|
931 return NULL;
|
|
932 }
|
|
933
|
|
934 b = out->buf;
|
|
935
|
|
936 b->temporary = buf->temporary;
|
|
937 b->memory = buf->memory;
|
|
938 b->mmap = buf->mmap;
|
|
939 b->flush = buf->flush;
|
|
940
|
|
941 b->pos = buf->pos;
|
|
942 b->last = src;
|
|
943
|
|
944 out->buf = b;
|
|
945 out->next = NULL;
|
|
946
|
|
947 size = buf->last - src;
|
|
948 size = size / 2 + size / 2 * ctx->length;
|
|
949
|
|
950 } else {
|
|
951 out = NULL;
|
|
952
|
|
953 size = buf->last - src;
|
|
954 size = len + size / 2 + size / 2 * ctx->length;
|
|
955
|
|
956 src = buf->pos;
|
|
957 }
|
|
958
|
|
959 cl = ngx_http_charset_get_buffer(pool, ctx, size);
|
|
960 if (cl == NULL) {
|
|
961 return NULL;
|
|
962 }
|
|
963
|
|
964 if (out) {
|
|
965 out->next = cl;
|
|
966
|
|
967 } else {
|
|
968 out = cl;
|
|
969 }
|
|
970
|
|
971 ll = &cl->next;
|
|
972
|
|
973 b = cl->buf;
|
|
974 dst = b->pos;
|
|
975
|
|
976 while (src < buf->last) {
|
|
977
|
|
978 p = &table[*src++ * NGX_UTF_LEN];
|
|
979 len = *p++;
|
|
980
|
|
981 if ((size_t) (b->end - dst) < len) {
|
|
982 b->last = dst;
|
|
983
|
|
984 size = buf->last - src;
|
|
985 size = len + size / 2 + size / 2 * ctx->length;
|
|
986
|
|
987 cl = ngx_http_charset_get_buffer(pool, ctx, size);
|
|
988 if (cl == NULL) {
|
|
989 return NULL;
|
|
990 }
|
|
991
|
|
992 *ll = cl;
|
|
993 ll = &cl->next;
|
|
994
|
|
995 b = cl->buf;
|
|
996 dst = b->pos;
|
|
997 }
|
|
998
|
|
999 while (len) {
|
|
1000 *dst++ = *p++;
|
|
1001 len--;
|
|
1002 }
|
|
1003 }
|
|
1004
|
|
1005 b->last = dst;
|
|
1006
|
|
1007 b->last_buf = buf->last_buf;
|
|
1008 b->last_in_chain = buf->last_in_chain;
|
|
1009 b->flush = buf->flush;
|
|
1010
|
|
1011 b->shadow = buf;
|
|
1012
|
|
1013 return out;
|
|
1014 }
|
|
1015
|
|
1016
|
|
1017 static ngx_chain_t *
|
|
1018 ngx_http_charset_get_buf(ngx_pool_t *pool, ngx_http_charset_ctx_t *ctx)
|
|
1019 {
|
|
1020 ngx_chain_t *cl;
|
|
1021
|
|
1022 cl = ctx->free_bufs;
|
|
1023
|
|
1024 if (cl) {
|
|
1025 ctx->free_bufs = cl->next;
|
|
1026
|
|
1027 cl->buf->shadow = NULL;
|
|
1028 cl->next = NULL;
|
|
1029
|
|
1030 return cl;
|
|
1031 }
|
|
1032
|
|
1033 cl = ngx_alloc_chain_link(pool);
|
|
1034 if (cl == NULL) {
|
|
1035 return NULL;
|
|
1036 }
|
|
1037
|
|
1038 cl->buf = ngx_calloc_buf(pool);
|
|
1039 if (cl->buf == NULL) {
|
|
1040 return NULL;
|
|
1041 }
|
|
1042
|
|
1043 cl->next = NULL;
|
|
1044
|
|
1045 cl->buf->tag = (ngx_buf_tag_t) &ngx_http_charset_filter_module;
|
|
1046
|
|
1047 return cl;
|
|
1048 }
|
|
1049
|
|
1050
|
|
1051 static ngx_chain_t *
|
|
1052 ngx_http_charset_get_buffer(ngx_pool_t *pool, ngx_http_charset_ctx_t *ctx,
|
|
1053 size_t size)
|
|
1054 {
|
|
1055 ngx_buf_t *b;
|
|
1056 ngx_chain_t *cl, **ll;
|
|
1057
|
|
1058 for (ll = &ctx->free_buffers, cl = ctx->free_buffers;
|
|
1059 cl;
|
|
1060 ll = &cl->next, cl = cl->next)
|
|
1061 {
|
|
1062 b = cl->buf;
|
|
1063
|
|
1064 if ((size_t) (b->end - b->start) >= size) {
|
|
1065 *ll = cl->next;
|
|
1066 cl->next = NULL;
|
|
1067
|
|
1068 b->pos = b->start;
|
|
1069 b->temporary = 1;
|
|
1070 b->shadow = NULL;
|
|
1071
|
|
1072 return cl;
|
|
1073 }
|
|
1074 }
|
|
1075
|
|
1076 cl = ngx_alloc_chain_link(pool);
|
|
1077 if (cl == NULL) {
|
|
1078 return NULL;
|
|
1079 }
|
|
1080
|
|
1081 cl->buf = ngx_create_temp_buf(pool, size);
|
|
1082 if (cl->buf == NULL) {
|
|
1083 return NULL;
|
|
1084 }
|
|
1085
|
|
1086 cl->next = NULL;
|
|
1087
|
|
1088 cl->buf->temporary = 1;
|
|
1089 cl->buf->tag = (ngx_buf_tag_t) &ngx_http_charset_filter_module;
|
|
1090
|
|
1091 return cl;
|
|
1092 }
|
|
1093
|
|
1094
|
50
|
1095 static char *
|
206
|
1096 ngx_http_charset_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
50
|
1097 {
|
|
1098 ngx_http_charset_main_conf_t *mcf = conf;
|
|
1099
|
206
|
1100 char *rv;
|
|
1101 u_char *p, *dst2src, **pp;
|
|
1102 ngx_int_t src, dst;
|
|
1103 ngx_uint_t i, n;
|
|
1104 ngx_str_t *value;
|
|
1105 ngx_conf_t pvcf;
|
|
1106 ngx_http_charset_t *charset;
|
|
1107 ngx_http_charset_tables_t *table;
|
|
1108 ngx_http_charset_conf_ctx_t ctx;
|
50
|
1109
|
|
1110 value = cf->args->elts;
|
|
1111
|
|
1112 src = ngx_http_add_charset(&mcf->charsets, &value[1]);
|
|
1113 if (src == NGX_ERROR) {
|
|
1114 return NGX_CONF_ERROR;
|
|
1115 }
|
|
1116
|
|
1117 dst = ngx_http_add_charset(&mcf->charsets, &value[2]);
|
|
1118 if (dst == NGX_ERROR) {
|
|
1119 return NGX_CONF_ERROR;
|
|
1120 }
|
|
1121
|
|
1122 if (src == dst) {
|
|
1123 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1124 "\"charset_map\" between the same charsets "
|
|
1125 "\"%V\" and \"%V\"", &value[1], &value[2]);
|
|
1126 return NGX_CONF_ERROR;
|
|
1127 }
|
|
1128
|
|
1129 table = mcf->tables.elts;
|
|
1130 for (i = 0; i < mcf->tables.nelts; i++) {
|
|
1131 if ((src == table->src && dst == table->dst)
|
|
1132 || (src == table->dst && dst == table->src))
|
|
1133 {
|
|
1134 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1135 "duplicate \"charset_map\" between "
|
|
1136 "\"%V\" and \"%V\"", &value[1], &value[2]);
|
|
1137 return NGX_CONF_ERROR;
|
|
1138 }
|
|
1139 }
|
|
1140
|
|
1141 table = ngx_array_push(&mcf->tables);
|
|
1142 if (table == NULL) {
|
|
1143 return NGX_CONF_ERROR;
|
|
1144 }
|
|
1145
|
|
1146 table->src = src;
|
|
1147 table->dst = dst;
|
|
1148
|
286
|
1149 if (ngx_strcasecmp(value[2].data, (u_char *) "utf-8") == 0) {
|
206
|
1150 table->src2dst = ngx_pcalloc(cf->pool, 256 * NGX_UTF_LEN);
|
|
1151 if (table->src2dst == NULL) {
|
|
1152 return NGX_CONF_ERROR;
|
|
1153 }
|
|
1154
|
|
1155 table->dst2src = ngx_pcalloc(cf->pool, 256 * sizeof(void *));
|
|
1156 if (table->dst2src == NULL) {
|
|
1157 return NGX_CONF_ERROR;
|
|
1158 }
|
|
1159
|
|
1160 dst2src = ngx_pcalloc(cf->pool, 256);
|
|
1161 if (dst2src == NULL) {
|
|
1162 return NGX_CONF_ERROR;
|
|
1163 }
|
|
1164
|
|
1165 pp = (u_char **) &table->dst2src[0];
|
|
1166 pp[0] = dst2src;
|
|
1167
|
|
1168 for (i = 0; i < 128; i++) {
|
|
1169 p = &table->src2dst[i * NGX_UTF_LEN];
|
|
1170 p[0] = '\1';
|
|
1171 p[1] = (u_char) i;
|
|
1172 dst2src[i] = (u_char) i;
|
|
1173 }
|
50
|
1174
|
206
|
1175 for (/* void */; i < 256; i++) {
|
|
1176 p = &table->src2dst[i * NGX_UTF_LEN];
|
|
1177 p[0] = '\1';
|
|
1178 p[1] = '?';
|
|
1179 }
|
|
1180
|
|
1181 } else {
|
|
1182 table->src2dst = ngx_palloc(cf->pool, 256);
|
|
1183 if (table->src2dst == NULL) {
|
|
1184 return NGX_CONF_ERROR;
|
|
1185 }
|
|
1186
|
|
1187 table->dst2src = ngx_palloc(cf->pool, 256);
|
|
1188 if (table->dst2src == NULL) {
|
|
1189 return NGX_CONF_ERROR;
|
|
1190 }
|
|
1191
|
|
1192 for (i = 0; i < 128; i++) {
|
|
1193 table->src2dst[i] = (u_char) i;
|
|
1194 table->dst2src[i] = (u_char) i;
|
|
1195 }
|
|
1196
|
|
1197 for (/* void */; i < 256; i++) {
|
|
1198 table->src2dst[i] = '?';
|
|
1199 table->dst2src[i] = '?';
|
|
1200 }
|
50
|
1201 }
|
|
1202
|
206
|
1203 charset = mcf->charsets.elts;
|
50
|
1204
|
206
|
1205 ctx.table = table;
|
|
1206 ctx.charset = &charset[dst];
|
|
1207 ctx.characters = 0;
|
50
|
1208
|
|
1209 pvcf = *cf;
|
206
|
1210 cf->ctx = &ctx;
|
|
1211 cf->handler = ngx_http_charset_map;
|
50
|
1212 cf->handler_conf = conf;
|
|
1213
|
|
1214 rv = ngx_conf_parse(cf, NULL);
|
|
1215
|
|
1216 *cf = pvcf;
|
|
1217
|
206
|
1218 if (ctx.characters) {
|
|
1219 n = ctx.charset->length;
|
|
1220 ctx.charset->length /= ctx.characters;
|
|
1221
|
|
1222 if (((n * 10) / ctx.characters) % 10 > 4) {
|
|
1223 ctx.charset->length++;
|
|
1224 }
|
|
1225 }
|
|
1226
|
50
|
1227 return rv;
|
|
1228 }
|
|
1229
|
|
1230
|
|
1231 static char *
|
206
|
1232 ngx_http_charset_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
50
|
1233 {
|
206
|
1234 u_char *p, *dst2src, **pp;
|
|
1235 uint32_t n;
|
|
1236 ngx_int_t src, dst;
|
|
1237 ngx_str_t *value;
|
|
1238 ngx_uint_t i;
|
|
1239 ngx_http_charset_tables_t *table;
|
|
1240 ngx_http_charset_conf_ctx_t *ctx;
|
50
|
1241
|
|
1242 if (cf->args->nelts != 2) {
|
|
1243 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameters number");
|
|
1244 return NGX_CONF_ERROR;
|
|
1245 }
|
|
1246
|
|
1247 value = cf->args->elts;
|
|
1248
|
|
1249 src = ngx_hextoi(value[0].data, value[0].len);
|
|
1250 if (src == NGX_ERROR || src > 255) {
|
|
1251 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1252 "invalid value \"%V\"", &value[0]);
|
|
1253 return NGX_CONF_ERROR;
|
|
1254 }
|
|
1255
|
206
|
1256 ctx = cf->ctx;
|
|
1257 table = ctx->table;
|
|
1258
|
|
1259 if (ctx->charset->utf8) {
|
|
1260 p = &table->src2dst[src * NGX_UTF_LEN];
|
|
1261
|
|
1262 *p++ = (u_char) (value[1].len / 2);
|
|
1263
|
|
1264 for (i = 0; i < value[1].len; i += 2) {
|
|
1265 dst = ngx_hextoi(&value[1].data[i], 2);
|
|
1266 if (dst == NGX_ERROR || dst > 255) {
|
|
1267 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1268 "invalid value \"%V\"", &value[1]);
|
|
1269 return NGX_CONF_ERROR;
|
|
1270 }
|
|
1271
|
|
1272 *p++ = (u_char) dst;
|
|
1273 }
|
|
1274
|
|
1275 i /= 2;
|
|
1276
|
|
1277 ctx->charset->length += i;
|
|
1278 ctx->characters++;
|
|
1279
|
|
1280 p = &table->src2dst[src * NGX_UTF_LEN] + 1;
|
|
1281
|
390
|
1282 n = ngx_utf8_decode(&p, i);
|
206
|
1283
|
|
1284 if (n > 0xffff) {
|
|
1285 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1286 "invalid value \"%V\"", &value[1]);
|
|
1287 return NGX_CONF_ERROR;
|
|
1288 }
|
|
1289
|
|
1290 pp = (u_char **) &table->dst2src[0];
|
|
1291
|
|
1292 dst2src = pp[n >> 8];
|
|
1293
|
|
1294 if (dst2src == NULL) {
|
|
1295 dst2src = ngx_pcalloc(cf->pool, 256);
|
|
1296 if (dst2src == NULL) {
|
|
1297 return NGX_CONF_ERROR;
|
|
1298 }
|
|
1299
|
|
1300 pp[n >> 8] = dst2src;
|
|
1301 }
|
|
1302
|
|
1303 dst2src[n & 0xff] = (u_char) src;
|
|
1304
|
|
1305 } else {
|
|
1306 dst = ngx_hextoi(value[1].data, value[1].len);
|
|
1307 if (dst == NGX_ERROR || dst > 255) {
|
|
1308 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1309 "invalid value \"%V\"", &value[1]);
|
|
1310 return NGX_CONF_ERROR;
|
|
1311 }
|
|
1312
|
|
1313 table->src2dst[src] = (u_char) dst;
|
|
1314 table->dst2src[dst] = (u_char) src;
|
50
|
1315 }
|
|
1316
|
|
1317 return NGX_CONF_OK;
|
|
1318 }
|
|
1319
|
|
1320
|
|
1321 static char *
|
|
1322 ngx_http_set_charset_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1323 {
|
|
1324 char *p = conf;
|
|
1325
|
|
1326 ngx_int_t *cp;
|
244
|
1327 ngx_str_t *value, var;
|
50
|
1328 ngx_http_charset_main_conf_t *mcf;
|
|
1329
|
|
1330 cp = (ngx_int_t *) (p + cmd->offset);
|
|
1331
|
|
1332 if (*cp != NGX_CONF_UNSET) {
|
|
1333 return "is duplicate";
|
|
1334 }
|
|
1335
|
78
|
1336 value = cf->args->elts;
|
|
1337
|
|
1338 if (cmd->offset == offsetof(ngx_http_charset_loc_conf_t, charset)
|
|
1339 && ngx_strcmp(value[1].data, "off") == 0)
|
|
1340 {
|
|
1341 *cp = NGX_HTTP_NO_CHARSET;
|
|
1342 return NGX_CONF_OK;
|
|
1343 }
|
|
1344
|
244
|
1345
|
|
1346 if (value[1].data[0] == '$') {
|
|
1347 var.len = value[1].len - 1;
|
|
1348 var.data = value[1].data + 1;
|
|
1349
|
|
1350 *cp = ngx_http_get_variable_index(cf, &var);
|
|
1351
|
|
1352 if (*cp == NGX_ERROR) {
|
|
1353 return NGX_CONF_ERROR;
|
|
1354 }
|
|
1355
|
|
1356 *cp += NGX_HTTP_CHARSET_VAR;
|
|
1357
|
|
1358 return NGX_CONF_OK;
|
|
1359 }
|
|
1360
|
50
|
1361 mcf = ngx_http_conf_get_module_main_conf(cf,
|
|
1362 ngx_http_charset_filter_module);
|
|
1363
|
|
1364 *cp = ngx_http_add_charset(&mcf->charsets, &value[1]);
|
|
1365 if (*cp == NGX_ERROR) {
|
|
1366 return NGX_CONF_ERROR;
|
|
1367 }
|
|
1368
|
|
1369 return NGX_CONF_OK;
|
|
1370 }
|
|
1371
|
|
1372
|
|
1373 static ngx_int_t
|
|
1374 ngx_http_add_charset(ngx_array_t *charsets, ngx_str_t *name)
|
|
1375 {
|
|
1376 ngx_uint_t i;
|
|
1377 ngx_http_charset_t *c;
|
|
1378
|
|
1379 c = charsets->elts;
|
|
1380 for (i = 0; i < charsets->nelts; i++) {
|
|
1381 if (name->len != c[i].name.len) {
|
|
1382 continue;
|
|
1383 }
|
|
1384
|
|
1385 if (ngx_strcasecmp(name->data, c[i].name.data) == 0) {
|
|
1386 break;
|
|
1387 }
|
|
1388 }
|
|
1389
|
|
1390 if (i < charsets->nelts) {
|
|
1391 return i;
|
|
1392 }
|
|
1393
|
|
1394 c = ngx_array_push(charsets);
|
|
1395 if (c == NULL) {
|
|
1396 return NGX_ERROR;
|
|
1397 }
|
|
1398
|
|
1399 c->tables = NULL;
|
|
1400 c->name = *name;
|
206
|
1401 c->length = 0;
|
50
|
1402
|
286
|
1403 if (ngx_strcasecmp(name->data, (u_char *) "utf-8") == 0) {
|
72
|
1404 c->utf8 = 1;
|
216
|
1405
|
|
1406 } else {
|
|
1407 c->utf8 = 0;
|
72
|
1408 }
|
|
1409
|
50
|
1410 return i;
|
|
1411 }
|
|
1412
|
|
1413
|
|
1414 static void *
|
|
1415 ngx_http_charset_create_main_conf(ngx_conf_t *cf)
|
|
1416 {
|
|
1417 ngx_http_charset_main_conf_t *mcf;
|
|
1418
|
|
1419 mcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_main_conf_t));
|
|
1420 if (mcf == NULL) {
|
|
1421 return NGX_CONF_ERROR;
|
|
1422 }
|
|
1423
|
|
1424 if (ngx_array_init(&mcf->charsets, cf->pool, 2, sizeof(ngx_http_charset_t))
|
454
|
1425 != NGX_OK)
|
50
|
1426 {
|
|
1427 return NGX_CONF_ERROR;
|
|
1428 }
|
|
1429
|
78
|
1430 if (ngx_array_init(&mcf->tables, cf->pool, 1,
|
454
|
1431 sizeof(ngx_http_charset_tables_t))
|
|
1432 != NGX_OK)
|
50
|
1433 {
|
|
1434 return NGX_CONF_ERROR;
|
|
1435 }
|
|
1436
|
78
|
1437 if (ngx_array_init(&mcf->recodes, cf->pool, 2,
|
454
|
1438 sizeof(ngx_http_charset_recode_t))
|
|
1439 != NGX_OK)
|
78
|
1440 {
|
|
1441 return NGX_CONF_ERROR;
|
|
1442 }
|
|
1443
|
50
|
1444 return mcf;
|
|
1445 }
|
|
1446
|
|
1447
|
|
1448 static void *
|
|
1449 ngx_http_charset_create_loc_conf(ngx_conf_t *cf)
|
|
1450 {
|
|
1451 ngx_http_charset_loc_conf_t *lcf;
|
|
1452
|
|
1453 lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_loc_conf_t));
|
|
1454 if (lcf == NULL) {
|
|
1455 return NGX_CONF_ERROR;
|
|
1456 }
|
|
1457
|
394
|
1458 /*
|
|
1459 * set by ngx_pcalloc():
|
|
1460 *
|
|
1461 * lcf->types = { NULL };
|
|
1462 * lcf->types_keys = NULL;
|
|
1463 */
|
|
1464
|
78
|
1465 lcf->charset = NGX_CONF_UNSET;
|
50
|
1466 lcf->source_charset = NGX_CONF_UNSET;
|
184
|
1467 lcf->override_charset = NGX_CONF_UNSET;
|
50
|
1468
|
|
1469 return lcf;
|
|
1470 }
|
|
1471
|
|
1472
|
|
1473 static char *
|
|
1474 ngx_http_charset_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
1475 {
|
|
1476 ngx_http_charset_loc_conf_t *prev = parent;
|
|
1477 ngx_http_charset_loc_conf_t *conf = child;
|
|
1478
|
78
|
1479 ngx_uint_t i;
|
|
1480 ngx_http_charset_recode_t *recode;
|
|
1481 ngx_http_charset_main_conf_t *mcf;
|
50
|
1482
|
396
|
1483 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types,
|
|
1484 prev->types_keys, &prev->types,
|
|
1485 ngx_http_charset_default_types)
|
|
1486 != NGX_OK)
|
|
1487 {
|
|
1488 return NGX_CONF_ERROR;
|
|
1489 }
|
|
1490
|
184
|
1491 ngx_conf_merge_value(conf->override_charset, prev->override_charset, 0);
|
78
|
1492 ngx_conf_merge_value(conf->charset, prev->charset, NGX_HTTP_NO_CHARSET);
|
50
|
1493
|
|
1494 if (conf->source_charset == NGX_CONF_UNSET) {
|
|
1495 conf->source_charset = prev->source_charset;
|
|
1496 }
|
|
1497
|
78
|
1498 if (conf->charset == NGX_HTTP_NO_CHARSET
|
|
1499 || conf->source_charset == NGX_CONF_UNSET
|
|
1500 || conf->charset == conf->source_charset)
|
50
|
1501 {
|
78
|
1502 return NGX_CONF_OK;
|
50
|
1503 }
|
|
1504
|
342
|
1505 if (conf->source_charset >= NGX_HTTP_CHARSET_VAR
|
|
1506 || conf->charset >= NGX_HTTP_CHARSET_VAR)
|
|
1507 {
|
|
1508 return NGX_CONF_OK;
|
|
1509 }
|
|
1510
|
78
|
1511 mcf = ngx_http_conf_get_module_main_conf(cf,
|
|
1512 ngx_http_charset_filter_module);
|
|
1513 recode = mcf->recodes.elts;
|
|
1514 for (i = 0; i < mcf->recodes.nelts; i++) {
|
|
1515 if (conf->source_charset == recode[i].src
|
|
1516 && conf->charset == recode[i].dst)
|
|
1517 {
|
|
1518 return NGX_CONF_OK;
|
|
1519 }
|
50
|
1520 }
|
|
1521
|
78
|
1522 recode = ngx_array_push(&mcf->recodes);
|
|
1523 if (recode == NULL) {
|
50
|
1524 return NGX_CONF_ERROR;
|
|
1525 }
|
|
1526
|
78
|
1527 recode->src = conf->source_charset;
|
|
1528 recode->dst = conf->charset;
|
|
1529
|
50
|
1530 return NGX_CONF_OK;
|
|
1531 }
|
78
|
1532
|
|
1533
|
|
1534 static ngx_int_t
|
|
1535 ngx_http_charset_postconfiguration(ngx_conf_t *cf)
|
|
1536 {
|
184
|
1537 u_char **src, **dst;
|
78
|
1538 ngx_int_t c;
|
|
1539 ngx_uint_t i, t;
|
|
1540 ngx_http_charset_t *charset;
|
|
1541 ngx_http_charset_recode_t *recode;
|
|
1542 ngx_http_charset_tables_t *tables;
|
|
1543 ngx_http_charset_main_conf_t *mcf;
|
|
1544
|
|
1545 mcf = ngx_http_conf_get_module_main_conf(cf,
|
|
1546 ngx_http_charset_filter_module);
|
|
1547
|
|
1548 recode = mcf->recodes.elts;
|
|
1549 tables = mcf->tables.elts;
|
|
1550 charset = mcf->charsets.elts;
|
|
1551
|
|
1552 for (i = 0; i < mcf->recodes.nelts; i++) {
|
|
1553
|
|
1554 c = recode[i].src;
|
|
1555
|
|
1556 for (t = 0; t < mcf->tables.nelts; t++) {
|
|
1557
|
|
1558 if (c == tables[t].src && recode[i].dst == tables[t].dst) {
|
|
1559 goto next;
|
|
1560 }
|
|
1561
|
|
1562 if (c == tables[t].dst && recode[i].dst == tables[t].src) {
|
|
1563 goto next;
|
|
1564 }
|
|
1565 }
|
|
1566
|
|
1567 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
342
|
1568 "no \"charset_map\" between the charsets \"%V\" and \"%V\"",
|
|
1569 &charset[c].name, &charset[recode[i].dst].name);
|
78
|
1570 return NGX_ERROR;
|
|
1571
|
|
1572 next:
|
|
1573 continue;
|
|
1574 }
|
|
1575
|
184
|
1576
|
|
1577 for (t = 0; t < mcf->tables.nelts; t++) {
|
|
1578
|
|
1579 src = charset[tables[t].src].tables;
|
|
1580
|
|
1581 if (src == NULL) {
|
|
1582 src = ngx_pcalloc(cf->pool, sizeof(u_char *) * mcf->charsets.nelts);
|
|
1583 if (src == NULL) {
|
|
1584 return NGX_ERROR;
|
|
1585 }
|
|
1586
|
|
1587 charset[tables[t].src].tables = src;
|
|
1588 }
|
|
1589
|
|
1590 dst = charset[tables[t].dst].tables;
|
|
1591
|
|
1592 if (dst == NULL) {
|
|
1593 dst = ngx_pcalloc(cf->pool, sizeof(u_char *) * mcf->charsets.nelts);
|
|
1594 if (dst == NULL) {
|
|
1595 return NGX_ERROR;
|
|
1596 }
|
|
1597
|
|
1598 charset[tables[t].dst].tables = dst;
|
|
1599 }
|
|
1600
|
|
1601 src[tables[t].dst] = tables[t].src2dst;
|
|
1602 dst[tables[t].src] = tables[t].dst2src;
|
|
1603 }
|
|
1604
|
230
|
1605 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
1606 ngx_http_top_header_filter = ngx_http_charset_header_filter;
|
|
1607
|
|
1608 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
1609 ngx_http_top_body_filter = ngx_http_charset_body_filter;
|
|
1610
|
78
|
1611 return NGX_OK;
|
|
1612 }
|