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