fix: non-ascii characters

When doing a request with the node request module with an URL
containing UTF-8 chars, some webservers will fail to find the file.
Therefore encodeURI on the url before requesting it.
Not sure if this is the best way to test it, but it works with all test cases.

Example of page which doesn't inline properly:
http://hotpoticeland.com/holsgerdislaug/
which links to the image:
http://hotpoticeland.com/wp-content/uploads/Hólsgerðislaug-300x225.jpg
This commit is contained in:
Mikael Finstad 2016-06-25 22:27:09 +02:00 committed by Remy Sharp
parent 6a1dec4f4c
commit a2869fc81a
5 changed files with 27 additions and 4 deletions

View File

@ -65,7 +65,7 @@ module.exports = function get(url, options) {
debug('request %s', url, settings);
cache[url] = new Promise(function (resolve, reject) {
request(url, settings, function response(error, res, body) {
request(encodeURI(url), settings, function response(error, res, body) {
if (error) {
debug('request failed: %s', error.message);
return reject(error);

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 B

View File

@ -0,0 +1 @@
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>inline image with special characters</title> </head> <body> <img src="data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs="> </body> </html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>inline image with special characters</title>
</head>
<body>
<img src="http://localhost:54321/Hólsgerðislaug-300x225.gif">
</body>
</html>

View File

@ -8,9 +8,17 @@ var st = require('st');
var server;
test('setup mock server', function (t) {
server = http.createServer(
st(path.resolve(__dirname, 'fixtures'))
).listen(54321);
server = http.createServer(function(req, res) {
if (isASCII(req.url)) {
st(path.resolve(__dirname, 'fixtures'))(req, res);
}
else {
// Fail because all non-ascii chars should be urlencoded
// (some http servers don't handle non-ascii)
res.statusCode = 404;
res.end();
}
}).listen(54321);
server.on('listening', function listening() {
t.pass('mock server ready');
@ -120,3 +128,7 @@ test('tear down', function (t) {
t.pass('tear down complete');
t.end();
});
function isASCII(str) {
return /^[\x00-\x7F]*$/.test(str);
}