Skip to content

Commit

Permalink
Fixed basic bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Nov 14, 2015
1 parent 6d240bb commit 94d61c4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 24 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module.exports = function(options){
options = options || {};
require('./lib/main.js')(options);
return require('./lib/main.js')(options);
};
40 changes: 31 additions & 9 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var MSG_TYPE = require('./consts').MSG_TYPE;
var PROTOCOL_STATUS = require('./consts').PROTOCOL_STATUS;
var parsekv = require('./parsekv');
var stringifykv = require('./stringifykv');
var genkv = require('./genkv');

module.exports = function(client, options){
var conn = null;
Expand Down Expand Up @@ -47,38 +46,60 @@ module.exports = function(client, options){
FCGI_MPXS_CONNS: ''
});
});
conn.on('error', function(e){
client.emit('error', e);
});
conn.on('close', function(){
state = 'disconnected';
client.emit('disconnect');
});

// send message
var paddingBufs = [
null,
new Buffer(1),
new Buffer(2),
new Buffer(3),
new Buffer(4),
new Buffer(5),
new Buffer(6),
new Buffer(7),
];
var sendMsg = function(reqId, msgType, obj){
var data = obj;
if(typeof(obj) === 'object') {
// kv pairs
data = stringifykv(obj);
}
// TODO split for length > 65535
var contentLen = data.length;
var paddingLen = (8 - (contentLen % 8)) % 8;
var buf = new Buffer(8);
buf.writeUint8(1, 0, true);
buf.writeUint8(msgType, 1, true);
buf.writeUint16BE(reqId, 2, true);
buf.writeUint16BE(contentLen, 4, true);
buf.writeUint8(paddingLen, 6, true);
buf.writeUint8(0, 7, true);
buf.writeUInt8(1, 0, true);
buf.writeUInt8(msgType, 1, true);
buf.writeUInt16BE(reqId, 2, true);
buf.writeUInt16BE(contentLen, 4, true);
buf.writeUInt8(paddingLen, 6, true);
buf.writeUInt8(0, 7, true);
console.info(buf);
console.info(data);
console.info(paddingBufs[paddingLen]);
conn.write(buf);
conn.write(data);
if(paddingLen) conn.write(paddingBufs[paddingLen]);
};

// receive message
var reqId = 0;
var msgType = -1;
var bodyLen = 0;
conn.on('readable', function(){
console.info('!!!!!!!');
if(msgType === -1) {
// read a new message header
var headData = conn.read(8);
if(headData === null) return;
console.info(headData);
if(headData.readUInt8(0, true) !== 1) {
client.emit('error', new Error('The server does not speak a compatible FastCGI protocol.'));
disconnect();
Expand All @@ -97,11 +118,12 @@ module.exports = function(client, options){
if(onControlMsg) onControlMsg(data);
} else if(msgType === MSG_TYPE.FCGI_END_REQUEST) {
// end request message
// TODO change to stream
var data = conn.read(bodyLen);
if(data === null) return;
msgType = -1;
exitCode = data.readInt32BE(0, true);
protocolStatus = data.readUint8(4, true);
var exitCode = data.readInt32BE(0, true);
var protocolStatus = data.readUInt8(4, true);
if(protocolStatus === PROTOCOL_STATUS.FCGI_REQUEST_COMPLETE) {
// client._endRequest(reqId);
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var createConnection = require('./connection');
module.exports = function(options){
options = options || {};

var eventEmitter = new EventEmitter();
var client = new EventEmitter();
var connection = createConnection(client, options);
client.disconnect = connection.disconnect;
client.request = require('./request')(client, options);
Expand Down
4 changes: 2 additions & 2 deletions lib/parsekv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var parsePair = function(msgData, start){
var keyLen = msgData.readUInt8(start, true);
if(keyLen > 127) {
if(msgData.length - start < 4) throw new Error('Unexpected server message: illegal key pair format.');
keyLen = ((msgData.readUInt16BE(start, true) - 0x7f00) << 16) + msgData.readUInt16BE(start + 2, true);
keyLen = (msgData.readInt32BE(start, true) & 0x7fffffff);
start += 4;
} else {
start++;
Expand All @@ -14,7 +14,7 @@ var parsePair = function(msgData, start){
var valueLen = msgData.readUInt8(start, true);
if(valueLen > 127) {
if(msgData.length - start < 4) throw new Error('Unexpected server message: illegal key pair format.');
valueLen = ((msgData.readUInt16BE(start, true) - 0x7f00) << 16) + msgData.readUInt16BE(start + 2, true);
valueLen = (msgData.readInt32BE(start, true) & 0x7fffffff);
start = start + 4;
} else {
start++;
Expand Down
16 changes: 8 additions & 8 deletions lib/stringifykv.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ var stringifyPair = function(key, value){
var valueLen = bufValue.length;
if(keyLen > 127 && valueLen > 127) {
bufHead = new Buffer(8);
headData.readUInt32BE(keyLen, 0, true);
headData.readUInt32BE(valueLen, 4, true);
bufHead.writeInt32BE(keyLen | 0x80000000, 0, true);
bufHead.writeInt32BE(valueLen | 0x80000000, 4, true);
} else if(keyLen > 127) {
bufHead = new Buffer(5);
headData.readUInt32BE(keyLen, 0, true);
headData.readUInt8(valueLen, 4, true);
bufHead.writeInt32BE(keyLen | 0x80000000, 0, true);
bufHead.writeUInt8(valueLen, 4, true);
} else if(valueLen > 127) {
bufHead = new Buffer(5);
headData.readUInt8(keyLen, 0, true);
headData.readUInt32BE(valueLen, 1, true);
bufHead.writeUInt8(keyLen, 0, true);
bufHead.writeInt32BE(valueLen | 0x80000000, 1, true);
} else {
bufHead = new Buffer(2);
headData.readUInt8(keyLen, 0, true);
headData.readUInt8(valueLen, 1, true);
bufHead.writeUInt8(keyLen, 0, true);
bufHead.writeUInt8(valueLen, 1, true);
}
return [
bufHead,
Expand Down
Empty file removed lib/test.js
Empty file.
12 changes: 9 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ var cases = [];
var childProcess = require('child_process');
var assert = require('assert');

var fcgiClient = require('../index');

var phpProc = null;

describe('start php server and test connection', function(){
before(function(done){
phpProc = childProcess.spawn('php-cgi', ['php-cgi', '-b', '127.0.0.1:9900'], {stdio: 'ignore'});
phpProc = childProcess.spawn('php-cgi', ['-b', '127.0.0.1:9900'], {stdio: 'ignore'});
phpProc.on('error', function(err){
assert.ifError(err);
});
setTimeout(done, 1000);
});
it('TODO', function(done){
done();
it('Connect', function(done){
var client = fcgiClient({
host: '127.0.0.1',
port: 9900
});
client.on('connect', done);
});
after(function(done){
phpProc.kill();
Expand Down

0 comments on commit 94d61c4

Please sign in to comment.