This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
nginx-xslt-html-parser.patch
201 lines (169 loc) · 6.77 KB
/
nginx-xslt-html-parser.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
diff --git a/src/http/modules/ngx_http_xslt_filter_module.c b/src/http/modules/ngx_http_xslt_filter_module.c
index 9e85693..bdffef4 100644
--- a/src/http/modules/ngx_http_xslt_filter_module.c
+++ b/src/http/modules/ngx_http_xslt_filter_module.c
@@ -10,6 +10,7 @@
#include <ngx_http.h>
#include <libxml/parser.h>
+#include <libxml/HTMLparser.h>
#include <libxml/tree.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
@@ -59,6 +60,7 @@ typedef struct {
ngx_array_t *types_keys;
ngx_array_t *params; /* ngx_http_xslt_param_t */
ngx_flag_t last_modified;
+ ngx_flag_t html_parser;
} ngx_http_xslt_filter_loc_conf_t;
@@ -68,6 +70,7 @@ typedef struct {
xsltTransformContextPtr transform;
ngx_http_request_t *request;
ngx_array_t params;
+ ngx_flag_t html_parser;
ngx_uint_t done; /* unsigned done:1; */
} ngx_http_xslt_filter_ctx_t;
@@ -159,6 +162,13 @@ static ngx_command_t ngx_http_xslt_filter_commands[] = {
offsetof(ngx_http_xslt_filter_loc_conf_t, last_modified),
NULL },
+ { ngx_string("xslt_html_parser"),
+ NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_flag_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_xslt_filter_loc_conf_t, html_parser),
+ NULL },
+
ngx_null_command
};
@@ -234,6 +244,8 @@ ngx_http_xslt_header_filter(ngx_http_request_t *r)
r->main_filter_need_in_memory = 1;
+ ctx->html_parser = conf->html_parser;
+
return NGX_OK;
}
@@ -270,24 +282,39 @@ ngx_http_xslt_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
xmlFreeDoc(ctx->ctxt->myDoc);
}
- xmlFreeParserCtxt(ctx->ctxt);
+ if (ctx->html_parser) {
+ htmlFreeParserCtxt(ctx->ctxt);
+ } else {
+ xmlFreeParserCtxt(ctx->ctxt);
+ }
return ngx_http_xslt_send(r, ctx, NULL);
}
if (cl->buf->last_buf || cl->buf->last_in_chain) {
+ if (ctx->ctxt == NULL) {
+ /* empty body */
+ return ngx_http_next_header_filter(r);
+ }
+
ctx->doc = ctx->ctxt->myDoc;
#if (NGX_HTTP_XSLT_REUSE_DTD)
- ctx->doc->extSubset = NULL;
+ if (ctx->doc) {
+ ctx->doc->extSubset = NULL;
+ }
#endif
wellFormed = ctx->ctxt->wellFormed;
- xmlFreeParserCtxt(ctx->ctxt);
+ if (ctx->html_parser) {
+ htmlFreeParserCtxt(ctx->ctxt);
+ } else {
+ xmlFreeParserCtxt(ctx->ctxt);
+ }
- if (wellFormed) {
+ if (wellFormed || ctx->html_parser) {
return ngx_http_xslt_send(r, ctx,
ngx_http_xslt_apply_stylesheet(r, ctx));
}
@@ -368,22 +395,51 @@ ngx_http_xslt_add_chunk(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx,
ngx_buf_t *b)
{
int err;
- xmlParserCtxtPtr ctxt;
+ xmlParserCtxtPtr ctxt = NULL;
+ xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
if (ctx->ctxt == NULL) {
- ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
- if (ctxt == NULL) {
- ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
- "xmlCreatePushParserCtxt() failed");
- return NGX_ERROR;
+ if (b->last == b->pos) {
+ return NGX_OK;
}
- xmlCtxtUseOptions(ctxt, XML_PARSE_NOENT|XML_PARSE_DTDLOAD
- |XML_PARSE_NOWARNING);
- ctxt->sax->externalSubset = ngx_http_xslt_sax_external_subset;
+ if (ctx->html_parser) {
+ if (r->headers_out.charset.len) {
+ enc = xmlParseCharEncoding(
+ (const char *) r->headers_out.charset.data);
+ if (enc == XML_CHAR_ENCODING_ERROR) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "xmlParseCharEncoding() failed charset: %s",
+ r->headers_out.charset.data);
+ return NGX_ERROR;
+ }
+ }
+
+ ctxt = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, enc);
+ if (ctxt == NULL) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "htmlCreatePushParserCtxt() failed");
+ return NGX_ERROR;
+ }
+
+ htmlCtxtUseOptions(ctxt, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR
+ |HTML_PARSE_NOWARNING|HTML_PARSE_COMPACT);
+
+ } else {
+ ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
+ if (ctxt == NULL) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "xmlCreatePushParserCtxt() failed");
+ return NGX_ERROR;
+ }
+ xmlCtxtUseOptions(ctxt, XML_PARSE_NOENT|XML_PARSE_DTDLOAD
+ |XML_PARSE_NOWARNING|XML_PARSE_COMPACT);
+
+ ctxt->sax->externalSubset = ngx_http_xslt_sax_external_subset;
+ ctxt->sax->error = ngx_http_xslt_sax_error;
+ }
ctxt->sax->setDocumentLocator = NULL;
- ctxt->sax->error = ngx_http_xslt_sax_error;
ctxt->sax->fatalError = ngx_http_xslt_sax_error;
ctxt->sax->_private = ctx;
@@ -391,10 +447,15 @@ ngx_http_xslt_add_chunk(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx,
ctx->request = r;
}
+ if (ctx->html_parser) {
+ err = htmlParseChunk(ctx->ctxt, (char *) b->pos, (int) (b->last - b->pos),
+ (b->last_buf) || (b->last_in_chain));
+ } else {
err = xmlParseChunk(ctx->ctxt, (char *) b->pos, (int) (b->last - b->pos),
(b->last_buf) || (b->last_in_chain));
+ }
- if (err == 0) {
+ if (ctx->done == 0) {
b->pos = b->last;
return NGX_OK;
}
@@ -479,6 +540,8 @@ ngx_http_xslt_sax_error(void *data, const char *msg, ...)
ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
"libxml2 error: \"%*s\"", n + 1, buf);
+
+ ctx->done = 1; /* stop further chunk parsing */
}
@@ -1075,6 +1138,8 @@ ngx_http_xslt_filter_create_conf(ngx_conf_t *cf)
conf->last_modified = NGX_CONF_UNSET;
+ conf->html_parser = NGX_CONF_UNSET;
+
return conf;
}
@@ -1107,6 +1172,8 @@ ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->last_modified, prev->last_modified, 0);
+ ngx_conf_merge_value(conf->html_parser, prev->html_parser, 0);
+
return NGX_CONF_OK;
}