fix #2
This commit is contained in:
		
							parent
							
								
									b3abd291e7
								
							
						
					
					
						commit
						699a97a902
					
				
							
								
								
									
										4
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								index.js
									
									
									
									
									
								
							@ -26,6 +26,10 @@ module.exports = function (opts) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    var url = req.url;
 | 
					    var url = req.url;
 | 
				
			||||||
    var host = req.headers.host || '';
 | 
					    var host = req.headers.host || '';
 | 
				
			||||||
 | 
							if (!/:\d+/.test(host) && 443 !== opts.port) {
 | 
				
			||||||
 | 
								// we are using standard port 80, but we aren't using standard port 443
 | 
				
			||||||
 | 
								host += ':80';
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
    var newLocation = 'https://'
 | 
					    var newLocation = 'https://'
 | 
				
			||||||
      + host.replace(/:\d+/, ':' + opts.port) + url
 | 
					      + host.replace(/:\d+/, ':' + opts.port) + url
 | 
				
			||||||
      ;
 | 
					      ;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										77
									
								
								test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								test.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
				
			|||||||
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var opts = { port: 0 };
 | 
				
			||||||
 | 
					var redirect = require('./')(opts);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function r80to443() {
 | 
				
			||||||
 | 
					  // using port 80 and 443
 | 
				
			||||||
 | 
					  opts.port = 443;
 | 
				
			||||||
 | 
					  var req = { connection: {}, headers: { host: 'example.com' }, url: '/path/to/somewhere.html' };
 | 
				
			||||||
 | 
					  var res = {
 | 
				
			||||||
 | 
					    setHeader: function () {}
 | 
				
			||||||
 | 
					  , end: function (body) {
 | 
				
			||||||
 | 
					      if (!/:/.test(body)) {
 | 
				
			||||||
 | 
					        throw new Error("test didn't pass with port 80 redirecting to 443");
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      console.log('PASS: 80 to 443');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  var next = function () {};
 | 
				
			||||||
 | 
					  redirect(req, res, next);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function r80to8443() {
 | 
				
			||||||
 | 
					  // using port 80, but not 443
 | 
				
			||||||
 | 
					  opts.port = 8443;
 | 
				
			||||||
 | 
					  var req = { connection: {}, headers: { host: 'example.com' }, url: '/path/to/somewhere.html' };
 | 
				
			||||||
 | 
					  var res = {
 | 
				
			||||||
 | 
					    setHeader: function () {}
 | 
				
			||||||
 | 
					  , end: function (body) {
 | 
				
			||||||
 | 
					      if (!/:8443/.test(body)) {
 | 
				
			||||||
 | 
					        throw new Error("test didn't pass with port 80 redirecting to non-443");
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      console.log('PASS: 80 to 8443');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  var next = function () {};
 | 
				
			||||||
 | 
					  redirect(req, res, next);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function r4080to8443() {
 | 
				
			||||||
 | 
					  // using port 80 and 443
 | 
				
			||||||
 | 
					  opts.port = 8443;
 | 
				
			||||||
 | 
					  var req = { connection: {}, headers: { host: 'example.com:4080' }, url: '/path/to/somewhere.html' };
 | 
				
			||||||
 | 
					  var res = {
 | 
				
			||||||
 | 
					    setHeader: function () {}
 | 
				
			||||||
 | 
					  , end: function (body) {
 | 
				
			||||||
 | 
					      if (!/:8443/.test(body)) {
 | 
				
			||||||
 | 
					        throw new Error("test didn't pass with port 4080 redirecting to 8443");
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      console.log('PASS: 4080 to 8443');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  var next = function () {};
 | 
				
			||||||
 | 
					  redirect(req, res, next);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function r4080to443() {
 | 
				
			||||||
 | 
					  // using port 80 and 443
 | 
				
			||||||
 | 
					  opts.port = 8443;
 | 
				
			||||||
 | 
					  var req = { connection: {}, headers: { host: 'example.com:4080' }, url: '/path/to/somewhere.html' };
 | 
				
			||||||
 | 
					  var res = {
 | 
				
			||||||
 | 
					    setHeader: function () {}
 | 
				
			||||||
 | 
					  , end: function (body) {
 | 
				
			||||||
 | 
					      if (/:/.test(body)) {
 | 
				
			||||||
 | 
					        throw new Error("test didn't pass with port 4080 redirecting to 8443");
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      console.log('PASS: 4080 to 8443');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  var next = function () {};
 | 
				
			||||||
 | 
					  redirect(req, res, next);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					r80to443();
 | 
				
			||||||
 | 
					r80to8443();
 | 
				
			||||||
 | 
					r4080to8443();
 | 
				
			||||||
 | 
					//r4080to443();
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user