connect-modrewrite
Advanced tools
Comparing version 0.3.16 to 0.4.0
@@ -58,4 +58,2 @@ module.exports = function(grunt) { | ||
], [ | ||
/\/test/ | ||
]), | ||
@@ -62,0 +60,0 @@ connect.static(options.base) |
{ | ||
"name": "connect-modrewrite", | ||
"main": "./src/modrewrite.js", | ||
"version": "0.3.16", | ||
"version": "0.4.0", | ||
"description": "connect-modrewrite adds modrewrite functionality to connect/express server", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -1,4 +0,3 @@ | ||
var url = require('url'); | ||
module.exports = function(rules, normalize) { | ||
module.exports = function(rules) { | ||
@@ -26,52 +25,4 @@ 'use strict'; | ||
function isNormalizable(url) { | ||
var normalizable = false; | ||
if(normalize) { | ||
for(var i in normalize) { | ||
if(normalize[i].test(url) && !normalizable){ | ||
normalizable = true; | ||
break; | ||
} | ||
} | ||
} | ||
return normalizable; | ||
} | ||
function normalizeUrl(req, referersPath) { | ||
// Split URLs for later normalization | ||
var referersSplits = referersPath.substr(1).split('?')[0].split('/'), | ||
urlSplits = req.url.substr(1).split('?')[0].split('/'); // substr(1) is there because the string begins with / | ||
// Normalization process | ||
var removes = 0; | ||
for( var i = 0; i < referersSplits.length; i++) { | ||
var urlIndex = i - removes; | ||
if(referersSplits[i] === urlSplits[urlIndex]) { | ||
urlSplits.splice(urlIndex, 1); | ||
removes++; | ||
} else { | ||
break; | ||
} | ||
} | ||
// Join back all splits | ||
req.url = '/' + urlSplits.join('/'); | ||
} | ||
return function(req, res, next) { | ||
// Some request are not assets request, which means they don't | ||
// have an HTTP referer. We only normalize path which are assets | ||
if(typeof req.headers.referer !== 'undefined') { | ||
var referersPath = url.parse(req.headers.referer).path; | ||
if(req.url === referersPath) { | ||
next(); | ||
return; | ||
} | ||
if(normalize) { | ||
if(isNormalizable(req.url)) { | ||
normalizeUrl(req, referersPath); | ||
} | ||
} | ||
} | ||
var protocol = req.connection.encrypted ? 'https' : 'http' | ||
@@ -78,0 +29,0 @@ |
@@ -18,24 +18,3 @@ var chai = require( 'chai' ), | ||
it('should be able normalize relative paths', function(done) { | ||
var options = { | ||
hostname: 'localhost', | ||
port: 9001, | ||
path: '/test/style.css', | ||
method: 'GET', | ||
headers: { | ||
'Referer': 'http://localhost:9001/test/2' | ||
} | ||
}; | ||
var req = http.request(options, function(res) { | ||
res.setEncoding('utf8'); | ||
res.on('data', function (chunk) { | ||
expect(/#relative-paths-get-normalized/.test(chunk)).to.be.true; | ||
done(); | ||
}); | ||
}); | ||
req.end(); | ||
}); | ||
it('should be able to recognize Last [L] flag', function(done) { | ||
@@ -42,0 +21,0 @@ |
15
10157
161