Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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); |