Skip to content

Commit 250680e

Browse files
committed
merge thehoffmann/master into master
1 parent 59aef24 commit 250680e

File tree

2 files changed

+90
-89
lines changed

2 files changed

+90
-89
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

main.js

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,124 +9,124 @@ var fs = require("fs");
99
function runPHP(req, response, next, phpdir){
1010
var parts = url.parse(req.url);
1111
var query = parts.query;
12-
13-
var file = path.join(phpdir, parts.pathname);
14-
12+
13+
var file = path.join(phpdir, parts.pathname);
14+
1515
// get file stats asynchronously
1616
fs.stat(file, function(err,stat){
1717

1818
if(err){ // file does not exist
1919
file = path.join(phpdir, "index.php");
2020
} else if(stat.isDirectory()){
2121
file = path.join(file, "index.php");
22-
}
22+
}
2323

24-
var pathinfo = "";
25-
var i = req.url.indexOf(".php");
26-
if(i > 0) pathinfo = parts.pathname.substring(i+4);
27-
else pathinfo = parts.pathname;
24+
var pathinfo = "";
25+
var i = req.url.indexOf(".php");
26+
if(i > 0) pathinfo = parts.pathname.substring(i+4);
27+
else pathinfo = parts.pathname;
2828

2929
var env = {
30-
SERVER_SIGNATURE: "NodeJS server at localhost",
30+
SERVER_SIGNATURE: "NodeJS server at localhost",
3131
PATH_INFO: pathinfo, //The extra path information, as given in the requested URL. In fact, scripts can be accessed by their virtual path, followed by extra information at the end of this path. The extra information is sent in PATH_INFO.
32-
PATH_TRANSLATED: "", //The virtual-to-real mapped version of PATH_INFO.
33-
SCRIPT_NAME: parts.pathname, //The virtual path of the script being executed.
34-
SCRIPT_FILENAME: file,
35-
REQUEST_FILENAME: file, //The real path of the script being executed.
36-
SCRIPT_URI: req.url, //The full URL to the current object requested by the client.
37-
URL: req.url, //The full URI of the current request. It is made of the concatenation of SCRIPT_NAME and PATH_INFO (if available.)
38-
SCRIPT_URL: req.url,
39-
REQUEST_URI: req.url, //The original request URI sent by the client.
40-
REQUEST_METHOD: req.method, //The method used by the current request; usually set to GET or POST.
41-
QUERY_STRING: parts.query||"", //The information which follows the ? character in the requested URL.
42-
CONTENT_TYPE: req.get("Content-type")||"", //"multipart/form-data", //"application/x-www-form-urlencoded", //The MIME type of the request body; set only for POST or PUT requests.
43-
CONTENT_LENGTH: req.get("Content-Length") || 0, //The length in bytes of the request body; set only for POST or PUT requests.
44-
AUTH_TYPE: "", //The authentication type if the client has authenticated itself to access the script.
45-
AUTH_USER: "",
46-
REMOTE_USER: "", //The name of the user as issued by the client when authenticating itself to access the script.
47-
ALL_HTTP: Object.keys(req.headers).map(function(x){return "HTTP_"+x.toUpperCase().replace("-", "_")+": "+req.headers[x];}).reduce(function(a, b){return a+b+"\n";}, ""), //All HTTP headers sent by the client. Headers are separated by carriage return characters (ASCII 13 - \n) and each header name is prefixed by HTTP_, transformed to upper cases, and - characters it contains are replaced by _ characters.
48-
ALL_RAW: Object.keys(req.headers).map(function(x){return x+": "+req.headers[x];}).reduce(function(a, b){return a+b+"\n";}, ""), //All HTTP headers as sent by the client in raw form. No transformation on the header names is applied.
49-
SERVER_SOFTWARE: "NodeJS", //The web server's software identity.
50-
SERVER_NAME: "localhost", //The host name or the IP address of the computer running the web server as given in the requested URL.
51-
SERVER_ADDR: "127.0.0.1", //The IP address of the computer running the web server.
52-
SERVER_PORT: 8011, //The port to which the request was sent.
53-
GATEWAY_INTERFACE: "CGI/1.1", //The CGI Specification version supported by the web server; always set to CGI/1.1.
54-
SERVER_PROTOCOL: "", //The HTTP protocol version used by the current request.
55-
REMOTE_ADDR: "", //The IP address of the computer that sent the request.
56-
REMOTE_PORT: "", //The port from which the request was sent.
57-
DOCUMENT_ROOT: "", //The absolute path of the web site files. It has the same value as Documents Path.
58-
INSTANCE_ID: "", //The numerical identifier of the host which served the request. On Abyss Web Server X1, it is always set to 1 since there is only a single host.
59-
APPL_MD_PATH: "", //The virtual path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to /.
60-
APPL_PHYSICAL_PATH: "", //The real path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to the same value as DOCUMENT_ROOT.
61-
IS_SUBREQ: "", //It is set to true if the current request is a subrequest, i.e. a request not directly invoked by a client. Otherwise, it is set to true. Subrequests are generated by the server for internal processing. XSSI includes for example result in subrequests.
62-
REDIRECT_STATUS: 1
63-
};
64-
65-
Object.keys(req.headers).map(function(x){return env["HTTP_"+x.toUpperCase().replace("-", "_")] = req.headers[x];});
32+
PATH_TRANSLATED: "", //The virtual-to-real mapped version of PATH_INFO.
33+
SCRIPT_NAME: parts.pathname, //The virtual path of the script being executed.
34+
SCRIPT_FILENAME: file,
35+
REQUEST_FILENAME: file, //The real path of the script being executed.
36+
SCRIPT_URI: req.url, //The full URL to the current object requested by the client.
37+
URL: req.url, //The full URI of the current request. It is made of the concatenation of SCRIPT_NAME and PATH_INFO (if available.)
38+
SCRIPT_URL: req.url,
39+
REQUEST_URI: req.url, //The original request URI sent by the client.
40+
REQUEST_METHOD: req.method, //The method used by the current request; usually set to GET or POST.
41+
QUERY_STRING: parts.query||"", //The information which follows the ? character in the requested URL.
42+
CONTENT_TYPE: req.get("Content-type")||"", //"multipart/form-data", //"application/x-www-form-urlencoded", //The MIME type of the request body; set only for POST or PUT requests.
43+
CONTENT_LENGTH: req.get("Content-Length") || 0, //The length in bytes of the request body; set only for POST or PUT requests.
44+
AUTH_TYPE: "", //The authentication type if the client has authenticated itself to access the script.
45+
AUTH_USER: "",
46+
REMOTE_USER: "", //The name of the user as issued by the client when authenticating itself to access the script.
47+
ALL_HTTP: Object.keys(req.headers).map(function(x){return "HTTP_"+x.toUpperCase().replace("-", "_")+": "+req.headers[x];}).reduce(function(a, b){return a+b+"\n";}, ""), //All HTTP headers sent by the client. Headers are separated by carriage return characters (ASCII 13 - \n) and each header name is prefixed by HTTP_, transformed to upper cases, and - characters it contains are replaced by _ characters.
48+
ALL_RAW: Object.keys(req.headers).map(function(x){return x+": "+req.headers[x];}).reduce(function(a, b){return a+b+"\n";}, ""), //All HTTP headers as sent by the client in raw form. No transformation on the header names is applied.
49+
SERVER_SOFTWARE: "NodeJS", //The web server's software identity.
50+
SERVER_NAME: "localhost", //The host name or the IP address of the computer running the web server as given in the requested URL.
51+
SERVER_ADDR: "127.0.0.1", //The IP address of the computer running the web server.
52+
SERVER_PORT: 8011, //The port to which the request was sent.
53+
GATEWAY_INTERFACE: "CGI/1.1", //The CGI Specification version supported by the web server; always set to CGI/1.1.
54+
SERVER_PROTOCOL: "", //The HTTP protocol version used by the current request.
55+
REMOTE_ADDR: req.ip||"", //The IP address of the computer that sent the request.
56+
REMOTE_PORT: "", //The port from which the request was sent.
57+
DOCUMENT_ROOT: "", //The absolute path of the web site files. It has the same value as Documents Path.
58+
INSTANCE_ID: "", //The numerical identifier of the host which served the request. On Abyss Web Server X1, it is always set to 1 since there is only a single host.
59+
APPL_MD_PATH: "", //The virtual path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to /.
60+
APPL_PHYSICAL_PATH: "", //The real path of the deepest alias which contains the request URI. If no alias contains the request URI, the variable is set to the same value as DOCUMENT_ROOT.
61+
IS_SUBREQ: "", //It is set to true if the current request is a subrequest, i.e. a request not directly invoked by a client. Otherwise, it is set to true. Subrequests are generated by the server for internal processing. XSSI includes for example result in subrequests.
62+
REDIRECT_STATUS: 1
63+
};
64+
65+
Object.keys(req.headers).map(function(x){return env["HTTP_"+x.toUpperCase().replace("-", "_")] = req.headers[x];});
6666

6767
if(/.*?\.php$/.test(file)){
68-
var res = "", err = "";
69-
68+
var res = "", err = "";
69+
7070
var php = child.spawn("php-cgi", [], {
7171
env: env
72-
});
73-
74-
//php.stdin.resume();
75-
//console.log(req.rawBody);
76-
//(new Stream(req.rawBody)).pipe(php.stdin);
77-
php.stdin.on("error", function(){});
78-
req.pipe(php.stdin); // pipe request stream directly into the php process
79-
req.resume();
80-
//php.stdin.write("\n");
81-
82-
//php.stdin.end();
83-
72+
});
73+
74+
//php.stdin.resume();
75+
//console.log(req.rawBody);
76+
//(new Stream(req.rawBody)).pipe(php.stdin);
77+
php.stdin.on("error", function(){});
78+
req.pipe(php.stdin); // pipe request stream directly into the php process
79+
req.resume();
80+
//php.stdin.write("\n");
81+
82+
//php.stdin.end();
83+
8484
php.stdout.on("data", function(data){
85-
//console.log(data.toString());
86-
res += data.toString();
87-
});
85+
//console.log(data.toString());
86+
res += data.toString();
87+
});
8888
php.stderr.on("data", function(data){
89-
err += err.toString();
90-
});
89+
err += err.toString();
90+
});
9191
php.on("error", function(err){
92-
console.error(err);
93-
});
92+
console.error(err);
93+
});
9494
php.on("exit", function(){
95-
// extract headers
96-
php.stdin.end();
97-
98-
var lines = res.split("\r\n");
99-
var line = 0;
100-
var html = "";
95+
// extract headers
96+
php.stdin.end();
97+
98+
var lines = res.split("\r\n");
99+
var line = 0;
100+
var html = "";
101101
if(lines.length){
102102
do {
103-
var m = lines[line].split(": ");
104-
if(m[0] === "") break;
105-
106-
//console.log("HEADER: "+m[0]+": "+m[1]);
103+
var m = lines[line].split(": ");
104+
if(m[0] === "") break;
105+
106+
//console.log("HEADER: "+m[0]+": "+m[1]);
107107
if(m[0] == "Status"){
108-
response.statusCode = parseInt(m[1]);
108+
response.statusCode = parseInt(m[1]);
109109
}
110110
if(m.length == 2){
111-
response.setHeader(m[0], m[1]);
111+
response.setHeader(m[0], m[1]);
112112
}
113-
line++;
114-
} while(lines[line] !== "");
115-
116-
html = lines.splice(line+1).join("\n");
113+
line++;
114+
} while(lines[line] !== "");
115+
116+
html = lines.splice(line+1).join("\n");
117117
} else {
118-
html = res;
118+
html = res;
119119
}
120-
//console.log("STATUS: "+response.statusCode);
121-
//console.log(html);
122-
response.status(response.statusCode).send(html);
123-
response.end();
124-
});
125-
120+
//console.log("STATUS: "+response.statusCode);
121+
//console.log(html);
122+
response.status(response.statusCode).send(html);
123+
response.end();
124+
});
125+
126126
} else {
127127
response.sendFile(file);
128-
//response.end();
129-
//next();
128+
//response.end();
129+
//next();
130130
}
131131
});
132132
}

0 commit comments

Comments
 (0)