* @private * @param {*} arg1 - arg1 * @param {*} arg2 - arg2 * @param {*} arg3 - arg3 * @fires redirect * @function redirect * @returns {undefined} no return value
(arg1, arg2, arg3)
| 737 | * @returns {undefined} no return value |
| 738 | */ |
| 739 | function redirect(arg1, arg2, arg3) { |
| 740 | var self = this; |
| 741 | var statusCode = 302; |
| 742 | var finalUri; |
| 743 | var redirectLocation; |
| 744 | var next; |
| 745 | |
| 746 | // 1) this is signature 1, where an explicit status code is passed in. |
| 747 | // MUST guard against null here, passing null is likely indicative |
| 748 | // of an attempt to call res.redirect(null, next); |
| 749 | // as a way to do a reload of the current page. |
| 750 | if (arg1 && !isNaN(arg1)) { |
| 751 | statusCode = arg1; |
| 752 | finalUri = arg2; |
| 753 | next = arg3; |
| 754 | } else if (typeof arg1 === 'string') { |
| 755 | // 2) this is signaure number 2 |
| 756 | // otherwise, it's a string, and use it directly |
| 757 | finalUri = arg1; |
| 758 | next = arg2; |
| 759 | } else if (typeof arg1 === 'object') { |
| 760 | // 3) signature number 3, using an options object. |
| 761 | // set next, then go to work. |
| 762 | next = arg2; |
| 763 | |
| 764 | var req = self.req; |
| 765 | var opt = arg1 || {}; |
| 766 | var currentFullPath = req.href(); |
| 767 | var secure = opt.hasOwnProperty('secure') |
| 768 | ? opt.secure |
| 769 | : req.isSecure(); |
| 770 | |
| 771 | // if hostname is passed in, use that as the base, |
| 772 | // otherwise fall back on current url. |
| 773 | var parsedUri = url.parse(opt.hostname || currentFullPath, true); |
| 774 | |
| 775 | // create the object we'll use to format for the final uri. |
| 776 | // this object will eventually get passed to url.format(). |
| 777 | // can't use parsedUri to seed it, as it confuses the url module |
| 778 | // with some existing parsed state. instead, we'll pick the things |
| 779 | // we want and use that as a starting point. |
| 780 | finalUri = { |
| 781 | port: parsedUri.port, |
| 782 | hostname: parsedUri.hostname, |
| 783 | query: parsedUri.query, |
| 784 | pathname: parsedUri.pathname |
| 785 | }; |
| 786 | |
| 787 | // start building url based on options. |
| 788 | // start with the host |
| 789 | if (opt.hostname) { |
| 790 | finalUri.hostname = opt.hostname; |
| 791 | } |
| 792 | |
| 793 | // then set protocol IFF hostname is set - otherwise we end up with |
| 794 | // malformed URL. |
| 795 | if (finalUri.hostname) { |
| 796 | finalUri.protocol = secure === true ? 'https' : 'http'; |