user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 02/11] nntp: start adding CRLF to responses natively
Date: Sat, 23 Jul 2022 04:41:46 +0000	[thread overview]
Message-ID: <20220723044155.3733949-3-e@80x24.org> (raw)
In-Reply-To: <20220723044155.3733949-1-e@80x24.org>

With IMAP and POP3, I've started to embed CRLF into constant
response codes to avoid triggering CoW and extra memory traffic
in Perl.

The end goal is to enable more code sharing between IMAP, NNTP,
and POP3 inside one -netd process.
---
 lib/PublicInbox/NNTP.pm | 35 ++++++++++++++---------------------
 1 file changed, 14 insertions(+), 21 deletions(-)

diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 3d304c52..1f9058ca 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -24,9 +24,9 @@ use constant {
 	LINE_MAX => 512, # RFC 977 section 2.3
 	r501 => '501 command syntax error',
 	r502 => '502 Command unavailable',
-	r221 => '221 Header follows',
+	r221 => "221 Header follows\r\n",
 	r224 => '224 Overview information follows (multi-line)',
-	r225 =>	'225 Headers follow (multi-line)',
+	r225 =>	"225 Headers follow (multi-line)\r\n",
 	r430 => '430 No article with that message-id',
 };
 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
@@ -82,8 +82,8 @@ sub process_line ($$) {
 	my ($self, $l) = @_;
 	my ($req, @args) = split(/[ \t]+/, $l);
 	return 1 unless defined($req); # skip blank line
-	$req = $self->can('cmd_'.lc($req));
-	return res($self, '500 command not recognized') unless $req;
+	$req = $self->can('cmd_'.lc($req)) //
+		return $self->write(\"500 command not recognized\r\n");
 	return res($self, r501) unless args_ok($req, scalar @args);
 
 	my $res = eval { $req->($self, @args) };
@@ -403,7 +403,7 @@ sub cmd_post ($) {
 
 sub cmd_quit ($) {
 	my ($self) = @_;
-	res($self, '205 closing connection - goodbye!');
+	$self->write(\"205 closing connection - goodbye!\r\n");
 	$self->shutdn;
 	undef;
 }
@@ -663,7 +663,7 @@ sub long_step {
 		$self->requeue if $new_size == 1;
 	} else { # all done!
 		delete $self->{long_cb};
-		res($self, '.');
+		$self->write(\".\r\n");
 		my $elapsed = now() - $t0;
 		my $fd = fileno($self->{sock});
 		out($self, " deferred[$fd] done - %0.6f", $elapsed);
@@ -703,7 +703,7 @@ sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
 		$range = $self->{article} unless defined $range;
 		my $r = get_range($self, $range);
 		return $r unless ref $r;
-		more($self, $xhdr ? r221 : r225);
+		$self->msg_more($xhdr ? r221 : r225);
 		long_response($self, \&hdr_msgid_range_i, @$r);
 	}
 }
@@ -775,7 +775,7 @@ sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
 		$range = $self->{article} unless defined $range;
 		my $r = get_range($self, $range);
 		return $r unless ref $r;
-		more($self, $xhdr ? r221 : r225);
+		$self->msg_more($xhdr ? r221 : r225);
 		long_response($self, \&xref_range_i, @$r);
 	}
 }
@@ -819,7 +819,7 @@ sub hdr_smsg ($$$$) {
 		$range = $self->{article} unless defined $range;
 		my $r = get_range($self, $range);
 		return $r unless ref $r;
-		more($self, $xhdr ? r221 : r225);
+		$self->msg_more($xhdr ? r221 : r225);
 		long_response($self, \&smsg_range_i, @$r, $field);
 	}
 }
@@ -837,7 +837,7 @@ sub do_hdr ($$$;$) {
 	} elsif ($sub =~ /\A:(bytes|lines)\z/) {
 		hdr_smsg($self, $xhdr, $1, $range);
 	} else {
-		$xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
+		$xhdr ? (r221 . '.') : "503 HDR not permitted on $header";
 	}
 }
 
@@ -867,16 +867,9 @@ sub hdr_mid_prefix ($$$$$) {
 
 sub hdr_mid_response ($$$$$$) {
 	my ($self, $xhdr, $ibx, $n, $mid, $v) = @_;
-	my $res = '';
-	if ($xhdr) {
-		$res .= r221 . "\r\n";
-		$res .= "$mid $v\r\n";
-	} else {
-		$res .= r225 . "\r\n";
-		my $pfx = hdr_mid_prefix($self, $xhdr, $ibx, $n, $mid);
-		$res .= "$pfx $v\r\n";
-	}
-	res($self, $res .= '.');
+	$self->write(($xhdr ? r221.$mid :
+		   r225.hdr_mid_prefix($self, $xhdr, $ibx, $n, $mid)) .
+		" $v\r\n.\r\n");
 	undef;
 }
 
@@ -972,7 +965,7 @@ sub cmd_starttls ($) {
 	return r502 if ($sock->can('accept_SSL') || $self->compressed);
 	my $opt = $self->{nntpd}->{accept_tls} or
 		return '580 can not initiate TLS negotiation';
-	res($self, '382 Continue with TLS negotiation');
+	$self->write(\"382 Continue with TLS negotiation\r\n");
 	$self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
 	$self->requeue if PublicInbox::DS::accept_tls_step($self);
 	undef;

  parent reply	other threads:[~2022-07-23  4:41 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-23  4:41 [PATCH 00/11] IMAP, NNTP, POP3 golfing Eric Wong
2022-07-23  4:41 ` [PATCH 01/11] nntp: pass regexp to split() callers Eric Wong
2022-07-23  4:41 ` Eric Wong [this message]
2022-07-23  4:41 ` [PATCH 03/11] nntp: remove more() wrapper Eric Wong
2022-07-23  4:41 ` [PATCH 04/11] ds: support greeting protocols Eric Wong
2022-07-23  4:41 ` [PATCH 05/11] ds: move no-op ->zflush to common base class Eric Wong
2022-07-23  4:41 ` [PATCH 06/11] ds: move requeue_once Eric Wong
2022-07-23  4:41 ` [PATCH 07/11] nntp: listgroup_range_i: remove useless `map' op Eric Wong
2022-07-23  4:41 ` [PATCH 08/11] nntp: inline CRLF in all response lines Eric Wong
2022-07-23  4:41 ` [PATCH 09/11] ds: share long_step between NNTP and IMAP Eric Wong
2022-07-23  4:41 ` [PATCH 10/11] nntp: resolve inboxes immediately on group listings Eric Wong
2022-07-23  4:41 ` [PATCH 11/11] imap+nntp: share COMPRESS implementation 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=20220723044155.3733949-3-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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).