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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/xpcshell/moz-http2/moz-http2.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,196 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +var http2 = require('../node-http2');
     1.9 +var fs = require('fs');
    1.10 +var url = require('url');
    1.11 +var crypto = require('crypto');
    1.12 +
    1.13 +// Hook into the decompression code to log the decompressed name-value pairs
    1.14 +var http2_compression = require('../node-http2/node_modules/http2-protocol/lib/compressor');
    1.15 +var HeaderSetDecompressor = http2_compression.HeaderSetDecompressor;
    1.16 +var originalRead = HeaderSetDecompressor.prototype.read;
    1.17 +var lastDecompressor;
    1.18 +var decompressedPairs;
    1.19 +HeaderSetDecompressor.prototype.read = function() {
    1.20 +  if (this != lastDecompressor) {
    1.21 +    lastDecompressor = this;
    1.22 +    decompressedPairs = [];
    1.23 +  }
    1.24 +  var pair = originalRead.apply(this, arguments);
    1.25 +  if (pair) {
    1.26 +    decompressedPairs.push(pair);
    1.27 +  }
    1.28 +  return pair;
    1.29 +}
    1.30 +
    1.31 +function getHttpContent(path) {
    1.32 +  var content = '<!doctype html>' +
    1.33 +                '<html>' +
    1.34 +                '<head><title>HOORAY!</title></head>' +
    1.35 +                '<body>You Win! (by requesting' + path + ')</body>' +
    1.36 +                '</html>';
    1.37 +  return content;
    1.38 +}
    1.39 +
    1.40 +function generateContent(size) {
    1.41 +  var content = '';
    1.42 +  for (var i = 0; i < size; i++) {
    1.43 +    content += '0';
    1.44 +  }
    1.45 +  return content;
    1.46 +}
    1.47 +
    1.48 +/* This takes care of responding to the multiplexed request for us */
    1.49 +var m = {
    1.50 +  mp1res: null,
    1.51 +  mp2res: null,
    1.52 +  buf: null,
    1.53 +  mp1start: 0,
    1.54 +  mp2start: 0,
    1.55 +
    1.56 +  checkReady: function() {
    1.57 +    if (this.mp1res != null && this.mp2res != null) {
    1.58 +      this.buf = generateContent(30*1024);
    1.59 +      this.mp1start = 0;
    1.60 +      this.mp2start = 0;
    1.61 +      this.send(this.mp1res, 0);
    1.62 +      setTimeout(this.send.bind(this, this.mp2res, 0), 5);
    1.63 +    }
    1.64 +  },
    1.65 +
    1.66 +  send: function(res, start) {
    1.67 +    var end = Math.min(start + 1024, this.buf.length);
    1.68 +    var content = this.buf.substring(start, end);
    1.69 +    res.write(content);
    1.70 +    if (end < this.buf.length) {
    1.71 +      setTimeout(this.send.bind(this, res, end), 10);
    1.72 +    } else {
    1.73 +      res.end();
    1.74 +    }
    1.75 +  }
    1.76 +};
    1.77 +
    1.78 +function handleRequest(req, res) {
    1.79 +  var u = url.parse(req.url);
    1.80 +  var content = getHttpContent(u.pathname);
    1.81 +  var push;
    1.82 +
    1.83 +  if (req.httpVersionMajor === 2) {
    1.84 +    res.setHeader('X-Connection-Http2', 'yes');
    1.85 +    res.setHeader('X-Http2-StreamId', '' + req.stream.id);
    1.86 +  } else {
    1.87 +    res.setHeader('X-Connection-Http2', 'no');
    1.88 +  }
    1.89 +
    1.90 +  if (u.pathname === '/exit') {
    1.91 +    res.setHeader('Content-Type', 'text/plain');
    1.92 +    res.writeHead(200);
    1.93 +    res.end('ok');
    1.94 +    process.exit();
    1.95 +  }
    1.96 +
    1.97 +  else if ((u.pathname === '/multiplex1') && (req.httpVersionMajor === 2)) {
    1.98 +    res.setHeader('Content-Type', 'text/plain');
    1.99 +    res.writeHead(200);
   1.100 +    m.mp1res = res;
   1.101 +    m.checkReady();
   1.102 +    return;
   1.103 +  }
   1.104 +
   1.105 +  else if ((u.pathname === '/multiplex2') && (req.httpVersionMajor === 2)) {
   1.106 +    res.setHeader('Content-Type', 'text/plain');
   1.107 +    res.writeHead(200);
   1.108 +    m.mp2res = res;
   1.109 +    m.checkReady();
   1.110 +    return;
   1.111 +  }
   1.112 +
   1.113 +  else if (u.pathname === "/header") {
   1.114 +    var val = req.headers["x-test-header"];
   1.115 +    if (val) {
   1.116 +      res.setHeader("X-Received-Test-Header", val);
   1.117 +    }
   1.118 +  }
   1.119 +
   1.120 +  else if (u.pathname === "/cookie_crumbling") {
   1.121 +    res.setHeader("X-Received-Header-Pairs", JSON.stringify(decompressedPairs));
   1.122 +  }
   1.123 +
   1.124 +  else if (u.pathname === "/push") {
   1.125 +    push = res.push('/push.js');
   1.126 +    push.writeHead(200, {
   1.127 +      'content-type': 'application/javascript',
   1.128 +      'pushed' : 'yes',
   1.129 +      'content-length' : 11,
   1.130 +      'X-Connection-Http2': 'yes'
   1.131 +    });
   1.132 +    push.end('// comments');
   1.133 +    content = '<head> <script src="push.js"/></head>body text';
   1.134 +  }
   1.135 +
   1.136 +  else if (u.pathname === "/push2") {
   1.137 +    push = res.push('/push2.js');
   1.138 +    push.writeHead(200, {
   1.139 +      'content-type': 'application/javascript',
   1.140 +      'pushed' : 'yes',
   1.141 +      // no content-length
   1.142 +      'X-Connection-Http2': 'yes'
   1.143 +    });
   1.144 +    push.end('// comments');
   1.145 +    content = '<head> <script src="push2.js"/></head>body text';
   1.146 +  }
   1.147 +
   1.148 +  else if (u.pathname === "/big") {
   1.149 +    content = generateContent(128 * 1024);
   1.150 +    var hash = crypto.createHash('md5');
   1.151 +    hash.update(content);
   1.152 +    var md5 = hash.digest('hex');
   1.153 +    res.setHeader("X-Expected-MD5", md5);
   1.154 +  }
   1.155 +
   1.156 +  else if (u.pathname === "/post") {
   1.157 +    if (req.method != "POST") {
   1.158 +      res.writeHead(405);
   1.159 +      res.end('Unexpected method: ' + req.method);
   1.160 +      return;
   1.161 +    }
   1.162 +
   1.163 +    var post_hash = crypto.createHash('md5');
   1.164 +    req.on('data', function receivePostData(chunk) {
   1.165 +      post_hash.update(chunk.toString());
   1.166 +    });
   1.167 +    req.on('end', function finishPost() {
   1.168 +      var md5 = post_hash.digest('hex');
   1.169 +      res.setHeader('X-Calculated-MD5', md5);
   1.170 +      res.writeHead(200);
   1.171 +      res.end(content);
   1.172 +    });
   1.173 +
   1.174 +    return;
   1.175 +  }
   1.176 +
   1.177 +  res.setHeader('Content-Type', 'text/html');
   1.178 +  res.writeHead(200);
   1.179 +  res.end(content);
   1.180 +}
   1.181 +
   1.182 +// Set up the SSL certs for our server
   1.183 +var options = {
   1.184 +  key: fs.readFileSync(__dirname + '/../moz-spdy/spdy-key.pem'),
   1.185 +  cert: fs.readFileSync(__dirname + '/../moz-spdy/spdy-cert.pem'),
   1.186 +  ca: fs.readFileSync(__dirname + '/../moz-spdy/spdy-ca.pem'),
   1.187 +  //, log: require('../node-http2/test/util').createLogger('server')
   1.188 +};
   1.189 +
   1.190 +var server = http2.createServer(options, handleRequest);
   1.191 +server.on('connection', function(socket) {
   1.192 +  socket.on('error', function() {
   1.193 +    // Ignoring SSL socket errors, since they usually represent a connection that was tore down
   1.194 +    // by the browser because of an untrusted certificate. And this happens at least once, when
   1.195 +    // the first test case if done.
   1.196 +  });
   1.197 +});
   1.198 +server.listen(6944);
   1.199 +console.log('HTTP2 server listening on port 6944');

mercurial