Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_charset_filter_module.c @ 2036:1c009c5a163b
fix error log message
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 26 May 2008 10:54:59 +0000 |
parents | be6c9033a798 |
children | c0bafb136d7b |
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 { |
1944
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
564 u_char *p, *last; |
635 | 565 |
1944
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
566 last = b->last; |
455 | 567 |
1944
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
568 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
|
569 |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
570 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
|
571 goto recode; |
455 | 572 } |
573 } | |
574 | |
635 | 575 return 0; |
1944
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
576 |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
577 recode: |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
578 |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
579 do { |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
580 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
|
581 *p = table[*p]; |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
582 } |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
583 |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
584 p++; |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
585 |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
586 } while (p < last); |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
587 |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
588 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
|
589 |
be6c9033a798
speed up ngx_http_charset_recode() for 25%: google-perftools reported
Igor Sysoev <igor@sysoev.ru>
parents:
1590
diff
changeset
|
590 return 1; |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
591 } |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
592 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
593 |
657 | 594 static ngx_chain_t * |
595 ngx_http_charset_recode_from_utf8(ngx_pool_t *pool, ngx_buf_t *buf, | |
596 ngx_http_charset_ctx_t *ctx) | |
597 { | |
598 size_t len, size; | |
599 u_char c, *p, *src, *dst, *saved, **table; | |
600 uint32_t n; | |
601 ngx_buf_t *b; | |
602 ngx_uint_t i; | |
603 ngx_chain_t *out, *cl, **ll; | |
604 | |
605 src = buf->pos; | |
606 | |
607 if (ctx->saved_len == 0) { | |
608 | |
609 for ( /* void */ ; src < buf->last; src++) { | |
610 | |
611 if (*src < 0x80) { | |
612 continue; | |
613 } | |
614 | |
615 len = src - buf->pos; | |
616 | |
617 if (len > 512) { | |
618 out = ngx_http_charset_get_buf(pool, ctx); | |
619 if (out == NULL) { | |
620 return NULL; | |
621 } | |
622 | |
623 b = out->buf; | |
624 | |
625 b->temporary = buf->temporary; | |
626 b->memory = buf->memory; | |
627 b->mmap = buf->mmap; | |
628 b->flush = buf->flush; | |
629 | |
630 b->pos = buf->pos; | |
631 b->last = src; | |
632 | |
633 out->buf = b; | |
634 out->next = NULL; | |
635 | |
636 size = buf->last - src; | |
637 | |
638 saved = src; | |
639 n = ngx_utf_decode(&saved, size); | |
640 | |
641 if (n == 0xfffffffe) { | |
642 /* incomplete UTF-8 symbol */ | |
643 | |
644 ngx_memcpy(ctx->saved, src, size); | |
645 ctx->saved_len = size; | |
646 | |
647 b->shadow = buf; | |
648 | |
649 return out; | |
650 } | |
651 | |
652 } else { | |
653 out = NULL; | |
654 size = len + buf->last - src; | |
655 src = buf->pos; | |
656 } | |
657 | |
658 if (size < NGX_HTML_ENTITY_LEN) { | |
659 size += NGX_HTML_ENTITY_LEN; | |
660 } | |
661 | |
662 cl = ngx_http_charset_get_buffer(pool, ctx, size); | |
663 if (cl == NULL) { | |
664 return NULL; | |
665 } | |
666 | |
667 if (out) { | |
668 out->next = cl; | |
669 | |
670 } else { | |
671 out = cl; | |
672 } | |
673 | |
674 b = cl->buf; | |
675 dst = b->pos; | |
676 | |
677 goto recode; | |
678 } | |
679 | |
680 out = ngx_alloc_chain_link(pool); | |
681 if (out == NULL) { | |
682 return NULL; | |
683 } | |
684 | |
685 out->buf = buf; | |
686 out->next = NULL; | |
687 | |
688 return out; | |
689 } | |
690 | |
691 /* process incomplete UTF sequence from previous buffer */ | |
692 | |
693 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pool->log, 0, | |
694 "http charset utf saved: %z", ctx->saved_len); | |
695 | |
696 p = src; | |
697 | |
698 for (i = ctx->saved_len; i < NGX_UTF_LEN; i++) { | |
699 ctx->saved[i] = *p++; | |
700 | |
701 if (p == buf->last) { | |
702 break; | |
703 } | |
704 } | |
705 | |
706 saved = ctx->saved; | |
707 n = ngx_utf_decode(&saved, i); | |
708 | |
709 c = '\0'; | |
710 | |
711 if (n < 0x10000) { | |
712 table = (u_char **) ctx->table; | |
713 p = table[n >> 8]; | |
714 | |
715 if (p) { | |
716 c = p[n & 0xff]; | |
717 } | |
718 | |
719 } else if (n == 0xfffffffe) { | |
720 | |
721 /* incomplete UTF-8 symbol */ | |
722 | |
723 if (i < NGX_UTF_LEN) { | |
724 out = ngx_http_charset_get_buf(pool, ctx); | |
725 if (out == NULL) { | |
726 return NULL; | |
727 } | |
728 | |
729 b = out->buf; | |
730 | |
731 b->pos = buf->pos; | |
732 b->last = buf->last; | |
733 b->sync = 1; | |
734 b->shadow = buf; | |
735 | |
736 ngx_memcpy(&ctx->saved[ctx->saved_len], src, i); | |
737 ctx->saved_len += i; | |
738 | |
739 return out; | |
740 } | |
741 } | |
742 | |
743 size = buf->last - buf->pos; | |
744 | |
745 if (size < NGX_HTML_ENTITY_LEN) { | |
746 size += NGX_HTML_ENTITY_LEN; | |
747 } | |
748 | |
749 cl = ngx_http_charset_get_buffer(pool, ctx, size); | |
750 if (cl == NULL) { | |
751 return NULL; | |
752 } | |
753 | |
754 out = cl; | |
755 | |
756 b = cl->buf; | |
757 dst = b->pos; | |
758 | |
759 if (c) { | |
760 *dst++ = c; | |
761 | |
762 } else if (n == 0xfffffffe) { | |
763 *dst++ = '?'; | |
764 | |
765 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pool->log, 0, | |
766 "http charset invalid utf 0"); | |
767 | |
768 saved = &ctx->saved[NGX_UTF_LEN]; | |
769 | |
770 } else if (n > 0x10ffff) { | |
771 *dst++ = '?'; | |
772 | |
773 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pool->log, 0, | |
774 "http charset invalid utf 1"); | |
775 | |
776 } else { | |
777 dst = ngx_sprintf(dst, "&#%uD;", n); | |
778 } | |
779 | |
780 src += (saved - ctx->saved) - ctx->saved_len; | |
781 ctx->saved_len = 0; | |
782 | |
783 recode: | |
784 | |
785 ll = &cl->next; | |
786 | |
787 table = (u_char **) ctx->table; | |
788 | |
789 while (src < buf->last) { | |
790 | |
791 if ((size_t) (b->end - dst) < NGX_HTML_ENTITY_LEN) { | |
792 b->last = dst; | |
793 | |
794 size = buf->last - src + NGX_HTML_ENTITY_LEN; | |
795 | |
796 cl = ngx_http_charset_get_buffer(pool, ctx, size); | |
797 if (cl == NULL) { | |
798 return NULL; | |
799 } | |
800 | |
801 *ll = cl; | |
802 ll = &cl->next; | |
803 | |
804 b = cl->buf; | |
805 dst = b->pos; | |
806 } | |
807 | |
808 if (*src < 0x80) { | |
809 *dst++ = *src++; | |
810 continue; | |
811 } | |
812 | |
813 len = buf->last - src; | |
814 | |
815 n = ngx_utf_decode(&src, len); | |
816 | |
817 if (n < 0x10000) { | |
818 | |
819 p = table[n >> 8]; | |
820 | |
821 if (p) { | |
822 c = p[n & 0xff]; | |
823 | |
824 if (c) { | |
825 *dst++ = c; | |
826 continue; | |
827 } | |
828 } | |
829 | |
830 dst = ngx_sprintf(dst, "&#%uD;", n); | |
831 | |
832 continue; | |
833 } | |
834 | |
835 if (n == 0xfffffffe) { | |
836 /* incomplete UTF-8 symbol */ | |
837 | |
838 ngx_memcpy(ctx->saved, src, len); | |
839 ctx->saved_len = len; | |
840 | |
841 if (b->pos == dst) { | |
842 b->sync = 1; | |
843 b->temporary = 0; | |
844 } | |
845 | |
846 break; | |
847 } | |
848 | |
849 if (n > 0x10ffff) { | |
850 *dst++ = '?'; | |
851 | |
852 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pool->log, 0, | |
853 "http charset invalid utf 2"); | |
854 | |
855 continue; | |
856 } | |
857 | |
858 /* n > 0xffff */ | |
859 | |
860 dst = ngx_sprintf(dst, "&#%uD;", n); | |
861 } | |
862 | |
863 b->last = dst; | |
864 | |
865 b->last_buf = buf->last_buf; | |
866 b->last_in_chain = buf->last_in_chain; | |
867 b->flush = buf->flush; | |
868 | |
869 b->shadow = buf; | |
870 | |
871 return out; | |
872 } | |
873 | |
874 | |
875 static ngx_chain_t * | |
876 ngx_http_charset_recode_to_utf8(ngx_pool_t *pool, ngx_buf_t *buf, | |
877 ngx_http_charset_ctx_t *ctx) | |
878 { | |
879 size_t len, size; | |
880 u_char *p, *src, *dst, *table; | |
881 ngx_buf_t *b; | |
882 ngx_chain_t *out, *cl, **ll; | |
883 | |
884 table = ctx->table; | |
885 | |
886 for (src = buf->pos; src < buf->last; src++) { | |
887 if (table[*src * NGX_UTF_LEN] == '\1') { | |
888 continue; | |
889 } | |
890 | |
891 goto recode; | |
892 } | |
893 | |
894 out = ngx_alloc_chain_link(pool); | |
895 if (out == NULL) { | |
896 return NULL; | |
897 } | |
898 | |
899 out->buf = buf; | |
900 out->next = NULL; | |
901 | |
902 return out; | |
903 | |
904 recode: | |
905 | |
906 /* | |
907 * we assume that there are about half of characters to be recoded, | |
908 * so we preallocate "size / 2 + size / 2 * ctx->length" | |
909 */ | |
910 | |
911 len = src - buf->pos; | |
912 | |
913 if (len > 512) { | |
914 out = ngx_http_charset_get_buf(pool, ctx); | |
915 if (out == NULL) { | |
916 return NULL; | |
917 } | |
918 | |
919 b = out->buf; | |
920 | |
921 b->temporary = buf->temporary; | |
922 b->memory = buf->memory; | |
923 b->mmap = buf->mmap; | |
924 b->flush = buf->flush; | |
925 | |
926 b->pos = buf->pos; | |
927 b->last = src; | |
928 | |
929 out->buf = b; | |
930 out->next = NULL; | |
931 | |
932 size = buf->last - src; | |
933 size = size / 2 + size / 2 * ctx->length; | |
934 | |
935 } else { | |
936 out = NULL; | |
937 | |
938 size = buf->last - src; | |
939 size = len + size / 2 + size / 2 * ctx->length; | |
940 | |
941 src = buf->pos; | |
942 } | |
943 | |
944 cl = ngx_http_charset_get_buffer(pool, ctx, size); | |
945 if (cl == NULL) { | |
946 return NULL; | |
947 } | |
948 | |
949 if (out) { | |
950 out->next = cl; | |
951 | |
952 } else { | |
953 out = cl; | |
954 } | |
955 | |
956 ll = &cl->next; | |
957 | |
958 b = cl->buf; | |
959 dst = b->pos; | |
960 | |
961 while (src < buf->last) { | |
962 | |
963 p = &table[*src++ * NGX_UTF_LEN]; | |
964 len = *p++; | |
965 | |
966 if ((size_t) (b->end - dst) < len) { | |
967 b->last = dst; | |
968 | |
969 size = buf->last - src; | |
970 size = len + size / 2 + size / 2 * ctx->length; | |
971 | |
972 cl = ngx_http_charset_get_buffer(pool, ctx, size); | |
973 if (cl == NULL) { | |
974 return NULL; | |
975 } | |
976 | |
977 *ll = cl; | |
978 ll = &cl->next; | |
979 | |
980 b = cl->buf; | |
981 dst = b->pos; | |
982 } | |
983 | |
984 while (len) { | |
985 *dst++ = *p++; | |
986 len--; | |
987 } | |
988 } | |
989 | |
990 b->last = dst; | |
991 | |
992 b->last_buf = buf->last_buf; | |
993 b->last_in_chain = buf->last_in_chain; | |
994 b->flush = buf->flush; | |
995 | |
996 b->shadow = buf; | |
997 | |
998 return out; | |
999 } | |
1000 | |
1001 | |
1002 static ngx_chain_t * | |
1003 ngx_http_charset_get_buf(ngx_pool_t *pool, ngx_http_charset_ctx_t *ctx) | |
1004 { | |
1005 ngx_chain_t *cl; | |
1006 | |
1007 cl = ctx->free_bufs; | |
1008 | |
1009 if (cl) { | |
1010 ctx->free_bufs = cl->next; | |
1011 | |
1012 cl->buf->shadow = NULL; | |
1013 cl->next = NULL; | |
1014 | |
1015 return cl; | |
1016 } | |
1017 | |
1018 cl = ngx_alloc_chain_link(pool); | |
1019 if (cl == NULL) { | |
1020 return NULL; | |
1021 } | |
1022 | |
1023 cl->buf = ngx_calloc_buf(pool); | |
1024 if (cl->buf == NULL) { | |
1025 return NULL; | |
1026 } | |
1027 | |
1028 cl->next = NULL; | |
1029 | |
1030 cl->buf->tag = (ngx_buf_tag_t) &ngx_http_charset_filter_module; | |
1031 | |
1032 return cl; | |
1033 } | |
1034 | |
1035 | |
1036 static ngx_chain_t * | |
1037 ngx_http_charset_get_buffer(ngx_pool_t *pool, ngx_http_charset_ctx_t *ctx, | |
1038 size_t size) | |
1039 { | |
1040 ngx_buf_t *b; | |
1041 ngx_chain_t *cl, **ll; | |
1042 | |
1043 for (ll = &ctx->free_buffers, cl = ctx->free_buffers; | |
1044 cl; | |
1045 ll = &cl->next, cl = cl->next) | |
1046 { | |
1047 b = cl->buf; | |
1048 | |
1049 if ((size_t) (b->end - b->start) >= size) { | |
1050 *ll = cl->next; | |
1051 cl->next = NULL; | |
1052 | |
1053 b->pos = b->start; | |
1054 b->temporary = 1; | |
1055 b->shadow = NULL; | |
1056 | |
1057 return cl; | |
1058 } | |
1059 } | |
1060 | |
1061 cl = ngx_alloc_chain_link(pool); | |
1062 if (cl == NULL) { | |
1063 return NULL; | |
1064 } | |
1065 | |
1066 cl->buf = ngx_create_temp_buf(pool, size); | |
1067 if (cl->buf == NULL) { | |
1068 return NULL; | |
1069 } | |
1070 | |
1071 cl->next = NULL; | |
1072 | |
1073 cl->buf->temporary = 1; | |
1074 cl->buf->tag = (ngx_buf_tag_t) &ngx_http_charset_filter_module; | |
1075 | |
1076 return cl; | |
1077 } | |
1078 | |
1079 | |
501 | 1080 static char * |
657 | 1081 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
|
1082 { |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1083 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
|
1084 |
657 | 1085 char *rv; |
1086 u_char *p, *dst2src, **pp; | |
1087 ngx_int_t src, dst; | |
1088 ngx_uint_t i, n; | |
1089 ngx_str_t *value; | |
1090 ngx_conf_t pvcf; | |
1091 ngx_http_charset_t *charset; | |
1092 ngx_http_charset_tables_t *table; | |
1093 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
|
1094 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1095 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
|
1096 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1097 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
|
1098 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
|
1099 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
|
1100 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1101 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1102 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
|
1103 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
|
1104 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
|
1105 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1106 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1107 if (src == dst) { |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1108 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
|
1109 "\"charset_map\" between the same charsets " |
461 | 1110 "\"%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
|
1111 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
|
1112 } |
338
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1113 |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1114 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
|
1115 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
|
1116 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
|
1117 || (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
|
1118 { |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1119 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
|
1120 "duplicate \"charset_map\" between " |
461 | 1121 "\"%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
|
1122 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
|
1123 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1124 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1125 |
501 | 1126 table = ngx_array_push(&mcf->tables); |
1127 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
|
1128 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
|
1129 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1130 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1131 table->src = src; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1132 table->dst = dst; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1133 |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
1134 if (ngx_strcasecmp(value[2].data, (u_char *) "utf-8") == 0) { |
657 | 1135 table->src2dst = ngx_pcalloc(cf->pool, 256 * NGX_UTF_LEN); |
1136 if (table->src2dst == NULL) { | |
1137 return NGX_CONF_ERROR; | |
1138 } | |
1139 | |
1140 table->dst2src = ngx_pcalloc(cf->pool, 256 * sizeof(void *)); | |
1141 if (table->dst2src == NULL) { | |
1142 return NGX_CONF_ERROR; | |
1143 } | |
1144 | |
1145 dst2src = ngx_pcalloc(cf->pool, 256); | |
1146 if (dst2src == NULL) { | |
1147 return NGX_CONF_ERROR; | |
1148 } | |
1149 | |
1150 pp = (u_char **) &table->dst2src[0]; | |
1151 pp[0] = dst2src; | |
1152 | |
1153 for (i = 0; i < 128; i++) { | |
1154 p = &table->src2dst[i * NGX_UTF_LEN]; | |
1155 p[0] = '\1'; | |
1156 p[1] = (u_char) i; | |
1157 dst2src[i] = (u_char) i; | |
1158 } | |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1159 |
657 | 1160 for (/* void */; i < 256; i++) { |
1161 p = &table->src2dst[i * NGX_UTF_LEN]; | |
1162 p[0] = '\1'; | |
1163 p[1] = '?'; | |
1164 } | |
1165 | |
1166 } else { | |
1167 table->src2dst = ngx_palloc(cf->pool, 256); | |
1168 if (table->src2dst == NULL) { | |
1169 return NGX_CONF_ERROR; | |
1170 } | |
1171 | |
1172 table->dst2src = ngx_palloc(cf->pool, 256); | |
1173 if (table->dst2src == NULL) { | |
1174 return NGX_CONF_ERROR; | |
1175 } | |
1176 | |
1177 for (i = 0; i < 128; i++) { | |
1178 table->src2dst[i] = (u_char) i; | |
1179 table->dst2src[i] = (u_char) i; | |
1180 } | |
1181 | |
1182 for (/* void */; i < 256; i++) { | |
1183 table->src2dst[i] = '?'; | |
1184 table->dst2src[i] = '?'; | |
1185 } | |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1186 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1187 |
657 | 1188 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
|
1189 |
657 | 1190 ctx.table = table; |
1191 ctx.charset = &charset[dst]; | |
1192 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
|
1193 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1194 pvcf = *cf; |
657 | 1195 cf->ctx = &ctx; |
1196 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
|
1197 cf->handler_conf = conf; |
501 | 1198 |
338
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1199 rv = ngx_conf_parse(cf, NULL); |
501 | 1200 |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1201 *cf = pvcf; |
338
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1202 |
657 | 1203 if (ctx.characters) { |
1204 n = ctx.charset->length; | |
1205 ctx.charset->length /= ctx.characters; | |
1206 | |
1207 if (((n * 10) / ctx.characters) % 10 > 4) { | |
1208 ctx.charset->length++; | |
1209 } | |
1210 } | |
1211 | |
338
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1212 return rv; |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1213 } |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1214 |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1215 |
501 | 1216 static char * |
657 | 1217 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
|
1218 { |
657 | 1219 u_char *p, *dst2src, **pp; |
1220 uint32_t n; | |
1221 ngx_int_t src, dst; | |
1222 ngx_str_t *value; | |
1223 ngx_uint_t i; | |
1224 ngx_http_charset_tables_t *table; | |
1225 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
|
1226 |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1227 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
|
1228 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
|
1229 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
|
1230 } |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1231 |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1232 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
|
1233 |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1234 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
|
1235 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
|
1236 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
461 | 1237 "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
|
1238 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
|
1239 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1240 |
657 | 1241 ctx = cf->ctx; |
1242 table = ctx->table; | |
1243 | |
1244 if (ctx->charset->utf8) { | |
1245 p = &table->src2dst[src * NGX_UTF_LEN]; | |
1246 | |
1247 *p++ = (u_char) (value[1].len / 2); | |
1248 | |
1249 for (i = 0; i < value[1].len; i += 2) { | |
1250 dst = ngx_hextoi(&value[1].data[i], 2); | |
1251 if (dst == NGX_ERROR || dst > 255) { | |
1252 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1253 "invalid value \"%V\"", &value[1]); | |
1254 return NGX_CONF_ERROR; | |
1255 } | |
1256 | |
1257 *p++ = (u_char) dst; | |
1258 } | |
1259 | |
1260 i /= 2; | |
1261 | |
1262 ctx->charset->length += i; | |
1263 ctx->characters++; | |
1264 | |
1265 p = &table->src2dst[src * NGX_UTF_LEN] + 1; | |
1266 | |
1267 n = ngx_utf_decode(&p, i); | |
1268 | |
1269 if (n > 0xffff) { | |
1270 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1271 "invalid value \"%V\"", &value[1]); | |
1272 return NGX_CONF_ERROR; | |
1273 } | |
1274 | |
1275 pp = (u_char **) &table->dst2src[0]; | |
1276 | |
1277 dst2src = pp[n >> 8]; | |
1278 | |
1279 if (dst2src == NULL) { | |
1280 dst2src = ngx_pcalloc(cf->pool, 256); | |
1281 if (dst2src == NULL) { | |
1282 return NGX_CONF_ERROR; | |
1283 } | |
1284 | |
1285 pp[n >> 8] = dst2src; | |
1286 } | |
1287 | |
1288 dst2src[n & 0xff] = (u_char) src; | |
1289 | |
1290 } else { | |
1291 dst = ngx_hextoi(value[1].data, value[1].len); | |
1292 if (dst == NGX_ERROR || dst > 255) { | |
1293 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1294 "invalid value \"%V\"", &value[1]); | |
1295 return NGX_CONF_ERROR; | |
1296 } | |
1297 | |
1298 table->src2dst[src] = (u_char) dst; | |
1299 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
|
1300 } |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1301 |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1302 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
|
1303 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1304 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1305 |
501 | 1306 static char * |
1307 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
|
1308 { |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1309 char *p = conf; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1310 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1311 ngx_int_t *cp; |
752
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1312 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
|
1313 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
|
1314 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1315 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
|
1316 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1317 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
|
1318 return "is duplicate"; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1319 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1320 |
529 | 1321 value = cf->args->elts; |
1322 | |
1323 if (cmd->offset == offsetof(ngx_http_charset_loc_conf_t, charset) | |
1324 && ngx_strcmp(value[1].data, "off") == 0) | |
1325 { | |
1326 *cp = NGX_HTTP_NO_CHARSET; | |
1327 return NGX_CONF_OK; | |
1328 } | |
1329 | |
752
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1330 |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1331 if (value[1].data[0] == '$') { |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1332 var.len = value[1].len - 1; |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1333 var.data = value[1].data + 1; |
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 *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
|
1336 |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1337 if (*cp == NGX_ERROR) { |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1338 return NGX_CONF_ERROR; |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1339 } |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1340 |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1341 *cp += NGX_HTTP_CHARSET_VAR; |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1342 |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1343 return NGX_CONF_OK; |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1344 } |
907361d07f25
ngx_http_charset_module supports the variables
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
1345 |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
389
diff
changeset
|
1346 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
|
1347 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
|
1348 |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1349 *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
|
1350 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
|
1351 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
|
1352 } |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1353 |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1354 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
|
1355 } |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1356 |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1357 |
501 | 1358 static ngx_int_t |
1359 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
|
1360 { |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1361 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
|
1362 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
|
1363 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1364 c = charsets->elts; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1365 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
|
1366 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
|
1367 continue; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1368 } |
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 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
|
1371 break; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1372 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1373 } |
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 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
|
1376 return i; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1377 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1378 |
501 | 1379 c = ngx_array_push(charsets); |
1380 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
|
1381 return NGX_ERROR; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1382 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1383 |
455 | 1384 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
|
1385 c->name = *name; |
657 | 1386 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
|
1387 |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
797
diff
changeset
|
1388 if (ngx_strcasecmp(name->data, (u_char *) "utf-8") == 0) { |
523 | 1389 c->utf8 = 1; |
667 | 1390 |
1391 } else { | |
1392 c->utf8 = 0; | |
523 | 1393 } |
1394 | |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1395 return i; |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1396 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1397 |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1398 |
501 | 1399 static void * |
1400 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
|
1401 { |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1402 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
|
1403 |
501 | 1404 mcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_main_conf_t)); |
1405 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
|
1406 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
|
1407 } |
338
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1408 |
501 | 1409 if (ngx_array_init(&mcf->charsets, cf->pool, 2, sizeof(ngx_http_charset_t)) |
529 | 1410 == NGX_ERROR) |
501 | 1411 { |
1412 return NGX_CONF_ERROR; | |
1413 } | |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1414 |
529 | 1415 if (ngx_array_init(&mcf->tables, cf->pool, 1, |
501 | 1416 sizeof(ngx_http_charset_tables_t)) == NGX_ERROR) |
1417 { | |
1418 return NGX_CONF_ERROR; | |
1419 } | |
338
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1420 |
529 | 1421 if (ngx_array_init(&mcf->recodes, cf->pool, 2, |
1422 sizeof(ngx_http_charset_recode_t)) == NGX_ERROR) | |
1423 { | |
1424 return NGX_CONF_ERROR; | |
1425 } | |
1426 | |
338
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1427 return mcf; |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1428 } |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1429 |
0376cffa29e6
nginx-0.0.3-2004-05-20-21:33:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
307
diff
changeset
|
1430 |
501 | 1431 static void * |
1432 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
|
1433 { |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1434 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
|
1435 |
501 | 1436 lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_loc_conf_t)); |
1437 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
|
1438 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
|
1439 } |
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1440 |
529 | 1441 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
|
1442 lcf->source_charset = NGX_CONF_UNSET; |
635 | 1443 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
|
1444 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1445 return lcf; |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1446 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1447 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1448 |
501 | 1449 static char * |
1450 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
|
1451 { |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1452 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
|
1453 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
|
1454 |
529 | 1455 ngx_uint_t i; |
1456 ngx_http_charset_recode_t *recode; | |
1457 ngx_http_charset_main_conf_t *mcf; | |
477 | 1458 |
635 | 1459 ngx_conf_merge_value(conf->override_charset, prev->override_charset, 0); |
529 | 1460 ngx_conf_merge_value(conf->charset, prev->charset, NGX_HTTP_NO_CHARSET); |
477 | 1461 |
339
8c5b69141dfd
nginx-0.0.3-2004-05-21-20:12:13 import
Igor Sysoev <igor@sysoev.ru>
parents:
338
diff
changeset
|
1462 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
|
1463 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
|
1464 } |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1465 |
529 | 1466 if (conf->charset == NGX_HTTP_NO_CHARSET |
1467 || conf->source_charset == NGX_CONF_UNSET | |
1468 || conf->charset == conf->source_charset) | |
477 | 1469 { |
529 | 1470 return NGX_CONF_OK; |
477 | 1471 } |
1472 | |
1590 | 1473 if (conf->source_charset >= NGX_HTTP_CHARSET_VAR |
1474 || conf->charset >= NGX_HTTP_CHARSET_VAR) | |
1475 { | |
1476 return NGX_CONF_OK; | |
1477 } | |
1478 | |
529 | 1479 mcf = ngx_http_conf_get_module_main_conf(cf, |
1480 ngx_http_charset_filter_module); | |
1481 recode = mcf->recodes.elts; | |
1482 for (i = 0; i < mcf->recodes.nelts; i++) { | |
1483 if (conf->source_charset == recode[i].src | |
1484 && conf->charset == recode[i].dst) | |
1485 { | |
1486 return NGX_CONF_OK; | |
1487 } | |
477 | 1488 } |
1489 | |
529 | 1490 recode = ngx_array_push(&mcf->recodes); |
1491 if (recode == NULL) { | |
477 | 1492 return NGX_CONF_ERROR; |
1493 } | |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
339
diff
changeset
|
1494 |
529 | 1495 recode->src = conf->source_charset; |
1496 recode->dst = conf->charset; | |
1497 | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1498 return NGX_CONF_OK; |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1499 } |
529 | 1500 |
1501 | |
1502 static ngx_int_t | |
1503 ngx_http_charset_postconfiguration(ngx_conf_t *cf) | |
1504 { | |
635 | 1505 u_char **src, **dst; |
529 | 1506 ngx_int_t c; |
1507 ngx_uint_t i, t; | |
1508 ngx_http_charset_t *charset; | |
1509 ngx_http_charset_recode_t *recode; | |
1510 ngx_http_charset_tables_t *tables; | |
1511 ngx_http_charset_main_conf_t *mcf; | |
1512 | |
1513 mcf = ngx_http_conf_get_module_main_conf(cf, | |
1514 ngx_http_charset_filter_module); | |
1515 | |
1516 recode = mcf->recodes.elts; | |
1517 tables = mcf->tables.elts; | |
1518 charset = mcf->charsets.elts; | |
1519 | |
1520 for (i = 0; i < mcf->recodes.nelts; i++) { | |
1521 | |
1522 c = recode[i].src; | |
1523 | |
1524 for (t = 0; t < mcf->tables.nelts; t++) { | |
1525 | |
1526 if (c == tables[t].src && recode[i].dst == tables[t].dst) { | |
1527 goto next; | |
1528 } | |
1529 | |
1530 if (c == tables[t].dst && recode[i].dst == tables[t].src) { | |
1531 goto next; | |
1532 } | |
1533 } | |
1534 | |
1535 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1588 | 1536 "no \"charset_map\" between the charsets \"%V\" and \"%V\"", |
1537 &charset[c].name, &charset[recode[i].dst].name); | |
529 | 1538 return NGX_ERROR; |
1539 | |
1540 next: | |
1541 continue; | |
1542 } | |
1543 | |
635 | 1544 |
1545 for (t = 0; t < mcf->tables.nelts; t++) { | |
1546 | |
1547 src = charset[tables[t].src].tables; | |
1548 | |
1549 if (src == NULL) { | |
1550 src = ngx_pcalloc(cf->pool, sizeof(u_char *) * mcf->charsets.nelts); | |
1551 if (src == NULL) { | |
1552 return NGX_ERROR; | |
1553 } | |
1554 | |
1555 charset[tables[t].src].tables = src; | |
1556 } | |
1557 | |
1558 dst = charset[tables[t].dst].tables; | |
1559 | |
1560 if (dst == NULL) { | |
1561 dst = ngx_pcalloc(cf->pool, sizeof(u_char *) * mcf->charsets.nelts); | |
1562 if (dst == NULL) { | |
1563 return NGX_ERROR; | |
1564 } | |
1565 | |
1566 charset[tables[t].dst].tables = dst; | |
1567 } | |
1568 | |
1569 src[tables[t].dst] = tables[t].src2dst; | |
1570 dst[tables[t].src] = tables[t].dst2src; | |
1571 } | |
1572 | |
681 | 1573 ngx_http_next_header_filter = ngx_http_top_header_filter; |
1574 ngx_http_top_header_filter = ngx_http_charset_header_filter; | |
1575 | |
1576 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1577 ngx_http_top_body_filter = ngx_http_charset_body_filter; | |
1578 | |
529 | 1579 return NGX_OK; |
1580 } |