connect-modrewrite
Advanced tools
Comparing version 0.7.9 to 0.7.11
83
index.js
@@ -6,7 +6,7 @@ | ||
var url = require('url') | ||
, qs = require('qs') | ||
, httpReq = require('http').request | ||
, httpsReq = require('https').request | ||
, defaultVia = '1.1 ' + require('os').hostname(); | ||
var url = require('url'); | ||
var qs = require('qs'); | ||
var httpReq = require('http').request; | ||
var httpsReq = require('https').request; | ||
var defaultVia = '1.1 ' + require('os').hostname(); | ||
@@ -17,14 +17,14 @@ /** | ||
var noCaseSyntax = /NC/ | ||
, lastSyntax = /L/ | ||
, proxySyntax = /P/ | ||
, redirectSyntax = /R=?(\d+)?/ | ||
, forbiddenSyntax = /F/ | ||
, goneSyntax = /G/ | ||
, typeSyntax = /T=([\w|\/]+,?)/ | ||
, hostSyntax = /H=([^,]+)/ | ||
, flagSyntax = /\[(.*)\]$/ | ||
, partsSyntax = /\s+|\t+/g | ||
, httpsSyntax = /^https/ | ||
, querySyntax = /\?(.*)/; | ||
var noCaseSyntax = /NC/; | ||
var lastSyntax = /L/; | ||
var proxySyntax = /P/; | ||
var redirectSyntax = /R=?(\d+)?/; | ||
var forbiddenSyntax = /F/; | ||
var goneSyntax = /G/; | ||
var typeSyntax = /T=([\w|\/]+,?)/; | ||
var hostSyntax = /H=([^,]+)/; | ||
var flagSyntax = /\[(.*)\]$/; | ||
var partsSyntax = /\s+|\t+/g; | ||
var httpsSyntax = /^https/; | ||
var querySyntax = /\?(.*)/; | ||
@@ -40,4 +40,4 @@ /** | ||
return function(req, res, next) { | ||
var protocol = req.connection.encrypted || req.headers['x-forwarded-proto'] === 'https' ? 'https' : 'http' | ||
, callNext = true; | ||
var protocol = req.connection.encrypted || req.headers['x-forwarded-proto'] === 'https' ? 'https' : 'http'; | ||
var callNext = true; | ||
@@ -52,5 +52,12 @@ rules.some(function(rule) { | ||
var location = protocol + '://' + req.headers.host + req.url.replace(rule.regexp, rule.replace) | ||
, match = rule.regexp.test(req.url); | ||
var location; | ||
if(/\:\/\//.test(rule.replace)) { | ||
location = req.url.replace(rule.regexp, rule.replace); | ||
} | ||
else { | ||
location = protocol + '://' + req.headers.host + req.url.replace(rule.regexp, rule.replace); | ||
} | ||
var match = rule.regexp.test(req.url); | ||
// If not match | ||
@@ -161,17 +168,17 @@ if(!match) { | ||
var redirectValue = redirectSyntax.exec(flags) | ||
, typeValue = typeSyntax.exec(flags) | ||
, hostValue = hostSyntax.exec(flags); | ||
var redirectValue = redirectSyntax.exec(flags); | ||
var typeValue = typeSyntax.exec(flags); | ||
var hostValue = hostSyntax.exec(flags); | ||
return { | ||
regexp : typeof parts[2] !== 'undefined' && noCaseSyntax.test(flags) ? new RegExp(parts[0], 'i') : new RegExp(parts[0]), | ||
replace : parts[1], | ||
inverted : inverted, | ||
last : lastSyntax.test(flags), | ||
proxy : proxySyntax.test(flags), | ||
redirect : redirectValue ? (typeof redirectValue[1] !== 'undefined' ? redirectValue[1] : 301) : false, | ||
forbidden : forbiddenSyntax.test(flags), | ||
gone : goneSyntax.test(flags), | ||
type : typeValue ? (typeof typeValue[1] !== 'undefined' ? typeValue[1] : 'text/plain') : false, | ||
host : hostValue ? new RegExp(hostValue[1]) : false | ||
regexp: typeof parts[2] !== 'undefined' && noCaseSyntax.test(flags) ? new RegExp(parts[0], 'i') : new RegExp(parts[0]), | ||
replace: parts[1], | ||
inverted: inverted, | ||
last: lastSyntax.test(flags), | ||
proxy: proxySyntax.test(flags), | ||
redirect: redirectValue ? (typeof redirectValue[1] !== 'undefined' ? redirectValue[1] : 301) : false, | ||
forbidden: forbiddenSyntax.test(flags), | ||
gone: goneSyntax.test(flags), | ||
type: typeValue ? (typeof typeValue[1] !== 'undefined' ? typeValue[1] : 'text/plain') : false, | ||
host: hostValue ? new RegExp(hostValue[1]) : false | ||
}; | ||
@@ -191,4 +198,4 @@ }); | ||
function _proxy(rule, metas) { | ||
var opts = _getRequestOpts(metas.req, rule) | ||
, request = httpsSyntax.test(rule.replace) ? httpsReq : httpReq; | ||
var opts = _getRequestOpts(metas.req, rule); | ||
var request = httpsSyntax.test(rule.replace) ? httpsReq : httpReq; | ||
@@ -225,4 +232,4 @@ var pipe = request(opts, function (res) { | ||
function _getRequestOpts(req, rule) { | ||
var opts = url.parse(req.url.replace(rule.regexp, rule.replace), true) | ||
, query = (opts.search != null) ? opts.search : ''; | ||
var opts = url.parse(req.url.replace(rule.regexp, rule.replace), true); | ||
var query = (opts.search != null) ? opts.search : ''; | ||
@@ -229,0 +236,0 @@ if(query) { |
{ | ||
"name": "connect-modrewrite", | ||
"main": "./index.js", | ||
"version": "0.7.9", | ||
"version": "0.7.11", | ||
"description": "connect-modrewrite adds modrewrite functionality to connect/express server", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -22,4 +22,4 @@ connect-modrewrite [![Build Status](https://travis-ci.org/tinganho/connect-modrewrite.png)](https://travis-ci.org/tinganho/connect-modrewrite) | ||
```javascript | ||
var app = connect() // express() for express server | ||
// app.configure(function() { for express server | ||
var app = connect() // express() for express 3.x server | ||
// app.configure(function() { for express 3.x server | ||
.use(modRewrite([ | ||
@@ -32,3 +32,3 @@ '^/test$ /index.html', | ||
.listen(3000) | ||
// }) for express server | ||
// }) for express 3.x server | ||
``` | ||
@@ -78,3 +78,2 @@ | ||
For more info about available flags, please go to the Apache page: | ||
@@ -84,10 +83,12 @@ http://httpd.apache.org/docs/current/rewrite/flags.html | ||
## Authors | ||
Tingan Ho, tingan87[at]gmail.com | ||
Tingan Ho, [@tingan87][] | ||
## License | ||
Copyright (c) 2012 Tingan Ho | ||
Licensed under the MIT license. | ||
## Other recommended projects | ||
### L10ns | ||
[L10ns][] is an internationalization workflow and formatting tool. It handles complex localization problems like pluralization, genus based formatting etc. It also provides a workflow similar to XGettext. | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/tinganho/connect-modrewrite/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
[@tingan87]: https://twitter.com/tingan87 | ||
[L10ns]: http://l10ns.org |
@@ -294,2 +294,22 @@ | ||
it('should be able to set a rewrite with hostname and protocol', function() { | ||
var middleware = modRewrite(['/(.*) http://localhost/$1 [R]']); | ||
var req = { | ||
connection : { encrypted : false }, | ||
header : function() {}, | ||
headers : { host : 'localhost' }, | ||
url : '/a' | ||
}; | ||
var res = { | ||
setHeader : function() {}, | ||
writeHead : sinon.spy(), | ||
end : sinon.spy() | ||
}; | ||
var next = function() {}; | ||
middleware(req, res, next); | ||
res.writeHead.should.have.been.calledWith(301, { Location : 'http://localhost/a'}); | ||
res.end.should.have.been.calledOnce; | ||
res.end.should.have.been.calledAfter(res.writeHead); | ||
}); | ||
it('should set custom status code if rewrite custom flag is set', function() { | ||
@@ -296,0 +316,0 @@ var middleware = modRewrite(['/a /b [R=307]']); |
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
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
24784
654
91