Permalink
Browse files

working around with php5-cgi

  • Loading branch information...
1 parent 02f4755 commit 9da44ac7decadc2bee19ce5ae535d975dcda8de5 @dresende committed Jun 24, 2011
Showing with 48 additions and 17 deletions.
  1. +15 −7 core/plugins/php5-cgi.js
  2. +24 −7 core/utils.js
  3. +8 −2 navajo.conf
  4. +1 −1 www/default/test.php
View
22 core/plugins/php5-cgi.js
@@ -1,15 +1,15 @@
var path = require("path"),
spawn = require("child_process").spawn;
-exports.run = function (file, url, req, res) {
+exports.run = function (file, url, req, res, cb) {
var php = spawn("php5-cgi", [ "-f", file ], {
"cwd" : path.dirname(file),
"env" : {
"CONTENT_TYPE" : req.headers["content-type"] || "",
"CONTENT_LENGTH" : 0,
"GATEWAY_INTERFACE" : "CGI/1.1",
"PATH_INFO" : file,
- "QUERY_STRING" : url.query,
+ "QUERY_STRING" : url.query || "",
"REMOTE_ADDR" : req.connection.remoteAddress,
"REDIRECT_STATUS" : 200,
"REQUEST_METHOD" : req.method,
@@ -21,14 +21,18 @@ exports.run = function (file, url, req, res) {
}
}),
headers = "",
- headers_sent = false;
+ headers_sent = false,
+ info = {
+ "size": 0
+ };
php.stdout.on("data", function (data) {
if (headers_sent) {
+ info.size += data.length;
return res.write(data);
}
data = String(data);
- var p = data.indexOf("\r\n\r\n");
+ var p = data.indexOf("\r\n\r\n"), q;
if (p == -1) {
headers += data;
return;
@@ -38,19 +42,23 @@ exports.run = function (file, url, req, res) {
headers = headers.split("\r\n");
for (var i = 0; i < headers.length; i++) {
- if ((p = headers[i].indexOf(":")) != -1) {
- res.setHeader(headers[i].substr(0, p), headers[i].substr(p + 1));
+ if ((q = headers[i].indexOf(":")) != -1) {
+ res.setHeader(headers[i].substr(0, q), headers[i].substr(q + 1));
}
}
+ info.size += data.length - p - 4;
+
return res.write(data.substr(p + 4));
});
php.stdout.on("end", function (code) {
res.end();
+ return cb(info);
});
php.stderr.on("data", function (data) {
console.log("ERROR", String(data));
res.write(data);
});
- php.stdin.write("x=2&y=3");
+ php.stdin.end();
+ //php.stdin.write("x=2&y=3");
};
View
31 core/utils.js
@@ -9,9 +9,7 @@ var path = require("path"),
"log" : null,
"index" : null
},
- plugins = { // hardcoded for now, working on php5 cgi
- "php" : require("./plugins/php5-cgi")
- };
+ plugins = {};
exports.loadConfig = loadConfig;
exports.processRequest = processRequest;
@@ -34,10 +32,19 @@ function loadConfig(path, cb) {
}
config.bind = parseHostPort(config.bind, { "host": "0.0.0.0", "port": 80 });
+ if (typeof config.plugins != "object") {
+ config.plugins = {};
+ }
print.setLogging(config.log);
if (typeof config.mime == "object") {
+ for (k in config.mime) {
+ if (!config.mime.hasOwnProperty(k)) continue;
+ if (typeof config.mime[k] == "string") {
+ config.mime[k] = [ config.mime[k] ];
+ }
+ }
mime.define(config.mime);
}
@@ -101,6 +108,9 @@ function replyToPath(base_path, url, req, res) {
url = parse_url(url);
var real_path = path.normalize(path.join(base_path, url.pathname));
+ if (real_path[0] != "/") {
+ real_path = path.normalize(path.join(__dirname + "/../", real_path));
+ }
if (real_path.substr(-1) == "/") {
return replyWithIndex(real_path, config.index, 0, url, req, res);
}
@@ -126,17 +136,24 @@ function replyWithIndex(base_path, files, index, url, req, res) {
}
function streamFile(file, url, req, res) {
- if (file.match(/\.php$/)) {
- // this is hardcoded for now just to test and complete the interface
- return plugins.php.run(file, url, req, res);
+ var mime_type = mime.lookup(file);
+
+ if (config.plugins.hasOwnProperty(mime_type)) {
+ if (!plugins.hasOwnProperty(config.plugins[mime_type])) {
+ plugins[config.plugins[mime_type]] = require("./plugins/" + config.plugins[mime_type]);
+ }
+
+ return plugins[config.plugins[mime_type]].run(file, url, req, res, function (info) {
+ print.log(req, 200, file, info.size);
+ });
}
var fd = fs.createReadStream(file);
fs.stat(file, function (err, stat) {
res.statusCode = 200;
res.setHeader("Server", "navajo");
- res.setHeader("Content-Type", mime.lookup(file));
+ res.setHeader("Content-Type", mime_type);
if (!err) {
res.setHeader("Content-Length", stat.size);
View
10 navajo.conf
@@ -6,7 +6,13 @@
"path" : "logs/access.log"
},
"root" : {
- "default" : "/opt/navajo/www/default",
- "*vhost" : "www/vhost--*"
+ "default" : "www/default",
+ "*vhost" : "www/vhost--*"
+ },
+ "mime" : {
+ "application/x-httpd-php" : "php"
+ },
+ "plugins": {
+ "application/x-httpd-php" : "php5-cgi"
}
}
View
2 www/default/test.php
@@ -1,5 +1,5 @@
<?php
- header('X-Header: xpto');
+ header('content-type: text/plain');
printf("GET:\n");
print_r($_GET);

0 comments on commit 9da44ac

Please sign in to comment.