public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 290cce743222735d4040db2b07cb9770996c2ab8 1178 bytes (raw)
$ git show HEAD:lib/PublicInbox/GetlineResponse.pm	# shows this blob on the CLI

 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
 
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# For generic PSGI servers (not public-inbox-httpd/netd) which assumes their
# getline response bodies can be backpressure-aware for slow clients
# This depends on rpipe being _blocking_ on getline.
package PublicInbox::GetlineResponse;
use v5.12;

sub response {
	my ($qsp) = @_;
	my ($res, $rbuf);
	do { # read header synchronously
		sysread($qsp->{rpipe}, $rbuf, 65536);
		$res = $qsp->parse_hdr_done($rbuf); # fills $bref
	} until defined($res);
	my ($wcb, $filter) = $qsp->yield_pass(undef, $res) or return;
	my $self = $res->[2] = bless {
		qsp => $qsp,
		filter => $filter,
	}, __PACKAGE__;
	my ($bref) = @{delete $qsp->{yield_parse_hdr}};
	$self->{rbuf} = $$bref if $$bref ne '';
	$wcb->($res);
}

sub getline {
	my ($self) = @_;
	my $rpipe = $self->{qsp}->{rpipe} // do {
		delete($self->{qsp})->finish;
		return; # EOF was set on previous call
	};
	my $buf = delete($self->{rbuf}) // $rpipe->getline;
	$buf // delete($self->{qsp}->{rpipe}); # set EOF for next call
	$self->{filter} ? $self->{filter}->translate($buf) : $buf;
}

sub close {}

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git