-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcgi.js
97 lines (84 loc) · 2.21 KB
/
fcgi.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
/**
* Created by cuccpkfs on 16-1-27.
*/
"use strict";
/*
typedef struct {
unsigned char version;
unsigned char type;
unsigned char requestIdB1;
unsigned char requestIdB0;
unsigned char contentLengthB1;
unsigned char contentLengthB0;
unsigned char paddingLength;
unsigned char reserved;
unsigned char contentData[contentLength];
unsigned char paddingData[paddingLength];
} FCGI_Record;
*/
var debug = require('debug')('noradle:fcgi')
;
exports.BEGIN = 1;
exports.ABORT = 2;
exports.END = 3;
exports.PARAMS = 4;
exports.STDIN = 5;
exports.STDOUT = 6;
exports.STDERR = 7;
exports.DATA = 8;
exports.GET = 9;
exports.RESULT = 10;
exports.UNKNOWN = 11;
exports.parseFrameStream = function(c, listener){
var check = false, head, slotID, type, flag, len, body;
c.on('readable', parseFCGI);
if (listener) {
c.on('frame', listener);
debug('registered on.frame');
}
var head, type, reqID, cLen, pLen, bLen, body;
function parseFCGI(trigger){
trigger || debug('on readable');
if (!head) {
head = c.read(8);
if (!head) return;
debug(head);
type = head[1];
reqID = head[2] * 256 + head[3];
cLen = head[4] * 256 + head[5];
pLen = head[6];
bLen = cLen + pLen;
debug('head: type=%d, reqID=%d, cLen=%d, pLen=%d, bLen=%d', type, reqID, cLen, pLen, bLen);
}
if (!body && bLen) {
body = c.read(bLen);
if(!body) return;
debug('body: len=%d', body.length);
if (!body) return;
if (type === exports.PARAMS) {
debug('body: %s', body.slice(0, 8).toString());
} else {
debug('body: %s', body.slice(0, 8).toString());
}
}
debug('read frame complete');
c.emit('frame', head, reqID, type, null, bLen, body);
head = body = undefined;
parseFCGI(true);
}
return function release(){
if (listener) {
c.removeListener('frame', listener);
}
c.removeListener('readable', parse);
}
};
exports.makeFrameHead = function(head, type, reqID, cLen, pLen){
// debug('before write frame', slotID, type, flag);
head.writeUInt8(1, 0);
head.writeUInt8(type, 1);
head.writeInt16BE(reqID, 2);
head.writeInt16BE(cLen, 4);
head.writeUInt8(pLen, 6);
head.writeUInt8(0, 7);
};