Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 'use strict' var url = require('url') var isUrl = /^https?:/ function Redirect (request) { this.request = request this.maxRedirects = 10 this.redirectsFollowed = 0 } Redirect.prototype.onResponse = function (response) { var self = this var request = self.request if (response.statusCode < 300 || response.statusCode >= 400 || !('location' in response.headers)) { return false } var redirectTo = response.headers.location // ignore any potential response body. it cannot possibly be useful // to us at this point. // response.resume should be defined, but check anyway before calling. Workaround for browserify. if (response.resume) { response.resume() } if (self.redirectsFollowed >= self.maxRedirects) { request.emit('error', new Error('Exceeded maxRedirects. Probably stuck in a redirect loop ' + request.uri.href)) return false } self.redirectsFollowed += 1 if (!isUrl.test(redirectTo)) { redirectTo = url.resolve(request.uri.href, redirectTo) } var uriPrev = request.uri request.uri = url.parse(redirectTo) delete request.req delete request._started request.headers.referer = uriPrev.href request.emit('redirect') request.init() return true } exports.Redirect = Redirect |