Skip to content

Commit 6a5d192

Browse files
dsuketRichienb
dsuket
authored andcommitted
fix: Properly parse meta tag when parameters are reversed (#682)
1 parent 47a24a0 commit 6a5d192

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/body.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ function convertBody(buffer, headers) {
306306
// html4
307307
if (!res && str) {
308308
res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
309+
if (!res) {
310+
res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
311+
if (res) {
312+
res.pop(); // drop last quote
313+
}
314+
}
309315

310316
if (res) {
311317
res = /charset=(.*)/i.exec(res.pop());

test/server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ export default class TestServer {
181181
res.end(convert('<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><div>中文</div>', 'gb2312'));
182182
}
183183

184+
if (p === '/encoding/gb2312-reverse') {
185+
res.statusCode = 200;
186+
res.setHeader('Content-Type', 'text/html');
187+
res.end(convert('<meta content="text/html; charset=gb2312" http-equiv="Content-Type"><div>中文</div>', 'gb2312'));
188+
}
189+
184190
if (p === '/encoding/shift-jis') {
185191
res.statusCode = 200;
186192
res.setHeader('Content-Type', 'text/html; charset=Shift-JIS');

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,6 +2767,16 @@ describe('external encoding', () => {
27672767
});
27682768
});
27692769

2770+
it('should support encoding decode, html4 detect reverse http-equiv', function() {
2771+
const url = `${base}encoding/gb2312-reverse`;
2772+
return fetch(url).then(res => {
2773+
expect(res.status).to.equal(200);
2774+
return res.textConverted().then(result => {
2775+
expect(result).to.equal('<meta content="text/html; charset=gb2312" http-equiv="Content-Type"><div>中文</div>');
2776+
});
2777+
});
2778+
});
2779+
27702780
it('should default to utf8 encoding', function() {
27712781
const url = `${base}encoding/utf8`;
27722782
return fetch(url).then(res => {

0 commit comments

Comments
 (0)