From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 91A661F4B7 for ; Mon, 24 Jun 2019 02:56:50 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 32/57] ds: hoist out do_read from NNTP and HTTP Date: Mon, 24 Jun 2019 02:52:33 +0000 Message-Id: <20190624025258.25592-33-e@80x24.org> In-Reply-To: <20190624025258.25592-1-e@80x24.org> References: <20190624025258.25592-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Both NNTP and HTTP have common needs and we can factor out some common code to make dealing with IO::Socket::SSL easier. --- lib/PublicInbox/DS.pm | 10 ++++++++++ lib/PublicInbox/HTTP.pm | 14 +++----------- lib/PublicInbox/NNTP.pm | 9 ++------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index 9811405b..8735e888 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -473,6 +473,15 @@ next_buf: 1; # all done } +sub do_read ($$$$) { + my ($self, $rbuf, $len, $off) = @_; + my $r = sysread($self->{sock}, $$rbuf, $len, $off); + return ($r == 0 ? $self->close : $r) if defined $r; + # common for clients to break connections without warning, + # would be too noisy to log here: + $! == EAGAIN ? $self->watch_in1 : $self->close; +} + sub write_in_full ($$$$) { my ($fh, $bref, $len, $off) = @_; my $rv = 0; @@ -583,6 +592,7 @@ sub watch ($$) { $KQueue->EV_SET($fd, EVFILT_READ(), kq_flag(EPOLLIN, $ev)); $KQueue->EV_SET($fd, EVFILT_WRITE(), kq_flag(EPOLLOUT, $ev)); } + 0; } sub watch_in1 ($) { watch($_[0], EPOLLIN | EPOLLONESHOT) } diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index fbca9a54..7697ac5c 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -75,17 +75,9 @@ sub event_step { # called by PublicInbox::DS # otherwise we can be buffering infinitely w/o backpressure return read_input($self) if defined $self->{env}; - - my $off = bytes::length($self->{rbuf}); - my $r = sysread($self->{sock}, $self->{rbuf}, 8192, $off); - if (defined $r) { - return $self->close if $r == 0; - return rbuf_process($self); - } - - # common for clients to break connections without warning, - # would be too noisy to log here: - $! == EAGAIN ? $self->watch_in1 : $self->close; + my $rbuf = \($self->{rbuf}); + my $off = bytes::length($$rbuf); + $self->do_read($rbuf, 8192, $off) and rbuf_process($self); } sub rbuf_process { diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm index a9e54a68..42fbb255 100644 --- a/lib/PublicInbox/NNTP.pm +++ b/lib/PublicInbox/NNTP.pm @@ -941,17 +941,12 @@ sub event_step { use constant LINE_MAX => 512; # RFC 977 section 2.3 my $rbuf = \($self->{rbuf}); - my $r; + my $r = 1; if (index($$rbuf, "\n") < 0) { my $off = bytes::length($$rbuf); - $r = sysread($self->{sock}, $$rbuf, LINE_MAX, $off); - unless (defined $r) { - return $! == EAGAIN ? $self->watch_in1 : $self->close; - } - return $self->close if $r == 0; + $r = $self->do_read($rbuf, LINE_MAX, $off) or return; } - $r = 1; while ($r > 0 && $$rbuf =~ s/\A[ \t\r\n]*([^\r\n]*)\r?\n//) { my $line = $1; return $self->close if $line =~ /[[:cntrl:]]/s; -- EW