unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 5/8] favor procedural calls for most private functions
Date: Mon, 29 Feb 2016 01:41:04 +0000	[thread overview]
Message-ID: <20160229014107.7396-6-e@80x24.org> (raw)
In-Reply-To: <20160229014107.7396-1-e@80x24.org>

This makes for better compile-time checking and also helps
document which calls are private for HTTP and NNTP.

While we're at it, use IO::Handle::* functions procedurally,
too, since we know we're working with native glob handles.
---
 lib/PublicInbox/HTTP.pm   | 22 +++++++++++-----------
 lib/PublicInbox/NNTP.pm   | 28 ++++++++++++++--------------
 lib/PublicInbox/Spawn.pm  |  3 ++-
 script/public-inbox-httpd |  2 +-
 4 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 14971f4..aaae7ab 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -68,7 +68,7 @@ sub rbuf_process {
 
 	# We do not support Trailers in chunked requests, for now
 	# (they are rarely-used and git (as of 2.7.2) does not use them)
-	return $self->quit(400) if $r == -1 || $env{HTTP_TRAILER};
+	return quit($self, 400) if $r == -1 || $env{HTTP_TRAILER};
 	return $self->watch_read(1) if $r < 0; # incomplete
 	$self->{rbuf} = substr($self->{rbuf}, $r);
 	my $len = input_prepare($self, \%env);
@@ -90,7 +90,7 @@ sub event_read_input ($) {
 	while ($len > 0) {
 		if ($$rbuf ne '') {
 			my $w = write_in_full($input, $rbuf, $len);
-			return $self->write_err unless $w;
+			return write_err($self) unless $w;
 			$len -= $w;
 			die "BUG: $len < 0 (w=$w)" if $len < 0;
 			if ($len == 0) { # next request may be pipelined
@@ -100,7 +100,7 @@ sub event_read_input ($) {
 			$$rbuf = '';
 		}
 		my $r = sysread($sock, $$rbuf, 8192);
-		return $self->recv_err($r, $len) unless $r;
+		return recv_err($self, $r, $len) unless $r;
 		# continue looping if $r > 0;
 	}
 	app_dispatch($self);
@@ -238,7 +238,7 @@ sub write_err {
 	my $err = $self->{env}->{'psgi.errors'};
 	my $msg = $! || '(zero write)';
 	$err->print("error buffering to input: $msg\n");
-	$self->quit(500);
+	quit($self, 500);
 }
 
 sub recv_err {
@@ -250,7 +250,7 @@ sub recv_err {
 	}
 	my $err = $self->{env}->{'psgi.errors'};
 	$err->print("error reading for input: $! ($len bytes remaining)\n");
-	$self->quit(500);
+	quit($self, 500);
 }
 
 sub write_in_full {
@@ -278,20 +278,20 @@ sub event_read_input_chunked { # unlikely...
 	while (1) { # chunk start
 		if ($len == CHUNK_ZEND) {
 			return app_dispatch($self) if $$rbuf =~ s/\A\r\n//s;
-			return $self->quit(400) if length($$rbuf) > 2;
+			return quit($self, 400) if length($$rbuf) > 2;
 		}
 		if ($len == CHUNK_END) {
 			if ($$rbuf =~ s/\A\r\n//s) {
 				$len = CHUNK_START;
 			} elsif (length($$rbuf) > 2) {
-				return $self->quit(400);
+				return quit($self, 400);
 			}
 		}
 		if ($len == CHUNK_START) {
 			if ($$rbuf =~ s/\A([a-f0-9]+).*?\r\n//i) {
 				$len = hex $1;
 			} elsif (length($$rbuf) > CHUNK_MAX_HDR) {
-				return $self->quit(400);
+				return quit($self, 400);
 			}
 			# will break from loop since $len >= 0
 		}
@@ -299,7 +299,7 @@ sub event_read_input_chunked { # unlikely...
 		if ($len < 0) { # chunk header is trickled, read more
 			my $off = length($$rbuf);
 			my $r = sysread($sock, $$rbuf, 8192, $off);
-			return $self->recv_err($r, $len) unless $r;
+			return recv_err($self, $r, $len) unless $r;
 			# (implicit) goto chunk_start if $r > 0;
 		}
 		$len = CHUNK_ZEND if $len == 0;
@@ -308,7 +308,7 @@ sub event_read_input_chunked { # unlikely...
 		until ($len <= 0) {
 			if ($$rbuf ne '') {
 				my $w = write_in_full($input, $rbuf, $len);
-				return $self->write_err unless $w;
+				return write_err($self) unless $w;
 				$len -= $w;
 				if ($len == 0) {
 					# we may have leftover data to parse
@@ -324,7 +324,7 @@ sub event_read_input_chunked { # unlikely...
 			if ($$rbuf eq '') {
 				# read more of current chunk
 				my $r = sysread($sock, $$rbuf, 8192);
-				return $self->recv_err($r, $len) unless $r;
+				return recv_err($self, $r, $len) unless $r;
 			}
 		}
 	}
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index fbf8f7f..8740377 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -212,7 +212,7 @@ sub cmd_listgroup ($;$) {
 	}
 
 	$self->{ng} or return '412 no newsgroup selected';
-	$self->long_response(0, long_response_limit, sub {
+	long_response($self, 0, long_response_limit, sub {
 		my ($i) = @_;
 		my $nr = $self->{ng}->mm->id_batch($$i, sub {
 			my ($ary) = @_;
@@ -322,7 +322,7 @@ sub cmd_newnews ($$$$;$$) {
 
 	$ts .= '..';
 	my $opts = { asc => 1, limit => 1000, offset => 0 };
-	$self->long_response(0, long_response_limit, sub {
+	long_response($self, 0, long_response_limit, sub {
 		my ($i) = @_;
 		my $srch = $srch[0];
 		my $res = $srch->query($ts, $opts);
@@ -462,7 +462,7 @@ sub set_art {
 
 sub cmd_article ($;$) {
 	my ($self, $art) = @_;
-	my $r = $self->art_lookup($art, 1);
+	my $r = art_lookup($self, $art, 1);
 	return $r unless ref $r;
 	my ($n, $mid, $s) = @$r;
 	set_art($self, $art);
@@ -474,7 +474,7 @@ sub cmd_article ($;$) {
 
 sub cmd_head ($;$) {
 	my ($self, $art) = @_;
-	my $r = $self->art_lookup($art, 2);
+	my $r = art_lookup($self, $art, 2);
 	return $r unless ref $r;
 	my ($n, $mid, $s) = @$r;
 	set_art($self, $art);
@@ -485,7 +485,7 @@ sub cmd_head ($;$) {
 
 sub cmd_body ($;$) {
 	my ($self, $art) = @_;
-	my $r = $self->art_lookup($art, 0);
+	my $r = art_lookup($self, $art, 0);
 	return $r unless ref $r;
 	my ($n, $mid, $s) = @$r;
 	set_art($self, $art);
@@ -495,7 +495,7 @@ sub cmd_body ($;$) {
 
 sub cmd_stat ($;$) {
 	my ($self, $art) = @_;
-	my $r = $self->art_lookup($art, 0);
+	my $r = art_lookup($self, $art, 0);
 	return $r unless ref $r;
 	my ($n, $mid, undef) = @$r;
 	set_art($self, $art);
@@ -612,7 +612,7 @@ sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
 		my $mm = $self->{ng}->mm;
 		my ($beg, $end) = @$r;
 		more($self, $xhdr ? r221 : r225);
-		$self->long_response($beg, $end, sub {
+		long_response($self, $beg, $end, sub {
 			my ($i) = @_;
 			my $mid = $mm->mid_for($$i);
 			more($self, "$$i <$mid>") if defined $mid;
@@ -655,7 +655,7 @@ sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
 		my $mm = $ng->mm;
 		my ($beg, $end) = @$r;
 		more($self, $xhdr ? r221 : r225);
-		$self->long_response($beg, $end, sub {
+		long_response($self, $beg, $end, sub {
 			my ($i) = @_;
 			my $mid = $mm->mid_for($$i);
 			more($self, "$$i ".xref($ng, $$i)) if defined $mid;
@@ -686,7 +686,7 @@ sub hdr_searchmsg ($$$$) {
 		my ($beg, $end) = @$r;
 		more($self, $xhdr ? r221 : r225);
 		my $off = 0;
-		$self->long_response($beg, $end, sub {
+		long_response($self, $beg, $end, sub {
 			my ($i) = @_;
 			my $res = $srch->query_xover($beg, $end, $off);
 			my $msgs = $res->{msgs};
@@ -772,7 +772,7 @@ sub cmd_xrover ($;$) {
 	my $mm = $ng->mm;
 	my $srch = $ng->search;
 	more($self, '224 Overview information follows');
-	$self->long_response($beg, $end, sub {
+	long_response($self, $beg, $end, sub {
 		my ($i) = @_;
 		my $mid = $mm->mid_for($$i) or return;
 		my $h = search_header_for($srch, $mid, 'references');
@@ -822,7 +822,7 @@ sub cmd_xover ($;$) {
 	more($self, "224 Overview information follows for $beg to $end");
 	my $srch = $self->{ng}->search;
 	my $off = 0;
-	$self->long_response($beg, $end, sub {
+	long_response($self, $beg, $end, sub {
 		my ($i) = @_;
 		my $res = $srch->query_xover($beg, $end, $off);
 		my $msgs = $res->{msgs};
@@ -896,7 +896,7 @@ sub do_more ($$) {
 			$data = substr($data, $n, $dlen - $n);
 		}
 	}
-	$self->do_write($data);
+	do_write($self, $data);
 }
 
 # callbacks for Danga::Socket
@@ -924,7 +924,7 @@ sub event_read {
 		my $line = $1;
 		my $t0 = now();
 		my $fd = $self->{fd};
-		$r = eval { $self->process_line($line) };
+		$r = eval { process_line($self, $line) };
 		my $d = $self->{long_res} ?
 			" deferred[$fd]" : '';
 		out($self, "[$fd] %s - %0.6f$d", $line, now() - $t0);
@@ -947,7 +947,7 @@ sub watch_read {
 		# another long response.
 		Danga::Socket->AddTimer(0, sub {
 			if (&Danga::Socket::POLLIN & $self->{event_watch}) {
-				$self->event_read;
+				event_read($self);
 			}
 		});
 	}
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index 02c5446..23f303f 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -15,6 +15,7 @@ use strict;
 use warnings;
 use base qw(Exporter);
 use Symbol qw(gensym);
+use IO::Handle;
 use PublicInbox::ProcessPipe;
 our @EXPORT_OK = qw/which spawn popen_rd/;
 
@@ -168,7 +169,7 @@ sub popen_rd {
 	pipe(my ($r, $w)) or die "pipe: $!\n";
 	$opts ||= {};
 	my $blocking = $opts->{Blocking};
-	$r->blocking($blocking) if defined $blocking;
+	IO::Handle::blocking($r, $blocking) if defined $blocking;
 	$opts->{1} = fileno($w);
 	my $pid = spawn($cmd, $env, $opts);
 	return ($r, $pid) if wantarray;
diff --git a/script/public-inbox-httpd b/script/public-inbox-httpd
index 19315bb..f1a5d79 100755
--- a/script/public-inbox-httpd
+++ b/script/public-inbox-httpd
@@ -68,7 +68,7 @@ use fields qw(cb);
 sub new {
 	my ($class, $io, $cb) = @_;
 	my $self = fields::new($class);
-	$io->blocking(0);
+	IO::Handle::blocking($io, 0);
 	$self->SUPER::new($io);
 	$self->{cb} = $cb;
 	$self->watch_read(1);
-- 
EW


  parent reply	other threads:[~2016-02-29  1:41 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-29  1:40 [PATCH 0/8] another round of HTTP-related fixes Eric Wong
2016-02-29  1:41 ` [PATCH 1/8] http: error check for sysseek on input Eric Wong
2016-02-29  1:41 ` [PATCH 2/8] http: document event_write usage Eric Wong
2016-02-29  1:41 ` [PATCH 3/8] http: avoid needless time2str calls Eric Wong
2016-02-29  1:41 ` [PATCH 4/8] distinguish error messages intended for users vs developers Eric Wong
2016-02-29  1:41 ` Eric Wong [this message]
2016-02-29  1:41 ` [PATCH 6/8] git-http-backend: stricter parsing of CRLF Eric Wong
2016-02-29  1:41 ` [PATCH 7/8] spawnpp: use env(1) for mod_perl compatibility Eric Wong
2016-02-29  1:41 ` [PATCH 8/8] git-http-backend: fixes for mod_perl Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160229014107.7396-6-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).