testing/xpcshell/moz-spdy/moz-spdy.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/xpcshell/moz-spdy/moz-spdy.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,173 @@
     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 spdy = require('../node-spdy/lib/spdy.js');
     1.9 +var fs = require('fs');
    1.10 +var url = require('url');
    1.11 +var crypto = require('crypto');
    1.12 +
    1.13 +function getHttpContent(path) {
    1.14 +  var content = '<!doctype html>' +
    1.15 +                '<html>' +
    1.16 +                '<head><title>HOORAY!</title></head>' +
    1.17 +                '<body>You Win! (by requesting' + path + ')</body>' +
    1.18 +                '</html>';
    1.19 +  return content;
    1.20 +}
    1.21 +
    1.22 +function getHugeContent(size) {
    1.23 +  var content = '';
    1.24 +
    1.25 +  for (var i = 0; i < size; i++) {
    1.26 +    content += '0';
    1.27 +  }
    1.28 +
    1.29 +  return content;
    1.30 +}
    1.31 +
    1.32 +/* This takes care of responding to the multiplexed request for us */
    1.33 +var Multiplex = function() {};
    1.34 +
    1.35 +Multiplex.prototype = {
    1.36 +  mp1res: null,
    1.37 +  mp2res: null,
    1.38 +  buf: null,
    1.39 +  mp1start: 0,
    1.40 +  mp2start: 0,
    1.41 +
    1.42 +  checkReady: function() {
    1.43 +    if (this.mp1res != null && this.mp2res != null) {
    1.44 +      this.buf = getHugeContent(30*1024);
    1.45 +      this.mp1start = 0;
    1.46 +      this.mp2start = 0;
    1.47 +      this.send(this.mp1res, 0);
    1.48 +      setTimeout(function() { this.send(this.mp2res, 0); }.bind(this), 5);
    1.49 +    }
    1.50 +  },
    1.51 +
    1.52 +  send: function(res, start) {
    1.53 +    var end = start + 1024;
    1.54 +    if (end > this.buf.length)
    1.55 +      end = this.buf.length;
    1.56 +    var content = this.buf.substring(start, end);
    1.57 +    if (end < this.buf.length) {
    1.58 +      res.write(content);
    1.59 +      setTimeout(function() { this.send(res, end); }.bind(this), 10);
    1.60 +    } else {
    1.61 +      res.end(content);
    1.62 +    }
    1.63 +  },
    1.64 +};
    1.65 +
    1.66 +var m = new Multiplex();
    1.67 +
    1.68 +var post_hash = null;
    1.69 +function receivePostData(chunk) {
    1.70 +  post_hash.update(chunk.toString());
    1.71 +}
    1.72 +
    1.73 +function finishPost(res, content) {
    1.74 +  var md5 = post_hash.digest('hex');
    1.75 +  res.setHeader('X-Calculated-MD5', md5);
    1.76 +  res.writeHead(200);
    1.77 +  res.end(content);
    1.78 +}
    1.79 +
    1.80 +function handleRequest(req, res) {
    1.81 +  var u = url.parse(req.url);
    1.82 +  var content = getHttpContent(u.pathname);
    1.83 +
    1.84 +  if (req.streamID) {
    1.85 +    res.setHeader('X-Connection-Spdy', 'yes');
    1.86 +    res.setHeader('X-Spdy-StreamId', '' + req.streamID);
    1.87 +  } else {
    1.88 +    res.setHeader('X-Connection-Spdy', 'no');
    1.89 +  }
    1.90 +
    1.91 +  if (u.pathname == '/exit') {
    1.92 +    res.setHeader('Content-Type', 'text/plain');
    1.93 +    res.writeHead(200);
    1.94 +    res.end('ok');
    1.95 +    process.exit();
    1.96 +  } else if (u.pathname == '/multiplex1' && req.streamID) {
    1.97 +    res.setHeader('Content-Type', 'text/plain');
    1.98 +    res.writeHead(200);
    1.99 +    m.mp1res = res;
   1.100 +    m.checkReady();
   1.101 +    return;
   1.102 +  } else if (u.pathname == '/multiplex2' && req.streamID) {
   1.103 +    res.setHeader('Content-Type', 'text/plain');
   1.104 +    res.writeHead(200);
   1.105 +    m.mp2res = res;
   1.106 +    m.checkReady();
   1.107 +    return;
   1.108 +  } else if (u.pathname == "/header") {
   1.109 +    m = new Multiplex();
   1.110 +    var val = req.headers["x-test-header"];
   1.111 +    if (val) {
   1.112 +      res.setHeader("X-Received-Test-Header", val);
   1.113 +    }
   1.114 +  } else if (u.pathname == "/push") {
   1.115 +    res.push('/push.js',
   1.116 +     { 'content-type': 'application/javascript',
   1.117 +       'pushed' : 'yes',
   1.118 +       'content-length' : 11,
   1.119 +       'X-Connection-Spdy': 'yes'},
   1.120 +     function(err, stream) {
   1.121 +       if (err) return;
   1.122 +         stream.end('// comments');
   1.123 +       });
   1.124 +      content = '<head> <script src="push.js"/></head>body text';
   1.125 +  } else if (u.pathname == "/push2") {
   1.126 +      res.push('/push2.js',
   1.127 +       { 'content-type': 'application/javascript',
   1.128 +	 'pushed' : 'yes',
   1.129 +	 // no content-length
   1.130 +	 'X-Connection-Spdy': 'yes'},
   1.131 +       function(err, stream) {
   1.132 +        if (err) return;
   1.133 +         stream.end('// comments');
   1.134 +       });
   1.135 +      content = '<head> <script src="push2.js"/></head>body text';
   1.136 +  } else if (u.pathname == "/big") {
   1.137 +    content = getHugeContent(128 * 1024);
   1.138 +    var hash = crypto.createHash('md5');
   1.139 +    hash.update(content);
   1.140 +    var md5 = hash.digest('hex');
   1.141 +    res.setHeader("X-Expected-MD5", md5);
   1.142 +  } else if (u.pathname == "/post") {
   1.143 +    if (req.method != "POST") {
   1.144 +      res.writeHead(405);
   1.145 +      res.end('Unexpected method: ' + req.method);
   1.146 +      return;
   1.147 +    }
   1.148 +
   1.149 +    post_hash = crypto.createHash('md5');
   1.150 +    req.on('data', receivePostData);
   1.151 +    req.on('end', function () { finishPost(res, content); });
   1.152 +
   1.153 +    return;
   1.154 +  }
   1.155 +
   1.156 +  res.setHeader('Content-Type', 'text/html');
   1.157 +  res.writeHead(200);
   1.158 +  res.end(content);
   1.159 +}
   1.160 +
   1.161 +// Set up the SSL certs for our server
   1.162 +var options = {
   1.163 +  key: fs.readFileSync(__dirname + '/spdy-key.pem'),
   1.164 +  cert: fs.readFileSync(__dirname + '/spdy-cert.pem'),
   1.165 +  ca: fs.readFileSync(__dirname + '/spdy-ca.pem'),
   1.166 +  windowSize: 16000000,
   1.167 +};
   1.168 +
   1.169 +spdy.createServer(options, handleRequest).listen(4443);
   1.170 +console.log('SPDY server listening on port 4443');
   1.171 +
   1.172 +// Set up to exit when the user finishes our stdin
   1.173 +process.stdin.resume();
   1.174 +process.stdin.on('end', function () {
   1.175 +  process.exit();
   1.176 +});

mercurial