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