connect-modrewrite
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -52,5 +52,10 @@ module.exports = function(grunt) { | ||
// Test defined params | ||
'^/test-defined-params/(.*)$ /$1', | ||
// Test inverted URL | ||
'!\\.js|\\.css$ /index.html', | ||
], [ | ||
/\/test/ | ||
]), | ||
@@ -57,0 +62,0 @@ connect.static(options.base) |
{ | ||
"name": "connect-modrewrite", | ||
"main": "./src/modrewrite.js", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "connect-modrewrite is a middleware for connect. It adds modrewrite functionality to your project", | ||
@@ -10,3 +10,3 @@ "dependencies": { | ||
}, | ||
"author": { | ||
"author": { | ||
"name": "Tingan Ho", | ||
@@ -13,0 +13,0 @@ "email": "tingan87@gmail.com" |
@@ -1,2 +0,2 @@ | ||
module.exports = function(rules) { | ||
module.exports = function(rules, normalize) { | ||
@@ -24,2 +24,36 @@ '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) { | ||
// Split URLs for later normalization | ||
var referersSplits = req.headers.referer.split('/'), | ||
urlSplits = req.url.substr(1).split('/'); // substr(1) is there because the string begins with / | ||
// Remove hostname | ||
referersSplits.splice(0, 3); | ||
// Remove the last part of the referer since it is not | ||
// supposed to be used in the normalization process | ||
referersSplits.pop(); | ||
// Normalization process | ||
urlSplits.forEach(function(value, index) { | ||
if(value === referersSplits[index]) { | ||
urlSplits.splice(index, 1); | ||
} else { | ||
return false; | ||
} | ||
}); | ||
// Join back all splits | ||
req.url = '/' + urlSplits.join('/'); | ||
} | ||
return function(req, res, next) { | ||
@@ -30,20 +64,7 @@ | ||
if(typeof req.headers.referer !== 'undefined') { | ||
// Split URLs for later normalization | ||
var referersSplits = req.headers.referer.split('/'), | ||
urlSplits = req.url.substr(1).split('/'); // substr(1) is there because the string begins with / | ||
// Remove hostname | ||
referersSplits.splice(0, 3); | ||
// Remove the last part of the referer since it is not | ||
// supposed to be used in the normalization process | ||
referersSplits.pop(); | ||
// Normalization process | ||
urlSplits.forEach(function(value, index) { | ||
if(value === referersSplits[index]) { | ||
urlSplits.splice(index, 1); | ||
} else { | ||
return false; | ||
if(normalize) { | ||
if(isNormalizable(req.url)) { | ||
normalizeUrl(req); | ||
} | ||
}); | ||
// Join back all splits | ||
req.url = '/' + urlSplits.join('/'); | ||
} | ||
} | ||
@@ -54,3 +75,2 @@ | ||
rules.some(function(rewrite) { | ||
var location = protocol + '://' + req.headers.host + rewrite.replace; | ||
@@ -63,9 +83,8 @@ // Rewrite Url | ||
req.url = rewrite.replace; | ||
return rewrite.last; | ||
return req.url.replace(rewrite.regex, rewrite.replace); | ||
} else if(rewrite.regex.test(req.url)) { | ||
res.setHeader('Location', location); | ||
req.url = rewrite.replace; | ||
req.url = req.url.replace(rewrite.regex, rewrite.replace); | ||
return rewrite.last; | ||
} | ||
}); | ||
@@ -72,0 +91,0 @@ |
@@ -1,7 +0,6 @@ | ||
var chai = require( 'chai' ), | ||
var chai = require( 'chai' ), | ||
expect = chai.expect, | ||
http = require('http') | ||
http = require('http'), | ||
exec = require('child_process').exec; | ||
// Please run `grunt connect` before executing this script | ||
describe('connect-modrewrite', function() { | ||
@@ -41,6 +40,6 @@ | ||
it('should be able to recognize Last [L] flag', function(done){ | ||
it('should be able to recognize Last [L] flag', function(done) { | ||
var doneRequest = 0; | ||
http.get('http://localhost:9001/test-flag', function(res){ | ||
http.get('http://localhost:9001/test-flag', function(res) { | ||
expect(res.statusCode).to.equal(404); | ||
@@ -52,3 +51,3 @@ doneRequest++; | ||
}); | ||
http.get('http://localhost:9001/test-flag-2', function(res){ | ||
http.get('http://localhost:9001/test-flag-2', function(res) { | ||
expect(res.statusCode).to.equal(200); | ||
@@ -62,6 +61,12 @@ doneRequest++; | ||
it('should be able to handle inverted urls', function(done){ | ||
it('should be able to handle defined params', function() { | ||
http.get('http://localhost:9001/test-defined-params/style.css', function(res) { | ||
expect(res.statusCode).to.equal(200); | ||
}); | ||
}); | ||
it('should be able to handle inverted urls', function(done) { | ||
var doneRequest = 0; | ||
var threshold = 3; | ||
http.get('http://localhost:9001/style.css', function(res){ | ||
http.get('http://localhost:9001/style.css', function(res) { | ||
expect(res.statusCode).to.equal(200); | ||
@@ -73,3 +78,3 @@ doneRequest++; | ||
}); | ||
http.get('http://localhost:9001/inverted.scss', function(res){ | ||
http.get('http://localhost:9001/inverted.scss', function(res) { | ||
expect(/index\.html/.test(res.headers.location)).to.be.true; | ||
@@ -81,3 +86,3 @@ doneRequest++; | ||
}); | ||
http.get('http://localhost:9001/inverkwhhwgyigheyted.js', function(res){ | ||
http.get('http://localhost:9001/inverkwhhwgyigheyted.js', function(res) { | ||
expect(res.statusCode).to.be.equal(404); | ||
@@ -84,0 +89,0 @@ doneRequest++; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11468
14
226
2