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