testing/xpcshell/node-http2/node_modules/http2-protocol/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/node_modules/http2-protocol/example/server.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +var fs = require('fs');
     1.5 +var path = require('path');
     1.6 +var http2 = require('..');
     1.7 +var tls = require('tls');
     1.8 +
     1.9 +var exists = fs.existsSync;
    1.10 +var stat = fs.statSync;
    1.11 +var read = fs.createReadStream;
    1.12 +var join = path.join;
    1.13 +
    1.14 +// Advertised protocol version
    1.15 +var implementedVersion = http2.ImplementedVersion;
    1.16 +
    1.17 +// Bunyan logger
    1.18 +var log = require('../test/util').createLogger('server');
    1.19 +
    1.20 +// We cache one file to be able to do simple performance tests without waiting for the disk
    1.21 +var cachedFile = fs.readFileSync(path.join(__dirname, './server.js'));
    1.22 +var cachedUrl = '/server.js';
    1.23 +
    1.24 +// Creating the server
    1.25 +tls.createServer({
    1.26 +  ALPNProtocols: [implementedVersion],
    1.27 +  NPNProtocols: [implementedVersion],
    1.28 +  key: fs.readFileSync(path.join(__dirname, '/localhost.key')),
    1.29 +  cert: fs.readFileSync(path.join(__dirname, '/localhost.crt'))
    1.30 +}).on('secureConnection', onConnection).listen(process.env.HTTP2_PORT || 8080);
    1.31 +
    1.32 +// Handling incoming connections
    1.33 +function onConnection(socket) {
    1.34 +  var endpoint = new http2.Endpoint(log, 'SERVER', {});
    1.35 +  endpoint.pipe(socket).pipe(endpoint);
    1.36 +
    1.37 +  endpoint.on('stream', function(stream) {
    1.38 +    stream.on('headers', function(headers) {
    1.39 +      var path = headers[':path'];
    1.40 +      var filename = join(__dirname, path);
    1.41 +
    1.42 +      // Serving server.js from cache. Useful for microbenchmarks.
    1.43 +      if (path == cachedUrl) {
    1.44 +        stream.headers({ ':status': 200 });
    1.45 +        stream.end(cachedFile);
    1.46 +      }
    1.47 +
    1.48 +      // Reading file from disk if it exists and is safe.
    1.49 +      else if ((filename.indexOf(__dirname) === 0) && exists(filename) && stat(filename).isFile()) {
    1.50 +        stream.headers({ ':status': 200 });
    1.51 +
    1.52 +        // If they download the certificate, push the private key too, they might need it.
    1.53 +        if (path === '/localhost.crt') {
    1.54 +          var push = stream.promise({
    1.55 +            ':method': 'GET',
    1.56 +            ':scheme': headers[':scheme'],
    1.57 +            ':authority': headers[':authority'],
    1.58 +            ':path': '/localhost.key'
    1.59 +          });
    1.60 +          push.headers({ ':status': 200 });
    1.61 +          read(join(__dirname, '/localhost.key')).pipe(push);
    1.62 +        }
    1.63 +
    1.64 +        read(filename).pipe(stream);
    1.65 +      }
    1.66 +
    1.67 +      // Otherwise responding with 404.
    1.68 +      else {
    1.69 +        stream.headers({ ':status': 404 });
    1.70 +        stream.end();
    1.71 +      }
    1.72 +    });
    1.73 +  });
    1.74 +}

mercurial