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