Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var gateway_rw = require('gateway-rewrite');

var rwGateway = function (dir){
return gateway_rw(require('path').resolve(dir), {
// ignoreExistingFiles: true // Uncomment this line to allow fallthrough to following middleware when requesting a file that actually exists.
rules: [
{
rule: '^(/api/.+)',
Expand Down
169 changes: 87 additions & 82 deletions gateway-rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,106 +47,111 @@ module.exports = function gateway_rewrite(docroot, options) {

req.pause()

for (var j = 0; j < options.rules.length && !matches; j++) {
var rule = options.rules[j].rule;
var re = new RegExp(rule);

if (url.pathname.match(re)) {
matches ++;
var handler = options.rules[j].cgi
var file = options.rules[j].to
var uri = url.pathname
var path = normalize(join(docroot, file))

// populate the environment
var host = (req.headers.host || '').split(':')
var env = {
SERVER_ROOT: docroot,
DOCUMENT_ROOT: docroot,
SERVER_NAME: host[0],
SERVER_PORT: host[1] || 80,
HTTPS: req.connection.encrypted ? 'On' : 'Off',
REDIRECT_STATUS: 200,

SCRIPT_NAME: file,
REQUEST_URI: uri,
SCRIPT_FILENAME: path,
PATH_TRANSLATED: path,
REQUEST_METHOD: req.method,
QUERY_STRING: url.query || '',
GATEWAY_INTERFACE: 'CGI/1.1',
SERVER_PROTOCOL: 'HTTP/1.1',
PATH: process.env.PATH,
__proto__: options.env || {},

REMOTE_ADDR: '127.0.0.1' // Fake
}
var ignoreExistingFiles = options.ignoreExistingFiles
var requestedPath = join(docroot, url.pathname)

if (!ignoreExistingFiles || !fs.existsSync(requestedPath) || !fs.lstatSync(requestedPath).isFile()) {
for (var j = 0; j < options.rules.length && !matches; j++) {
var rule = options.rules[j].rule;
var re = new RegExp(rule);

if (url.pathname.match(re)) {
matches ++;
var handler = options.rules[j].cgi
var file = options.rules[j].to
var uri = url.pathname
var path = normalize(join(docroot, file))

// populate the environment
var host = (req.headers.host || '').split(':')
var env = {
SERVER_ROOT: docroot,
DOCUMENT_ROOT: docroot,
SERVER_NAME: host[0],
SERVER_PORT: host[1] || 80,
HTTPS: req.connection.encrypted ? 'On' : 'Off',
REDIRECT_STATUS: 200,

SCRIPT_NAME: file,
REQUEST_URI: uri,
SCRIPT_FILENAME: path,
PATH_TRANSLATED: path,
REQUEST_METHOD: req.method,
QUERY_STRING: url.query || '',
GATEWAY_INTERFACE: 'CGI/1.1',
SERVER_PROTOCOL: 'HTTP/1.1',
PATH: process.env.PATH,
__proto__: options.env || {},

REMOTE_ADDR: '127.0.0.1' // Fake
}

// expose request headers
for (var header in req.headers) {
var name = 'HTTP_' + header.toUpperCase().replace(/-/g, '_')
env[name] = req.headers[header]
}
// expose request headers
for (var header in req.headers) {
var name = 'HTTP_' + header.toUpperCase().replace(/-/g, '_')
env[name] = req.headers[header]
}

if ('content-length' in req.headers) {
env.CONTENT_LENGTH = req.headers['content-length']
}
if ('content-length' in req.headers) {
env.CONTENT_LENGTH = req.headers['content-length']
}

if ('content-type' in req.headers) {
env.CONTENT_TYPE = req.headers['content-type']
}
if ('content-type' in req.headers) {
env.CONTENT_TYPE = req.headers['content-type']
}


var child = spawn(handler, [], {
'env': env
}).on('exit', function (code) {
exit = code
done()
})
var child = spawn(handler, [], {
'env': env
}).on('exit', function (code) {
exit = code
done()
})

var line = []
var line = []

child.stdout.on('end', done).on('data', function (buf) {
child.stdout.on('end', done).on('data', function (buf) {

if (body) {
return res.write(buf)
}
if (body) {
return res.write(buf)
}

for (var i = 0; i < buf.length; i++) {
for (var i = 0; i < buf.length; i++) {

var c = buf[i]
var c = buf[i]

if (c == 0xA) {
if (!line.length) {
body = true
res.writeHead(statusCode || 200, reason)
res.write(buf.slice(i + 1))
return
}
if (c == 0xA) {
if (!line.length) {
body = true
res.writeHead(statusCode || 200, reason)
res.write(buf.slice(i + 1))
return
}

var s = line.join('')
line = []
if (!statusCode) {
var m = statusExp.exec(s)
if (m) {
statusCode = m[1]
reason = m[2]
continue;
var s = line.join('')
line = []
if (!statusCode) {
var m = statusExp.exec(s)
if (m) {
statusCode = m[1]
reason = m[2]
continue;
}
}
}

var idx = s.indexOf(':')
if ( !body ) {
var idx = s.indexOf(':')
if ( !body ) {
}
res.setHeader(s.slice(0, idx), s.slice(idx + 1).trim())
} else if (c != 0xD) {
line.push(String.fromCharCode(c))
}
res.setHeader(s.slice(0, idx), s.slice(idx + 1).trim())
} else if (c != 0xD) {
line.push(String.fromCharCode(c))
}
}

})
})

req.pipe(child.stdin)
req.pipe(child.stdin)
}
}
}

Expand Down