Skip to content

Commit

Permalink
Handles redirects to relative path (#11)
Browse files Browse the repository at this point in the history
* Handles redirects to relative path

If url redirects to a relative url e.g. `/another_path` parse the url with previous protocol, host and port

* if no srcUrl.protocol, emit error
  • Loading branch information
ffflabs authored and bearjaws committed May 20, 2018
1 parent 7de439a commit 90b338b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/wget.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function download(src, output, options, _parentEvent, redirects) {
};
}
srcUrl = url.parse(src);
if(!srcUrl.protocol) {
downloader.emit('error', 'Cannot parse url protocol for '+src);
return;
}
srcUrl.protocol = cleanProtocol(srcUrl.protocol);

req = request({
Expand All @@ -50,11 +54,17 @@ function download(src, output, options, _parentEvent, redirects) {

// Handle 302 redirects
if(res.statusCode === 301 || res.statusCode === 302 || res.statusCode === 307) {

var new_location=res.headers.location;
if(res.headers.location.indexOf('/')===0) {
new_location = srcUrl.protocol+'://'+srcUrl.hostname+(srcUrl.port? ':'+srcUrl.port:'')+res.headers.location;
}

redirects++;
if(redirects >= 10) {
downloader.emit('error', 'Infinite redirect loop detected');
}
download(res.headers.location, output, options, downloader, redirects);
download(new_location, output, options, downloader, redirects);
}

if (res.statusCode === 200) {
Expand Down

0 comments on commit 90b338b

Please sign in to comment.