File: gzip-streamed.psgi

package info (click to toggle)
libtest-http-localserver-perl 0.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: perl: 793; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,173 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!perl
# Created by Hauke Daempfling 2018
use strict;
use warnings;
use IO::Compress::Gzip qw/$GzipError Z_PARTIAL_FLUSH/;

our $VERSION = '0.67';

my $app = sub {
	my $env = shift;
	die "This app needs a server that supports psgi.streaming"
		unless $env->{'psgi.streaming'};
	die "The client did not send the 'Accept-Encoding: gzip' header"
		unless defined $env->{HTTP_ACCEPT_ENCODING}
			&& $env->{HTTP_ACCEPT_ENCODING} =~ /\bgzip\b/;
	# Note some browsers don't correctly support gzip correctly,
	# see e.g. https://metacpan.org/pod/Plack::Middleware::Deflater
	# but we're not checking that here (and we don't set the Vary header)
	return sub {
		my $respond = shift;
		my $zipped;
		my $z = IO::Compress::Gzip->new(\$zipped)
			or die "IO::Compress::Gzip: $GzipError";
		my $w = $respond->([ 200, [
				'Content-Type' => 'text/plain; charset=ascii',
				'Content-Encoding' => 'gzip',
			] ]);
		for (1..10) {
			$z->print("Hello, it is ".gmtime." GMT\n");
			$z->flush(Z_PARTIAL_FLUSH);
			$w->write($zipped) if defined $zipped;
			$zipped = undef;
			sleep 1;
		}
		$z->print("Goodbye!\n");
		$z->close;
		$w->write($zipped) if defined $zipped;
		$w->close;
	};
};