michael@0: var protocol = exports; michael@0: michael@0: // michael@0: // ### function parseSynHead (type, flags, data) michael@0: // #### @type {Number} Frame type michael@0: // #### @flags {Number} Frame flags michael@0: // #### @data {Buffer} input data michael@0: // Returns parsed syn_* frame's head michael@0: // michael@0: protocol.parseSynHead = function parseSynHead(type, flags, data) { michael@0: var stream = type === 0x01; michael@0: michael@0: return { michael@0: type: stream ? 'SYN_STREAM' : 'SYN_REPLY', michael@0: id: data.readUInt32BE(0, true) & 0x7fffffff, michael@0: version: 2, michael@0: associated: stream ? data.readUInt32BE(4, true) & 0x7fffffff : 0, michael@0: priority: stream ? data[8] >> 6 : 0, michael@0: fin: (flags & 0x01) === 0x01, michael@0: unidir: (flags & 0x02) === 0x02, michael@0: _offset: stream ? 10 : 6 michael@0: }; michael@0: }; michael@0: michael@0: // michael@0: // ### function parseHeaders (pairs) michael@0: // #### @pairs {Buffer} header pairs michael@0: // Returns hashmap of parsed headers michael@0: // michael@0: protocol.parseHeaders = function parseHeaders(pairs) { michael@0: var count = pairs.readUInt16BE(0, true), michael@0: headers = {}; michael@0: michael@0: pairs = pairs.slice(2); michael@0: michael@0: function readString() { michael@0: var len = pairs.readUInt16BE(0, true), michael@0: value = pairs.slice(2, 2 + len); michael@0: michael@0: pairs = pairs.slice(2 + len); michael@0: michael@0: return value.toString(); michael@0: } michael@0: michael@0: while(count > 0) { michael@0: headers[readString()] = readString(); michael@0: count--; michael@0: } michael@0: michael@0: return headers; michael@0: }; michael@0: michael@0: // michael@0: // ### function parsesRst frame michael@0: protocol.parseRst = function parseRst(data) { michael@0: return { michael@0: type: 'RST_STREAM', michael@0: id: data.readUInt32BE(0, true) & 0x7fffffff, michael@0: status: data.readUInt32BE(4, true) michael@0: }; michael@0: }; michael@0: michael@0: protocol.parseGoaway = function parseGoaway(data) { michael@0: return { michael@0: type: 'GOAWAY', michael@0: lastId: data.readUInt32BE(0, true) & 0x7fffffff michael@0: }; michael@0: };