diff --git a/index.js b/index.js index 5c22241..b7a9a71 100644 --- a/index.js +++ b/index.js @@ -2,5 +2,5 @@ module.exports = function(options){ options = options || {}; - require('./lib/main.js')(options); + return require('./lib/main.js')(options); }; diff --git a/lib/connection.js b/lib/connection.js index 597baa6..1902996 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -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; @@ -47,27 +46,47 @@ 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 @@ -75,10 +94,12 @@ module.exports = function(client, options){ 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(); @@ -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 { diff --git a/lib/main.js b/lib/main.js index 05583a3..29df30f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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); diff --git a/lib/parsekv.js b/lib/parsekv.js index d53740e..75dfca4 100644 --- a/lib/parsekv.js +++ b/lib/parsekv.js @@ -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++; @@ -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++; diff --git a/lib/stringifykv.js b/lib/stringifykv.js index c291bbc..dae5358 100644 --- a/lib/stringifykv.js +++ b/lib/stringifykv.js @@ -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, diff --git a/lib/test.js b/lib/test.js deleted file mode 100644 index e69de29..0000000 diff --git a/test/test.js b/test/test.js index c1b4fc5..d92627e 100644 --- a/test/test.js +++ b/test/test.js @@ -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();