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