Created
November 17, 2016 14:33
-
-
Save Aldlevine/501a3a3c04e1e88cd433088812901234 to your computer and use it in GitHub Desktop.
cgi.js forwards a CGI request to a server. It works with both http.Server and Express (if using http.Server as well). Performance is lacking as there is a lot of overhead from CGI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Options +ExecCGI | |
AddHandler cgi-script cgi | |
RewriteEngine on | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteRule (.*) server.cgi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net'); | |
const http = require('http'); | |
const path = require('path'); | |
module.exports = function cgi (server, port, cb) { | |
let env = process.env; | |
if( !env['DOCUMENT_ROOT'] ) { | |
server.listen(port, cb); | |
} else { | |
let data = ''; | |
process.stdin.on('data', function(chunk){ | |
data += chunk; | |
}); | |
process.stdin.on('end', function(){ | |
server.listen(0, function(){ | |
let method = env['REQUEST_METHOD'] || 'GET'; | |
let uri = path.join(env['DOCUMENT_ROOT'], env['REQUEST_URI']).replace(__dirname, ''); | |
let opts = { | |
method: method, | |
path: uri, | |
hostname: '127.0.0.1', | |
port: server.address().port, | |
headers:{} | |
}; | |
for( let key in env ) { | |
if( /^http_/i.test(key) ) { | |
opts.headers[key.replace(/^http_/i, '').replace(/_/g, '-')] = env[key]; | |
} | |
} | |
opts.headers['Content-Length'] = Buffer.byteLength(data); | |
let rawHttp = new Buffer(''); | |
let con = http.request(opts, function(res){ | |
res.on('data', function(chunk){ | |
}).on('end', function(){ | |
let str = rawHttp.toString(); | |
str = str.replace(/^HTTP(S?)\/\d\.\d (.+)$/m, 'Status: $2'); | |
server.close(); | |
}); | |
}).on('socket', function(socket){ | |
socket.on('data', function(chunk){ | |
rawHttp += chunk; | |
}); | |
}).on('error', function(err){ | |
console.log('ERROR CGI', err); | |
}); | |
con.on('close', function(){ | |
server.close(); | |
}); | |
con.write(data+'\r\n'); | |
con.end(); | |
}); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env node | |
require('./server.js'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env node | |
const http = require('http'); | |
const express = require('express'); | |
const cgi = require('./cgi'); | |
const PORT = process.env['PORT'] || 3000; | |
const DOCUMENT_ROOT = process.env['DOCUMENT_ROOT'] || process.env['NODE_ROOT']; | |
let app = express(); | |
let server = http.createServer(app); | |
app.get('*', (req, res)=>{ | |
res.send('Hello World'); | |
}); | |
cgi(server, PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment