]> Sergey Matveev's repositories - public-inbox.git/commitdiff
http: async getline supports push_back_read
authorEric Wong <e@80x24.org>
Sun, 22 May 2016 00:33:59 +0000 (00:33 +0000)
committerEric Wong <e@80x24.org>
Sun, 22 May 2016 00:34:07 +0000 (00:34 +0000)
Sometimes we need to read something to ensure it's a successful
response.

lib/PublicInbox/HTTPD/Async.pm

index ceba738e3ce3f9793c314b16c7a3fe3733b55358..8f3a6a0907c72d00aadcb69b09a651197032dd31 100644 (file)
@@ -26,7 +26,23 @@ sub event_read { $_[0]->{cb}->() }
 sub event_hup { $_[0]->{cb}->() }
 sub event_err { $_[0]->{cb}->() }
 sub sysread { shift->{sock}->sysread(@_) }
-sub getline { $_[0]->{sock}->getline };
+
+sub getline {
+       my ($self) = @_;
+       die 'getline called without $/ ref' unless ref $/;
+       while (1) {
+               my $ret = $self->read(8192); # Danga::Socket::read
+               return $$ret if defined $ret;
+
+               return unless $!{EAGAIN} || $!{EINTR};
+
+               # in case of spurious wakeup, hopefully we never hit this
+               my $vin = '';
+               vec($vin, $self->{fd}, 1) = 1;
+               my $n;
+               do { $n = select($vin, undef, undef, undef) } until $n;
+       }
+}
 
 sub close {
        my $self = shift;