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