Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly parse meta tag when parameters are reversed #682

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/body.js
Expand Up @@ -306,6 +306,12 @@ function convertBody(buffer, headers) {
// html4
if (!res && str) {
res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
if (!res) {
res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
if (res) {
res.pop(); // drop last quote
}
}

if (res) {
res = /charset=(.*)/i.exec(res.pop());
Expand Down
6 changes: 6 additions & 0 deletions test/server.js
Expand Up @@ -181,6 +181,12 @@ export default class TestServer {
res.end(convert('<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><div>中文</div>', 'gb2312'));
}

if (p === '/encoding/gb2312-reverse') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(convert('<meta content="text/html; charset=gb2312" http-equiv="Content-Type"><div>中文</div>', 'gb2312'));
}

if (p === '/encoding/shift-jis') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html; charset=Shift-JIS');
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Expand Up @@ -2767,6 +2767,16 @@ describe('external encoding', () => {
});
});

it('should support encoding decode, html4 detect reverse http-equiv', function() {
const url = `${base}encoding/gb2312-reverse`;
return fetch(url).then(res => {
expect(res.status).to.equal(200);
return res.textConverted().then(result => {
expect(result).to.equal('<meta content="text/html; charset=gb2312" http-equiv="Content-Type"><div>中文</div>');
});
});
});

it('should default to utf8 encoding', function() {
const url = `${base}encoding/utf8`;
return fetch(url).then(res => {
Expand Down