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
|
|
|
|
2018-09-10 15:07:57 +03:00
|
|
|
const urlService = require('../../services/url'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common'),
|
2018-09-10 15:07:57 +03:00
|
|
|
localUtils = require('../utils');
|
|
|
|
|
|
|
|
const uncapitalise = function uncapitalise(req, res, next) {
|
|
|
|
let pathToTest = (req.baseUrl ? req.baseUrl : '') + req.path,
|
|
|
|
redirectPath,
|
|
|
|
decodedURI;
|
|
|
|
|
|
|
|
const isSignupOrReset = pathToTest.match(/^(.*\/ghost\/(signup|reset)\/)/i),
|
|
|
|
isAPI = pathToTest.match(/^(.*\/ghost\/api\/v[\d\.]+\/.*?\/)/i);
|
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) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.errors.pageNotFound'),
|
2017-11-28 14:39:38 +03:00
|
|
|
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)) {
|
2017-12-14 00:06:31 +03:00
|
|
|
redirectPath = (localUtils.removeOpenRedirectFromUrl((req.originalUrl || req.url).replace(pathToTest, pathToTest.toLowerCase())));
|
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;
|