angel_http2 1.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 0

http2

Pub build status

An Angel adapter to serve applications over HTTP/2. Supports server push, falling back to HTTP/1.x, and more.

Requires SSL.

Usage

The library package:angel_http2/angel_http2.dart exports AngelHttp2, a class that implements a multi-protocol server. HTTP/2 requests will be handled by the class, while HTTP/1.x requests will be fired out of the onHttp1 stream. Typically, you should hook up the onHttp1 stream to an instance of AngelHttp to serve the same application instance over multiple protocols.

main() async {
  var app = new Angel();
  app.logger = new Logger('angel')..onRecord.listen(print);

  app.get('/', 'Hello HTTP/2!!!');

  var ctx = new SecurityContext()
    ..useCertificateChain('dev.pem')
    ..usePrivateKey('dev.key', password: 'dartdart');

  try {
    ctx.setAlpnProtocols(['h2'], true);
  } catch (e, st) {
    app.logger.severe(
      'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
      e,
      st,
    );
  }

  var http1 = new AngelHttp(app);
  var http2 = new AngelHttp2(app, ctx);

  // HTTP/1.x requests will fallback to `AngelHttp`
  http2.onHttp1.listen(http1.handleRequest);

  var server = await http2.startServer('127.0.0.1', 3000);
  print('Listening at https://${server.address.address}:${server.port}');
}

Server Push

In HTTP/2, the server can send down resources, even if they were not explicitly requested. This can be very useful for SPA's, and to decrease initial load times in general.

This is also essential for an implementation of the PRPL pattern.

To push a resource, call Http2ResponseContextImpl.push. This method itself returns an Http2ResponseContextImpl, which means that pushed resources also can be used with API's like sendFile, serialize, and everything available for use with a ResponseContext.

You must use streaming methods to push content via server push. i.e. addStream, pipe, streamFile.

Response buffering does not work with server push.

configureServer(Angel app) async {
    var publicDir = new Directory('example/public');
    var indexHtml = new File.fromUri(publicDir.uri.resolve('index.html'));
    var styleCss = new File.fromUri(publicDir.uri.resolve('style.css'));
    var appJs = new File.fromUri(publicDir.uri.resolve('app.js'));
    
    // Send files when requested
    app
      ..get('/style.css', (res) => res.sendFile(styleCss))
      ..get('/app.js', (res) => res.sendFile(appJs));
    
    app.get('/', (ResponseContext res) async {
      // Regardless of whether we pushed other resources, let's still send /index.html.
      await res.streamFile(indexHtml);
  
      // If the client is HTTP/2 and supports server push, let's
      // send down /style.css and /app.js as well, to improve initial load time.
      if (res is Http2ResponseContext && res.canPush) {
        await res.push('/style.css').streamFile(styleCss);
        await res.push('/app.js').streamFile(appJs);
      }
    });
}

1.0.0

  • First release.

example/main.dart

import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_http2/angel_http2.dart';
import 'package:logging/logging.dart';
import 'pretty_logging.dart';

main() async {
  var app = new Angel()
    ..injectEncoders({
      'gzip': GZIP.encoder,
      'deflate': ZLIB.encoder,
    });
  app.logger = new Logger('angel')..onRecord.listen(prettyLog);

  app.get('/', 'Hello HTTP/2!!!');

  app.use((RequestContext req) => throw new AngelHttpException.notFound(
      message: 'No file exists at ${req.uri.path}'));

  var ctx = new SecurityContext()
    ..useCertificateChain('dev.pem')
    ..usePrivateKey('dev.key', password: 'dartdart');

  try {
    ctx.setAlpnProtocols(['h2'], true);
  } catch (e, st) {
    app.logger.severe(
      'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
      e,
      st,
    );
  }

  var http1 = new AngelHttp(app);
  var http2 = new AngelHttp2(app, ctx);

  // HTTP/1.x requests will fallback to `AngelHttp`
  http2.onHttp1.listen(http1.handleRequest);

  var server = await http2.startServer('127.0.0.1', 3000);
  print('Listening at https://${server.address.address}:${server.port}');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_http2: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_http2/angel_http2.dart';
  
Version Uploaded Documentation Archive
1.0.0 Mar 5, 2018 Go to the documentation of angel_http2 1.0.0 Download angel_http2 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Fix dependencies in pubspec.yaml.

Running pub upgrade failed with the following output:

ERR: The current Dart SDK version is 2.0.0.
 
 Because angel_http2 depends on uuid >=0.0.7 <1.0.3 which requires SDK version <2.0.0, version solving failed.

Health suggestions

Format lib/angel_http2.dart.

Run dartfmt to format lib/angel_http2.dart.

Format lib/src/adapter.dart.

Run dartfmt to format lib/src/adapter.dart.

Maintenance issues and suggestions

Fix platform conflicts. (-20 points)

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0