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