Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Custom SSL Port support #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ Azure has a slightly different way of signaling encrypted connections. To tell e

Please do *not* set this flag if you are not behind an Azure proxy as this flag can easily be spoofed outside of an Azure environment.

### Custom SSL Port

Specify on which SSL port should connections be redirected by setting `port` in options.

`app.use(enforce.HTTPS({ port: 3001 }))`

This will redirect requests for example from `http://foo.bar:3000/baz` to `https://foo.bar:3001/baz`.


## Tests
Download the whole repository and call:
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use strict";

var url = require('url');

var defaults = {
port: null,
trustProtoHeader: false,
trustAzureHeader: false
};
Expand All @@ -26,6 +29,7 @@ function applyOptions(options) {
* enforceHTTPS
*
* @param {Hash} options
* @param {Number} options[port]
* @param {Boolean} options[trustProtoHeader]
* @param {Boolean} options[trustAzureHeader]
* @api public
Expand Down Expand Up @@ -59,7 +63,16 @@ var enforceHTTPS = function(options) {
} else {
// Only redirect GET methods
if(req.method === "GET" || req.method === 'HEAD') {
res.redirect(301, "https://" + req.headers.host + req.originalUrl);
var hostname = url.parse('http://' + req.headers.host).hostname;

var redirectUrl
if (options.port != null) {
redirectUrl = "https://" + hostname + ':' + options.port + req.url
} else {
redirectUrl = "https://" + hostname + req.url;
}

res.redirect(301, redirectUrl);
} else {
res.status(403).send("Please use HTTPS when submitting data to this server.");
}
Expand Down
14 changes: 14 additions & 0 deletions test/express-sslify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ describe('express-sslify', function() {
proxyTests('head');
})

describe('Custom SSL Port', function () {

it('should redirect to specified port', function (done) {
var app = express();

app.use(enforce.HTTPS({ port: 3001 }));

request.agent(app)
.get('/ssl')
.expect(301)
.expect('location', new RegExp('^https://[\\S]*\:3001/ssl$'), done);
});
});

describe('Pre-1.0.0-style arguments', function() {

var app = express();
Expand Down