-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcgi_client.js
39 lines (32 loc) · 1.09 KB
/
fcgi_client.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
const net = require('net')
const FCGI = require('./fcgi')
module.exports = class Client {
constructor(socketOptions) {
this.buffer = Buffer.alloc(0)
this.reqId = 0
this.socket = net.connect(socketOptions)
this.socket.on('data', this.onData.bind(this))
if(this.onClose) { this.socket.on('close', this.onClose.bind(this)) }
if(this.onError) { this.socket.on('error', this.onError.bind(this)) }
}
send(msgType, content) {
for(let offset = 0; offset < content.length || offset == 0; offset += 0xFFFF) {
const chunk = content.slice(offset, offset + 0xFFFF)
const header = FCGI.Header(FCGI.VERSION_1, msgType, this.reqId, chunk.length, 0)
this.socket.write(header)
this.socket.write(chunk)
}
}
onData(data) {
this.buffer = Buffer.concat([ this.buffer, data ])
while(this.buffer.length) {
const record = FCGI.ParseHeader(this.buffer)
if(!record) { break }
this.buffer = this.buffer.slice(record.recordLength)
this.got(record)
}
}
got(record) {
// to be implemented in parent class
}
}