testing/xpcshell/moz-http2/moz-http2.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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 http2 = require('../node-http2');
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 // Hook into the decompression code to log the decompressed name-value pairs
michael@0 11 var http2_compression = require('../node-http2/node_modules/http2-protocol/lib/compressor');
michael@0 12 var HeaderSetDecompressor = http2_compression.HeaderSetDecompressor;
michael@0 13 var originalRead = HeaderSetDecompressor.prototype.read;
michael@0 14 var lastDecompressor;
michael@0 15 var decompressedPairs;
michael@0 16 HeaderSetDecompressor.prototype.read = function() {
michael@0 17 if (this != lastDecompressor) {
michael@0 18 lastDecompressor = this;
michael@0 19 decompressedPairs = [];
michael@0 20 }
michael@0 21 var pair = originalRead.apply(this, arguments);
michael@0 22 if (pair) {
michael@0 23 decompressedPairs.push(pair);
michael@0 24 }
michael@0 25 return pair;
michael@0 26 }
michael@0 27
michael@0 28 function getHttpContent(path) {
michael@0 29 var content = '<!doctype html>' +
michael@0 30 '<html>' +
michael@0 31 '<head><title>HOORAY!</title></head>' +
michael@0 32 '<body>You Win! (by requesting' + path + ')</body>' +
michael@0 33 '</html>';
michael@0 34 return content;
michael@0 35 }
michael@0 36
michael@0 37 function generateContent(size) {
michael@0 38 var content = '';
michael@0 39 for (var i = 0; i < size; i++) {
michael@0 40 content += '0';
michael@0 41 }
michael@0 42 return content;
michael@0 43 }
michael@0 44
michael@0 45 /* This takes care of responding to the multiplexed request for us */
michael@0 46 var m = {
michael@0 47 mp1res: null,
michael@0 48 mp2res: null,
michael@0 49 buf: null,
michael@0 50 mp1start: 0,
michael@0 51 mp2start: 0,
michael@0 52
michael@0 53 checkReady: function() {
michael@0 54 if (this.mp1res != null && this.mp2res != null) {
michael@0 55 this.buf = generateContent(30*1024);
michael@0 56 this.mp1start = 0;
michael@0 57 this.mp2start = 0;
michael@0 58 this.send(this.mp1res, 0);
michael@0 59 setTimeout(this.send.bind(this, this.mp2res, 0), 5);
michael@0 60 }
michael@0 61 },
michael@0 62
michael@0 63 send: function(res, start) {
michael@0 64 var end = Math.min(start + 1024, this.buf.length);
michael@0 65 var content = this.buf.substring(start, end);
michael@0 66 res.write(content);
michael@0 67 if (end < this.buf.length) {
michael@0 68 setTimeout(this.send.bind(this, res, end), 10);
michael@0 69 } else {
michael@0 70 res.end();
michael@0 71 }
michael@0 72 }
michael@0 73 };
michael@0 74
michael@0 75 function handleRequest(req, res) {
michael@0 76 var u = url.parse(req.url);
michael@0 77 var content = getHttpContent(u.pathname);
michael@0 78 var push;
michael@0 79
michael@0 80 if (req.httpVersionMajor === 2) {
michael@0 81 res.setHeader('X-Connection-Http2', 'yes');
michael@0 82 res.setHeader('X-Http2-StreamId', '' + req.stream.id);
michael@0 83 } else {
michael@0 84 res.setHeader('X-Connection-Http2', 'no');
michael@0 85 }
michael@0 86
michael@0 87 if (u.pathname === '/exit') {
michael@0 88 res.setHeader('Content-Type', 'text/plain');
michael@0 89 res.writeHead(200);
michael@0 90 res.end('ok');
michael@0 91 process.exit();
michael@0 92 }
michael@0 93
michael@0 94 else if ((u.pathname === '/multiplex1') && (req.httpVersionMajor === 2)) {
michael@0 95 res.setHeader('Content-Type', 'text/plain');
michael@0 96 res.writeHead(200);
michael@0 97 m.mp1res = res;
michael@0 98 m.checkReady();
michael@0 99 return;
michael@0 100 }
michael@0 101
michael@0 102 else if ((u.pathname === '/multiplex2') && (req.httpVersionMajor === 2)) {
michael@0 103 res.setHeader('Content-Type', 'text/plain');
michael@0 104 res.writeHead(200);
michael@0 105 m.mp2res = res;
michael@0 106 m.checkReady();
michael@0 107 return;
michael@0 108 }
michael@0 109
michael@0 110 else if (u.pathname === "/header") {
michael@0 111 var val = req.headers["x-test-header"];
michael@0 112 if (val) {
michael@0 113 res.setHeader("X-Received-Test-Header", val);
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 else if (u.pathname === "/cookie_crumbling") {
michael@0 118 res.setHeader("X-Received-Header-Pairs", JSON.stringify(decompressedPairs));
michael@0 119 }
michael@0 120
michael@0 121 else if (u.pathname === "/push") {
michael@0 122 push = res.push('/push.js');
michael@0 123 push.writeHead(200, {
michael@0 124 'content-type': 'application/javascript',
michael@0 125 'pushed' : 'yes',
michael@0 126 'content-length' : 11,
michael@0 127 'X-Connection-Http2': 'yes'
michael@0 128 });
michael@0 129 push.end('// comments');
michael@0 130 content = '<head> <script src="push.js"/></head>body text';
michael@0 131 }
michael@0 132
michael@0 133 else if (u.pathname === "/push2") {
michael@0 134 push = res.push('/push2.js');
michael@0 135 push.writeHead(200, {
michael@0 136 'content-type': 'application/javascript',
michael@0 137 'pushed' : 'yes',
michael@0 138 // no content-length
michael@0 139 'X-Connection-Http2': 'yes'
michael@0 140 });
michael@0 141 push.end('// comments');
michael@0 142 content = '<head> <script src="push2.js"/></head>body text';
michael@0 143 }
michael@0 144
michael@0 145 else if (u.pathname === "/big") {
michael@0 146 content = generateContent(128 * 1024);
michael@0 147 var hash = crypto.createHash('md5');
michael@0 148 hash.update(content);
michael@0 149 var md5 = hash.digest('hex');
michael@0 150 res.setHeader("X-Expected-MD5", md5);
michael@0 151 }
michael@0 152
michael@0 153 else if (u.pathname === "/post") {
michael@0 154 if (req.method != "POST") {
michael@0 155 res.writeHead(405);
michael@0 156 res.end('Unexpected method: ' + req.method);
michael@0 157 return;
michael@0 158 }
michael@0 159
michael@0 160 var post_hash = crypto.createHash('md5');
michael@0 161 req.on('data', function receivePostData(chunk) {
michael@0 162 post_hash.update(chunk.toString());
michael@0 163 });
michael@0 164 req.on('end', function finishPost() {
michael@0 165 var md5 = post_hash.digest('hex');
michael@0 166 res.setHeader('X-Calculated-MD5', md5);
michael@0 167 res.writeHead(200);
michael@0 168 res.end(content);
michael@0 169 });
michael@0 170
michael@0 171 return;
michael@0 172 }
michael@0 173
michael@0 174 res.setHeader('Content-Type', 'text/html');
michael@0 175 res.writeHead(200);
michael@0 176 res.end(content);
michael@0 177 }
michael@0 178
michael@0 179 // Set up the SSL certs for our server
michael@0 180 var options = {
michael@0 181 key: fs.readFileSync(__dirname + '/../moz-spdy/spdy-key.pem'),
michael@0 182 cert: fs.readFileSync(__dirname + '/../moz-spdy/spdy-cert.pem'),
michael@0 183 ca: fs.readFileSync(__dirname + '/../moz-spdy/spdy-ca.pem'),
michael@0 184 //, log: require('../node-http2/test/util').createLogger('server')
michael@0 185 };
michael@0 186
michael@0 187 var server = http2.createServer(options, handleRequest);
michael@0 188 server.on('connection', function(socket) {
michael@0 189 socket.on('error', function() {
michael@0 190 // Ignoring SSL socket errors, since they usually represent a connection that was tore down
michael@0 191 // by the browser because of an untrusted certificate. And this happens at least once, when
michael@0 192 // the first test case if done.
michael@0 193 });
michael@0 194 });
michael@0 195 server.listen(6944);
michael@0 196 console.log('HTTP2 server listening on port 6944');

mercurial