Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var req = fn.request(arg, function (response) {
var statusCode = response.statusCode;
var res = response;
// redirect
if (status.redirect[statusCode] && 'location' in res.headers) {
res.resume(); // Discard response
if (++redirectCount > 10) {
cb(new Error('Redirected 10 times. Aborting.'), undefined, res);
return;
}
get(urlLib.resolve(url, res.headers.location), opts, cb);
return;
}
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
var unzip = zlib.createUnzip();
res.pipe(unzip);
res = unzip;
}
function handleRedirect(res) {
var err = null;
if (args.followRedirect && statuses.redirect[res.statusCode]) { // handle redirect
args._followRedirectCount = (args._followRedirectCount || 0) + 1;
var location = res.headers.location;
if (!location) {
err = new Error('Got statusCode ' + res.statusCode + ' but cannot resolve next location from headers');
err.name = 'FollowRedirectError';
} else if (args._followRedirectCount > args.maxRedirects) {
err = new Error('Exceeded maxRedirects. Probably stuck in a redirect loop ' + url);
err.name = 'MaxRedirectError';
} else {
var newUrl = args.formatRedirectUrl ? args.formatRedirectUrl(url, location) : urlutil.resolve(url, location);
debug('Request#%d %s: `redirected` from %s to %s', reqId, options.path, url, newUrl);
// make sure timer stop
cancelResponseTimer();
// should clean up headers.Host on `location: http://other-domain/url`
if (options.headers.Host && PROTO_RE.test(location)) {
options.headers.Host = null;
function handleRedirect(res) {
var err = null;
if (args.followRedirect && statuses.redirect[res.statusCode]) { // handle redirect
args._followRedirectCount = (args._followRedirectCount || 0) + 1;
var location = res.headers.location;
if (!location) {
err = new Error('Got statusCode ' + res.statusCode + ' but cannot resolve next location from headers');
err.name = 'FollowRedirectError';
} else if (args._followRedirectCount > args.maxRedirects) {
err = new Error('Exceeded maxRedirects. Probably stuck in a redirect loop ' + url);
err.name = 'MaxRedirectError';
} else {
var newUrl = args.formatRedirectUrl ? args.formatRedirectUrl(url, location) : urlutil.resolve(url, location);
debug('Request#%d %s: `redirected` from %s to %s', reqId, options.path, url, newUrl);
// make sure timer stop
cancelResponseTimer();
// should clean up headers.Host on `location: http://other-domain/url`
if (options.headers.Host && PROTO_RE.test(location)) {
options.headers.Host = null;
return async function nosniff(ctx, next) {
await next();
// ignore redirect response
if (statuses.redirect[ctx.status]) return;
const opts = utils.merge(options, ctx.securityOptions.nosniff);
if (utils.checkIfIgnore(opts, ctx)) return;
ctx.set('x-content-type-options', 'nosniff');
};
};
redirect(url, alt) {
// location
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
this.set('Location', url);
// status
if (!statuses.redirect[this.status]) this.status = 302;
// html
if (this.ctx.accepts('html')) {
url = escape(url);
this.type = 'text/html; charset=utf-8';
this.body = `Redirecting to <a href="${url}">${url}</a>.`;
return;
}
// text
this.type = 'text/plain; charset=utf-8';
this.body = `Redirecting to ${url}.`;
},
function Response(req, res, options) {
this.request = req;
this.response = res;
this.options = options;
this.status =
this.statusCode = res.statusCode;
this.header =
this.headers = res.headers;
// empty the stream when the response body is irrelevant
// because node streams are dumb like that
var status = this.status;
if (this.length === 0
|| statuses.empty[status]
|| statuses.redirect[status]) res.resume();
}
redirect: function (url, alt) {
// location
if (url === 'back') url = this.ctx.get('referrer') || alt || '/'
this.set('location', url)
// status
if (!statuses.redirect[this.status]) this.status = 302
// html
if (this.ctx.accepts('html')) {
url = escapeHtml(url)
this.type = 'text/html; charset=utf-8'
this.body = 'Redirecting to <a href="' + url + '">' + url + '</a>.'
return
}
// text
this.type = 'text/plain; charset=utf-8'
this.body = 'Redirecting to ' + url + '.'
},
public redirect(url: string, alt?: string): void {
const { req } = this.ctx;
// Back uses request referrer header as a url.
url = url === "back" ? (req.get("Referrer") as string) : url;
// Default url to alternative.
url = url || alt;
if (!url) {
throw new TypeError(
"Rill#ctx.res.redirect: Cannot redirect, url not specified and alternative not provided."
);
}
if (!statuses.redirect[this.status]) {
this.status = 302;
}
this.set("Location", parseURL(url, req.href).href);
}