testing/xpcshell/node-http2/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
michael@0 5 var options = process.env.HTTP2_PLAIN ? {
michael@0 6 plain: true
michael@0 7 } : {
michael@0 8 key: fs.readFileSync(path.join(__dirname, '/localhost.key')),
michael@0 9 cert: fs.readFileSync(path.join(__dirname, '/localhost.crt'))
michael@0 10 };
michael@0 11
michael@0 12 // Passing bunyan logger (optional)
michael@0 13 options.log = require('../test/util').createLogger('server');
michael@0 14
michael@0 15 // We cache one file to be able to do simple performance tests without waiting for the disk
michael@0 16 var cachedFile = fs.readFileSync(path.join(__dirname, './server.js'));
michael@0 17 var cachedUrl = '/server.js';
michael@0 18
michael@0 19 // Creating the server
michael@0 20 var server = http2.createServer(options, function(request, response) {
michael@0 21 var filename = path.join(__dirname, request.url);
michael@0 22
michael@0 23 // Serving server.js from cache. Useful for microbenchmarks.
michael@0 24 if (request.url === cachedUrl) {
michael@0 25 response.end(cachedFile);
michael@0 26 }
michael@0 27
michael@0 28 // Reading file from disk if it exists and is safe.
michael@0 29 else if ((filename.indexOf(__dirname) === 0) && fs.existsSync(filename) && fs.statSync(filename).isFile()) {
michael@0 30 response.writeHead('200');
michael@0 31
michael@0 32 // If they download the certificate, push the private key too, they might need it.
michael@0 33 if (response.push && request.url === '/localhost.crt') {
michael@0 34 var push = response.push('/localhost.key');
michael@0 35 push.writeHead(200);
michael@0 36 fs.createReadStream(path.join(__dirname, '/localhost.key')).pipe(push);
michael@0 37 }
michael@0 38
michael@0 39 fs.createReadStream(filename).pipe(response);
michael@0 40 }
michael@0 41
michael@0 42 // Otherwise responding with 404.
michael@0 43 else {
michael@0 44 response.writeHead('404');
michael@0 45 response.end();
michael@0 46 }
michael@0 47 });
michael@0 48
michael@0 49 server.listen(process.env.HTTP2_PORT || 8080);

mercurial