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