392
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
392
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12 #include <libxml/parser.h>
|
|
13 #include <libxml/tree.h>
|
|
14 #include <libxslt/xslt.h>
|
|
15 #include <libxslt/xsltInternals.h>
|
|
16 #include <libxslt/transform.h>
|
668
|
17 #include <libxslt/variables.h>
|
392
|
18 #include <libxslt/xsltutils.h>
|
|
19
|
418
|
20 #if (NGX_HAVE_EXSLT)
|
|
21 #include <libexslt/exslt.h>
|
|
22 #endif
|
|
23
|
392
|
24
|
|
25 #ifndef NGX_HTTP_XSLT_REUSE_DTD
|
|
26 #define NGX_HTTP_XSLT_REUSE_DTD 1
|
|
27 #endif
|
|
28
|
|
29
|
|
30 typedef struct {
|
668
|
31 u_char *name;
|
|
32 void *data;
|
394
|
33 } ngx_http_xslt_file_t;
|
|
34
|
|
35
|
|
36 typedef struct {
|
668
|
37 ngx_array_t dtd_files; /* ngx_http_xslt_file_t */
|
|
38 ngx_array_t sheet_files; /* ngx_http_xslt_file_t */
|
394
|
39 } ngx_http_xslt_filter_main_conf_t;
|
|
40
|
|
41
|
|
42 typedef struct {
|
668
|
43 u_char *name;
|
|
44 ngx_http_complex_value_t value;
|
|
45 ngx_uint_t quote; /* unsigned quote:1; */
|
|
46 } ngx_http_xslt_param_t;
|
|
47
|
|
48
|
|
49 typedef struct {
|
|
50 xsltStylesheetPtr stylesheet;
|
|
51 ngx_array_t params; /* ngx_http_xslt_param_t */
|
392
|
52 } ngx_http_xslt_sheet_t;
|
|
53
|
|
54
|
|
55 typedef struct {
|
668
|
56 xmlDtdPtr dtd;
|
|
57 ngx_array_t sheets; /* ngx_http_xslt_sheet_t */
|
|
58 ngx_hash_t types;
|
|
59 ngx_array_t *types_keys;
|
|
60 ngx_array_t *params; /* ngx_http_xslt_param_t */
|
394
|
61 } ngx_http_xslt_filter_loc_conf_t;
|
392
|
62
|
|
63
|
|
64 typedef struct {
|
668
|
65 xmlDocPtr doc;
|
|
66 xmlParserCtxtPtr ctxt;
|
|
67 xsltTransformContextPtr transform;
|
|
68 ngx_http_request_t *request;
|
|
69 ngx_array_t params;
|
394
|
70
|
668
|
71 ngx_uint_t done; /* unsigned done:1; */
|
392
|
72 } ngx_http_xslt_filter_ctx_t;
|
|
73
|
|
74
|
|
75 static ngx_int_t ngx_http_xslt_send(ngx_http_request_t *r,
|
|
76 ngx_http_xslt_filter_ctx_t *ctx, ngx_buf_t *b);
|
|
77 static ngx_int_t ngx_http_xslt_add_chunk(ngx_http_request_t *r,
|
|
78 ngx_http_xslt_filter_ctx_t *ctx, ngx_buf_t *b);
|
|
79
|
|
80
|
|
81 static void ngx_http_xslt_sax_external_subset(void *data, const xmlChar *name,
|
|
82 const xmlChar *externalId, const xmlChar *systemId);
|
|
83 static void ngx_cdecl ngx_http_xslt_sax_error(void *data, const char *msg, ...);
|
|
84
|
|
85
|
|
86 static ngx_buf_t *ngx_http_xslt_apply_stylesheet(ngx_http_request_t *r,
|
|
87 ngx_http_xslt_filter_ctx_t *ctx);
|
|
88 static ngx_int_t ngx_http_xslt_params(ngx_http_request_t *r,
|
668
|
89 ngx_http_xslt_filter_ctx_t *ctx, ngx_array_t *params, ngx_uint_t final);
|
394
|
90 static u_char *ngx_http_xslt_content_type(xsltStylesheetPtr s);
|
|
91 static u_char *ngx_http_xslt_encoding(xsltStylesheetPtr s);
|
392
|
92 static void ngx_http_xslt_cleanup(void *data);
|
|
93
|
|
94 static char *ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
95 void *conf);
|
|
96 static char *ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
97 void *conf);
|
668
|
98 static char *ngx_http_xslt_param(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
99 void *conf);
|
394
|
100 static void ngx_http_xslt_cleanup_dtd(void *data);
|
392
|
101 static void ngx_http_xslt_cleanup_stylesheet(void *data);
|
394
|
102 static void *ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf);
|
392
|
103 static void *ngx_http_xslt_filter_create_conf(ngx_conf_t *cf);
|
|
104 static char *ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent,
|
|
105 void *child);
|
|
106 static ngx_int_t ngx_http_xslt_filter_init(ngx_conf_t *cf);
|
400
|
107 static void ngx_http_xslt_filter_exit(ngx_cycle_t *cycle);
|
392
|
108
|
|
109
|
|
110 ngx_str_t ngx_http_xslt_default_types[] = {
|
|
111 ngx_string("text/xml"),
|
|
112 ngx_null_string
|
|
113 };
|
|
114
|
|
115
|
|
116 static ngx_command_t ngx_http_xslt_filter_commands[] = {
|
|
117
|
|
118 { ngx_string("xml_entities"),
|
394
|
119 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
392
|
120 ngx_http_xslt_entities,
|
|
121 NGX_HTTP_LOC_CONF_OFFSET,
|
|
122 0,
|
|
123 NULL },
|
|
124
|
|
125 { ngx_string("xslt_stylesheet"),
|
394
|
126 NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
392
|
127 ngx_http_xslt_stylesheet,
|
|
128 NGX_HTTP_LOC_CONF_OFFSET,
|
|
129 0,
|
|
130 NULL },
|
|
131
|
668
|
132 { ngx_string("xslt_param"),
|
|
133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
134 ngx_http_xslt_param,
|
|
135 NGX_HTTP_LOC_CONF_OFFSET,
|
|
136 0,
|
|
137 NULL },
|
|
138
|
|
139 { ngx_string("xslt_string_param"),
|
|
140 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
141 ngx_http_xslt_param,
|
|
142 NGX_HTTP_LOC_CONF_OFFSET,
|
|
143 0,
|
|
144 (void *) 1 },
|
|
145
|
392
|
146 { ngx_string("xslt_types"),
|
394
|
147 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
392
|
148 ngx_http_types_slot,
|
|
149 NGX_HTTP_LOC_CONF_OFFSET,
|
394
|
150 offsetof(ngx_http_xslt_filter_loc_conf_t, types_keys),
|
392
|
151 &ngx_http_xslt_default_types[0] },
|
|
152
|
|
153 ngx_null_command
|
|
154 };
|
|
155
|
|
156
|
|
157 static ngx_http_module_t ngx_http_xslt_filter_module_ctx = {
|
|
158 NULL, /* preconfiguration */
|
|
159 ngx_http_xslt_filter_init, /* postconfiguration */
|
|
160
|
394
|
161 ngx_http_xslt_filter_create_main_conf, /* create main configuration */
|
392
|
162 NULL, /* init main configuration */
|
|
163
|
|
164 NULL, /* create server configuration */
|
|
165 NULL, /* merge server configuration */
|
|
166
|
|
167 ngx_http_xslt_filter_create_conf, /* create location configuration */
|
|
168 ngx_http_xslt_filter_merge_conf /* merge location configuration */
|
|
169 };
|
|
170
|
|
171
|
|
172 ngx_module_t ngx_http_xslt_filter_module = {
|
|
173 NGX_MODULE_V1,
|
|
174 &ngx_http_xslt_filter_module_ctx, /* module context */
|
|
175 ngx_http_xslt_filter_commands, /* module directives */
|
|
176 NGX_HTTP_MODULE, /* module type */
|
|
177 NULL, /* init master */
|
|
178 NULL, /* init module */
|
|
179 NULL, /* init process */
|
|
180 NULL, /* init thread */
|
|
181 NULL, /* exit thread */
|
508
|
182 ngx_http_xslt_filter_exit, /* exit process */
|
400
|
183 ngx_http_xslt_filter_exit, /* exit master */
|
392
|
184 NGX_MODULE_V1_PADDING
|
|
185 };
|
|
186
|
|
187
|
|
188 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
189 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
190
|
|
191
|
|
192 static ngx_int_t
|
|
193 ngx_http_xslt_header_filter(ngx_http_request_t *r)
|
|
194 {
|
394
|
195 ngx_http_xslt_filter_ctx_t *ctx;
|
|
196 ngx_http_xslt_filter_loc_conf_t *conf;
|
392
|
197
|
|
198 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
199 "xslt filter header");
|
|
200
|
|
201 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) {
|
|
202 return ngx_http_next_header_filter(r);
|
|
203 }
|
|
204
|
|
205 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module);
|
|
206
|
|
207 if (conf->sheets.nelts == 0
|
394
|
208 || ngx_http_test_content_type(r, &conf->types) == NULL)
|
392
|
209 {
|
|
210 return ngx_http_next_header_filter(r);
|
|
211 }
|
|
212
|
|
213 ctx = ngx_http_get_module_ctx(r, ngx_http_xslt_filter_module);
|
|
214
|
|
215 if (ctx) {
|
|
216 return ngx_http_next_header_filter(r);
|
|
217 }
|
|
218
|
|
219 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_xslt_filter_ctx_t));
|
|
220 if (ctx == NULL) {
|
|
221 return NGX_ERROR;
|
|
222 }
|
|
223
|
|
224 ngx_http_set_ctx(r, ctx, ngx_http_xslt_filter_module);
|
|
225
|
|
226 r->main_filter_need_in_memory = 1;
|
|
227
|
|
228 return NGX_OK;
|
|
229 }
|
|
230
|
|
231
|
|
232 static ngx_int_t
|
|
233 ngx_http_xslt_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
234 {
|
508
|
235 int wellFormed;
|
392
|
236 ngx_chain_t *cl;
|
|
237 ngx_http_xslt_filter_ctx_t *ctx;
|
|
238
|
|
239 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
240 "xslt filter body");
|
|
241
|
|
242 if (in == NULL) {
|
|
243 return ngx_http_next_body_filter(r, in);
|
|
244 }
|
|
245
|
|
246 ctx = ngx_http_get_module_ctx(r, ngx_http_xslt_filter_module);
|
|
247
|
|
248 if (ctx == NULL || ctx->done) {
|
|
249 return ngx_http_next_body_filter(r, in);
|
|
250 }
|
|
251
|
|
252 for (cl = in; cl; cl = cl->next) {
|
|
253
|
|
254 if (ngx_http_xslt_add_chunk(r, ctx, cl->buf) != NGX_OK) {
|
|
255
|
582
|
256 if (ctx->ctxt->myDoc) {
|
392
|
257
|
|
258 #if (NGX_HTTP_XSLT_REUSE_DTD)
|
|
259 ctx->ctxt->myDoc->extSubset = NULL;
|
|
260 #endif
|
|
261 xmlFreeDoc(ctx->ctxt->myDoc);
|
|
262 }
|
|
263
|
|
264 xmlFreeParserCtxt(ctx->ctxt);
|
|
265
|
|
266 return ngx_http_xslt_send(r, ctx, NULL);
|
|
267 }
|
|
268
|
496
|
269 if (cl->buf->last_buf || cl->buf->last_in_chain) {
|
392
|
270
|
|
271 ctx->doc = ctx->ctxt->myDoc;
|
|
272
|
|
273 #if (NGX_HTTP_XSLT_REUSE_DTD)
|
|
274 ctx->doc->extSubset = NULL;
|
|
275 #endif
|
|
276
|
508
|
277 wellFormed = ctx->ctxt->wellFormed;
|
|
278
|
392
|
279 xmlFreeParserCtxt(ctx->ctxt);
|
|
280
|
508
|
281 if (wellFormed) {
|
392
|
282 return ngx_http_xslt_send(r, ctx,
|
|
283 ngx_http_xslt_apply_stylesheet(r, ctx));
|
|
284 }
|
|
285
|
|
286 xmlFreeDoc(ctx->doc);
|
|
287
|
|
288 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
289 "not well formed XML document");
|
|
290
|
|
291 return ngx_http_xslt_send(r, ctx, NULL);
|
|
292 }
|
|
293 }
|
|
294
|
|
295 return NGX_OK;
|
|
296 }
|
|
297
|
|
298
|
|
299 static ngx_int_t
|
|
300 ngx_http_xslt_send(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx,
|
|
301 ngx_buf_t *b)
|
|
302 {
|
|
303 ngx_int_t rc;
|
|
304 ngx_chain_t out;
|
|
305 ngx_pool_cleanup_t *cln;
|
|
306
|
|
307 ctx->done = 1;
|
|
308
|
|
309 if (b == NULL) {
|
488
|
310 return ngx_http_filter_finalize_request(r, NULL,
|
484
|
311 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
392
|
312 }
|
|
313
|
|
314 cln = ngx_pool_cleanup_add(r->pool, 0);
|
|
315
|
|
316 if (cln == NULL) {
|
|
317 ngx_free(b->pos);
|
488
|
318 return ngx_http_filter_finalize_request(r, NULL,
|
392
|
319 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
320 }
|
|
321
|
394
|
322 if (r == r->main) {
|
|
323 r->headers_out.content_length_n = b->last - b->pos;
|
392
|
324
|
394
|
325 if (r->headers_out.content_length) {
|
|
326 r->headers_out.content_length->hash = 0;
|
|
327 r->headers_out.content_length = NULL;
|
|
328 }
|
392
|
329
|
394
|
330 ngx_http_clear_last_modified(r);
|
392
|
331 }
|
|
332
|
|
333 rc = ngx_http_next_header_filter(r);
|
|
334
|
|
335 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
336 ngx_free(b->pos);
|
|
337 return rc;
|
|
338 }
|
|
339
|
|
340 cln->handler = ngx_http_xslt_cleanup;
|
|
341 cln->data = b->pos;
|
|
342
|
|
343 out.buf = b;
|
|
344 out.next = NULL;
|
|
345
|
|
346 return ngx_http_next_body_filter(r, &out);
|
|
347 }
|
|
348
|
|
349
|
|
350 static ngx_int_t
|
|
351 ngx_http_xslt_add_chunk(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx,
|
|
352 ngx_buf_t *b)
|
|
353 {
|
586
|
354 int err;
|
|
355 xmlParserCtxtPtr ctxt;
|
392
|
356
|
|
357 if (ctx->ctxt == NULL) {
|
|
358
|
|
359 ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
|
|
360 if (ctxt == NULL) {
|
|
361 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
362 "xmlCreatePushParserCtxt() failed");
|
|
363 return NGX_ERROR;
|
|
364 }
|
668
|
365 xmlCtxtUseOptions(ctxt, XML_PARSE_NOENT|XML_PARSE_DTDLOAD
|
|
366 |XML_PARSE_NOWARNING);
|
392
|
367
|
586
|
368 ctxt->sax->externalSubset = ngx_http_xslt_sax_external_subset;
|
|
369 ctxt->sax->setDocumentLocator = NULL;
|
|
370 ctxt->sax->error = ngx_http_xslt_sax_error;
|
|
371 ctxt->sax->fatalError = ngx_http_xslt_sax_error;
|
|
372 ctxt->sax->_private = ctx;
|
392
|
373
|
|
374 ctx->ctxt = ctxt;
|
|
375 ctx->request = r;
|
|
376 }
|
|
377
|
496
|
378 err = xmlParseChunk(ctx->ctxt, (char *) b->pos, (int) (b->last - b->pos),
|
|
379 (b->last_buf) || (b->last_in_chain));
|
392
|
380
|
|
381 if (err == 0) {
|
|
382 b->pos = b->last;
|
|
383 return NGX_OK;
|
|
384 }
|
|
385
|
|
386 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
387 "xmlParseChunk() failed, error:%d", err);
|
|
388
|
|
389 return NGX_ERROR;
|
|
390 }
|
|
391
|
|
392
|
|
393 static void
|
|
394 ngx_http_xslt_sax_external_subset(void *data, const xmlChar *name,
|
|
395 const xmlChar *externalId, const xmlChar *systemId)
|
|
396 {
|
586
|
397 xmlParserCtxtPtr ctxt = data;
|
392
|
398
|
394
|
399 xmlDocPtr doc;
|
|
400 xmlDtdPtr dtd;
|
|
401 ngx_http_request_t *r;
|
586
|
402 ngx_http_xslt_filter_ctx_t *ctx;
|
394
|
403 ngx_http_xslt_filter_loc_conf_t *conf;
|
392
|
404
|
586
|
405 ctx = ctxt->sax->_private;
|
392
|
406 r = ctx->request;
|
|
407
|
|
408 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module);
|
|
409
|
|
410 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
411 "xslt filter extSubset: \"%s\" \"%s\" \"%s\"",
|
|
412 name ? name : (xmlChar *) "",
|
|
413 externalId ? externalId : (xmlChar *) "",
|
|
414 systemId ? systemId : (xmlChar *) "");
|
|
415
|
586
|
416 doc = ctxt->myDoc;
|
392
|
417
|
|
418 #if (NGX_HTTP_XSLT_REUSE_DTD)
|
|
419
|
|
420 dtd = conf->dtd;
|
|
421
|
|
422 #else
|
|
423
|
|
424 dtd = xmlCopyDtd(conf->dtd);
|
|
425 if (dtd == NULL) {
|
|
426 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
427 "xmlCopyDtd() failed");
|
|
428 return;
|
|
429 }
|
|
430
|
|
431 if (doc->children == NULL) {
|
|
432 xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd);
|
|
433
|
|
434 } else {
|
|
435 xmlAddPrevSibling(doc->children, (xmlNodePtr) dtd);
|
|
436 }
|
|
437
|
|
438 #endif
|
|
439
|
|
440 doc->extSubset = dtd;
|
|
441 }
|
|
442
|
|
443
|
|
444 static void ngx_cdecl
|
|
445 ngx_http_xslt_sax_error(void *data, const char *msg, ...)
|
|
446 {
|
586
|
447 xmlParserCtxtPtr ctxt = data;
|
392
|
448
|
586
|
449 size_t n;
|
|
450 va_list args;
|
|
451 ngx_http_xslt_filter_ctx_t *ctx;
|
|
452 u_char buf[NGX_MAX_ERROR_STR];
|
|
453
|
|
454 ctx = ctxt->sax->_private;
|
392
|
455
|
|
456 buf[0] = '\0';
|
|
457
|
|
458 va_start(args, msg);
|
|
459 n = (size_t) vsnprintf((char *) buf, NGX_MAX_ERROR_STR, msg, args);
|
|
460 va_end(args);
|
|
461
|
|
462 while (--n && (buf[n] == CR || buf[n] == LF)) { /* void */ }
|
|
463
|
|
464 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
|
510
|
465 "libxml2 error: \"%*s\"", n + 1, buf);
|
392
|
466 }
|
|
467
|
|
468
|
|
469 static ngx_buf_t *
|
|
470 ngx_http_xslt_apply_stylesheet(ngx_http_request_t *r,
|
|
471 ngx_http_xslt_filter_ctx_t *ctx)
|
|
472 {
|
394
|
473 int len, rc, doc_type;
|
|
474 u_char *type, *encoding;
|
|
475 ngx_buf_t *b;
|
|
476 ngx_uint_t i;
|
|
477 xmlChar *buf;
|
|
478 xmlDocPtr doc, res;
|
|
479 ngx_http_xslt_sheet_t *sheet;
|
|
480 ngx_http_xslt_filter_loc_conf_t *conf;
|
392
|
481
|
|
482 conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module);
|
|
483 sheet = conf->sheets.elts;
|
|
484 doc = ctx->doc;
|
|
485
|
|
486 /* preallocate array for 4 params */
|
|
487
|
|
488 if (ngx_array_init(&ctx->params, r->pool, 4 * 2 + 1, sizeof(char *))
|
|
489 != NGX_OK)
|
|
490 {
|
|
491 xmlFreeDoc(doc);
|
|
492 return NULL;
|
|
493 }
|
|
494
|
|
495 for (i = 0; i < conf->sheets.nelts; i++) {
|
|
496
|
668
|
497 ctx->transform = xsltNewTransformContext(sheet[i].stylesheet, doc);
|
|
498 if (ctx->transform == NULL) {
|
392
|
499 xmlFreeDoc(doc);
|
|
500 return NULL;
|
|
501 }
|
|
502
|
668
|
503 if (conf->params
|
|
504 && ngx_http_xslt_params(r, ctx, conf->params, 0) != NGX_OK)
|
|
505 {
|
|
506 xsltFreeTransformContext(ctx->transform);
|
|
507 xmlFreeDoc(doc);
|
|
508 return NULL;
|
|
509 }
|
392
|
510
|
668
|
511 if (ngx_http_xslt_params(r, ctx, &sheet[i].params, 1) != NGX_OK) {
|
|
512 xsltFreeTransformContext(ctx->transform);
|
|
513 xmlFreeDoc(doc);
|
|
514 return NULL;
|
|
515 }
|
|
516
|
|
517 res = xsltApplyStylesheetUser(sheet[i].stylesheet, doc,
|
|
518 ctx->params.elts, NULL, NULL,
|
|
519 ctx->transform);
|
|
520
|
|
521 xsltFreeTransformContext(ctx->transform);
|
392
|
522 xmlFreeDoc(doc);
|
|
523
|
|
524 if (res == NULL) {
|
|
525 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
526 "xsltApplyStylesheet() failed");
|
|
527 return NULL;
|
|
528 }
|
|
529
|
|
530 doc = res;
|
|
531
|
|
532 /* reset array elements */
|
|
533 ctx->params.nelts = 0;
|
|
534 }
|
|
535
|
394
|
536 /* there must be at least one stylesheet */
|
|
537
|
|
538 if (r == r->main) {
|
|
539 type = ngx_http_xslt_content_type(sheet[i - 1].stylesheet);
|
392
|
540
|
394
|
541 } else {
|
|
542 type = NULL;
|
|
543 }
|
|
544
|
|
545 encoding = ngx_http_xslt_encoding(sheet[i - 1].stylesheet);
|
|
546 doc_type = doc->type;
|
|
547
|
|
548 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
549 "xslt filter type: %d t:%s e:%s",
|
|
550 doc_type, type ? type : (u_char *) "(null)",
|
|
551 encoding ? encoding : (u_char *) "(null)");
|
392
|
552
|
|
553 rc = xsltSaveResultToString(&buf, &len, doc, sheet[i - 1].stylesheet);
|
|
554
|
|
555 xmlFreeDoc(doc);
|
|
556
|
|
557 if (rc != 0) {
|
|
558 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
559 "xsltSaveResultToString() failed");
|
|
560 return NULL;
|
|
561 }
|
|
562
|
|
563 if (len == 0) {
|
|
564 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
565 "xsltSaveResultToString() returned zero-length result");
|
|
566 return NULL;
|
|
567 }
|
|
568
|
|
569 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
570 if (b == NULL) {
|
|
571 ngx_free(buf);
|
|
572 return NULL;
|
|
573 }
|
|
574
|
|
575 b->pos = buf;
|
|
576 b->last = buf + len;
|
|
577 b->memory = 1;
|
|
578
|
394
|
579 if (encoding) {
|
|
580 r->headers_out.charset.len = ngx_strlen(encoding);
|
|
581 r->headers_out.charset.data = encoding;
|
|
582 }
|
|
583
|
|
584 if (r != r->main) {
|
|
585 return b;
|
|
586 }
|
|
587
|
496
|
588 b->last_buf = 1;
|
|
589
|
394
|
590 if (type) {
|
|
591 len = ngx_strlen(type);
|
|
592
|
|
593 r->headers_out.content_type_len = len;
|
|
594 r->headers_out.content_type.len = len;
|
|
595 r->headers_out.content_type.data = type;
|
|
596
|
|
597 } else if (doc_type == XML_HTML_DOCUMENT_NODE) {
|
|
598
|
|
599 r->headers_out.content_type_len = sizeof("text/html") - 1;
|
570
|
600 ngx_str_set(&r->headers_out.content_type, "text/html");
|
394
|
601 }
|
|
602
|
494
|
603 r->headers_out.content_type_lowcase = NULL;
|
|
604
|
392
|
605 return b;
|
|
606 }
|
|
607
|
|
608
|
|
609 static ngx_int_t
|
|
610 ngx_http_xslt_params(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx,
|
668
|
611 ngx_array_t *params, ngx_uint_t final)
|
392
|
612 {
|
668
|
613 u_char *p, *last, *value, *dst, *src, **s;
|
|
614 size_t len;
|
|
615 ngx_uint_t i;
|
|
616 ngx_str_t string;
|
|
617 ngx_http_xslt_param_t *param;
|
392
|
618
|
|
619 param = params->elts;
|
|
620
|
|
621 for (i = 0; i < params->nelts; i++) {
|
|
622
|
668
|
623 if (ngx_http_complex_value(r, ¶m[i].value, &string) != NGX_OK) {
|
392
|
624 return NGX_ERROR;
|
|
625 }
|
|
626
|
|
627 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
628 "xslt filter param: \"%s\"", string.data);
|
|
629
|
668
|
630 if (param[i].name) {
|
|
631
|
|
632 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
633 "xslt filter param name: \"%s\"", param[i].name);
|
|
634
|
|
635 if (param[i].quote) {
|
|
636 if (xsltQuoteOneUserParam(ctx->transform, param[i].name,
|
|
637 string.data)
|
|
638 != 0)
|
|
639 {
|
|
640 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
641 "xsltQuoteOneUserParam(\"%s\", \"%s\") failed",
|
|
642 param[i].name, string.data);
|
|
643 return NGX_ERROR;
|
|
644 }
|
|
645
|
|
646 continue;
|
|
647 }
|
|
648
|
|
649 s = ngx_array_push(&ctx->params);
|
|
650 if (s == NULL) {
|
|
651 return NGX_ERROR;
|
|
652 }
|
|
653
|
|
654 *s = param[i].name;
|
|
655
|
|
656 s = ngx_array_push(&ctx->params);
|
|
657 if (s == NULL) {
|
|
658 return NGX_ERROR;
|
|
659 }
|
|
660
|
|
661 *s = string.data;
|
|
662
|
|
663 continue;
|
|
664 }
|
|
665
|
|
666 /*
|
|
667 * parse param1=value1:param2=value2 syntax as used by parameters
|
|
668 * specified in xslt_stylesheet directives
|
|
669 */
|
|
670
|
392
|
671 p = string.data;
|
668
|
672 last = string.data + string.len;
|
392
|
673
|
|
674 while (p && *p) {
|
|
675
|
|
676 value = p;
|
|
677 p = (u_char *) ngx_strchr(p, '=');
|
|
678 if (p == NULL) {
|
|
679 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
680 "invalid libxslt parameter \"%s\"", value);
|
|
681 return NGX_ERROR;
|
|
682 }
|
|
683 *p++ = '\0';
|
|
684
|
|
685 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
686 "xslt filter param name: \"%s\"", value);
|
|
687
|
|
688 s = ngx_array_push(&ctx->params);
|
|
689 if (s == NULL) {
|
|
690 return NGX_ERROR;
|
|
691 }
|
|
692
|
|
693 *s = value;
|
|
694
|
|
695 value = p;
|
|
696 p = (u_char *) ngx_strchr(p, ':');
|
|
697
|
|
698 if (p) {
|
|
699 len = p - value;
|
|
700 *p++ = '\0';
|
|
701
|
|
702 } else {
|
|
703 len = last - value;
|
|
704 }
|
|
705
|
|
706 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
707 "xslt filter param value: \"%s\"", value);
|
|
708
|
|
709 dst = value;
|
|
710 src = value;
|
|
711
|
|
712 ngx_unescape_uri(&dst, &src, len, 0);
|
|
713
|
|
714 *dst = '\0';
|
|
715
|
|
716 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
717 "xslt filter param unescaped: \"%s\"", value);
|
|
718
|
|
719 s = ngx_array_push(&ctx->params);
|
|
720 if (s == NULL) {
|
|
721 return NGX_ERROR;
|
|
722 }
|
|
723
|
|
724 *s = value;
|
|
725 }
|
|
726 }
|
|
727
|
668
|
728 if (final) {
|
|
729 s = ngx_array_push(&ctx->params);
|
|
730 if (s == NULL) {
|
|
731 return NGX_ERROR;
|
|
732 }
|
|
733
|
|
734 *s = NULL;
|
392
|
735 }
|
|
736
|
|
737 return NGX_OK;
|
|
738 }
|
|
739
|
|
740
|
394
|
741 static u_char *
|
|
742 ngx_http_xslt_content_type(xsltStylesheetPtr s)
|
|
743 {
|
|
744 u_char *type;
|
|
745
|
|
746 if (s->mediaType) {
|
|
747 return s->mediaType;
|
|
748 }
|
|
749
|
|
750 for (s = s->imports; s; s = s->next) {
|
|
751
|
|
752 type = ngx_http_xslt_content_type(s);
|
|
753
|
|
754 if (type) {
|
|
755 return type;
|
|
756 }
|
|
757 }
|
|
758
|
|
759 return NULL;
|
|
760 }
|
|
761
|
|
762
|
|
763 static u_char *
|
|
764 ngx_http_xslt_encoding(xsltStylesheetPtr s)
|
|
765 {
|
|
766 u_char *encoding;
|
|
767
|
|
768 if (s->encoding) {
|
|
769 return s->encoding;
|
|
770 }
|
|
771
|
|
772 for (s = s->imports; s; s = s->next) {
|
|
773
|
|
774 encoding = ngx_http_xslt_encoding(s);
|
|
775
|
|
776 if (encoding) {
|
|
777 return encoding;
|
|
778 }
|
|
779 }
|
|
780
|
|
781 return NULL;
|
|
782 }
|
|
783
|
|
784
|
392
|
785 static void
|
|
786 ngx_http_xslt_cleanup(void *data)
|
|
787 {
|
|
788 ngx_free(data);
|
|
789 }
|
|
790
|
|
791
|
|
792 static char *
|
|
793 ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
794 {
|
394
|
795 ngx_http_xslt_filter_loc_conf_t *xlcf = conf;
|
392
|
796
|
394
|
797 ngx_str_t *value;
|
|
798 ngx_uint_t i;
|
|
799 ngx_pool_cleanup_t *cln;
|
|
800 ngx_http_xslt_file_t *file;
|
|
801 ngx_http_xslt_filter_main_conf_t *xmcf;
|
392
|
802
|
|
803 if (xlcf->dtd) {
|
|
804 return "is duplicate";
|
|
805 }
|
|
806
|
|
807 value = cf->args->elts;
|
|
808
|
394
|
809 xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xslt_filter_module);
|
|
810
|
|
811 file = xmcf->dtd_files.elts;
|
|
812 for (i = 0; i < xmcf->dtd_files.nelts; i++) {
|
676
|
813 if (ngx_strcmp(file[i].name, value[1].data) == 0) {
|
394
|
814 xlcf->dtd = file[i].data;
|
|
815 return NGX_CONF_OK;
|
|
816 }
|
|
817 }
|
|
818
|
|
819 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
820 if (cln == NULL) {
|
|
821 return NGX_CONF_ERROR;
|
|
822 }
|
|
823
|
392
|
824 xlcf->dtd = xmlParseDTD(NULL, (xmlChar *) value[1].data);
|
|
825
|
|
826 if (xlcf->dtd == NULL) {
|
|
827 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "xmlParseDTD() failed");
|
|
828 return NGX_CONF_ERROR;
|
|
829 }
|
|
830
|
394
|
831 cln->handler = ngx_http_xslt_cleanup_dtd;
|
|
832 cln->data = xlcf->dtd;
|
|
833
|
|
834 file = ngx_array_push(&xmcf->dtd_files);
|
|
835 if (file == NULL) {
|
|
836 return NGX_CONF_ERROR;
|
|
837 }
|
|
838
|
|
839 file->name = value[1].data;
|
|
840 file->data = xlcf->dtd;
|
|
841
|
392
|
842 return NGX_CONF_OK;
|
|
843 }
|
|
844
|
|
845
|
|
846
|
|
847 static char *
|
|
848 ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
849 {
|
394
|
850 ngx_http_xslt_filter_loc_conf_t *xlcf = conf;
|
392
|
851
|
394
|
852 ngx_str_t *value;
|
|
853 ngx_uint_t i, n;
|
|
854 ngx_pool_cleanup_t *cln;
|
|
855 ngx_http_xslt_file_t *file;
|
|
856 ngx_http_xslt_sheet_t *sheet;
|
668
|
857 ngx_http_xslt_param_t *param;
|
464
|
858 ngx_http_compile_complex_value_t ccv;
|
394
|
859 ngx_http_xslt_filter_main_conf_t *xmcf;
|
392
|
860
|
|
861 value = cf->args->elts;
|
|
862
|
|
863 if (xlcf->sheets.elts == NULL) {
|
|
864 if (ngx_array_init(&xlcf->sheets, cf->pool, 1,
|
|
865 sizeof(ngx_http_xslt_sheet_t))
|
|
866 != NGX_OK)
|
|
867 {
|
|
868 return NGX_CONF_ERROR;
|
|
869 }
|
|
870 }
|
|
871
|
|
872 sheet = ngx_array_push(&xlcf->sheets);
|
|
873 if (sheet == NULL) {
|
|
874 return NGX_CONF_ERROR;
|
|
875 }
|
|
876
|
|
877 ngx_memzero(sheet, sizeof(ngx_http_xslt_sheet_t));
|
|
878
|
|
879 if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
|
|
880 return NGX_CONF_ERROR;
|
|
881 }
|
|
882
|
394
|
883 xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xslt_filter_module);
|
|
884
|
|
885 file = xmcf->sheet_files.elts;
|
|
886 for (i = 0; i < xmcf->sheet_files.nelts; i++) {
|
676
|
887 if (ngx_strcmp(file[i].name, value[1].data) == 0) {
|
394
|
888 sheet->stylesheet = file[i].data;
|
|
889 goto found;
|
|
890 }
|
|
891 }
|
|
892
|
392
|
893 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
894 if (cln == NULL) {
|
|
895 return NGX_CONF_ERROR;
|
|
896 }
|
|
897
|
|
898 sheet->stylesheet = xsltParseStylesheetFile(value[1].data);
|
|
899 if (sheet->stylesheet == NULL) {
|
|
900 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
901 "xsltParseStylesheetFile(\"%s\") failed",
|
|
902 value[1].data);
|
|
903 return NGX_CONF_ERROR;
|
|
904 }
|
|
905
|
|
906 cln->handler = ngx_http_xslt_cleanup_stylesheet;
|
|
907 cln->data = sheet->stylesheet;
|
|
908
|
394
|
909 file = ngx_array_push(&xmcf->sheet_files);
|
|
910 if (file == NULL) {
|
|
911 return NGX_CONF_ERROR;
|
|
912 }
|
|
913
|
|
914 file->name = value[1].data;
|
|
915 file->data = sheet->stylesheet;
|
|
916
|
|
917 found:
|
|
918
|
392
|
919 n = cf->args->nelts;
|
|
920
|
|
921 if (n == 2) {
|
|
922 return NGX_CONF_OK;
|
|
923 }
|
|
924
|
|
925 if (ngx_array_init(&sheet->params, cf->pool, n - 2,
|
668
|
926 sizeof(ngx_http_xslt_param_t))
|
392
|
927 != NGX_OK)
|
|
928 {
|
|
929 return NGX_CONF_ERROR;
|
|
930 }
|
|
931
|
|
932 for (i = 2; i < n; i++) {
|
|
933
|
|
934 param = ngx_array_push(&sheet->params);
|
|
935 if (param == NULL) {
|
|
936 return NGX_CONF_ERROR;
|
|
937 }
|
|
938
|
668
|
939 ngx_memzero(param, sizeof(ngx_http_xslt_param_t));
|
464
|
940 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
392
|
941
|
464
|
942 ccv.cf = cf;
|
|
943 ccv.value = &value[i];
|
668
|
944 ccv.complex_value = ¶m->value;
|
464
|
945 ccv.zero = 1;
|
392
|
946
|
464
|
947 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
392
|
948 return NGX_CONF_ERROR;
|
|
949 }
|
|
950 }
|
|
951
|
|
952 return NGX_CONF_OK;
|
|
953 }
|
|
954
|
|
955
|
668
|
956 static char *
|
|
957 ngx_http_xslt_param(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
958 {
|
|
959 ngx_http_xslt_filter_loc_conf_t *xlcf = conf;
|
|
960
|
|
961 ngx_http_xslt_param_t *param;
|
|
962 ngx_http_compile_complex_value_t ccv;
|
|
963 ngx_str_t *value;
|
|
964
|
|
965 value = cf->args->elts;
|
|
966
|
|
967 if (xlcf->params == NULL) {
|
|
968 xlcf->params = ngx_array_create(cf->pool, 2,
|
|
969 sizeof(ngx_http_xslt_param_t));
|
|
970 if (xlcf->params == NULL) {
|
|
971 return NGX_CONF_ERROR;
|
|
972 }
|
|
973 }
|
|
974
|
|
975 param = ngx_array_push(xlcf->params);
|
|
976 if (param == NULL) {
|
|
977 return NGX_CONF_ERROR;
|
|
978 }
|
|
979
|
|
980 param->name = value[1].data;
|
|
981 param->quote = (cmd->post == NULL) ? 0 : 1;
|
|
982
|
|
983 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
984
|
|
985 ccv.cf = cf;
|
|
986 ccv.value = &value[2];
|
|
987 ccv.complex_value = ¶m->value;
|
|
988 ccv.zero = 1;
|
|
989
|
|
990 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
991 return NGX_CONF_ERROR;
|
|
992 }
|
|
993
|
|
994 return NGX_CONF_OK;
|
|
995 }
|
|
996
|
|
997
|
392
|
998 static void
|
394
|
999 ngx_http_xslt_cleanup_dtd(void *data)
|
|
1000 {
|
|
1001 xmlFreeDtd(data);
|
|
1002 }
|
|
1003
|
|
1004
|
|
1005 static void
|
392
|
1006 ngx_http_xslt_cleanup_stylesheet(void *data)
|
|
1007 {
|
394
|
1008 xsltFreeStylesheet(data);
|
392
|
1009 }
|
|
1010
|
|
1011
|
394
|
1012 static void *
|
|
1013 ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf)
|
|
1014 {
|
|
1015 ngx_http_xslt_filter_main_conf_t *conf;
|
|
1016
|
|
1017 conf = ngx_palloc(cf->pool, sizeof(ngx_http_xslt_filter_main_conf_t));
|
|
1018 if (conf == NULL) {
|
496
|
1019 return NULL;
|
394
|
1020 }
|
|
1021
|
|
1022 if (ngx_array_init(&conf->dtd_files, cf->pool, 1,
|
|
1023 sizeof(ngx_http_xslt_file_t))
|
|
1024 != NGX_OK)
|
|
1025 {
|
|
1026 return NULL;
|
|
1027 }
|
|
1028
|
|
1029 if (ngx_array_init(&conf->sheet_files, cf->pool, 1,
|
|
1030 sizeof(ngx_http_xslt_file_t))
|
|
1031 != NGX_OK)
|
|
1032 {
|
|
1033 return NULL;
|
|
1034 }
|
|
1035
|
|
1036 return conf;
|
|
1037 }
|
|
1038
|
392
|
1039
|
|
1040 static void *
|
|
1041 ngx_http_xslt_filter_create_conf(ngx_conf_t *cf)
|
|
1042 {
|
394
|
1043 ngx_http_xslt_filter_loc_conf_t *conf;
|
392
|
1044
|
394
|
1045 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_xslt_filter_loc_conf_t));
|
392
|
1046 if (conf == NULL) {
|
496
|
1047 return NULL;
|
392
|
1048 }
|
|
1049
|
|
1050 /*
|
|
1051 * set by ngx_pcalloc():
|
|
1052 *
|
394
|
1053 * conf->dtd = NULL;
|
|
1054 * conf->sheets = { NULL };
|
|
1055 * conf->types = { NULL };
|
|
1056 * conf->types_keys = NULL;
|
668
|
1057 * conf->params = NULL;
|
392
|
1058 */
|
|
1059
|
|
1060 return conf;
|
|
1061 }
|
|
1062
|
|
1063
|
|
1064 static char *
|
|
1065 ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
1066 {
|
394
|
1067 ngx_http_xslt_filter_loc_conf_t *prev = parent;
|
|
1068 ngx_http_xslt_filter_loc_conf_t *conf = child;
|
392
|
1069
|
|
1070 if (conf->dtd == NULL) {
|
|
1071 conf->dtd = prev->dtd;
|
|
1072 }
|
|
1073
|
|
1074 if (conf->sheets.nelts == 0) {
|
|
1075 conf->sheets = prev->sheets;
|
|
1076 }
|
|
1077
|
668
|
1078 if (conf->params == NULL) {
|
|
1079 conf->params = prev->params;
|
|
1080 }
|
|
1081
|
554
|
1082 if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
|
|
1083 &prev->types_keys, &prev->types,
|
394
|
1084 ngx_http_xslt_default_types)
|
392
|
1085 != NGX_OK)
|
|
1086 {
|
|
1087 return NGX_CONF_ERROR;
|
|
1088 }
|
|
1089
|
|
1090 return NGX_CONF_OK;
|
|
1091 }
|
|
1092
|
|
1093
|
|
1094 static ngx_int_t
|
|
1095 ngx_http_xslt_filter_init(ngx_conf_t *cf)
|
|
1096 {
|
|
1097 xmlInitParser();
|
|
1098
|
418
|
1099 #if (NGX_HAVE_EXSLT)
|
|
1100 exsltRegisterAll();
|
|
1101 #endif
|
|
1102
|
392
|
1103 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
1104 ngx_http_top_header_filter = ngx_http_xslt_header_filter;
|
|
1105
|
|
1106 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
1107 ngx_http_top_body_filter = ngx_http_xslt_body_filter;
|
|
1108
|
|
1109 return NGX_OK;
|
|
1110 }
|
|
1111
|
|
1112
|
|
1113 static void
|
|
1114 ngx_http_xslt_filter_exit(ngx_cycle_t *cycle)
|
|
1115 {
|
|
1116 xsltCleanupGlobals();
|
|
1117 xmlCleanupParser();
|
|
1118 }
|