mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-14 18:52:05 +03:00
05e49d9a96
refs #5286 - Moved the logic into its own file - Added unit tests
36 lines
922 B
JavaScript
36 lines
922 B
JavaScript
// # uncapitalise Middleware
|
|
// Usage: uncapitalise(req, res, next)
|
|
// After:
|
|
// Before:
|
|
// App: Admin|Blog|API
|
|
//
|
|
// Detect upper case in req.path.
|
|
|
|
var utils = require('../utils'),
|
|
uncapitalise;
|
|
|
|
uncapitalise = function uncapitalise(req, res, next) {
|
|
/*jslint unparam:true*/
|
|
var pathToTest = req.path,
|
|
isSignupOrReset = req.path.match(/(\/ghost\/(signup|reset)\/)/i),
|
|
isAPI = req.path.match(/(\/ghost\/api\/v[\d\.]+\/.*?\/)/i);
|
|
|
|
if (isSignupOrReset) {
|
|
pathToTest = isSignupOrReset[1];
|
|
}
|
|
|
|
// Do not lowercase anything after /api/v0.1/ to protect :key/:slug
|
|
if (isAPI) {
|
|
pathToTest = isAPI[1];
|
|
}
|
|
|
|
if (/[A-Z]/.test(pathToTest)) {
|
|
res.set('Cache-Control', 'public, max-age=' + utils.ONE_YEAR_S);
|
|
res.redirect(301, req.url.replace(pathToTest, pathToTest.toLowerCase()));
|
|
} else {
|
|
next();
|
|
}
|
|
};
|
|
|
|
module.exports = uncapitalise;
|