Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_xslt_filter_module.c @ 2812:dcb1b4d6ff54
bump version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 06 May 2009 14:11:03 +0000 |
parents | 3daf68f2efe3 |
children | 26e06e009ced |
rev | line source |
---|---|
2139 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 #include <libxml/parser.h> | |
12 #include <libxml/tree.h> | |
13 #include <libxslt/xslt.h> | |
14 #include <libxslt/xsltInternals.h> | |
15 #include <libxslt/transform.h> | |
16 #include <libxslt/xsltutils.h> | |
17 | |
2299 | 18 #if (NGX_HAVE_EXSLT) |
19 #include <libexslt/exslt.h> | |
20 #endif | |
21 | |
2139 | 22 |
23 #ifndef NGX_HTTP_XSLT_REUSE_DTD | |
24 #define NGX_HTTP_XSLT_REUSE_DTD 1 | |
25 #endif | |
26 | |
27 | |
28 typedef struct { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
29 u_char *name; |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
30 void *data; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
31 } ngx_http_xslt_file_t; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
32 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
33 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
34 typedef struct { |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
35 ngx_array_t dtd_files; /* ngx_http_xslt_file_t */ |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
36 ngx_array_t sheet_files; /* ngx_http_xslt_file_t */ |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
37 } ngx_http_xslt_filter_main_conf_t; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
38 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
39 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
40 typedef struct { |
2139 | 41 xsltStylesheetPtr stylesheet; |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
42 ngx_array_t params; /* ngx_http_complex_value_t */ |
2139 | 43 } ngx_http_xslt_sheet_t; |
44 | |
45 | |
46 typedef struct { | |
47 xmlDtdPtr dtd; | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
48 ngx_array_t sheets; /* ngx_http_xslt_sheet_t */ |
2148 | 49 ngx_hash_t types; |
50 ngx_array_t *types_keys; | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
51 } ngx_http_xslt_filter_loc_conf_t; |
2139 | 52 |
53 | |
54 typedef struct { | |
55 xmlDocPtr doc; | |
56 xmlParserCtxtPtr ctxt; | |
57 xmlSAXHandler *sax; | |
58 ngx_http_request_t *request; | |
59 ngx_array_t params; | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
60 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
61 ngx_uint_t done; /* unsigned done:1; */ |
2139 | 62 } ngx_http_xslt_filter_ctx_t; |
63 | |
64 | |
65 static ngx_int_t ngx_http_xslt_send(ngx_http_request_t *r, | |
66 ngx_http_xslt_filter_ctx_t *ctx, ngx_buf_t *b); | |
67 static ngx_int_t ngx_http_xslt_add_chunk(ngx_http_request_t *r, | |
68 ngx_http_xslt_filter_ctx_t *ctx, ngx_buf_t *b); | |
69 | |
70 | |
71 static void ngx_http_xslt_sax_start_document(void *data); | |
72 static void ngx_http_xslt_sax_end_document(void *data); | |
73 static void ngx_http_xslt_sax_internal_subset(void *data, const xmlChar *name, | |
74 const xmlChar *externalId, const xmlChar *systemId); | |
75 static void ngx_http_xslt_sax_external_subset(void *data, const xmlChar *name, | |
76 const xmlChar *externalId, const xmlChar *systemId); | |
77 static void ngx_http_xslt_sax_entity_decl(void *data, const xmlChar *name, | |
78 int type, const xmlChar *publicId, const xmlChar *systemId, | |
79 xmlChar *content); | |
80 static void ngx_http_xslt_sax_attribute_decl(void *data, const xmlChar *elem, | |
81 const xmlChar *fullname, int type, int def, const xmlChar *defaultValue, | |
82 xmlEnumerationPtr tree); | |
83 static void ngx_http_xslt_sax_element_decl(void *data, const xmlChar * name, | |
84 int type, xmlElementContentPtr content); | |
85 static void ngx_http_xslt_sax_notation_decl(void *data, const xmlChar *name, | |
86 const xmlChar *publicId, const xmlChar *systemId); | |
87 static void ngx_http_xslt_sax_unparsed_entity_decl(void *data, | |
88 const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId, | |
89 const xmlChar *notationName); | |
90 static void ngx_http_xslt_sax_start_element(void *data, | |
91 const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, | |
92 int nb_namespaces, const xmlChar **namespaces, int nb_attributes, | |
93 int nb_defaulted, const xmlChar **attributes); | |
94 static void ngx_http_xslt_sax_end_element(void *data, | |
95 const xmlChar * localname ATTRIBUTE_UNUSED, | |
96 const xmlChar * prefix ATTRIBUTE_UNUSED, | |
97 const xmlChar * URI ATTRIBUTE_UNUSED); | |
98 static void ngx_http_xslt_sax_characters(void *data, const xmlChar *p, int len); | |
99 static void ngx_http_xslt_sax_cdata_block(void *data, const xmlChar *p, | |
100 int len); | |
101 static xmlEntityPtr ngx_http_xslt_sax_get_entity(void *data, | |
102 const xmlChar *name); | |
103 static xmlEntityPtr ngx_http_xslt_sax_get_parameter_entity(void *data, | |
104 const xmlChar *name); | |
105 static xmlParserInputPtr ngx_http_xslt_sax_resolve_entity(void *data, | |
106 const xmlChar *publicId, const xmlChar *systemId); | |
107 static void ngx_http_xslt_sax_reference(void *data, const xmlChar *name); | |
108 static void ngx_http_xslt_sax_comment(void *data, const xmlChar *value); | |
109 static void ngx_http_xslt_sax_processing_instruction(void *data, | |
110 const xmlChar *target, const xmlChar *pidata); | |
111 static int ngx_http_xslt_sax_is_standalone(void *data); | |
112 static int ngx_http_xslt_sax_has_internal_subset(void *data); | |
113 static int ngx_http_xslt_sax_has_external_subset(void *data); | |
114 static void ngx_cdecl ngx_http_xslt_sax_error(void *data, const char *msg, ...); | |
115 | |
116 | |
117 static ngx_buf_t *ngx_http_xslt_apply_stylesheet(ngx_http_request_t *r, | |
118 ngx_http_xslt_filter_ctx_t *ctx); | |
119 static ngx_int_t ngx_http_xslt_params(ngx_http_request_t *r, | |
120 ngx_http_xslt_filter_ctx_t *ctx, ngx_array_t *params); | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
121 static u_char *ngx_http_xslt_content_type(xsltStylesheetPtr s); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
122 static u_char *ngx_http_xslt_encoding(xsltStylesheetPtr s); |
2139 | 123 static void ngx_http_xslt_cleanup(void *data); |
124 | |
125 static char *ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd, | |
126 void *conf); | |
127 static char *ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd, | |
128 void *conf); | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
129 static void ngx_http_xslt_cleanup_dtd(void *data); |
2139 | 130 static void ngx_http_xslt_cleanup_stylesheet(void *data); |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
131 static void *ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf); |
2139 | 132 static void *ngx_http_xslt_filter_create_conf(ngx_conf_t *cf); |
133 static char *ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent, | |
134 void *child); | |
135 static ngx_int_t ngx_http_xslt_filter_init(ngx_conf_t *cf); | |
2196
fab3fa7339ff
call ngx_http_xslt_filter_exit()
Igor Sysoev <igor@sysoev.ru>
parents:
2159
diff
changeset
|
136 static void ngx_http_xslt_filter_exit(ngx_cycle_t *cycle); |
2139 | 137 |
138 | |
139 ngx_str_t ngx_http_xslt_default_types[] = { | |
140 ngx_string("text/xml"), | |
141 ngx_null_string | |
142 }; | |
143 | |
144 | |
145 static ngx_command_t ngx_http_xslt_filter_commands[] = { | |
146 | |
147 { ngx_string("xml_entities"), | |
2150
50cede290146
fix xslt module context levels
Igor Sysoev <igor@sysoev.ru>
parents:
2148
diff
changeset
|
148 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
2139 | 149 ngx_http_xslt_entities, |
150 NGX_HTTP_LOC_CONF_OFFSET, | |
151 0, | |
152 NULL }, | |
153 | |
154 { ngx_string("xslt_stylesheet"), | |
2151
c8331f70d799
xslt_stylesheet should be valid for location only
Igor Sysoev <igor@sysoev.ru>
parents:
2150
diff
changeset
|
155 NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
2139 | 156 ngx_http_xslt_stylesheet, |
157 NGX_HTTP_LOC_CONF_OFFSET, | |
158 0, | |
159 NULL }, | |
160 | |
161 { ngx_string("xslt_types"), | |
2150
50cede290146
fix xslt module context levels
Igor Sysoev <igor@sysoev.ru>
parents:
2148
diff
changeset
|
162 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
2139 | 163 ngx_http_types_slot, |
164 NGX_HTTP_LOC_CONF_OFFSET, | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
165 offsetof(ngx_http_xslt_filter_loc_conf_t, types_keys), |
2139 | 166 &ngx_http_xslt_default_types[0] }, |
167 | |
168 ngx_null_command | |
169 }; | |
170 | |
171 | |
172 static ngx_http_module_t ngx_http_xslt_filter_module_ctx = { | |
173 NULL, /* preconfiguration */ | |
174 ngx_http_xslt_filter_init, /* postconfiguration */ | |
175 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
176 ngx_http_xslt_filter_create_main_conf, /* create main configuration */ |
2139 | 177 NULL, /* init main configuration */ |
178 | |
179 NULL, /* create server configuration */ | |
180 NULL, /* merge server configuration */ | |
181 | |
182 ngx_http_xslt_filter_create_conf, /* create location configuration */ | |
183 ngx_http_xslt_filter_merge_conf /* merge location configuration */ | |
184 }; | |
185 | |
186 | |
187 ngx_module_t ngx_http_xslt_filter_module = { | |
188 NGX_MODULE_V1, | |
189 &ngx_http_xslt_filter_module_ctx, /* module context */ | |
190 ngx_http_xslt_filter_commands, /* module directives */ | |
191 NGX_HTTP_MODULE, /* module type */ | |
192 NULL, /* init master */ | |
193 NULL, /* init module */ | |
194 NULL, /* init process */ | |
195 NULL, /* init thread */ | |
196 NULL, /* exit thread */ | |
2196
fab3fa7339ff
call ngx_http_xslt_filter_exit()
Igor Sysoev <igor@sysoev.ru>
parents:
2159
diff
changeset
|
197 ngx_http_xslt_filter_exit, /* exit process */ |
fab3fa7339ff
call ngx_http_xslt_filter_exit()
Igor Sysoev <igor@sysoev.ru>
parents:
2159
diff
changeset
|
198 ngx_http_xslt_filter_exit, /* exit master */ |
2139 | 199 NGX_MODULE_V1_PADDING |
200 }; | |
201 | |
202 | |
203 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
204 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
205 | |
206 | |
207 static ngx_int_t | |
208 ngx_http_xslt_header_filter(ngx_http_request_t *r) | |
209 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
210 ngx_http_xslt_filter_ctx_t *ctx; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
211 ngx_http_xslt_filter_loc_conf_t *conf; |
2139 | 212 |
213 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
214 "xslt filter header"); | |
215 | |
216 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) { | |
217 return ngx_http_next_header_filter(r); | |
218 } | |
219 | |
220 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module); | |
221 | |
222 if (conf->sheets.nelts == 0 | |
2148 | 223 || ngx_http_test_content_type(r, &conf->types) == NULL) |
2139 | 224 { |
225 return ngx_http_next_header_filter(r); | |
226 } | |
227 | |
228 ctx = ngx_http_get_module_ctx(r, ngx_http_xslt_filter_module); | |
229 | |
230 if (ctx) { | |
231 return ngx_http_next_header_filter(r); | |
232 } | |
233 | |
234 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_xslt_filter_ctx_t)); | |
235 if (ctx == NULL) { | |
236 return NGX_ERROR; | |
237 } | |
238 | |
239 ngx_http_set_ctx(r, ctx, ngx_http_xslt_filter_module); | |
240 | |
241 r->main_filter_need_in_memory = 1; | |
242 | |
243 return NGX_OK; | |
244 } | |
245 | |
246 | |
247 static ngx_int_t | |
248 ngx_http_xslt_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
249 { | |
250 ngx_chain_t *cl; | |
251 ngx_http_xslt_filter_ctx_t *ctx; | |
252 | |
253 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
254 "xslt filter body"); | |
255 | |
256 if (in == NULL) { | |
257 return ngx_http_next_body_filter(r, in); | |
258 } | |
259 | |
260 ctx = ngx_http_get_module_ctx(r, ngx_http_xslt_filter_module); | |
261 | |
262 if (ctx == NULL || ctx->done) { | |
263 return ngx_http_next_body_filter(r, in); | |
264 } | |
265 | |
266 for (cl = in; cl; cl = cl->next) { | |
267 | |
268 if (ngx_http_xslt_add_chunk(r, ctx, cl->buf) != NGX_OK) { | |
269 | |
270 if (ctx->ctxt->myDoc){ | |
271 | |
272 #if (NGX_HTTP_XSLT_REUSE_DTD) | |
273 ctx->ctxt->myDoc->extSubset = NULL; | |
274 #endif | |
275 xmlFreeDoc(ctx->ctxt->myDoc); | |
276 } | |
277 | |
278 xmlFreeParserCtxt(ctx->ctxt); | |
279 | |
280 return ngx_http_xslt_send(r, ctx, NULL); | |
281 } | |
282 | |
283 if (cl->buf->last_buf) { | |
284 | |
285 ctx->doc = ctx->ctxt->myDoc; | |
286 | |
287 #if (NGX_HTTP_XSLT_REUSE_DTD) | |
288 ctx->doc->extSubset = NULL; | |
289 #endif | |
290 | |
291 xmlFreeParserCtxt(ctx->ctxt); | |
292 | |
293 if (ctx->ctxt->wellFormed) { | |
294 return ngx_http_xslt_send(r, ctx, | |
295 ngx_http_xslt_apply_stylesheet(r, ctx)); | |
296 } | |
297 | |
298 xmlFreeDoc(ctx->doc); | |
299 | |
300 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
301 "not well formed XML document"); | |
302 | |
303 return ngx_http_xslt_send(r, ctx, NULL); | |
304 } | |
305 } | |
306 | |
307 return NGX_OK; | |
308 } | |
309 | |
310 | |
311 static ngx_int_t | |
312 ngx_http_xslt_send(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx, | |
313 ngx_buf_t *b) | |
314 { | |
315 ngx_int_t rc; | |
316 ngx_chain_t out; | |
317 ngx_pool_cleanup_t *cln; | |
318 | |
319 ctx->done = 1; | |
320 | |
321 if (b == NULL) { | |
2787
3daf68f2efe3
ngx_http_filter_finalize_request() and ngx_http_clean_header()
Igor Sysoev <igor@sysoev.ru>
parents:
2588
diff
changeset
|
322 return ngx_http_filter_finalize_request(r, |
3daf68f2efe3
ngx_http_filter_finalize_request() and ngx_http_clean_header()
Igor Sysoev <igor@sysoev.ru>
parents:
2588
diff
changeset
|
323 NGX_HTTP_INTERNAL_SERVER_ERROR); |
2139 | 324 } |
325 | |
326 cln = ngx_pool_cleanup_add(r->pool, 0); | |
327 | |
328 if (cln == NULL) { | |
329 ngx_free(b->pos); | |
2787
3daf68f2efe3
ngx_http_filter_finalize_request() and ngx_http_clean_header()
Igor Sysoev <igor@sysoev.ru>
parents:
2588
diff
changeset
|
330 return ngx_http_filter_finalize_request(r, |
2139 | 331 NGX_HTTP_INTERNAL_SERVER_ERROR); |
332 } | |
333 | |
2159
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
334 if (r == r->main) { |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
335 r->headers_out.content_length_n = b->last - b->pos; |
2139 | 336 |
2159
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
337 if (r->headers_out.content_length) { |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
338 r->headers_out.content_length->hash = 0; |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
339 r->headers_out.content_length = NULL; |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
340 } |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
341 |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
342 ngx_http_clear_last_modified(r); |
2139 | 343 } |
344 | |
345 rc = ngx_http_next_header_filter(r); | |
346 | |
347 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
348 ngx_free(b->pos); | |
349 return rc; | |
350 } | |
351 | |
352 cln->handler = ngx_http_xslt_cleanup; | |
353 cln->data = b->pos; | |
354 | |
355 out.buf = b; | |
356 out.next = NULL; | |
357 | |
358 return ngx_http_next_body_filter(r, &out); | |
359 } | |
360 | |
361 | |
362 static ngx_int_t | |
363 ngx_http_xslt_add_chunk(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx, | |
364 ngx_buf_t *b) | |
365 { | |
366 int err; | |
367 xmlSAXHandler *sax; | |
368 xmlParserCtxtPtr ctxt; | |
369 | |
370 if (ctx->ctxt == NULL) { | |
371 | |
372 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL); | |
373 if (ctxt == NULL) { | |
374 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
375 "xmlCreatePushParserCtxt() failed"); | |
376 return NGX_ERROR; | |
377 } | |
378 | |
379 ctx->sax = ngx_palloc(r->pool, sizeof(xmlSAXHandler)); | |
380 if (ctx->sax == NULL) { | |
381 return NGX_ERROR; | |
382 } | |
383 | |
384 sax = ctxt->sax; | |
385 | |
386 ngx_memcpy(ctx->sax, sax, sizeof(xmlSAXHandler)); | |
387 | |
388 sax->startDocument = ngx_http_xslt_sax_start_document; | |
389 sax->endDocument = ngx_http_xslt_sax_end_document; | |
390 | |
391 sax->internalSubset = ngx_http_xslt_sax_internal_subset; | |
392 sax->externalSubset = ngx_http_xslt_sax_external_subset; | |
393 sax->entityDecl = ngx_http_xslt_sax_entity_decl; | |
394 sax->attributeDecl = ngx_http_xslt_sax_attribute_decl; | |
395 sax->elementDecl = ngx_http_xslt_sax_element_decl; | |
396 sax->notationDecl = ngx_http_xslt_sax_notation_decl; | |
397 sax->unparsedEntityDecl = ngx_http_xslt_sax_unparsed_entity_decl; | |
398 sax->setDocumentLocator = NULL; | |
399 | |
400 sax->startElementNs = ngx_http_xslt_sax_start_element; | |
401 sax->endElementNs = ngx_http_xslt_sax_end_element; | |
402 | |
403 sax->characters = ngx_http_xslt_sax_characters; | |
404 sax->ignorableWhitespace = ngx_http_xslt_sax_characters; | |
405 sax->cdataBlock = ngx_http_xslt_sax_cdata_block; | |
406 sax->getEntity = ngx_http_xslt_sax_get_entity; | |
407 sax->resolveEntity = ngx_http_xslt_sax_resolve_entity; | |
408 sax->getParameterEntity = ngx_http_xslt_sax_get_parameter_entity; | |
409 sax->reference = ngx_http_xslt_sax_reference; | |
410 sax->comment = ngx_http_xslt_sax_comment; | |
411 sax->processingInstruction = ngx_http_xslt_sax_processing_instruction; | |
412 | |
413 sax->isStandalone = ngx_http_xslt_sax_is_standalone; | |
414 sax->hasInternalSubset = ngx_http_xslt_sax_has_internal_subset; | |
415 sax->hasExternalSubset = ngx_http_xslt_sax_has_external_subset; | |
416 | |
417 sax->warning = NULL; | |
418 sax->error = ngx_http_xslt_sax_error; | |
419 sax->fatalError = ngx_http_xslt_sax_error; | |
420 | |
421 ctxt->userData = ctx; | |
422 | |
423 ctxt->replaceEntities = 1; | |
424 ctxt->loadsubset = 1; | |
425 | |
426 ctx->ctxt = ctxt; | |
427 ctx->request = r; | |
428 } | |
429 | |
430 err = xmlParseChunk(ctx->ctxt, (char *) b->pos, | |
431 (int) (b->last - b->pos), b->last_buf); | |
432 | |
433 if (err == 0) { | |
434 b->pos = b->last; | |
435 return NGX_OK; | |
436 } | |
437 | |
438 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
439 "xmlParseChunk() failed, error:%d", err); | |
440 | |
441 return NGX_ERROR; | |
442 } | |
443 | |
444 | |
445 static void | |
446 ngx_http_xslt_sax_start_document(void *data) | |
447 { | |
448 ngx_http_xslt_filter_ctx_t *ctx = data; | |
449 | |
450 ctx->sax->startDocument(ctx->ctxt); | |
451 } | |
452 | |
453 | |
454 static void | |
455 ngx_http_xslt_sax_end_document(void *data) | |
456 { | |
457 ngx_http_xslt_filter_ctx_t *ctx = data; | |
458 | |
459 ctx->sax->endDocument(ctx->ctxt); | |
460 } | |
461 | |
462 | |
463 static void | |
464 ngx_http_xslt_sax_internal_subset(void *data, const xmlChar *name, | |
465 const xmlChar *externalId, const xmlChar *systemId) | |
466 { | |
467 ngx_http_xslt_filter_ctx_t *ctx = data; | |
468 | |
469 ctx->sax->internalSubset(ctx->ctxt, name, externalId, systemId); | |
470 } | |
471 | |
472 | |
473 static void | |
474 ngx_http_xslt_sax_external_subset(void *data, const xmlChar *name, | |
475 const xmlChar *externalId, const xmlChar *systemId) | |
476 { | |
477 ngx_http_xslt_filter_ctx_t *ctx = data; | |
478 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
479 xmlDocPtr doc; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
480 xmlDtdPtr dtd; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
481 ngx_http_request_t *r; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
482 ngx_http_xslt_filter_loc_conf_t *conf; |
2139 | 483 |
484 r = ctx->request; | |
485 | |
486 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module); | |
487 | |
488 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
489 "xslt filter extSubset: \"%s\" \"%s\" \"%s\"", | |
490 name ? name : (xmlChar *) "", | |
491 externalId ? externalId : (xmlChar *) "", | |
492 systemId ? systemId : (xmlChar *) ""); | |
493 | |
494 doc = ctx->ctxt->myDoc; | |
495 | |
496 #if (NGX_HTTP_XSLT_REUSE_DTD) | |
497 | |
498 dtd = conf->dtd; | |
499 | |
500 #else | |
501 | |
502 dtd = xmlCopyDtd(conf->dtd); | |
503 if (dtd == NULL) { | |
504 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
505 "xmlCopyDtd() failed"); | |
506 return; | |
507 } | |
508 | |
509 if (doc->children == NULL) { | |
510 xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd); | |
511 | |
512 } else { | |
513 xmlAddPrevSibling(doc->children, (xmlNodePtr) dtd); | |
514 } | |
515 | |
516 #endif | |
517 | |
518 doc->extSubset = dtd; | |
519 } | |
520 | |
521 | |
522 static void | |
523 ngx_http_xslt_sax_entity_decl(void *data, const xmlChar *name, int type, | |
524 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content) | |
525 { | |
526 ngx_http_xslt_filter_ctx_t *ctx = data; | |
527 | |
528 ctx->sax->entityDecl(ctx->ctxt, name, type, publicId, systemId, content); | |
529 } | |
530 | |
531 | |
532 static void | |
533 ngx_http_xslt_sax_attribute_decl(void *data, const xmlChar *elem, | |
534 const xmlChar *fullname, int type, int def, const xmlChar *defaultValue, | |
535 xmlEnumerationPtr tree) | |
536 { | |
537 ngx_http_xslt_filter_ctx_t *ctx = data; | |
538 | |
539 ctx->sax->attributeDecl(ctx->ctxt, elem, fullname, type, def, defaultValue, | |
540 tree); | |
541 } | |
542 | |
543 | |
544 static void | |
545 ngx_http_xslt_sax_element_decl(void *data, const xmlChar * name, int type, | |
546 xmlElementContentPtr content) | |
547 { | |
548 ngx_http_xslt_filter_ctx_t *ctx = data; | |
549 | |
550 ctx->sax->elementDecl(ctx->ctxt, name, type, content); | |
551 } | |
552 | |
553 | |
554 static void | |
555 ngx_http_xslt_sax_notation_decl(void *data, const xmlChar *name, | |
556 const xmlChar *publicId, const xmlChar *systemId) | |
557 { | |
558 ngx_http_xslt_filter_ctx_t *ctx = data; | |
559 | |
560 ctx->sax->notationDecl(ctx->ctxt, name, publicId, systemId); | |
561 } | |
562 | |
563 | |
564 static void | |
565 ngx_http_xslt_sax_unparsed_entity_decl(void *data, const xmlChar *name, | |
566 const xmlChar *publicId, const xmlChar *systemId, | |
567 const xmlChar *notationName) | |
568 { | |
569 ngx_http_xslt_filter_ctx_t *ctx = data; | |
570 | |
571 ctx->sax->unparsedEntityDecl(ctx->ctxt, name, publicId, systemId, | |
572 notationName); | |
573 } | |
574 | |
575 | |
576 static void | |
577 ngx_http_xslt_sax_start_element(void *data, const xmlChar *localname, | |
578 const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, | |
579 const xmlChar **namespaces, int nb_attributes, int nb_defaulted, | |
580 const xmlChar **attributes) | |
581 { | |
582 ngx_http_xslt_filter_ctx_t *ctx = data; | |
583 | |
584 ctx->sax->startElementNs(ctx->ctxt, localname, prefix, URI, nb_namespaces, | |
585 namespaces, nb_attributes, nb_defaulted, attributes); | |
586 } | |
587 | |
588 | |
589 static void | |
590 ngx_http_xslt_sax_end_element(void *data, | |
591 const xmlChar * localname ATTRIBUTE_UNUSED, | |
592 const xmlChar * prefix ATTRIBUTE_UNUSED, | |
593 const xmlChar * URI ATTRIBUTE_UNUSED) | |
594 { | |
595 ngx_http_xslt_filter_ctx_t *ctx = data; | |
596 | |
597 ctx->sax->endElementNs(ctx->ctxt, localname, prefix, URI); | |
598 } | |
599 | |
600 | |
601 static void | |
602 ngx_http_xslt_sax_characters(void *data, const xmlChar *p, int len) | |
603 { | |
604 ngx_http_xslt_filter_ctx_t *ctx = data; | |
605 | |
606 ctx->sax->characters(ctx->ctxt, p, len); | |
607 } | |
608 | |
609 | |
610 static void | |
611 ngx_http_xslt_sax_cdata_block(void *data, const xmlChar *p, int len) | |
612 { | |
613 ngx_http_xslt_filter_ctx_t *ctx = data; | |
614 | |
615 ctx->sax->cdataBlock(ctx->ctxt, p, len); | |
616 } | |
617 | |
618 | |
619 static xmlEntityPtr | |
620 ngx_http_xslt_sax_get_entity(void *data, const xmlChar *name) | |
621 { | |
622 ngx_http_xslt_filter_ctx_t *ctx = data; | |
623 | |
624 return ctx->sax->getEntity(ctx->ctxt, name); | |
625 } | |
626 | |
627 | |
628 static xmlEntityPtr | |
629 ngx_http_xslt_sax_get_parameter_entity(void *data, const xmlChar *name) | |
630 { | |
631 ngx_http_xslt_filter_ctx_t *ctx = data; | |
632 | |
633 return ctx->sax->getParameterEntity(ctx->ctxt, name); | |
634 } | |
635 | |
636 | |
637 static xmlParserInputPtr | |
638 ngx_http_xslt_sax_resolve_entity(void *data, const xmlChar *publicId, | |
639 const xmlChar *systemId) | |
640 { | |
641 ngx_http_xslt_filter_ctx_t *ctx = data; | |
642 | |
643 return ctx->sax->resolveEntity(ctx->ctxt, publicId, systemId); | |
644 } | |
645 | |
646 | |
647 static void | |
648 ngx_http_xslt_sax_reference(void *data, const xmlChar *name) | |
649 { | |
650 ngx_http_xslt_filter_ctx_t *ctx = data; | |
651 | |
652 ctx->sax->reference(ctx->ctxt, name); | |
653 } | |
654 | |
655 | |
656 static void | |
657 ngx_http_xslt_sax_comment(void *data, const xmlChar *value) | |
658 { | |
659 ngx_http_xslt_filter_ctx_t *ctx = data; | |
660 | |
661 ctx->sax->comment(ctx->ctxt, value); | |
662 } | |
663 | |
664 | |
665 static void | |
666 ngx_http_xslt_sax_processing_instruction(void *data, const xmlChar *target, | |
667 const xmlChar *pidata) | |
668 { | |
669 ngx_http_xslt_filter_ctx_t *ctx = data; | |
670 | |
671 ctx->sax->processingInstruction(ctx->ctxt, target, pidata); | |
672 } | |
673 | |
674 | |
675 static int | |
676 ngx_http_xslt_sax_is_standalone(void *data) | |
677 { | |
678 ngx_http_xslt_filter_ctx_t *ctx = data; | |
679 | |
680 return ctx->sax->isStandalone(ctx->ctxt); | |
681 } | |
682 | |
683 | |
684 static int | |
685 ngx_http_xslt_sax_has_internal_subset(void *data) | |
686 { | |
687 ngx_http_xslt_filter_ctx_t *ctx = data; | |
688 | |
689 return ctx->sax->hasInternalSubset(ctx->ctxt); | |
690 } | |
691 | |
692 | |
693 static int | |
694 ngx_http_xslt_sax_has_external_subset(void *data) | |
695 { | |
696 ngx_http_xslt_filter_ctx_t *ctx = data; | |
697 | |
698 return ctx->sax->hasExternalSubset(ctx->ctxt); | |
699 } | |
700 | |
701 | |
702 static void ngx_cdecl | |
703 ngx_http_xslt_sax_error(void *data, const char *msg, ...) | |
704 { | |
705 ngx_http_xslt_filter_ctx_t *ctx = data; | |
706 | |
707 size_t n; | |
708 va_list args; | |
709 u_char buf[NGX_MAX_ERROR_STR]; | |
710 | |
711 buf[0] = '\0'; | |
712 | |
713 va_start(args, msg); | |
714 n = (size_t) vsnprintf((char *) buf, NGX_MAX_ERROR_STR, msg, args); | |
715 va_end(args); | |
716 | |
717 while (--n && (buf[n] == CR || buf[n] == LF)) { /* void */ } | |
718 | |
719 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, | |
720 "libxml2 error: \"%*s\"", n, buf); | |
721 } | |
722 | |
723 | |
724 static ngx_buf_t * | |
725 ngx_http_xslt_apply_stylesheet(ngx_http_request_t *r, | |
726 ngx_http_xslt_filter_ctx_t *ctx) | |
727 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
728 int len, rc, doc_type; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
729 u_char *type, *encoding; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
730 ngx_buf_t *b; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
731 ngx_uint_t i; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
732 xmlChar *buf; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
733 xmlDocPtr doc, res; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
734 ngx_http_xslt_sheet_t *sheet; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
735 ngx_http_xslt_filter_loc_conf_t *conf; |
2139 | 736 |
737 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module); | |
738 sheet = conf->sheets.elts; | |
739 doc = ctx->doc; | |
740 | |
741 /* preallocate array for 4 params */ | |
742 | |
743 if (ngx_array_init(&ctx->params, r->pool, 4 * 2 + 1, sizeof(char *)) | |
744 != NGX_OK) | |
745 { | |
746 xmlFreeDoc(doc); | |
747 return NULL; | |
748 } | |
749 | |
750 for (i = 0; i < conf->sheets.nelts; i++) { | |
751 | |
752 if (ngx_http_xslt_params(r, ctx, &sheet[i].params) != NGX_OK) { | |
753 xmlFreeDoc(doc); | |
754 return NULL; | |
755 } | |
756 | |
757 res = xsltApplyStylesheet(sheet[i].stylesheet, doc, ctx->params.elts); | |
758 | |
759 xmlFreeDoc(doc); | |
760 | |
761 if (res == NULL) { | |
762 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
763 "xsltApplyStylesheet() failed"); | |
764 return NULL; | |
765 } | |
766 | |
767 doc = res; | |
768 | |
769 /* reset array elements */ | |
770 ctx->params.nelts = 0; | |
771 } | |
772 | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
773 /* there must be at least one stylesheet */ |
2139 | 774 |
2159
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
775 if (r == r->main) { |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
776 type = ngx_http_xslt_content_type(sheet[i - 1].stylesheet); |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
777 |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
778 } else { |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
779 type = NULL; |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
780 } |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
781 |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
782 encoding = ngx_http_xslt_encoding(sheet[i - 1].stylesheet); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
783 doc_type = doc->type; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
784 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
785 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
786 "xslt filter type: %d t:%s e:%s", |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
787 doc_type, type ? type : (u_char *) "(null)", |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
788 encoding ? encoding : (u_char *) "(null)"); |
2139 | 789 |
790 rc = xsltSaveResultToString(&buf, &len, doc, sheet[i - 1].stylesheet); | |
791 | |
792 xmlFreeDoc(doc); | |
793 | |
794 if (rc != 0) { | |
795 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
796 "xsltSaveResultToString() failed"); | |
797 return NULL; | |
798 } | |
799 | |
800 if (len == 0) { | |
801 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
802 "xsltSaveResultToString() returned zero-length result"); | |
803 return NULL; | |
804 } | |
805 | |
806 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
807 if (b == NULL) { | |
808 ngx_free(buf); | |
809 return NULL; | |
810 } | |
811 | |
812 b->pos = buf; | |
813 b->last = buf + len; | |
814 b->memory = 1; | |
815 b->last_buf = 1; | |
816 | |
2159
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
817 if (encoding) { |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
818 r->headers_out.charset.len = ngx_strlen(encoding); |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
819 r->headers_out.charset.data = encoding; |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
820 } |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
821 |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
822 if (r != r->main) { |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
823 return b; |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
824 } |
0ec936b0010a
clear Last-Modified; set content-type and length for main request only
Igor Sysoev <igor@sysoev.ru>
parents:
2158
diff
changeset
|
825 |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
826 if (type) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
827 len = ngx_strlen(type); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
828 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
829 r->headers_out.content_type_len = len; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
830 r->headers_out.content_type.len = len; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
831 r->headers_out.content_type.data = type; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
832 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
833 } else if (doc_type == XML_HTML_DOCUMENT_NODE) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
834 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
835 r->headers_out.content_type_len = sizeof("text/html") - 1; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
836 r->headers_out.content_type.len = sizeof("text/html") - 1; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
837 r->headers_out.content_type.data = (u_char *) "text/html"; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
838 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
839 |
2139 | 840 return b; |
841 } | |
842 | |
843 | |
844 static ngx_int_t | |
845 ngx_http_xslt_params(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx, | |
846 ngx_array_t *params) | |
847 { | |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
848 u_char *p, *last, *value, *dst, *src, **s; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
849 size_t len; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
850 ngx_uint_t i; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
851 ngx_str_t string; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
852 ngx_http_complex_value_t *param; |
2139 | 853 |
854 param = params->elts; | |
855 | |
856 for (i = 0; i < params->nelts; i++) { | |
857 | |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
858 if (ngx_http_complex_value(r, ¶m[i], &string) != NGX_OK) { |
2139 | 859 return NGX_ERROR; |
860 } | |
861 | |
862 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
863 "xslt filter param: \"%s\"", string.data); | |
864 | |
865 p = string.data; | |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
866 last = string.data + string.len - 1; |
2139 | 867 |
868 while (p && *p) { | |
869 | |
870 value = p; | |
871 p = (u_char *) ngx_strchr(p, '='); | |
872 if (p == NULL) { | |
873 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
874 "invalid libxslt parameter \"%s\"", value); | |
875 return NGX_ERROR; | |
876 } | |
877 *p++ = '\0'; | |
878 | |
879 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
880 "xslt filter param name: \"%s\"", value); | |
881 | |
882 s = ngx_array_push(&ctx->params); | |
883 if (s == NULL) { | |
884 return NGX_ERROR; | |
885 } | |
886 | |
887 *s = value; | |
888 | |
889 value = p; | |
890 p = (u_char *) ngx_strchr(p, ':'); | |
891 | |
892 if (p) { | |
893 len = p - value; | |
894 *p++ = '\0'; | |
895 | |
896 } else { | |
897 len = last - value; | |
898 } | |
899 | |
900 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
901 "xslt filter param value: \"%s\"", value); | |
902 | |
903 dst = value; | |
904 src = value; | |
905 | |
906 ngx_unescape_uri(&dst, &src, len, 0); | |
907 | |
908 *dst = '\0'; | |
909 | |
910 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
911 "xslt filter param unescaped: \"%s\"", value); | |
912 | |
913 s = ngx_array_push(&ctx->params); | |
914 if (s == NULL) { | |
915 return NGX_ERROR; | |
916 } | |
917 | |
918 *s = value; | |
919 } | |
920 } | |
921 | |
922 s = ngx_array_push(&ctx->params); | |
923 if (s == NULL) { | |
924 return NGX_ERROR; | |
925 } | |
926 | |
927 *s = NULL; | |
928 | |
929 return NGX_OK; | |
930 } | |
931 | |
932 | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
933 static u_char * |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
934 ngx_http_xslt_content_type(xsltStylesheetPtr s) |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
935 { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
936 u_char *type; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
937 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
938 if (s->mediaType) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
939 return s->mediaType; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
940 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
941 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
942 for (s = s->imports; s; s = s->next) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
943 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
944 type = ngx_http_xslt_content_type(s); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
945 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
946 if (type) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
947 return type; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
948 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
949 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
950 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
951 return NULL; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
952 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
953 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
954 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
955 static u_char * |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
956 ngx_http_xslt_encoding(xsltStylesheetPtr s) |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
957 { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
958 u_char *encoding; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
959 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
960 if (s->encoding) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
961 return s->encoding; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
962 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
963 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
964 for (s = s->imports; s; s = s->next) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
965 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
966 encoding = ngx_http_xslt_encoding(s); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
967 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
968 if (encoding) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
969 return encoding; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
970 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
971 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
972 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
973 return NULL; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
974 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
975 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
976 |
2139 | 977 static void |
978 ngx_http_xslt_cleanup(void *data) | |
979 { | |
980 ngx_free(data); | |
981 } | |
982 | |
983 | |
984 static char * | |
985 ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
986 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
987 ngx_http_xslt_filter_loc_conf_t *xlcf = conf; |
2139 | 988 |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
989 ngx_str_t *value; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
990 ngx_uint_t i; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
991 ngx_pool_cleanup_t *cln; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
992 ngx_http_xslt_file_t *file; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
993 ngx_http_xslt_filter_main_conf_t *xmcf; |
2139 | 994 |
995 if (xlcf->dtd) { | |
996 return "is duplicate"; | |
997 } | |
998 | |
999 value = cf->args->elts; | |
1000 | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1001 xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xslt_filter_module); |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1002 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1003 file = xmcf->dtd_files.elts; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1004 for (i = 0; i < xmcf->dtd_files.nelts; i++) { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1005 if (ngx_strcmp(file[i].name, &value[1].data) == 0) { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1006 xlcf->dtd = file[i].data; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1007 return NGX_CONF_OK; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1008 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1009 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1010 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1011 cln = ngx_pool_cleanup_add(cf->pool, 0); |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1012 if (cln == NULL) { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1013 return NGX_CONF_ERROR; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1014 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1015 |
2139 | 1016 xlcf->dtd = xmlParseDTD(NULL, (xmlChar *) value[1].data); |
1017 | |
1018 if (xlcf->dtd == NULL) { | |
1019 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "xmlParseDTD() failed"); | |
1020 return NGX_CONF_ERROR; | |
1021 } | |
1022 | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1023 cln->handler = ngx_http_xslt_cleanup_dtd; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1024 cln->data = xlcf->dtd; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1025 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1026 file = ngx_array_push(&xmcf->dtd_files); |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1027 if (file == NULL) { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1028 return NGX_CONF_ERROR; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1029 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1030 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1031 file->name = value[1].data; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1032 file->data = xlcf->dtd; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1033 |
2139 | 1034 return NGX_CONF_OK; |
1035 } | |
1036 | |
1037 | |
1038 | |
1039 static char * | |
1040 ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1041 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1042 ngx_http_xslt_filter_loc_conf_t *xlcf = conf; |
2139 | 1043 |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1044 ngx_str_t *value; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1045 ngx_uint_t i, n; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1046 ngx_pool_cleanup_t *cln; |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1047 ngx_http_xslt_file_t *file; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1048 ngx_http_xslt_sheet_t *sheet; |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1049 ngx_http_complex_value_t *param; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1050 ngx_http_compile_complex_value_t ccv; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1051 ngx_http_xslt_filter_main_conf_t *xmcf; |
2139 | 1052 |
1053 value = cf->args->elts; | |
1054 | |
1055 if (xlcf->sheets.elts == NULL) { | |
1056 if (ngx_array_init(&xlcf->sheets, cf->pool, 1, | |
1057 sizeof(ngx_http_xslt_sheet_t)) | |
1058 != NGX_OK) | |
1059 { | |
1060 return NGX_CONF_ERROR; | |
1061 } | |
1062 } | |
1063 | |
1064 sheet = ngx_array_push(&xlcf->sheets); | |
1065 if (sheet == NULL) { | |
1066 return NGX_CONF_ERROR; | |
1067 } | |
1068 | |
1069 ngx_memzero(sheet, sizeof(ngx_http_xslt_sheet_t)); | |
1070 | |
1071 if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) { | |
1072 return NGX_CONF_ERROR; | |
1073 } | |
1074 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1075 xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xslt_filter_module); |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1076 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1077 file = xmcf->sheet_files.elts; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1078 for (i = 0; i < xmcf->sheet_files.nelts; i++) { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1079 if (ngx_strcmp(file[i].name, &value[1].data) == 0) { |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1080 sheet->stylesheet = file[i].data; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1081 goto found; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1082 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1083 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1084 |
2139 | 1085 cln = ngx_pool_cleanup_add(cf->pool, 0); |
1086 if (cln == NULL) { | |
1087 return NGX_CONF_ERROR; | |
1088 } | |
1089 | |
1090 sheet->stylesheet = xsltParseStylesheetFile(value[1].data); | |
1091 if (sheet->stylesheet == NULL) { | |
1092 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
1093 "xsltParseStylesheetFile(\"%s\") failed", | |
1094 value[1].data); | |
1095 return NGX_CONF_ERROR; | |
1096 } | |
1097 | |
1098 cln->handler = ngx_http_xslt_cleanup_stylesheet; | |
1099 cln->data = sheet->stylesheet; | |
1100 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1101 file = ngx_array_push(&xmcf->sheet_files); |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1102 if (file == NULL) { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1103 return NGX_CONF_ERROR; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1104 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1105 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1106 file->name = value[1].data; |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1107 file->data = sheet->stylesheet; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1108 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1109 found: |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1110 |
2139 | 1111 n = cf->args->nelts; |
1112 | |
1113 if (n == 2) { | |
1114 return NGX_CONF_OK; | |
1115 } | |
1116 | |
1117 if (ngx_array_init(&sheet->params, cf->pool, n - 2, | |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1118 sizeof(ngx_http_complex_value_t)) |
2139 | 1119 != NGX_OK) |
1120 { | |
1121 return NGX_CONF_ERROR; | |
1122 } | |
1123 | |
1124 for (i = 2; i < n; i++) { | |
1125 | |
1126 param = ngx_array_push(&sheet->params); | |
1127 if (param == NULL) { | |
1128 return NGX_CONF_ERROR; | |
1129 } | |
1130 | |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1131 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
2139 | 1132 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1133 ccv.cf = cf; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1134 ccv.value = &value[i]; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1135 ccv.complex_value = param; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1136 ccv.zero = 1; |
2139 | 1137 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1138 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
2139 | 1139 return NGX_CONF_ERROR; |
1140 } | |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2299
diff
changeset
|
1141 |
2139 | 1142 } |
1143 | |
1144 return NGX_CONF_OK; | |
1145 } | |
1146 | |
1147 | |
1148 static void | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1149 ngx_http_xslt_cleanup_dtd(void *data) |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1150 { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1151 xmlFreeDtd(data); |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1152 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1153 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1154 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1155 static void |
2139 | 1156 ngx_http_xslt_cleanup_stylesheet(void *data) |
1157 { | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1158 xsltFreeStylesheet(data); |
2139 | 1159 } |
1160 | |
1161 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1162 static void * |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1163 ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf) |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1164 { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1165 ngx_http_xslt_filter_main_conf_t *conf; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1166 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1167 conf = ngx_palloc(cf->pool, sizeof(ngx_http_xslt_filter_main_conf_t)); |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1168 if (conf == NULL) { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1169 return NGX_CONF_ERROR; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1170 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1171 |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1172 if (ngx_array_init(&conf->dtd_files, cf->pool, 1, |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1173 sizeof(ngx_http_xslt_file_t)) |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1174 != NGX_OK) |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1175 { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1176 return NULL; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1177 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1178 |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1179 if (ngx_array_init(&conf->sheet_files, cf->pool, 1, |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1180 sizeof(ngx_http_xslt_file_t)) |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1181 != NGX_OK) |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1182 { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1183 return NULL; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1184 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1185 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1186 return conf; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1187 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1188 |
2139 | 1189 |
1190 static void * | |
1191 ngx_http_xslt_filter_create_conf(ngx_conf_t *cf) | |
1192 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1193 ngx_http_xslt_filter_loc_conf_t *conf; |
2139 | 1194 |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1195 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_xslt_filter_loc_conf_t)); |
2139 | 1196 if (conf == NULL) { |
1197 return NGX_CONF_ERROR; | |
1198 } | |
1199 | |
1200 /* | |
1201 * set by ngx_pcalloc(): | |
1202 * | |
2148 | 1203 * conf->dtd = NULL; |
1204 * conf->sheets = { NULL }; | |
1205 * conf->types = { NULL }; | |
1206 * conf->types_keys = NULL; | |
2139 | 1207 */ |
1208 | |
1209 return conf; | |
1210 } | |
1211 | |
1212 | |
1213 static char * | |
1214 ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
1215 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1216 ngx_http_xslt_filter_loc_conf_t *prev = parent; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1217 ngx_http_xslt_filter_loc_conf_t *conf = child; |
2139 | 1218 |
1219 if (conf->dtd == NULL) { | |
1220 conf->dtd = prev->dtd; | |
1221 } | |
1222 | |
1223 if (conf->sheets.nelts == 0) { | |
1224 conf->sheets = prev->sheets; | |
1225 } | |
1226 | |
2148 | 1227 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types, |
1228 prev->types_keys, &prev->types, | |
1229 ngx_http_xslt_default_types) | |
2139 | 1230 != NGX_OK) |
1231 { | |
1232 return NGX_CONF_ERROR; | |
1233 } | |
1234 | |
1235 return NGX_CONF_OK; | |
1236 } | |
1237 | |
1238 | |
1239 static ngx_int_t | |
1240 ngx_http_xslt_filter_init(ngx_conf_t *cf) | |
1241 { | |
1242 xmlInitParser(); | |
1243 | |
2299 | 1244 #if (NGX_HAVE_EXSLT) |
1245 exsltRegisterAll(); | |
1246 #endif | |
1247 | |
2139 | 1248 ngx_http_next_header_filter = ngx_http_top_header_filter; |
1249 ngx_http_top_header_filter = ngx_http_xslt_header_filter; | |
1250 | |
1251 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1252 ngx_http_top_body_filter = ngx_http_xslt_body_filter; | |
1253 | |
1254 return NGX_OK; | |
1255 } | |
1256 | |
1257 | |
1258 static void | |
1259 ngx_http_xslt_filter_exit(ngx_cycle_t *cycle) | |
1260 { | |
1261 xsltCleanupGlobals(); | |
1262 xmlCleanupParser(); | |
1263 } |