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