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