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