Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | var protocol = exports; |
michael@0 | 2 | |
michael@0 | 3 | // |
michael@0 | 4 | // ### function parseSynHead (type, flags, data) |
michael@0 | 5 | // #### @type {Number} Frame type |
michael@0 | 6 | // #### @flags {Number} Frame flags |
michael@0 | 7 | // #### @data {Buffer} input data |
michael@0 | 8 | // Returns parsed syn_* frame's head |
michael@0 | 9 | // |
michael@0 | 10 | protocol.parseSynHead = function parseSynHead(type, flags, data) { |
michael@0 | 11 | var stream = type === 0x01; |
michael@0 | 12 | |
michael@0 | 13 | return { |
michael@0 | 14 | type: stream ? 'SYN_STREAM' : 'SYN_REPLY', |
michael@0 | 15 | id: data.readUInt32BE(0, true) & 0x7fffffff, |
michael@0 | 16 | version: 3, |
michael@0 | 17 | associated: stream ? data.readUInt32BE(4, true) & 0x7fffffff : 0, |
michael@0 | 18 | priority: stream ? data[8] >> 5 : 0, |
michael@0 | 19 | fin: (flags & 0x01) === 0x01, |
michael@0 | 20 | unidir: (flags & 0x02) === 0x02, |
michael@0 | 21 | _offset: stream ? 10 : 4 |
michael@0 | 22 | }; |
michael@0 | 23 | }; |
michael@0 | 24 | |
michael@0 | 25 | // |
michael@0 | 26 | // ### function parseHeaders (pairs) |
michael@0 | 27 | // #### @pairs {Buffer} header pairs |
michael@0 | 28 | // Returns hashmap of parsed headers |
michael@0 | 29 | // |
michael@0 | 30 | protocol.parseHeaders = function parseHeaders(pairs) { |
michael@0 | 31 | var count = pairs.readUInt32BE(0, true), |
michael@0 | 32 | headers = {}; |
michael@0 | 33 | |
michael@0 | 34 | pairs = pairs.slice(4); |
michael@0 | 35 | |
michael@0 | 36 | function readString() { |
michael@0 | 37 | var len = pairs.readUInt32BE(0, true), |
michael@0 | 38 | value = pairs.slice(4, 4 + len); |
michael@0 | 39 | |
michael@0 | 40 | pairs = pairs.slice(4 + len); |
michael@0 | 41 | |
michael@0 | 42 | return value.toString(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | while(count > 0) { |
michael@0 | 46 | headers[readString().replace(/^:/, '')] = readString(); |
michael@0 | 47 | count--; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | return headers; |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | // |
michael@0 | 54 | // ### function parsesRst frame |
michael@0 | 55 | protocol.parseRst = function parseRst(data) { |
michael@0 | 56 | return { |
michael@0 | 57 | type: 'RST_STREAM', |
michael@0 | 58 | id: data.readUInt32BE(0, true) & 0x7fffffff, |
michael@0 | 59 | status: data.readUInt32BE(4, true) |
michael@0 | 60 | }; |
michael@0 | 61 | }; |
michael@0 | 62 | |
michael@0 | 63 | protocol.parseSettings = function parseSettings(data) { |
michael@0 | 64 | var settings = {}, |
michael@0 | 65 | number = data.readUInt32BE(0, true), |
michael@0 | 66 | idMap = { |
michael@0 | 67 | 1: 'upload_bandwidth', |
michael@0 | 68 | 2: 'download_bandwidth', |
michael@0 | 69 | 3: 'round_trip_time', |
michael@0 | 70 | 4: 'max_concurrent_streams', |
michael@0 | 71 | 5: 'current_cwnd', |
michael@0 | 72 | 6: 'download_retrans_rate', |
michael@0 | 73 | 7: 'initial_window_size', |
michael@0 | 74 | 8: 'client_certificate_vector_size' |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | for (var i=0; i<number; i++) { |
michael@0 | 78 | var id = data.readUInt32BE(4 + (i*8), true) & 0x00ffffff, |
michael@0 | 79 | flags = data.readUInt8(4 + (i*8), true), |
michael@0 | 80 | name = idMap[id]; |
michael@0 | 81 | settings[id] = settings[name] = { |
michael@0 | 82 | persist: !!(flags & 0x1), |
michael@0 | 83 | persisted: !!(flags & 0x2), |
michael@0 | 84 | value: data.readUInt32BE(8 + (i*8), true) |
michael@0 | 85 | }; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | return { |
michael@0 | 89 | type: 'SETTINGS', |
michael@0 | 90 | settings: settings |
michael@0 | 91 | }; |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | protocol.parseGoaway = function parseGoaway(data) { |
michael@0 | 95 | return { |
michael@0 | 96 | type: 'GOAWAY', |
michael@0 | 97 | lastId: data.readUInt32BE(0, true) & 0x7fffffff |
michael@0 | 98 | }; |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | protocol.parseWindowUpdate = function parseWindowUpdate(data) { |
michael@0 | 102 | var ret = { |
michael@0 | 103 | type: 'WINDOW_UPDATE', |
michael@0 | 104 | id: data.readUInt32BE(0, true) & 0x7fffffff, |
michael@0 | 105 | delta: data.readUInt32BE(4, true) & 0x7fffffff |
michael@0 | 106 | }; |
michael@0 | 107 | |
michael@0 | 108 | return ret; |
michael@0 | 109 | }; |