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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 var fs = require('fs');
michael@0 2 var path = require('path');
michael@0 3 var http2 = require('..');
michael@0 4 var tls = require('tls');
michael@0 5
michael@0 6 var exists = fs.existsSync;
michael@0 7 var stat = fs.statSync;
michael@0 8 var read = fs.createReadStream;
michael@0 9 var join = path.join;
michael@0 10
michael@0 11 // Advertised protocol version
michael@0 12 var implementedVersion = http2.ImplementedVersion;
michael@0 13
michael@0 14 // Bunyan logger
michael@0 15 var log = require('../test/util').createLogger('server');
michael@0 16
michael@0 17 // We cache one file to be able to do simple performance tests without waiting for the disk
michael@0 18 var cachedFile = fs.readFileSync(path.join(__dirname, './server.js'));
michael@0 19 var cachedUrl = '/server.js';
michael@0 20
michael@0 21 // Creating the server
michael@0 22 tls.createServer({
michael@0 23 ALPNProtocols: [implementedVersion],
michael@0 24 NPNProtocols: [implementedVersion],
michael@0 25 key: fs.readFileSync(path.join(__dirname, '/localhost.key')),
michael@0 26 cert: fs.readFileSync(path.join(__dirname, '/localhost.crt'))
michael@0 27 }).on('secureConnection', onConnection).listen(process.env.HTTP2_PORT || 8080);
michael@0 28
michael@0 29 // Handling incoming connections
michael@0 30 function onConnection(socket) {
michael@0 31 var endpoint = new http2.Endpoint(log, 'SERVER', {});
michael@0 32 endpoint.pipe(socket).pipe(endpoint);
michael@0 33
michael@0 34 endpoint.on('stream', function(stream) {
michael@0 35 stream.on('headers', function(headers) {
michael@0 36 var path = headers[':path'];
michael@0 37 var filename = join(__dirname, path);
michael@0 38
michael@0 39 // Serving server.js from cache. Useful for microbenchmarks.
michael@0 40 if (path == cachedUrl) {
michael@0 41 stream.headers({ ':status': 200 });
michael@0 42 stream.end(cachedFile);
michael@0 43 }
michael@0 44
michael@0 45 // Reading file from disk if it exists and is safe.
michael@0 46 else if ((filename.indexOf(__dirname) === 0) && exists(filename) && stat(filename).isFile()) {
michael@0 47 stream.headers({ ':status': 200 });
michael@0 48
michael@0 49 // If they download the certificate, push the private key too, they might need it.
michael@0 50 if (path === '/localhost.crt') {
michael@0 51 var push = stream.promise({
michael@0 52 ':method': 'GET',
michael@0 53 ':scheme': headers[':scheme'],
michael@0 54 ':authority': headers[':authority'],
michael@0 55 ':path': '/localhost.key'
michael@0 56 });
michael@0 57 push.headers({ ':status': 200 });
michael@0 58 read(join(__dirname, '/localhost.key')).pipe(push);
michael@0 59 }
michael@0 60
michael@0 61 read(filename).pipe(stream);
michael@0 62 }
michael@0 63
michael@0 64 // Otherwise responding with 404.
michael@0 65 else {
michael@0 66 stream.headers({ ':status': 404 });
michael@0 67 stream.end();
michael@0 68 }
michael@0 69 });
michael@0 70 });
michael@0 71 }

mercurial