testing/xpcshell/node-http2/example/server.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/xpcshell/node-http2/example/server.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,49 @@
     1.4 +var fs = require('fs');
     1.5 +var path = require('path');
     1.6 +var http2 = require('..');
     1.7 +
     1.8 +var options = process.env.HTTP2_PLAIN ? {
     1.9 +  plain: true
    1.10 +} : {
    1.11 +  key: fs.readFileSync(path.join(__dirname, '/localhost.key')),
    1.12 +  cert: fs.readFileSync(path.join(__dirname, '/localhost.crt'))
    1.13 +};
    1.14 +
    1.15 +// Passing bunyan logger (optional)
    1.16 +options.log = require('../test/util').createLogger('server');
    1.17 +
    1.18 +// We cache one file to be able to do simple performance tests without waiting for the disk
    1.19 +var cachedFile = fs.readFileSync(path.join(__dirname, './server.js'));
    1.20 +var cachedUrl = '/server.js';
    1.21 +
    1.22 +// Creating the server
    1.23 +var server = http2.createServer(options, function(request, response) {
    1.24 +  var filename = path.join(__dirname, request.url);
    1.25 +
    1.26 +  // Serving server.js from cache. Useful for microbenchmarks.
    1.27 +  if (request.url === cachedUrl) {
    1.28 +    response.end(cachedFile);
    1.29 +  }
    1.30 +
    1.31 +  // Reading file from disk if it exists and is safe.
    1.32 +  else if ((filename.indexOf(__dirname) === 0) && fs.existsSync(filename) && fs.statSync(filename).isFile()) {
    1.33 +    response.writeHead('200');
    1.34 +
    1.35 +    // If they download the certificate, push the private key too, they might need it.
    1.36 +    if (response.push && request.url === '/localhost.crt') {
    1.37 +      var push = response.push('/localhost.key');
    1.38 +      push.writeHead(200);
    1.39 +      fs.createReadStream(path.join(__dirname, '/localhost.key')).pipe(push);
    1.40 +    }
    1.41 +
    1.42 +    fs.createReadStream(filename).pipe(response);
    1.43 +  }
    1.44 +
    1.45 +  // Otherwise responding with 404.
    1.46 +  else {
    1.47 +    response.writeHead('404');
    1.48 +    response.end();
    1.49 +  }
    1.50 +});
    1.51 +
    1.52 +server.listen(process.env.HTTP2_PORT || 8080);

mercurial