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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | var spdy = require('../node-spdy/lib/spdy.js'); |
michael@0 | 6 | var fs = require('fs'); |
michael@0 | 7 | var url = require('url'); |
michael@0 | 8 | var crypto = require('crypto'); |
michael@0 | 9 | |
michael@0 | 10 | function getHttpContent(path) { |
michael@0 | 11 | var content = '<!doctype html>' + |
michael@0 | 12 | '<html>' + |
michael@0 | 13 | '<head><title>HOORAY!</title></head>' + |
michael@0 | 14 | '<body>You Win! (by requesting' + path + ')</body>' + |
michael@0 | 15 | '</html>'; |
michael@0 | 16 | return content; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | function getHugeContent(size) { |
michael@0 | 20 | var content = ''; |
michael@0 | 21 | |
michael@0 | 22 | for (var i = 0; i < size; i++) { |
michael@0 | 23 | content += '0'; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | return content; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | /* This takes care of responding to the multiplexed request for us */ |
michael@0 | 30 | var Multiplex = function() {}; |
michael@0 | 31 | |
michael@0 | 32 | Multiplex.prototype = { |
michael@0 | 33 | mp1res: null, |
michael@0 | 34 | mp2res: null, |
michael@0 | 35 | buf: null, |
michael@0 | 36 | mp1start: 0, |
michael@0 | 37 | mp2start: 0, |
michael@0 | 38 | |
michael@0 | 39 | checkReady: function() { |
michael@0 | 40 | if (this.mp1res != null && this.mp2res != null) { |
michael@0 | 41 | this.buf = getHugeContent(30*1024); |
michael@0 | 42 | this.mp1start = 0; |
michael@0 | 43 | this.mp2start = 0; |
michael@0 | 44 | this.send(this.mp1res, 0); |
michael@0 | 45 | setTimeout(function() { this.send(this.mp2res, 0); }.bind(this), 5); |
michael@0 | 46 | } |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | send: function(res, start) { |
michael@0 | 50 | var end = start + 1024; |
michael@0 | 51 | if (end > this.buf.length) |
michael@0 | 52 | end = this.buf.length; |
michael@0 | 53 | var content = this.buf.substring(start, end); |
michael@0 | 54 | if (end < this.buf.length) { |
michael@0 | 55 | res.write(content); |
michael@0 | 56 | setTimeout(function() { this.send(res, end); }.bind(this), 10); |
michael@0 | 57 | } else { |
michael@0 | 58 | res.end(content); |
michael@0 | 59 | } |
michael@0 | 60 | }, |
michael@0 | 61 | }; |
michael@0 | 62 | |
michael@0 | 63 | var m = new Multiplex(); |
michael@0 | 64 | |
michael@0 | 65 | var post_hash = null; |
michael@0 | 66 | function receivePostData(chunk) { |
michael@0 | 67 | post_hash.update(chunk.toString()); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function finishPost(res, content) { |
michael@0 | 71 | var md5 = post_hash.digest('hex'); |
michael@0 | 72 | res.setHeader('X-Calculated-MD5', md5); |
michael@0 | 73 | res.writeHead(200); |
michael@0 | 74 | res.end(content); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function handleRequest(req, res) { |
michael@0 | 78 | var u = url.parse(req.url); |
michael@0 | 79 | var content = getHttpContent(u.pathname); |
michael@0 | 80 | |
michael@0 | 81 | if (req.streamID) { |
michael@0 | 82 | res.setHeader('X-Connection-Spdy', 'yes'); |
michael@0 | 83 | res.setHeader('X-Spdy-StreamId', '' + req.streamID); |
michael@0 | 84 | } else { |
michael@0 | 85 | res.setHeader('X-Connection-Spdy', 'no'); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | if (u.pathname == '/exit') { |
michael@0 | 89 | res.setHeader('Content-Type', 'text/plain'); |
michael@0 | 90 | res.writeHead(200); |
michael@0 | 91 | res.end('ok'); |
michael@0 | 92 | process.exit(); |
michael@0 | 93 | } else if (u.pathname == '/multiplex1' && req.streamID) { |
michael@0 | 94 | res.setHeader('Content-Type', 'text/plain'); |
michael@0 | 95 | res.writeHead(200); |
michael@0 | 96 | m.mp1res = res; |
michael@0 | 97 | m.checkReady(); |
michael@0 | 98 | return; |
michael@0 | 99 | } else if (u.pathname == '/multiplex2' && req.streamID) { |
michael@0 | 100 | res.setHeader('Content-Type', 'text/plain'); |
michael@0 | 101 | res.writeHead(200); |
michael@0 | 102 | m.mp2res = res; |
michael@0 | 103 | m.checkReady(); |
michael@0 | 104 | return; |
michael@0 | 105 | } else if (u.pathname == "/header") { |
michael@0 | 106 | m = new Multiplex(); |
michael@0 | 107 | var val = req.headers["x-test-header"]; |
michael@0 | 108 | if (val) { |
michael@0 | 109 | res.setHeader("X-Received-Test-Header", val); |
michael@0 | 110 | } |
michael@0 | 111 | } else if (u.pathname == "/push") { |
michael@0 | 112 | res.push('/push.js', |
michael@0 | 113 | { 'content-type': 'application/javascript', |
michael@0 | 114 | 'pushed' : 'yes', |
michael@0 | 115 | 'content-length' : 11, |
michael@0 | 116 | 'X-Connection-Spdy': 'yes'}, |
michael@0 | 117 | function(err, stream) { |
michael@0 | 118 | if (err) return; |
michael@0 | 119 | stream.end('// comments'); |
michael@0 | 120 | }); |
michael@0 | 121 | content = '<head> <script src="push.js"/></head>body text'; |
michael@0 | 122 | } else if (u.pathname == "/push2") { |
michael@0 | 123 | res.push('/push2.js', |
michael@0 | 124 | { 'content-type': 'application/javascript', |
michael@0 | 125 | 'pushed' : 'yes', |
michael@0 | 126 | // no content-length |
michael@0 | 127 | 'X-Connection-Spdy': 'yes'}, |
michael@0 | 128 | function(err, stream) { |
michael@0 | 129 | if (err) return; |
michael@0 | 130 | stream.end('// comments'); |
michael@0 | 131 | }); |
michael@0 | 132 | content = '<head> <script src="push2.js"/></head>body text'; |
michael@0 | 133 | } else if (u.pathname == "/big") { |
michael@0 | 134 | content = getHugeContent(128 * 1024); |
michael@0 | 135 | var hash = crypto.createHash('md5'); |
michael@0 | 136 | hash.update(content); |
michael@0 | 137 | var md5 = hash.digest('hex'); |
michael@0 | 138 | res.setHeader("X-Expected-MD5", md5); |
michael@0 | 139 | } else if (u.pathname == "/post") { |
michael@0 | 140 | if (req.method != "POST") { |
michael@0 | 141 | res.writeHead(405); |
michael@0 | 142 | res.end('Unexpected method: ' + req.method); |
michael@0 | 143 | return; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | post_hash = crypto.createHash('md5'); |
michael@0 | 147 | req.on('data', receivePostData); |
michael@0 | 148 | req.on('end', function () { finishPost(res, content); }); |
michael@0 | 149 | |
michael@0 | 150 | return; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | res.setHeader('Content-Type', 'text/html'); |
michael@0 | 154 | res.writeHead(200); |
michael@0 | 155 | res.end(content); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | // Set up the SSL certs for our server |
michael@0 | 159 | var options = { |
michael@0 | 160 | key: fs.readFileSync(__dirname + '/spdy-key.pem'), |
michael@0 | 161 | cert: fs.readFileSync(__dirname + '/spdy-cert.pem'), |
michael@0 | 162 | ca: fs.readFileSync(__dirname + '/spdy-ca.pem'), |
michael@0 | 163 | windowSize: 16000000, |
michael@0 | 164 | }; |
michael@0 | 165 | |
michael@0 | 166 | spdy.createServer(options, handleRequest).listen(4443); |
michael@0 | 167 | console.log('SPDY server listening on port 4443'); |
michael@0 | 168 | |
michael@0 | 169 | // Set up to exit when the user finishes our stdin |
michael@0 | 170 | process.stdin.resume(); |
michael@0 | 171 | process.stdin.on('end', function () { |
michael@0 | 172 | process.exit(); |
michael@0 | 173 | }); |