example/server.js
function onConnection(socket) {
  var endpoint = new http2.Endpoint(log, 'SERVER', {});
  endpoint.pipe(socket).pipe(endpoint);

  endpoint.on('stream', function(stream) {
    stream.on('headers', function(headers) {
      var path = headers[':path'];
      var filename = join(__dirname, path);

      // Serving server.js from cache. Useful for microbenchmarks.
      if (path == cachedUrl) {
        stream.headers({ ':status': 200 });
        stream.end(cachedFile);
      }

      // Reading file from disk if it exists and is safe.
      else if ((filename.indexOf(__dirname) === 0) && exists(filename) && stat(filename).isFile()) {
        stream.headers({ ':status': 200 });

        // If they download the certificate, push the private key too, they might need it.
        if (path === '/localhost.crt') {
          var push = stream.promise({
            ':method': 'GET',
            ':scheme': headers[':scheme'],
            ':authority': headers[':authority'],
            ':path': '/localhost.key'
          });
          push.headers({ ':status': 200 });
          read(join(__dirname, '/localhost.key')).pipe(push);
        }

        read(filename).pipe(stream);
      }

      // Otherwise responding with 404.
      else {
        stream.headers({ ':status': 404 });
        stream.end();
      }
    });
  });
}
function onConnection(socket) {
  var endpoint = new http2.Endpoint(log, 'SERVER', {});
  endpoint.pipe(socket).pipe(endpoint);

  endpoint.on('stream', function(stream) {
    stream.on('headers', function(headers) {
      var path = headers[':path'];
      var filename = join(__dirname, path);

      // Serving server.js from cache. Useful for microbenchmarks.
      if (path == cachedUrl) {
        stream.headers({ ':status': 200 });
        stream.end(cachedFile);
      }

      // Reading file from disk if it exists and is safe.
      else if ((filename.indexOf(__dirname) === 0) && exists(filename) && stat(filename).isFile()) {
        stream.headers({ ':status': 200 });

        // If they download the certificate, push the private key too, they might need it.
        if (path === '/localhost.crt') {
          var push = stream.promise({
            ':method': 'GET',
            ':scheme': headers[':scheme'],
            ':authority': headers[':authority'],
            ':path': '/localhost.key'
          });
          push.headers({ ':status': 200 });
          read(join(__dirname, '/localhost.key')).pipe(push);
        }

        read(filename).pipe(stream);
      }

      // Otherwise responding with 404.
      else {
        stream.headers({ ':status': 404 });
        stream.end();
      }
    });
  });
}
function onConnection ( socket ) { var endpoint = new http2 . Endpoint ( log , 'SERVER' , { } ) ; endpoint . pipe ( socket ) . pipe ( endpoint ) ; endpoint . on ( 'stream' , function ( stream ) { stream . on ( 'headers' , function ( headers ) { var path = headers [ ':path' ] ; var filename = join ( __dirname , path ) ; // Serving server.js from cache. Useful for microbenchmarks. if ( path == cachedUrl ) { stream . headers ( { ':status' : 200 } ) ; stream . end ( cachedFile ) ; } // Reading file from disk if it exists and is safe. else if ( ( filename . indexOf ( __dirname ) === 0 ) && exists ( filename ) && stat ( filename ) . isFile ( ) ) { stream . headers ( { ':status' : 200 } ) ; // If they download the certificate, push the private key too, they might need it. if ( path === '/localhost.crt' ) { var push = stream . promise ( { ':method' : 'GET' , ':scheme' : headers [ ':scheme' ] , ':authority' : headers [ ':authority' ] , ':path' : '/localhost.key' } ) ; push . headers ( { ':status' : 200 } ) ; read ( join ( __dirname , '/localhost.key' ) ) . pipe ( push ) ; } read ( filename ) . pipe ( stream ) ; } // Otherwise responding with 404. else { stream . headers ( { ':status' : 404 } ) ; stream . end ( ) ; } } ) ; } ) ; }
Handling incoming connections
Handling incoming connections