Mercurial > hg > nginx
annotate src/http/modules/ngx_http_xslt_filter_module.c @ 2158:18e85d78fb0c
disable ranges, because xslt filter runs after range body filter
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 06 Aug 2008 09:38:34 +0000 |
parents | 0c6d0bd60579 |
children | 0ec936b0010a |
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 | |
335 r->headers_out.content_length_n = b->last - b->pos; | |
336 | |
337 if (r->headers_out.content_length) { | |
338 r->headers_out.content_length->hash = 0; | |
339 r->headers_out.content_length = NULL; | |
340 } | |
341 | |
342 rc = ngx_http_next_header_filter(r); | |
343 | |
344 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
345 ngx_free(b->pos); | |
346 return rc; | |
347 } | |
348 | |
349 cln->handler = ngx_http_xslt_cleanup; | |
350 cln->data = b->pos; | |
351 | |
352 out.buf = b; | |
353 out.next = NULL; | |
354 | |
355 return ngx_http_next_body_filter(r, &out); | |
356 } | |
357 | |
358 | |
359 static ngx_int_t | |
360 ngx_http_xslt_filter_internal_error(ngx_http_request_t *r) | |
361 { | |
362 ngx_int_t rc; | |
363 | |
364 /* clear the modules contexts */ | |
365 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module); | |
366 | |
367 rc = ngx_http_special_response_handler(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
368 | |
369 /* NGX_ERROR resets any pending data */ | |
370 | |
371 return (rc == NGX_OK) ? NGX_ERROR : rc; | |
372 } | |
373 | |
374 | |
375 static ngx_int_t | |
376 ngx_http_xslt_add_chunk(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx, | |
377 ngx_buf_t *b) | |
378 { | |
379 int err; | |
380 xmlSAXHandler *sax; | |
381 xmlParserCtxtPtr ctxt; | |
382 | |
383 if (ctx->ctxt == NULL) { | |
384 | |
385 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL); | |
386 if (ctxt == NULL) { | |
387 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
388 "xmlCreatePushParserCtxt() failed"); | |
389 return NGX_ERROR; | |
390 } | |
391 | |
392 ctx->sax = ngx_palloc(r->pool, sizeof(xmlSAXHandler)); | |
393 if (ctx->sax == NULL) { | |
394 return NGX_ERROR; | |
395 } | |
396 | |
397 sax = ctxt->sax; | |
398 | |
399 ngx_memcpy(ctx->sax, sax, sizeof(xmlSAXHandler)); | |
400 | |
401 sax->startDocument = ngx_http_xslt_sax_start_document; | |
402 sax->endDocument = ngx_http_xslt_sax_end_document; | |
403 | |
404 sax->internalSubset = ngx_http_xslt_sax_internal_subset; | |
405 sax->externalSubset = ngx_http_xslt_sax_external_subset; | |
406 sax->entityDecl = ngx_http_xslt_sax_entity_decl; | |
407 sax->attributeDecl = ngx_http_xslt_sax_attribute_decl; | |
408 sax->elementDecl = ngx_http_xslt_sax_element_decl; | |
409 sax->notationDecl = ngx_http_xslt_sax_notation_decl; | |
410 sax->unparsedEntityDecl = ngx_http_xslt_sax_unparsed_entity_decl; | |
411 sax->setDocumentLocator = NULL; | |
412 | |
413 sax->startElementNs = ngx_http_xslt_sax_start_element; | |
414 sax->endElementNs = ngx_http_xslt_sax_end_element; | |
415 | |
416 sax->characters = ngx_http_xslt_sax_characters; | |
417 sax->ignorableWhitespace = ngx_http_xslt_sax_characters; | |
418 sax->cdataBlock = ngx_http_xslt_sax_cdata_block; | |
419 sax->getEntity = ngx_http_xslt_sax_get_entity; | |
420 sax->resolveEntity = ngx_http_xslt_sax_resolve_entity; | |
421 sax->getParameterEntity = ngx_http_xslt_sax_get_parameter_entity; | |
422 sax->reference = ngx_http_xslt_sax_reference; | |
423 sax->comment = ngx_http_xslt_sax_comment; | |
424 sax->processingInstruction = ngx_http_xslt_sax_processing_instruction; | |
425 | |
426 sax->isStandalone = ngx_http_xslt_sax_is_standalone; | |
427 sax->hasInternalSubset = ngx_http_xslt_sax_has_internal_subset; | |
428 sax->hasExternalSubset = ngx_http_xslt_sax_has_external_subset; | |
429 | |
430 sax->warning = NULL; | |
431 sax->error = ngx_http_xslt_sax_error; | |
432 sax->fatalError = ngx_http_xslt_sax_error; | |
433 | |
434 ctxt->userData = ctx; | |
435 | |
436 ctxt->replaceEntities = 1; | |
437 ctxt->loadsubset = 1; | |
438 | |
439 ctx->ctxt = ctxt; | |
440 ctx->request = r; | |
441 } | |
442 | |
443 err = xmlParseChunk(ctx->ctxt, (char *) b->pos, | |
444 (int) (b->last - b->pos), b->last_buf); | |
445 | |
446 if (err == 0) { | |
447 b->pos = b->last; | |
448 return NGX_OK; | |
449 } | |
450 | |
451 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
452 "xmlParseChunk() failed, error:%d", err); | |
453 | |
454 return NGX_ERROR; | |
455 } | |
456 | |
457 | |
458 static void | |
459 ngx_http_xslt_sax_start_document(void *data) | |
460 { | |
461 ngx_http_xslt_filter_ctx_t *ctx = data; | |
462 | |
463 ctx->sax->startDocument(ctx->ctxt); | |
464 } | |
465 | |
466 | |
467 static void | |
468 ngx_http_xslt_sax_end_document(void *data) | |
469 { | |
470 ngx_http_xslt_filter_ctx_t *ctx = data; | |
471 | |
472 ctx->sax->endDocument(ctx->ctxt); | |
473 } | |
474 | |
475 | |
476 static void | |
477 ngx_http_xslt_sax_internal_subset(void *data, const xmlChar *name, | |
478 const xmlChar *externalId, const xmlChar *systemId) | |
479 { | |
480 ngx_http_xslt_filter_ctx_t *ctx = data; | |
481 | |
482 ctx->sax->internalSubset(ctx->ctxt, name, externalId, systemId); | |
483 } | |
484 | |
485 | |
486 static void | |
487 ngx_http_xslt_sax_external_subset(void *data, const xmlChar *name, | |
488 const xmlChar *externalId, const xmlChar *systemId) | |
489 { | |
490 ngx_http_xslt_filter_ctx_t *ctx = data; | |
491 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
492 xmlDocPtr doc; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
493 xmlDtdPtr dtd; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
494 ngx_http_request_t *r; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
495 ngx_http_xslt_filter_loc_conf_t *conf; |
2139 | 496 |
497 r = ctx->request; | |
498 | |
499 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module); | |
500 | |
501 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
502 "xslt filter extSubset: \"%s\" \"%s\" \"%s\"", | |
503 name ? name : (xmlChar *) "", | |
504 externalId ? externalId : (xmlChar *) "", | |
505 systemId ? systemId : (xmlChar *) ""); | |
506 | |
507 doc = ctx->ctxt->myDoc; | |
508 | |
509 #if (NGX_HTTP_XSLT_REUSE_DTD) | |
510 | |
511 dtd = conf->dtd; | |
512 | |
513 #else | |
514 | |
515 dtd = xmlCopyDtd(conf->dtd); | |
516 if (dtd == NULL) { | |
517 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
518 "xmlCopyDtd() failed"); | |
519 return; | |
520 } | |
521 | |
522 if (doc->children == NULL) { | |
523 xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd); | |
524 | |
525 } else { | |
526 xmlAddPrevSibling(doc->children, (xmlNodePtr) dtd); | |
527 } | |
528 | |
529 #endif | |
530 | |
531 doc->extSubset = dtd; | |
532 } | |
533 | |
534 | |
535 static void | |
536 ngx_http_xslt_sax_entity_decl(void *data, const xmlChar *name, int type, | |
537 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content) | |
538 { | |
539 ngx_http_xslt_filter_ctx_t *ctx = data; | |
540 | |
541 ctx->sax->entityDecl(ctx->ctxt, name, type, publicId, systemId, content); | |
542 } | |
543 | |
544 | |
545 static void | |
546 ngx_http_xslt_sax_attribute_decl(void *data, const xmlChar *elem, | |
547 const xmlChar *fullname, int type, int def, const xmlChar *defaultValue, | |
548 xmlEnumerationPtr tree) | |
549 { | |
550 ngx_http_xslt_filter_ctx_t *ctx = data; | |
551 | |
552 ctx->sax->attributeDecl(ctx->ctxt, elem, fullname, type, def, defaultValue, | |
553 tree); | |
554 } | |
555 | |
556 | |
557 static void | |
558 ngx_http_xslt_sax_element_decl(void *data, const xmlChar * name, int type, | |
559 xmlElementContentPtr content) | |
560 { | |
561 ngx_http_xslt_filter_ctx_t *ctx = data; | |
562 | |
563 ctx->sax->elementDecl(ctx->ctxt, name, type, content); | |
564 } | |
565 | |
566 | |
567 static void | |
568 ngx_http_xslt_sax_notation_decl(void *data, const xmlChar *name, | |
569 const xmlChar *publicId, const xmlChar *systemId) | |
570 { | |
571 ngx_http_xslt_filter_ctx_t *ctx = data; | |
572 | |
573 ctx->sax->notationDecl(ctx->ctxt, name, publicId, systemId); | |
574 } | |
575 | |
576 | |
577 static void | |
578 ngx_http_xslt_sax_unparsed_entity_decl(void *data, const xmlChar *name, | |
579 const xmlChar *publicId, const xmlChar *systemId, | |
580 const xmlChar *notationName) | |
581 { | |
582 ngx_http_xslt_filter_ctx_t *ctx = data; | |
583 | |
584 ctx->sax->unparsedEntityDecl(ctx->ctxt, name, publicId, systemId, | |
585 notationName); | |
586 } | |
587 | |
588 | |
589 static void | |
590 ngx_http_xslt_sax_start_element(void *data, const xmlChar *localname, | |
591 const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, | |
592 const xmlChar **namespaces, int nb_attributes, int nb_defaulted, | |
593 const xmlChar **attributes) | |
594 { | |
595 ngx_http_xslt_filter_ctx_t *ctx = data; | |
596 | |
597 ctx->sax->startElementNs(ctx->ctxt, localname, prefix, URI, nb_namespaces, | |
598 namespaces, nb_attributes, nb_defaulted, attributes); | |
599 } | |
600 | |
601 | |
602 static void | |
603 ngx_http_xslt_sax_end_element(void *data, | |
604 const xmlChar * localname ATTRIBUTE_UNUSED, | |
605 const xmlChar * prefix ATTRIBUTE_UNUSED, | |
606 const xmlChar * URI ATTRIBUTE_UNUSED) | |
607 { | |
608 ngx_http_xslt_filter_ctx_t *ctx = data; | |
609 | |
610 ctx->sax->endElementNs(ctx->ctxt, localname, prefix, URI); | |
611 } | |
612 | |
613 | |
614 static void | |
615 ngx_http_xslt_sax_characters(void *data, const xmlChar *p, int len) | |
616 { | |
617 ngx_http_xslt_filter_ctx_t *ctx = data; | |
618 | |
619 ctx->sax->characters(ctx->ctxt, p, len); | |
620 } | |
621 | |
622 | |
623 static void | |
624 ngx_http_xslt_sax_cdata_block(void *data, const xmlChar *p, int len) | |
625 { | |
626 ngx_http_xslt_filter_ctx_t *ctx = data; | |
627 | |
628 ctx->sax->cdataBlock(ctx->ctxt, p, len); | |
629 } | |
630 | |
631 | |
632 static xmlEntityPtr | |
633 ngx_http_xslt_sax_get_entity(void *data, const xmlChar *name) | |
634 { | |
635 ngx_http_xslt_filter_ctx_t *ctx = data; | |
636 | |
637 return ctx->sax->getEntity(ctx->ctxt, name); | |
638 } | |
639 | |
640 | |
641 static xmlEntityPtr | |
642 ngx_http_xslt_sax_get_parameter_entity(void *data, const xmlChar *name) | |
643 { | |
644 ngx_http_xslt_filter_ctx_t *ctx = data; | |
645 | |
646 return ctx->sax->getParameterEntity(ctx->ctxt, name); | |
647 } | |
648 | |
649 | |
650 static xmlParserInputPtr | |
651 ngx_http_xslt_sax_resolve_entity(void *data, const xmlChar *publicId, | |
652 const xmlChar *systemId) | |
653 { | |
654 ngx_http_xslt_filter_ctx_t *ctx = data; | |
655 | |
656 return ctx->sax->resolveEntity(ctx->ctxt, publicId, systemId); | |
657 } | |
658 | |
659 | |
660 static void | |
661 ngx_http_xslt_sax_reference(void *data, const xmlChar *name) | |
662 { | |
663 ngx_http_xslt_filter_ctx_t *ctx = data; | |
664 | |
665 ctx->sax->reference(ctx->ctxt, name); | |
666 } | |
667 | |
668 | |
669 static void | |
670 ngx_http_xslt_sax_comment(void *data, const xmlChar *value) | |
671 { | |
672 ngx_http_xslt_filter_ctx_t *ctx = data; | |
673 | |
674 ctx->sax->comment(ctx->ctxt, value); | |
675 } | |
676 | |
677 | |
678 static void | |
679 ngx_http_xslt_sax_processing_instruction(void *data, const xmlChar *target, | |
680 const xmlChar *pidata) | |
681 { | |
682 ngx_http_xslt_filter_ctx_t *ctx = data; | |
683 | |
684 ctx->sax->processingInstruction(ctx->ctxt, target, pidata); | |
685 } | |
686 | |
687 | |
688 static int | |
689 ngx_http_xslt_sax_is_standalone(void *data) | |
690 { | |
691 ngx_http_xslt_filter_ctx_t *ctx = data; | |
692 | |
693 return ctx->sax->isStandalone(ctx->ctxt); | |
694 } | |
695 | |
696 | |
697 static int | |
698 ngx_http_xslt_sax_has_internal_subset(void *data) | |
699 { | |
700 ngx_http_xslt_filter_ctx_t *ctx = data; | |
701 | |
702 return ctx->sax->hasInternalSubset(ctx->ctxt); | |
703 } | |
704 | |
705 | |
706 static int | |
707 ngx_http_xslt_sax_has_external_subset(void *data) | |
708 { | |
709 ngx_http_xslt_filter_ctx_t *ctx = data; | |
710 | |
711 return ctx->sax->hasExternalSubset(ctx->ctxt); | |
712 } | |
713 | |
714 | |
715 static void ngx_cdecl | |
716 ngx_http_xslt_sax_error(void *data, const char *msg, ...) | |
717 { | |
718 ngx_http_xslt_filter_ctx_t *ctx = data; | |
719 | |
720 size_t n; | |
721 va_list args; | |
722 u_char buf[NGX_MAX_ERROR_STR]; | |
723 | |
724 buf[0] = '\0'; | |
725 | |
726 va_start(args, msg); | |
727 n = (size_t) vsnprintf((char *) buf, NGX_MAX_ERROR_STR, msg, args); | |
728 va_end(args); | |
729 | |
730 while (--n && (buf[n] == CR || buf[n] == LF)) { /* void */ } | |
731 | |
732 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, | |
733 "libxml2 error: \"%*s\"", n, buf); | |
734 } | |
735 | |
736 | |
737 static ngx_buf_t * | |
738 ngx_http_xslt_apply_stylesheet(ngx_http_request_t *r, | |
739 ngx_http_xslt_filter_ctx_t *ctx) | |
740 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
741 int len, rc, doc_type; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
742 u_char *type, *encoding; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
743 ngx_buf_t *b; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
744 ngx_uint_t i; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
745 xmlChar *buf; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
746 xmlDocPtr doc, res; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
747 ngx_http_xslt_sheet_t *sheet; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
748 ngx_http_xslt_filter_loc_conf_t *conf; |
2139 | 749 |
750 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module); | |
751 sheet = conf->sheets.elts; | |
752 doc = ctx->doc; | |
753 | |
754 /* preallocate array for 4 params */ | |
755 | |
756 if (ngx_array_init(&ctx->params, r->pool, 4 * 2 + 1, sizeof(char *)) | |
757 != NGX_OK) | |
758 { | |
759 xmlFreeDoc(doc); | |
760 return NULL; | |
761 } | |
762 | |
763 for (i = 0; i < conf->sheets.nelts; i++) { | |
764 | |
765 if (ngx_http_xslt_params(r, ctx, &sheet[i].params) != NGX_OK) { | |
766 xmlFreeDoc(doc); | |
767 return NULL; | |
768 } | |
769 | |
770 res = xsltApplyStylesheet(sheet[i].stylesheet, doc, ctx->params.elts); | |
771 | |
772 xmlFreeDoc(doc); | |
773 | |
774 if (res == NULL) { | |
775 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
776 "xsltApplyStylesheet() failed"); | |
777 return NULL; | |
778 } | |
779 | |
780 doc = res; | |
781 | |
782 /* reset array elements */ | |
783 ctx->params.nelts = 0; | |
784 } | |
785 | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
786 /* there must be at least one stylesheet */ |
2139 | 787 |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
788 type = ngx_http_xslt_content_type(sheet[i - 1].stylesheet); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
789 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
|
790 doc_type = doc->type; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
791 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
792 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
|
793 "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
|
794 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
|
795 encoding ? encoding : (u_char *) "(null)"); |
2139 | 796 |
797 rc = xsltSaveResultToString(&buf, &len, doc, sheet[i - 1].stylesheet); | |
798 | |
799 xmlFreeDoc(doc); | |
800 | |
801 if (rc != 0) { | |
802 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
803 "xsltSaveResultToString() failed"); | |
804 return NULL; | |
805 } | |
806 | |
807 if (len == 0) { | |
808 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
809 "xsltSaveResultToString() returned zero-length result"); | |
810 return NULL; | |
811 } | |
812 | |
813 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
814 if (b == NULL) { | |
815 ngx_free(buf); | |
816 return NULL; | |
817 } | |
818 | |
819 b->pos = buf; | |
820 b->last = buf + len; | |
821 b->memory = 1; | |
822 b->last_buf = 1; | |
823 | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
824 if (type) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
825 len = ngx_strlen(type); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
826 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
827 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
|
828 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
|
829 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
|
830 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
831 } 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
|
832 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
833 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
|
834 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
|
835 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
|
836 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
837 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
838 if (encoding) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
839 r->headers_out.charset.len = ngx_strlen(encoding); |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
840 r->headers_out.charset.data = encoding; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
841 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
842 |
2139 | 843 return b; |
844 } | |
845 | |
846 | |
847 static ngx_int_t | |
848 ngx_http_xslt_params(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx, | |
849 ngx_array_t *params) | |
850 { | |
851 u_char *p, *last, *value, *dst, *src, **s; | |
852 size_t len; | |
853 ngx_uint_t i; | |
854 ngx_str_t string; | |
855 ngx_http_xslt_param_t *param; | |
856 | |
857 param = params->elts; | |
858 | |
859 for (i = 0; i < params->nelts; i++) { | |
860 | |
861 if (ngx_http_script_run(r, &string, param[i].lengths->elts, 1, | |
862 param[i].values->elts) | |
863 == NULL) | |
864 { | |
865 return NGX_ERROR; | |
866 } | |
867 | |
868 last = string.data + string.len - 1; | |
869 *last = '\0'; | |
870 | |
871 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
872 "xslt filter param: \"%s\"", string.data); | |
873 | |
874 p = string.data; | |
875 | |
876 while (p && *p) { | |
877 | |
878 value = p; | |
879 p = (u_char *) ngx_strchr(p, '='); | |
880 if (p == NULL) { | |
881 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
882 "invalid libxslt parameter \"%s\"", value); | |
883 return NGX_ERROR; | |
884 } | |
885 *p++ = '\0'; | |
886 | |
887 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
888 "xslt filter param name: \"%s\"", value); | |
889 | |
890 s = ngx_array_push(&ctx->params); | |
891 if (s == NULL) { | |
892 return NGX_ERROR; | |
893 } | |
894 | |
895 *s = value; | |
896 | |
897 value = p; | |
898 p = (u_char *) ngx_strchr(p, ':'); | |
899 | |
900 if (p) { | |
901 len = p - value; | |
902 *p++ = '\0'; | |
903 | |
904 } else { | |
905 len = last - value; | |
906 } | |
907 | |
908 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
909 "xslt filter param value: \"%s\"", value); | |
910 | |
911 dst = value; | |
912 src = value; | |
913 | |
914 ngx_unescape_uri(&dst, &src, len, 0); | |
915 | |
916 *dst = '\0'; | |
917 | |
918 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
919 "xslt filter param unescaped: \"%s\"", value); | |
920 | |
921 s = ngx_array_push(&ctx->params); | |
922 if (s == NULL) { | |
923 return NGX_ERROR; | |
924 } | |
925 | |
926 *s = value; | |
927 } | |
928 } | |
929 | |
930 s = ngx_array_push(&ctx->params); | |
931 if (s == NULL) { | |
932 return NGX_ERROR; | |
933 } | |
934 | |
935 *s = NULL; | |
936 | |
937 return NGX_OK; | |
938 } | |
939 | |
940 | |
2153
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
941 static u_char * |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
942 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
|
943 { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
944 u_char *type; |
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 (s->mediaType) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
947 return s->mediaType; |
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 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
|
951 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
952 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
|
953 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
954 if (type) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
955 return type; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
956 } |
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 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
959 return NULL; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
960 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
961 |
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 static u_char * |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
964 ngx_http_xslt_encoding(xsltStylesheetPtr s) |
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 u_char *encoding; |
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 (s->encoding) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
969 return s->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 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
|
973 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
974 encoding = ngx_http_xslt_encoding(s); |
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 if (encoding) { |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
977 return encoding; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
978 } |
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 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
981 return NULL; |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
982 } |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
983 |
6476e445d6ce
xsl:output's media-type and encoding support
Igor Sysoev <igor@sysoev.ru>
parents:
2152
diff
changeset
|
984 |
2139 | 985 static void |
986 ngx_http_xslt_cleanup(void *data) | |
987 { | |
988 ngx_free(data); | |
989 } | |
990 | |
991 | |
992 static char * | |
993 ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
994 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
995 ngx_http_xslt_filter_loc_conf_t *xlcf = conf; |
2139 | 996 |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
997 ngx_str_t *value; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
998 ngx_uint_t i; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
999 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
|
1000 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
|
1001 ngx_http_xslt_filter_main_conf_t *xmcf; |
2139 | 1002 |
1003 if (xlcf->dtd) { | |
1004 return "is duplicate"; | |
1005 } | |
1006 | |
1007 value = cf->args->elts; | |
1008 | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1009 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
|
1010 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1011 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
|
1012 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
|
1013 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
|
1014 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
|
1015 return NGX_CONF_OK; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1016 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1017 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1018 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1019 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
|
1020 if (cln == NULL) { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1021 return NGX_CONF_ERROR; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1022 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1023 |
2139 | 1024 xlcf->dtd = xmlParseDTD(NULL, (xmlChar *) value[1].data); |
1025 | |
1026 if (xlcf->dtd == NULL) { | |
1027 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "xmlParseDTD() failed"); | |
1028 return NGX_CONF_ERROR; | |
1029 } | |
1030 | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1031 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
|
1032 cln->data = xlcf->dtd; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1033 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1034 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
|
1035 if (file == NULL) { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1036 return NGX_CONF_ERROR; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1037 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1038 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1039 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
|
1040 file->data = xlcf->dtd; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1041 |
2139 | 1042 return NGX_CONF_OK; |
1043 } | |
1044 | |
1045 | |
1046 | |
1047 static char * | |
1048 ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1049 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1050 ngx_http_xslt_filter_loc_conf_t *xlcf = conf; |
2139 | 1051 |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1052 ngx_str_t *value; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1053 ngx_uint_t i, n; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1054 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
|
1055 ngx_http_xslt_file_t *file; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1056 ngx_http_xslt_sheet_t *sheet; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1057 ngx_http_xslt_param_t *param; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1058 ngx_http_script_compile_t sc; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1059 ngx_http_xslt_filter_main_conf_t *xmcf; |
2139 | 1060 |
1061 value = cf->args->elts; | |
1062 | |
1063 if (xlcf->sheets.elts == NULL) { | |
1064 if (ngx_array_init(&xlcf->sheets, cf->pool, 1, | |
1065 sizeof(ngx_http_xslt_sheet_t)) | |
1066 != NGX_OK) | |
1067 { | |
1068 return NGX_CONF_ERROR; | |
1069 } | |
1070 } | |
1071 | |
1072 sheet = ngx_array_push(&xlcf->sheets); | |
1073 if (sheet == NULL) { | |
1074 return NGX_CONF_ERROR; | |
1075 } | |
1076 | |
1077 ngx_memzero(sheet, sizeof(ngx_http_xslt_sheet_t)); | |
1078 | |
1079 if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) { | |
1080 return NGX_CONF_ERROR; | |
1081 } | |
1082 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1083 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
|
1084 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1085 file = xmcf->sheet_files.elts; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1086 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
|
1087 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
|
1088 sheet->stylesheet = file[i].data; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1089 goto found; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1090 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1091 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1092 |
2139 | 1093 cln = ngx_pool_cleanup_add(cf->pool, 0); |
1094 if (cln == NULL) { | |
1095 return NGX_CONF_ERROR; | |
1096 } | |
1097 | |
1098 sheet->stylesheet = xsltParseStylesheetFile(value[1].data); | |
1099 if (sheet->stylesheet == NULL) { | |
1100 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
1101 "xsltParseStylesheetFile(\"%s\") failed", | |
1102 value[1].data); | |
1103 return NGX_CONF_ERROR; | |
1104 } | |
1105 | |
1106 cln->handler = ngx_http_xslt_cleanup_stylesheet; | |
1107 cln->data = sheet->stylesheet; | |
1108 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1109 file = ngx_array_push(&xmcf->sheet_files); |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1110 if (file == NULL) { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1111 return NGX_CONF_ERROR; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1112 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1113 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1114 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
|
1115 file->data = sheet->stylesheet; |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1116 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1117 found: |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1118 |
2139 | 1119 n = cf->args->nelts; |
1120 | |
1121 if (n == 2) { | |
1122 return NGX_CONF_OK; | |
1123 } | |
1124 | |
1125 if (ngx_array_init(&sheet->params, cf->pool, n - 2, | |
1126 sizeof(ngx_http_xslt_param_t)) | |
1127 != NGX_OK) | |
1128 { | |
1129 return NGX_CONF_ERROR; | |
1130 } | |
1131 | |
1132 for (i = 2; i < n; i++) { | |
1133 | |
1134 param = ngx_array_push(&sheet->params); | |
1135 if (param == NULL) { | |
1136 return NGX_CONF_ERROR; | |
1137 } | |
1138 | |
1139 param->lengths = NULL; | |
1140 param->values = NULL; | |
1141 | |
1142 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1143 | |
1144 sc.cf = cf; | |
1145 sc.source = &value[i]; | |
1146 sc.lengths = ¶m->lengths; | |
1147 sc.values = ¶m->values; | |
1148 sc.variables = ngx_http_script_variables_count(&value[i]); | |
1149 sc.complete_lengths = 1; | |
1150 sc.complete_values = 1; | |
1151 | |
1152 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1153 return NGX_CONF_ERROR; | |
1154 } | |
1155 } | |
1156 | |
1157 return NGX_CONF_OK; | |
1158 } | |
1159 | |
1160 | |
1161 static void | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1162 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
|
1163 { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1164 xmlFreeDtd(data); |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1165 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1166 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1167 |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1168 static void |
2139 | 1169 ngx_http_xslt_cleanup_stylesheet(void *data) |
1170 { | |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1171 xsltFreeStylesheet(data); |
2139 | 1172 } |
1173 | |
1174 | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1175 static void * |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1176 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
|
1177 { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1178 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
|
1179 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1180 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
|
1181 if (conf == NULL) { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1182 return NGX_CONF_ERROR; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1183 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1184 |
2156
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1185 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
|
1186 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
|
1187 != NGX_OK) |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1188 { |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1189 return NULL; |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1190 } |
0c6d0bd60579
reuse compiled DTD hash in different locations, add DTD cleanup
Igor Sysoev <igor@sysoev.ru>
parents:
2154
diff
changeset
|
1191 |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1192 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
|
1193 sizeof(ngx_http_xslt_file_t)) |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1194 != NGX_OK) |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1195 { |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1196 return NULL; |
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 |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1199 return conf; |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1200 } |
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1201 |
2139 | 1202 |
1203 static void * | |
1204 ngx_http_xslt_filter_create_conf(ngx_conf_t *cf) | |
1205 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1206 ngx_http_xslt_filter_loc_conf_t *conf; |
2139 | 1207 |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1208 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_xslt_filter_loc_conf_t)); |
2139 | 1209 if (conf == NULL) { |
1210 return NGX_CONF_ERROR; | |
1211 } | |
1212 | |
1213 /* | |
1214 * set by ngx_pcalloc(): | |
1215 * | |
2148 | 1216 * conf->dtd = NULL; |
1217 * conf->sheets = { NULL }; | |
1218 * conf->types = { NULL }; | |
1219 * conf->types_keys = NULL; | |
2139 | 1220 */ |
1221 | |
1222 return conf; | |
1223 } | |
1224 | |
1225 | |
1226 static char * | |
1227 ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
1228 { | |
2154
4f48a2765da0
reuse compiled XSLT tree in different locations
Igor Sysoev <igor@sysoev.ru>
parents:
2153
diff
changeset
|
1229 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
|
1230 ngx_http_xslt_filter_loc_conf_t *conf = child; |
2139 | 1231 |
1232 if (conf->dtd == NULL) { | |
1233 conf->dtd = prev->dtd; | |
1234 } | |
1235 | |
1236 if (conf->sheets.nelts == 0) { | |
1237 conf->sheets = prev->sheets; | |
1238 } | |
1239 | |
2148 | 1240 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types, |
1241 prev->types_keys, &prev->types, | |
1242 ngx_http_xslt_default_types) | |
2139 | 1243 != NGX_OK) |
1244 { | |
1245 return NGX_CONF_ERROR; | |
1246 } | |
1247 | |
1248 return NGX_CONF_OK; | |
1249 } | |
1250 | |
1251 | |
1252 static ngx_int_t | |
1253 ngx_http_xslt_filter_init(ngx_conf_t *cf) | |
1254 { | |
1255 xmlInitParser(); | |
1256 | |
1257 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
1258 ngx_http_top_header_filter = ngx_http_xslt_header_filter; | |
1259 | |
1260 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1261 ngx_http_top_body_filter = ngx_http_xslt_body_filter; | |
1262 | |
1263 return NGX_OK; | |
1264 } | |
1265 | |
1266 | |
1267 static void | |
1268 ngx_http_xslt_filter_exit(ngx_cycle_t *cycle) | |
1269 { | |
1270 xsltCleanupGlobals(); | |
1271 xmlCleanupParser(); | |
1272 } |