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