Skip to content

Commit 5a1b991

Browse files
authored
Fix malformed url issue, closes #251 (#252)
1 parent 2eddaae commit 5a1b991

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

lib/utils/index.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function isUrl (path) {
1919
}
2020

2121
function getUrl (currentUrl, path) {
22-
var pathObject = url.parse(path);
22+
const pathObject = url.parse(path);
2323
if (isUrl(path) && !pathObject.protocol) {
24-
var urlObject = url.parse(currentUrl);
24+
const urlObject = url.parse(currentUrl);
2525
pathObject.protocol = urlObject.protocol;
2626
path = url.format(pathObject);
2727
}
@@ -33,8 +33,8 @@ function getUnixPath (filepath) {
3333
}
3434

3535
function getRelativePath (path1, path2) {
36-
var dirname = path.dirname(path1);
37-
var relativePath = path.relative(dirname, path2);
36+
const dirname = path.dirname(path1);
37+
const relativePath = path.relative(dirname, path2);
3838
return getUnixPath(relativePath);
3939
}
4040

@@ -45,8 +45,12 @@ function getRelativePath (path1, path2) {
4545
* @returns {string} decoded pathname
4646
*/
4747
function getPathnameFromUrl (u) {
48-
var pathname = url.parse(u).pathname;
49-
return decodeURI(pathname);
48+
const pathname = url.parse(u).pathname;
49+
try {
50+
return decodeURI(pathname);
51+
} catch (e) {
52+
return pathname;
53+
}
5054
}
5155

5256
/**
@@ -96,8 +100,8 @@ function getFilenameExtension (filepath) {
96100

97101
function shortenFilename (filename) {
98102
if (filename.length >= MAX_FILENAME_LENGTH) {
99-
var shortFilename = filename.substring(0, 20) + getFilenameExtension(filename);
100-
logger.debug('shorten filename: ' + filename + ' -> ' + shortFilename);
103+
const shortFilename = filename.substring(0, 20) + getFilenameExtension(filename);
104+
logger.debug(`[utils] shorten filename: ${filename} -> ${shortFilename}`);
101105
return shortFilename;
102106
}
103107
return filename;
@@ -110,15 +114,19 @@ function waitAllFulfilled (promises) {
110114
}
111115

112116
function normalizeUrl (u, opts) {
113-
return normalize(u, extend({removeTrailingSlash: false}, opts));
117+
try {
118+
return normalize(u, extend({removeTrailingSlash: false}, opts));
119+
} catch (e) {
120+
return u;
121+
}
114122
}
115123

116124
function urlsEqual (url1, url2) {
117125
return normalizeUrl(url1) === normalizeUrl(url2);
118126
}
119127

120128
function isUriSchemaSupported (path) {
121-
var protocol = url.parse(path).protocol;
129+
const protocol = url.parse(path).protocol;
122130
return !protocol || protocol && isUrl(path);
123131
}
124132

@@ -127,7 +135,7 @@ function getTypeByMime (mimeType) {
127135
}
128136

129137
function getTypeByFilename (filename) {
130-
var ext = getFilenameExtension(filename);
138+
const ext = getFilenameExtension(filename);
131139
return typeByExt[ext];
132140
}
133141

test/unit/utils/utils-test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ describe('Utils', function () {
9191
it('should decode escaped chars', function () {
9292
utils.getFilepathFromUrl('https://example.co/logo/logo-mobile%20(1).svg?q=650').should.equal('logo/logo-mobile (1).svg');
9393
});
94+
it('should return path as is if url is malformed', () => {
95+
utils.getFilepathFromUrl('https://example.co/%%IMAGE%%/logo.png').should.equal('%%IMAGE%%/logo.png');
96+
});
9497
});
9598

9699
describe('#getHashFromUrl', function () {
@@ -225,5 +228,12 @@ describe('Utils', function () {
225228
it('should return false for /path and /path/', function() {
226229
should(utils.urlsEqual('http://example.com/path', 'http://example.com/path/')).be.eql(false);
227230
});
228-
})
231+
});
232+
233+
describe('#normalizeUrl', () => {
234+
it('should return original url if it is malformed', () => {
235+
const malformedUrl = 'http://example.com/%%IMAGEURL%%/bar1q2blitz.png';
236+
should(utils.normalizeUrl(malformedUrl)).be.eql(malformedUrl);
237+
});
238+
});
229239
});

0 commit comments

Comments
 (0)