From 9156fe143e0fc54fa4fa510979d94f5830a9b90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Chamb=C3=B3?= Date: Thu, 17 Mar 2016 16:16:01 -0300 Subject: [PATCH 1/3] #381 use variables in url_prefix --- lib/index-web.js | 7 ++++--- lib/index.js | 3 +++ lib/utils.js | 11 ++++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/index-web.js b/lib/index-web.js index d1a15a9c..4c8eb1ba 100644 --- a/lib/index-web.js +++ b/lib/index-web.js @@ -7,6 +7,7 @@ var Handlebars = require('handlebars') var renderReadme = require('render-readme') var Search = require('./search') var Middleware = require('./middleware') +var utils = require('./utils') var match = Middleware.match var validate_name = Middleware.validate_name var validate_pkg = Middleware.validate_package @@ -42,7 +43,7 @@ module.exports = function(config, auth, storage) { } app.get('/', function(req, res, next) { var base = config.url_prefix - ? config.url_prefix.replace(/\/$/, '') + ? utils.buildUrlPrefix(config.url_prefix, req) : req.protocol + '://' + req.get('host') res.setHeader('Content-Type', 'text/html') @@ -104,7 +105,7 @@ module.exports = function(config, auth, storage) { } var base = config.url_prefix - ? config.url_prefix.replace(/\/$/, '') + ? utils.buildUrlPrefix(config.url_prefix, req) : req.protocol + '://' + req.get('host') res.redirect(base) }) @@ -112,7 +113,7 @@ module.exports = function(config, auth, storage) { app.post('/-/logout', function(req, res, next) { var base = config.url_prefix - ? config.url_prefix.replace(/\/$/, '') + ? utils.buildUrlPrefix(config.url_prefix, req) : req.protocol + '://' + req.get('host') res.cookies.set('token', '') res.redirect(base) diff --git a/lib/index.js b/lib/index.js index 8e9c8ecc..5cb82469 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,6 +20,9 @@ module.exports = function(config_hash) { // it shouldn't make any difference anyway app.set('env', process.env.NODE_ENV || 'production') + // use x-forwarded-for if present + app.enable('trust proxy'); + function error_reporting_middleware(req, res, next) { res.report_error = res.report_error || function(err) { if (err.status && err.status >= 400 && err.status < 600) { diff --git a/lib/utils.js b/lib/utils.js index bf558115..4d26858e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -62,7 +62,7 @@ module.exports.filter_tarball_urls = function(pkg, req, config) { var filename = URL.parse(_url).pathname.replace(/^.*\//, '') if (config.url_prefix != null) { - var result = config.url_prefix.replace(/\/$/, '') + var result = buildUrlPrefix(config.url_prefix, req); } else { var result = req.protocol + '://' + req.headers.host } @@ -80,6 +80,15 @@ module.exports.filter_tarball_urls = function(pkg, req, config) { return pkg } +module.exports.buildUrlPrefix = buildUrlPrefix; + +function buildUrlPrefix(url_prefix, req){ + var result = url_prefix.replace(/\/$/, '') + result = result.replace(/\${scheme}/,req.protocol); + result = result.replace(/\${host}/,req.hostname); + result = result.replace(/\${port}/,req.port); +} + function can_add_tag(tag, config) { if (!tag) return false if (tag === 'latest' && config.ignore_latest_tag) return false From 88eedc37083cb50439d618c68ad5b41f74ff8c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Chamb=C3=B3?= Date: Thu, 17 Mar 2016 16:53:20 -0300 Subject: [PATCH 2/3] added missing semicolon --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 4d26858e..bc7f8d0d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -83,7 +83,7 @@ module.exports.filter_tarball_urls = function(pkg, req, config) { module.exports.buildUrlPrefix = buildUrlPrefix; function buildUrlPrefix(url_prefix, req){ - var result = url_prefix.replace(/\/$/, '') + var result = url_prefix.replace(/\/$/, ''); result = result.replace(/\${scheme}/,req.protocol); result = result.replace(/\${host}/,req.hostname); result = result.replace(/\${port}/,req.port); From ed2d9b7c152bb39530c1f387e958ba91a2b90caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Chamb=C3=B3?= Date: Thu, 17 Mar 2016 17:06:37 -0300 Subject: [PATCH 3/3] #381 removed variable as it is returned in req.get('host') --- lib/utils.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index bc7f8d0d..673da16e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -85,8 +85,7 @@ module.exports.buildUrlPrefix = buildUrlPrefix; function buildUrlPrefix(url_prefix, req){ var result = url_prefix.replace(/\/$/, ''); result = result.replace(/\${scheme}/,req.protocol); - result = result.replace(/\${host}/,req.hostname); - result = result.replace(/\${port}/,req.port); + result = result.replace(/\${host}/,req.get('host')); } function can_add_tag(tag, config) {