-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.js
More file actions
292 lines (253 loc) · 7.45 KB
/
main.js
File metadata and controls
292 lines (253 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* @file main
* @author hushicai(bluthcy@gmail.com)
*/
/* eslint-env node */
var url = require('url');
var path = require('path');
var fs = require('fs');
function bufferToString(buffer) {
return Buffer.concat(buffer).toString();
}
/**
* a simple parser for http response string
*
* @private
* @param {Buffer} buffer buffer
* @return {Object} result {headers: {Status: 200, 'content-type': 'text/html'}, body: 'xxxx'}
*/
function parse(buffer) {
var source = bufferToString(buffer);
// http spec: use `\r\n` as line break, except body
var result = {};
var lines = source.split('\r\n');
var line;
// headers
var headers = {};
while (lines.length) {
line = lines.shift();
if (line) {
line = line.split(':');
if (headers[line[0]]) {
if (typeof (headers[line[0]]) === "string") {
let oldHeader = headers[line[0]];
headers[line[0]] = [];
headers[line[0]].push(oldHeader);
}
if (line.length > 2) {
headers[line[0]].push(line.splice(1).join(':'));
} else {
headers[line[0]].push(line[1]);
}
} else {
if (line.length > 2) {
headers[line[0]] = line.splice(1).join(':');
} else {
headers[line[0]] = line[1];
}
}
}
else {
break;
}
}
result.headers = headers;
// body
// if body has `\r\n`, join it back to the body string
result.body = lines.join('\r\n');
return result;
}
var HEADER_NEED_HTTP_PREFIX = [
'COOKIE',
'HOST',
'REFERER',
'USER-AGENT',
'CONNECTION',
'ACCEPT',
'ACCEPT-ENCODING',
'ACCEPT-LANGUAGE'
];
function isNeedHttpPrefix(header) {
return HEADER_NEED_HTTP_PREFIX.indexOf(header.toUpperCase()) > -1;
}
function empty() {}
/**
* phpcgi
*
* @public
* @param {Object} options options
* @param {string} options.documentRoot root
* @param {?string} options.handler handler
* @param {?array} options.args args
* @param {HttpRequest} options.req req
* @param {HttpResponse} options.res res
* @return {void} none
*/
exports = module.exports = function (options) {
options = options || {};
var documentRoot = options.documentRoot || '.';
var handler = options.handler || 'php-cgi';
var phpcgiArguments = options.args || [];
var extensions = options.extensions || ['.php'];
var includePath = options.includePath || false;
var req = options.req;
var res = options.res;
var next = options.next || empty;
// 请求开始
var start = options.start || empty;
// 请求结束
var end = options.end || function (e) {
var statusCode = e.statusCode;
var headers = e.headers;
var body = e.body || '';
res.writeHead(statusCode, headers);
res.end(body);
};
var info = url.parse(req.url);
var scriptName = options.entryPoint || info.pathname;
var requestPath = info.pathname;
var ext = path.extname(requestPath);
if (
extensions.indexOf(ext) < 0
&& (
includePath === false
|| requestPath.search(new RegExp('^' + includePath)) < 0
)
) {
return next();
}
start();
console.log('PHP Request: ' + req.method + ' ' + req.url);
var query = info.query;
var method = req.method;
var scriptFileName = path.normalize(documentRoot + scriptName);
// windows may fail when the request file does not exist in the documentRoot
// so check it before spawn a child process
if (!fs.existsSync(scriptFileName)) {
return end({
statusCode: 404,
body: 'No input file specified!'
});
}
// @see: http://www.cgi101.com/book/ch3/text.html
var headers = req.headers;
var host = (headers.host || '').split(':');
var hostname = host[0];
var port = host[1] || 80;
var env = {
PATH: process.env.PATH,
GATEWAY_INTERFACE: 'CGI/1.1',
SERVER_SOFTWARE: 'node',
SERVER_PROTOCOL: 'HTTP/1.1',
SERVER_ROOT: documentRoot,
DOCUMENT_ROOT: documentRoot,
REDIRECT_STATUS: 200,
SERVER_NAME: hostname,
SERVER_PORT: port,
// 非必须
// REMOTE_ADDR: req.connection.remoteAddress,
// REMOTE_PORT: port,
// HTTPS: req.connection.encrypted ? 'On' : 'Off',
REQUEST_METHOD: method,
REQUEST_URI: req.url,
SCRIPT_NAME: scriptName,
SCRIPT_FILENAME: scriptFileName,
QUERY_STRING: query || ''
};
// @see: http://en.wikipedia.org/wiki/Common_Gateway_Interface
// @see: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Expressions_8.html
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
var name = header.toUpperCase().replace(/-/g, '_');
if (isNeedHttpPrefix(header)) {
name = 'HTTP_' + name;
}
env[name] = headers[header];
}
}
var child = require('child_process').spawn(
handler,
phpcgiArguments,
{
env: env,
detached: true
}
);
// specially for post
// pipe data into child progress
// if not, php-cgi could not receive the post data, and exit with something error.
if (req.readable) {
req.pipe(child.stdin);
}
else if (req.body) {
child.kill('SIGHUP');
return end({
statusCode: 502,
body: 'phpcgi could not receive data. please check if you are using body-parser or multer.'
});
}
else if (req.bodyBuffer) {
// edp
child.stdin.end(req.bodyBuffer);
}
// buffer data
var buffer = [];
// The child process can't be spawned
child.on(
'error',
function () {
return error('You may have a wrong php-cgi path.');
}
);
// dump stderr info
child.stderr
.on(
'data',
function () {
console.log(
'php-cgi error data: ' + [].slice.call(arguments)
);
}
);
// collect data
child.stdout
.on(
'data',
function (buf) {
buffer.push(buf);
}
);
// multi-byte char, like zh-cn, buffer data may lead to messy code.
// .setEncoding('utf8');
// done
child.on('close', function (code) {
if (code) {
return error('phpcgi exited with code ' + code);
}
return done();
});
// exit with error
function error(err) {
console.log(err);
// code如果非0,说明php-cgi异常退出了
// 但是异常退出,有可能是php程序错误导致的
// 因此,这里还得判断一下buffer
// 如果buffer中有数据,则把buffer内容返回
var result = {};
if (buffer.length) {
result = parse(buffer);
}
else {
result.body = 'service unavailable!';
}
result.statusCode = 500;
return end(result);
}
// success with data
function done() {
var result = parse(buffer);
result.headers.Status = result.headers.Status || '200 OK';
result.statusCode = parseInt(result.headers.Status, 10);
return end(result);
}
};