angel_http2 1.0.0

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

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

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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

Analysis

We analyzed this package on Jul 13, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
39 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
69
Learn more about scoring.

Platforms

Detected platforms: Flutter, other

Primary library: package:angel_http2/angel_http2.dart with components: io.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 6 hints.

    Run dartfmt to format lib/angel_http2.dart.

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

    Similar analysis of the following files failed:

    • lib/src/http2_request_context.dart (hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
angel_framework >=1.1.1 <2.0.0 1.1.5
body_parser ^1.0.0 1.1.0
combinator >=1.0.0-beta <2.0.0 1.0.0+3
http2 >=0.1.7 <2.0.0 0.1.8+1
http_parser ^3.0.0 3.1.2
mock_request ^1.0.0 1.0.3
pool ^1.0.0 1.3.5
tuple ^1.0.0 1.0.1
uuid ^0.5.3 0.5.3 1.0.1
Transitive dependencies
angel_http_exception 1.0.0+3
angel_model 1.0.0
angel_route 2.0.7
async 2.0.7
charcode 1.1.2
code_buffer 1.0.1
collection 1.14.10
container 0.1.2
convert 2.0.1
crypto 2.0.5
dart2_constant 1.0.1+dart2
http_server 0.9.8
json_god 2.0.0-beta+3
logging 0.11.3+1
matcher 0.12.3
merge_map 1.0.0
meta 1.1.5
mime 0.9.6+1
path 1.6.1
quiver_hashcode 1.0.0
random_string 0.0.1
source_span 1.4.0
stack_trace 1.9.2
string_scanner 1.0.2
typed_data 1.1.5
Dev dependencies
console ^2.0.0
http ^0.11.3
test ^0.12.0