Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

playing around with vhost variables on hosts; working on php5-cgi

  • Loading branch information...
commit 02f475548a972dd9b71e7e3e9afd3f85e847b69a 1 parent 247a456
@dresende authored
View
56 core/plugins/php5-cgi.js
@@ -0,0 +1,56 @@
+var path = require("path"),
+ spawn = require("child_process").spawn;
+
+exports.run = function (file, url, req, res) {
+ 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,
+ "REMOTE_ADDR" : req.connection.remoteAddress,
+ "REDIRECT_STATUS" : 200,
+ "REQUEST_METHOD" : req.method,
+ "REQUEST_URI" : url.pathname,
+ "SCRIPT_FILENAME" : file,
+ "SCRIPT_NAME" : url.pathname,
+ "SERVER_PROTOCOL" : "HTTP/" + req.httpVersion,
+ "SERVER_SOFTWARE" : "navajo"
+ }
+ }),
+ headers = "",
+ headers_sent = false;
+
+ php.stdout.on("data", function (data) {
+ if (headers_sent) {
+ return res.write(data);
+ }
+ data = String(data);
+ var p = data.indexOf("\r\n\r\n");
+ if (p == -1) {
+ headers += data;
+ return;
+ }
+ headers += data.substr(0, p);
+ headers_sent = true;
+
+ 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));
+ }
+ }
+
+ return res.write(data.substr(p + 4));
+ });
+ php.stdout.on("end", function (code) {
+ res.end();
+ });
+ php.stderr.on("data", function (data) {
+ console.log("ERROR", String(data));
+ res.write(data);
+ });
+ php.stdin.write("x=2&y=3");
+};
View
45 core/utils.js
@@ -1,13 +1,17 @@
var path = require("path"),
fs = require("fs"),
+ parse_url = require("url").parse,
mime = require("mime"),
print = require("./print"),
config = {
- "bind" : { "host": "0.0.0.0", "port": 80 },
+ "bind" : "0.0.0.0:80",
"root" : "www/",
"log" : null,
"index" : null
- };
+ },
+ plugins = { // hardcoded for now, working on php5 cgi
+ "php" : require("./plugins/php5-cgi")
+ };
exports.loadConfig = loadConfig;
exports.processRequest = processRequest;
@@ -66,6 +70,23 @@ function replyTo(url, req, res) {
for (vhost in config.root) {
if (!config.root.hasOwnProperty(vhost)) continue;
+
+ // expression
+ if (vhost.indexOf("*") != -1) {
+ var re = new RegExp("^" + vhost.replace("*", "(.+)") + "$"),
+ m = re.exec(host);
+
+ if (!m) continue;
+
+ var vhost_path = config.root[vhost];
+ for (var i = 1; i <= m.length; i++) {
+ vhost_path = vhost_path.replace("*", m[i]);
+ }
+
+ return replyToPath(vhost_path, url, req, res);
+ }
+
+ // simple
if (host.substr(-vhost.length) != vhost) continue;
return replyToPath(config.root[vhost], url, req, res);
@@ -77,33 +98,39 @@ function replyTo(url, req, res) {
}
function replyToPath(base_path, url, req, res) {
- var real_path = path.normalize(path.join(base_path, url));
+ url = parse_url(url);
+ var real_path = path.normalize(path.join(base_path, url.pathname));
if (real_path.substr(-1) == "/") {
- return replyWithIndex(real_path, config.index, 0, req, res);
+ return replyWithIndex(real_path, config.index, 0, url, req, res);
}
path.exists(real_path, function (exists) {
if (exists) {
- return streamFile(real_path, req, res);
+ return streamFile(real_path, url, req, res);
}
return replyNotFound(req, res);
});
}
-function replyWithIndex(base_path, files, index, req, res) {
+function replyWithIndex(base_path, files, index, url, req, res) {
path.exists(base_path + files[index], function (exists) {
if (exists) {
- return streamFile(base_path + files[index], req, res);
+ return streamFile(base_path + files[index], url, req, res);
}
if (files.length > index + 1) {
- return replyWithIndex(base_path, files, index + 1, req, res);
+ return replyWithIndex(base_path, files, index + 1, url, req, res);
}
return replyNotFound(req, res);
});
}
-function streamFile(file, 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 fd = fs.createReadStream(file);
fs.stat(file, function (err, stat) {
View
4 navajo.conf
@@ -6,7 +6,7 @@
"path" : "logs/access.log"
},
"root" : {
- "default" : "www/default",
- "localvhost" : "www/localvhost"
+ "default" : "/opt/navajo/www/default",
+ "*vhost" : "www/vhost--*"
}
}
View
8 www/default/test.php
@@ -0,0 +1,8 @@
+<?php
+ header('X-Header: xpto');
+
+ printf("GET:\n");
+ print_r($_GET);
+ printf("SERVER:\n");
+ print_r($_SERVER);
+?>
View
0  www/localvhost/index.html → www/vhost--local/index.html
File renamed without changes
View
0  www/localvhost/screen.css → www/vhost--local/screen.css
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.