Created
September 2, 2011 13:40
-
-
Save cadwallion/1188609 to your computer and use it in GitHub Desktop.
Refactored parseURL
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
exports.parseURL = function(url, opts) { | |
var options={}; | |
if(typeof opts=="function"){ | |
options.cb = opts; | |
} else { | |
options=opts; | |
} | |
request(url, function(error, response, body) { | |
if (!error) { | |
easyParser(options.cb,options).parseString(body); | |
} | |
}); | |
}; | |
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
exports.parseURL = function(url, opts) { | |
var options={}; | |
if(typeof opts=="function"){ | |
options.cb = opts; | |
} else { | |
options=opts; | |
} | |
get_rss(url); | |
function get_rss(url) { | |
var parts = parseURL(url); | |
// set the default port to 80 | |
if(!parts.port) { parts.port = 80; } | |
var redirection_level = 0; | |
var client = http.createClient(parts.port, parts.hostname); | |
// include search terms in pathname if present | |
var address = parts.pathname + (parts.search==undefined ? "" : parts.search); | |
var request = client.request('GET', address, {'host': parts.hostname}); | |
request.addListener('response', function (response) { | |
//sys.puts('STATUS: ' + response.statusCode); | |
//sys.puts('HEADERS: ' + JSON.stringify(response.headers)); | |
// check to see the type of status | |
switch(response.statusCode) { | |
// check for ALL OK | |
case 200: | |
var body = ''; | |
response.addListener('data', function (chunk) { body += chunk; }); | |
response.addListener('end', function() { | |
easyParser(options.cb,options).parseString(body); | |
}); | |
break; | |
// redirect status returned | |
case 301: | |
case 302: | |
if(redirection_level > 10) { | |
console.log("too many redirects"); | |
} else { | |
console.log("redirect to "+response.headers.location); | |
get_rss(response.headers.location); | |
} | |
break; | |
default: | |
/* | |
response.setEncoding('utf8'); | |
response.addListener('data', function (chunk) { | |
//sys.puts('BODY: ' + chunk); | |
}); | |
*/ | |
break; | |
} | |
}); | |
request.end(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment