Skip to content

Commit 4967c43

Browse files
manuel-di-ioriorbren
authored andcommitted
Follow redirects (rbren#23)
* browserify * 2.6.0 * Revert package.json, test/ and dist/ * Fix settings object for parseURL when passing an empty object
1 parent c1f59ec commit 4967c43

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ var decorateItunes = function decorateItunes(json, channel) {
164164
}
165165
json.feed.itunes.owner = owner;
166166
}
167-
167+
168168
PODCAST_TOP_FIELDS.forEach(function(f) {
169169
if (channel['itunes:' + f]) json.feed.itunes[f] = channel['itunes:' + f][0];
170170
});
@@ -197,7 +197,14 @@ Parser.parseString = function(xml, callback) {
197197
});
198198
}
199199

200-
Parser.parseURL = function(feedUrl, callback) {
200+
Parser.parseURL = function(feedUrl, settings, callback) {
201+
if (!callback) {
202+
callback = settings;
203+
settings = {};
204+
}
205+
settings.__redirectCount = settings.__redirectCount || 0;
206+
if (settings.maxRedirects === undefined) settings.maxRedirects = 1;
207+
201208
var xml = '';
202209
var get = feedUrl.indexOf('https') === 0 ? HTTPS.get : HTTP.get;
203210
var parsedUrl = url.parse(feedUrl);
@@ -207,7 +214,12 @@ Parser.parseURL = function(feedUrl, callback) {
207214
path: parsedUrl.path,
208215
headers: {'User-Agent': 'rss-parser'}
209216
}, function(res) {
210-
if (res.statusCode >= 300) return callback(new Error("Status code " + res.statusCode))
217+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers['location']) {
218+
if (settings.maxRedirects === 0) return callback(new Error("Status code " + res.statusCode));
219+
if (settings.__redirectCount === settings.maxRedirects) return callback(new Error("Too many redirects"));
220+
settings.__redirectCount++;
221+
return Parser.parseURL(res.headers['location'], settings, callback);
222+
}
211223
res.setEncoding('utf8');
212224
res.on('data', function(chunk) {
213225
xml += chunk;

0 commit comments

Comments
 (0)