http_server icon indicating copy to clipboard operation
http_server copied to clipboard

Add gzip compression support

Open davenotik opened this issue 10 years ago • 4 comments

I discovered that my server wasn't respecting autoCompress = true because this http_server package adds its own content-length headers. See what @sgjesse said here:

https://github.com/dart-lang/sdk/issues/23350#issuecomment-108464543

Can we add support? I imagine many folks use this package with their servers and want to gzip.

davenotik avatar Oct 22 '15 19:10 davenotik

Have you seen shelf and shelf_static?

https://pub.dartlang.org/packages/shelf

https://pub.dartlang.org/packages/shelf_static

@kevmoo is there something we can recommend for gzip + shelf?

sethladd avatar Oct 22 '15 19:10 sethladd

This shouldn't be a problem with shelf. Just provide your own instance.

http://www.dartdocs.org/documentation/shelf/0.6.3+1/shelf.io/serveRequests.html

kevmoo avatar Oct 22 '15 19:10 kevmoo

Thanks for the help. Finally done refactoring for Shelf. Alas, still doesn't gzip.

    io.serve(_handler, address, port).then((server) {
      server.autoCompress = true;
      server.sessionTimeout = new DateTime.now()
              .add(new Duration(days: 365))
              .toUtc()
              .millisecondsSinceEpoch *
          1000;

      print('Serving at http://${server.address.host}:${server.port}');
    });

davenotik avatar Oct 27 '15 04:10 davenotik

Ok, got it working. I had to set chunked transfer-encoding in the headers. So I change the Response wherever I return files, e.g.:

  Future<shelf.Response> _handleFile(shelf.Request request) async {
    shelf.Response response = await this.staticHandler(request);
    response = response.change(headers: {'Transfer-Encoding': 'chunked'});
    return response;
  }

Voila: http://d.pr/i/Iaco (p.s. nice plug there)

davenotik avatar Oct 27 '15 05:10 davenotik