2015-06-10 22:18:31 +03:00
|
|
|
// # uncapitalise Middleware
|
|
|
|
// Usage: uncapitalise(req, res, next)
|
|
|
|
// After:
|
|
|
|
// Before:
|
2017-10-26 19:24:08 +03:00
|
|
|
// App: Admin|Site|API
|
2015-06-10 22:18:31 +03:00
|
|
|
//
|
|
|
|
// Detect upper case in req.path.
|
2016-10-10 22:14:32 +03:00
|
|
|
//
|
|
|
|
// Example req:
|
|
|
|
// req.originalUrl = /blog/ghost/signin/?asdAD=asdAS
|
|
|
|
// req.url = /ghost/signin/?asdAD=asdAS
|
|
|
|
// req.baseUrl = /blog
|
|
|
|
// req.path = /ghost/signin/
|
2015-06-10 22:18:31 +03:00
|
|
|
|
2017-12-11 21:14:05 +03:00
|
|
|
var urlService = require('../../services/url'),
|
2017-12-11 22:27:09 +03:00
|
|
|
errors = require('../../lib/common/errors'),
|
|
|
|
i18n = require('../../lib/common/i18n'),
|
2017-12-11 21:14:05 +03:00
|
|
|
globalUtils = require('../../utils'),
|
2015-06-10 22:18:31 +03:00
|
|
|
uncapitalise;
|
|
|
|
|
|
|
|
uncapitalise = function uncapitalise(req, res, next) {
|
2016-10-10 22:14:32 +03:00
|
|
|
var pathToTest = (req.baseUrl ? req.baseUrl : '') + req.path,
|
|
|
|
isSignupOrReset = pathToTest.match(/^(.*\/ghost\/(signup|reset)\/)/i),
|
|
|
|
isAPI = pathToTest.match(/^(.*\/ghost\/api\/v[\d\.]+\/.*?\/)/i),
|
2017-11-28 14:39:38 +03:00
|
|
|
redirectPath, decodedURI;
|
2015-06-10 22:18:31 +03:00
|
|
|
|
|
|
|
if (isSignupOrReset) {
|
|
|
|
pathToTest = isSignupOrReset[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not lowercase anything after /api/v0.1/ to protect :key/:slug
|
|
|
|
if (isAPI) {
|
|
|
|
pathToTest = isAPI[1];
|
|
|
|
}
|
|
|
|
|
2017-11-28 14:39:38 +03:00
|
|
|
try {
|
|
|
|
decodedURI = decodeURIComponent(pathToTest);
|
|
|
|
} catch (err) {
|
|
|
|
return next(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.errors.pageNotFound'),
|
|
|
|
err: err
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-09-24 16:40:48 +03:00
|
|
|
/**
|
|
|
|
* In node < 0.11.1 req.path is not encoded, afterwards, it is always encoded such that | becomes %7C etc.
|
|
|
|
* That encoding isn't useful here, as it triggers an extra uncapitalise redirect, so we decode the path first
|
|
|
|
*/
|
2017-11-28 14:39:38 +03:00
|
|
|
if (/[A-Z]/.test(decodedURI)) {
|
2016-08-23 14:47:59 +03:00
|
|
|
redirectPath = (
|
2017-12-11 21:14:05 +03:00
|
|
|
globalUtils.removeOpenRedirectFromUrl((req.originalUrl || req.url).replace(pathToTest, pathToTest.toLowerCase()))
|
2016-08-23 14:47:59 +03:00
|
|
|
);
|
|
|
|
|
2017-12-11 21:14:05 +03:00
|
|
|
return urlService.utils.redirect301(res, redirectPath);
|
2015-06-10 22:18:31 +03:00
|
|
|
}
|
2017-11-01 16:44:54 +03:00
|
|
|
|
|
|
|
next();
|
2015-06-10 22:18:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = uncapitalise;
|