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