-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
150 lines (131 loc) · 4.28 KB
/
index.js
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
var fastcgiConnector = require('fastcgi-client');
module.exports = phpfpm;
var urlencode = require('urlencode-for-php');
/**
* phpfpm
* @param options
*
* default options will be { host:127.0.0.1, port:9000 }
*/
function phpfpm(options) {
options = options || {};
!options.host && (options.host = '127.0.0.1');
!options.port && (options.port = 9000);
!options.documentRoot && (options.documentRoot = '');
this.options = options;
var self = this;
options.skipCheckServer = true;
this.client = fastcgiConnector(options);
this.ready = false;
this.client.on('ready', function() {
self.ready = true;
self._clearQueue();
});
this.queue = [];
}
/**
* clear the queued tasks after connected to phpfpm
*/
phpfpm.prototype._clearQueue = function() {
var evt;
while (evt = this.queue.shift()) {
this.run(evt.info, evt.cb);
}
};
/**
* send command to phpfpm to run a php script
*/
phpfpm.prototype.run = function(info, cb) {
if (typeof info == 'string') info = { method: 'GET', uri: info };
if (info.url && !info.uri) info.uri = info.url;
if (!this.ready) {
this.queue.push({ info: info, cb: cb });
return;
}
//support form data
if (info.form && info.method != 'GET') {
info.body = urlencode(info.form);
info.method = 'POST';
}
if (info.form && info.method == 'GET') {
info.body = '';
var qs = urlencode(info.form);;
info.uri += (info.uri.indexOf('?') === -1) ? '?' + qs : '&' + qs;
}
if (info.body && !info.method) info.method = 'POST';
//support json data
if (info.json) {
info.body = JSON.stringify(info.json);
info.method = 'POST';
info.contentType = 'application/json';
}
!info.method && (info.method = 'GET');
info.method = info.method.toUpperCase();
if (info.method == 'POST') {
!info.body && (info.body = '');
if (typeof info.body === 'string') info.body = new Buffer(info.body, 'utf8');
!info.contentType && (info.contentType = 'application/x-www-form-urlencoded');
!info.contentLength && (info.contentLength = info.body.length);
}
if (info.uri.match(/\?/)) {
var ms = info.uri.match(/^([^\?]+)\?(.*)$/);
info.queryString = ms[2];
info.uri = ms[1];
}
var phpfile = info.uri;
if (!phpfile.match(/^\//)) phpfile = this.options.documentRoot + phpfile;
var HELLOWORLD_PARAMS = {
QUERY_STRING: info.queryString || '',
REQUEST_METHOD: info.method,
CONTENT_TYPE: info.contentType || '',
CONTENT_LENGTH: info.contentLength || '',
SCRIPT_FILENAME: phpfile,
SCRIPT_NAME: phpfile.split('/').pop(),
REQUEST_URI: info.uri,
DOCUMENT_URI: phpfile,
DOCUMENT_ROOT: this.options.documentRoot,
SERVER_PROTOCOL: 'HTTP/1.1',
GATEWAY_INTERFACE: 'CGI/1.1',
REMOTE_ADDR: '127.0.0.1',
REMOTE_PORT: 1234,
SERVER_ADDR: '127.0.0.1',
SERVER_PORT: 80,
SERVER_NAME: '127.0.0.1',
SERVER_SOFTWARE: 'node-phpfpm',
REDIRECT_STATUS: 200
};
var self = this;
self.client.request(HELLOWORLD_PARAMS, function(err, request) {
if (err) {
cb(99, err.toString(), err.toString());
return;
}
var body = '',
errors = '',
headers = '';
request.stdout.on('data', function(data) {
body += data.toString('utf8');
});
request.stderr.on('data', function(data) {
errors += data.toString('utf8');
});
request.stdout.on('end', function() {
headers = body.match(/^([\s\S]*?)(?=\r\n\r\n)/)[0];
headers = headers.split("\n").reduce(function(acc, line) {
var head = line.split(":");
if (head.length) {
var key = head[0].trim();
var value = head[1].trim();
acc[key] = value;
}
return acc;
}, {});
body = body.replace(/^[\s\S]*?\r\n\r\n/, '');
cb(false, body, errors, headers);
});
if (info.method == 'POST') {
request.stdin._write(info.body, 'utf8');
}
request.stdin.end();
});
};