Created
May 12, 2010 23:39
-
-
Save simonw/399280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* For Node.js - resolve a chain of HTTP redirects | |
Uses getResponse() from http://gist.github.com/399276 | |
Example: resolveHttpRedirects('http://ow.ly/1Kn4j', function(url) { sys.puts(url) }); | |
*/ | |
function resolveHttpRedirects(url, callback, maxnum) { | |
maxnum = maxnum || 3; | |
var count = 0; | |
function next(url) { | |
getResponse(url, function(response) { | |
if (response.statusCode == 301 || response.statusCode == 302) { | |
if (count >= maxnum) { | |
callback(response.headers.location); | |
} else { | |
count += 1; | |
next(response.headers.location); | |
} | |
} else { | |
callback(url); | |
} | |
}) | |
} | |
next(url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment