Skip to content

Instantly share code, notes, and snippets.

@simonw
Created May 12, 2010 23:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/399280 to your computer and use it in GitHub Desktop.
Save simonw/399280 to your computer and use it in GitHub Desktop.
/* 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