-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (146 loc) · 4.49 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
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
const express = require('express')
const FCGI = require('./fcgi')
const Client = require('./fcgi_client')
const debug = require('debug')('express-php-fpm')
module.exports = init
function init(opt) {
return new Handler(opt).router
}
class Handler {
constructor(opt) {
debug('new Router')
// locals
this.opt = opt
this.connections = new Array(100)
this.router = express.Router()
// router
this.router.use(this.handle.bind(this))
this.router.use(express.static(opt.documentRoot))
}
handle(req, res, next) {
let file = this.withoutQueryString(req.url)
if(file.endsWith('/')) { file += 'index.php' }
if(!file.endsWith('.php')) { next(); return }
new Responder(this, file, req, res, next)
}
getFreeReqId() {
let i = 0
while(this.connections[++i]) { }
this.connections[i] = true
return i
}
freeUpReqId(reqId) {
this.connections[reqId] = false
}
withoutQueryString(url) {
const sep = url.indexOf('?')
return (sep == -1) ? url : url.substr(0, sep)
}
}
class Responder extends Client {
constructor(handler, file, req, res, next) {
// init sockets
super(handler.opt.socketOptions)
// locals
this.handler = handler
this.res = res
this.next = next
this.reqId = handler.getFreeReqId()
this.gotHead = false
// debug
debug('new Responder %d for %s', this.reqId, file)
// send req
const env = createEnviroment(handler.opt.documentRoot, file, req, handler.opt.env)
this.send(FCGI.MSG.BEGIN_REQUEST, FCGI.BeginRequestBody(FCGI.ROLE.RESPONDER, FCGI.DONT_KEEP_CONN))
this.send(FCGI.MSG.PARAMS, FCGI.NameValuePair(env))
this.send(FCGI.MSG.PARAMS, Buffer.alloc(0))
// express request
req.on('data', this.reqData.bind(this))
req.on('end', this.reqEnd.bind(this))
}
reqData(chunk) {
this.send(FCGI.MSG.STDIN, chunk)
}
reqEnd() {
this.send(FCGI.MSG.STDIN, Buffer.alloc(0))
}
onError(e) {
this.next(e)
}
onClose(hadError) {
this.handler.freeUpReqId(this.reqId)
}
send(msgType, content) {
debug('send %s', FCGI.GetMsgType(msgType))
super.send(msgType, content)
}
got(record) {
debug('got %s', FCGI.GetMsgType(record.type))
switch(record.type) {
case FCGI.MSG.STDERR: break
case FCGI.MSG.STDOUT: this.stdout(record.content); break
case FCGI.MSG.END_REQUEST: this.res.end(); break
case FCGI.MSG.GET_VALUES_RESULT: break
}
}
stdout(content) {
if(this.gotHead) {
this.res.write(content)
return
}
this.gotHead = true
const sep = content.indexOf('\r\n\r\n')
const head = content.slice(0, sep)
const body = content.slice(sep + 4)
const headers = {}
for(const h of head.toString().split('\r\n')) {
const hsep = h.indexOf(':')
const hkey = h.substr(0, hsep)
const hval = h.substr(hsep + 2)
if(hkey == "Status") {
this.res.status(parseInt(hval.substr(0, 3)))
continue
}
this.res.append(hkey, hval)
}
this.res.write(body)
}
}
function createEnviroment(documentRoot, file, req, extraEnv) {
const sep = req.url.indexOf('?')
const qs = (sep == -1) ? '' : req.url.substr(sep + 1)
const env = {
GATEWAY_INTERFACE: 'CGI/1.1',
PATH: '',
REQUEST_METHOD: req.method,
REDIRECT_STATUS: 200, // https://stackoverflow.com/questions/24378472/what-is-php-serverredirect-status
REMOTE_ADDR: req.connection.remoteAddress,
REMOTE_PORT: req.connection.remotePort,
SERVER_PROTOCOL: req.protocol.toUpperCase() + '/' + req.httpVersion,
SERVER_ADDR: req.connection.localAddress,
SERVER_PORT: req.connection.localPort,
SERVER_SOFTWARE: 'express-php-fpm',
SERVER_NAME: '',
SERVER_ADMIN: '',
SERVER_SIGNATURE: '',
DOCUMENT_ROOT: documentRoot,
SCRIPT_FILENAME: documentRoot + file,
SCRIPT_NAME: file,
REQUEST_URI: req.url,
QUERY_STRING: qs,
CONTENT_TYPE: req.headers['content-type'] || '',
CONTENT_LENGTH: req.headers['content-length'] || '',
// AUTH_TYPE
// PATH_INFO
// PATH_TRANSLATED
// REMOTE_HOST
// REMOTE_IDENT
// REMOTE_USER
// UNIQUE_ID
}
for(const key of Object.keys(req.headers)) {
env['HTTP_' + key.toUpperCase().replace(/-/g, '_')] = req.headers[key]
}
Object.assign(env, extraEnv)
return env
}