[Pkg-javascript-commits] [pdf.js] 121/161: Handles HTTP redirects
David Prévot
taffit at moszumanska.debian.org
Sat Apr 19 14:16:37 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository pdf.js.
commit 1e1667c5920c0c20072e3e6de79dabd178158c76
Author: Yury Delendik <ydelendik at mozilla.com>
Date: Tue Mar 25 12:24:46 2014 -0500
Handles HTTP redirects
---
test/downloadutils.js | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/test/downloadutils.js b/test/downloadutils.js
index 0818969..483734e 100644
--- a/test/downloadutils.js
+++ b/test/downloadutils.js
@@ -24,10 +24,27 @@ var crypto = require('crypto');
var http = require('http');
var https = require('https');
-function downloadFile(file, url, callback) {
+function downloadFile(file, url, callback, redirects) {
var completed = false;
var protocol = /^https:\/\//.test(url) ? https : http;
protocol.get(url, function (response) {
+ if (response.statusCode === 301 || response.statusCode === 302 ||
+ response.statusCode === 307 || response.statusCode === 308) {
+ if (redirects > 10) {
+ callback('Too many redirects');
+ }
+ var redirectTo = response.headers.location;
+ redirectTo = require('url').resolve(url, redirectTo);
+ downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
+ return;
+ }
+ if (response.statusCode === 404 && url.indexOf('web.archive.org') < 0) {
+ // trying waybackmachine
+ var redirectTo = 'http://web.archive.org/web/' + url;
+ downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
+ return;
+ }
+
if (response.statusCode !== 200) {
if (!completed) {
completed = true;
@@ -52,6 +69,13 @@ function downloadFile(file, url, callback) {
});
}).on('error', function (err) {
if (!completed) {
+ if (typeof err === 'object' && err.errno === 'ENOTFOUND' &&
+ url.indexOf('web.archive.org') < 0) {
+ // trying waybackmachine
+ var redirectTo = 'http://web.archive.org/web/' + url;
+ downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
+ return;
+ }
completed = true;
callback(err);
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git
More information about the Pkg-javascript-commits
mailing list