user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH 0/2] daemon updates
@ 2016-03-03  9:32 Eric Wong
  2016-03-03  9:32 ` [PATCH 1/2] daemon: avoid polluting the main package Eric Wong
  2016-03-03  9:32 ` [PATCH 2/2] daemon: introduce host_with_port for identifying sockets Eric Wong
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Wong @ 2016-03-03  9:32 UTC (permalink / raw)
  To: meta

Preparation work for Unix socket listeners.

Eric Wong (2):
      daemon: avoid polluting the main package
      daemon: introduce host_with_port for identifying sockets

 lib/PublicInbox/Daemon.pm | 31 ++++++++++++++++---------------
 lib/PublicInbox/HTTP.pm   |  8 +++++---
 script/public-inbox-httpd | 14 +++-----------
 script/public-inbox-nntpd |  2 +-
 t/httpd-corner.psgi       |  3 +++
 t/httpd-corner.t          | 10 ++++++++++
 6 files changed, 38 insertions(+), 30 deletions(-)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] daemon: avoid polluting the main package
  2016-03-03  9:32 [PATCH 0/2] daemon updates Eric Wong
@ 2016-03-03  9:32 ` Eric Wong
  2016-03-03  9:32 ` [PATCH 2/2] daemon: introduce host_with_port for identifying sockets Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2016-03-03  9:32 UTC (permalink / raw)
  To: meta

We've distilled the daemon code into one public function ("run"),
so avoid polluting the main namespace and just have users
prefix with the full package name for this rarely-used class.
---
 lib/PublicInbox/Daemon.pm | 6 ++----
 script/public-inbox-httpd | 2 +-
 script/public-inbox-nntpd | 2 +-
 3 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index 45c1563..77ab2a7 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -1,10 +1,8 @@
 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
-package PublicInbox::Daemon; # empty class :p
-
 # contains common daemon code for the nntpd and httpd servers.
 # This may be used for read-only IMAP server if we decide to implement it.
-package main;
+package PublicInbox::Daemon;
 use strict;
 use warnings;
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
@@ -383,7 +381,7 @@ sub daemon_loop ($$) {
 }
 
 
-sub daemon_run ($$$) {
+sub run ($$$) {
 	my ($default, $refresh, $post_accept) = @_;
 	daemon_prepare($default);
 	daemonize();
diff --git a/script/public-inbox-httpd b/script/public-inbox-httpd
index 2157962..d867b47 100755
--- a/script/public-inbox-httpd
+++ b/script/public-inbox-httpd
@@ -49,7 +49,7 @@ my $refresh = sub {
 	}
 };
 
-daemon_run('0.0.0.0:8080', $refresh,
+PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
 	sub ($$$) { # post_accept
 		my ($client, $addr, $srv) = @_;
 		my $fd = fileno($srv);
diff --git a/script/public-inbox-nntpd b/script/public-inbox-nntpd
index 23d269d..cea8816 100755
--- a/script/public-inbox-nntpd
+++ b/script/public-inbox-nntpd
@@ -10,7 +10,7 @@ require PublicInbox::NewsGroup;
 require PublicInbox::NNTP;
 require PublicInbox::Config;
 my $nntpd = PublicInbox::NNTPD->new;
-daemon_run('0.0.0.0:119',
+PublicInbox::Daemon::run('0.0.0.0:119',
 	sub { $nntpd->refresh_groups }, # refresh
 	sub ($$$) { PublicInbox::NNTP->new($_[0], $nntpd) }); # post_accept
 
-- 
EW


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] daemon: introduce host_with_port for identifying sockets
  2016-03-03  9:32 [PATCH 0/2] daemon updates Eric Wong
  2016-03-03  9:32 ` [PATCH 1/2] daemon: avoid polluting the main package Eric Wong
@ 2016-03-03  9:32 ` Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2016-03-03  9:32 UTC (permalink / raw)
  To: meta

This allows us to share more code between daemons and avoids
having to make additional syscalls for preparing REMOTE_HOST
and REMOTE_PORT in the PSGI env in -httpd.

This will also make supporting HTTP (and NNTP) over Unix sockets
easier in a future commit.
---
 lib/PublicInbox/Daemon.pm | 25 ++++++++++++++-----------
 lib/PublicInbox/HTTP.pm   |  8 +++++---
 script/public-inbox-httpd | 12 ++----------
 t/httpd-corner.psgi       |  3 +++
 t/httpd-corner.t          | 10 ++++++++++
 5 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index 77ab2a7..c101ecb 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -157,20 +157,23 @@ sub reopen_logs {
 
 sub sockname ($) {
 	my ($s) = @_;
-	my $n = getsockname($s) or return;
-	my ($port, $addr);
-	if (length($n) >= 28) {
+	my $addr = getsockname($s) or return;
+	my ($host, $port) = host_with_port($addr);
+	"$host:$port";
+}
+
+sub host_with_port ($) {
+	my ($addr) = @_;
+	my ($port, $host);
+	if (length($addr) >= 28) {
 		require Socket6;
-		($port, $addr) = Socket6::unpack_sockaddr_in6($n);
-	} else {
-		($port, $addr) = Socket::sockaddr_in($n);
-	}
-	if (length($addr) == 4) {
-		$n = Socket::inet_ntoa($addr)
+		($port, $host) = Socket6::unpack_sockaddr_in6($addr);
+		$host = '['.Socket6::inet_ntop(Socket6::AF_INET6(), $host).']';
 	} else {
-		$n = '['.Socket6::inet_ntop(Socket6::AF_INET6(), $addr).']';
+		($port, $host) = Socket::sockaddr_in($addr);
+		$host = Socket::inet_ntoa($host);
 	}
-	$n .= ":$port";
+	($host, $port);
 }
 
 sub inherit () {
diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 17e7447..6c4c20d 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -11,7 +11,7 @@ package PublicInbox::HTTP;
 use strict;
 use warnings;
 use base qw(Danga::Socket);
-use fields qw(httpd env rbuf input_left);
+use fields qw(httpd env rbuf input_left remote_addr remote_port);
 use Fcntl qw(:seek);
 use HTTP::Parser::XS qw(parse_http_request); # supports pure Perl fallback
 use HTTP::Status qw(status_message);
@@ -38,6 +38,8 @@ sub new ($$$) {
 	$self->SUPER::new($sock);
 	$self->{httpd} = $httpd;
 	$self->{rbuf} = '';
+	($self->{remote_addr}, $self->{remote_port}) =
+		PublicInbox::Daemon::host_with_port($addr);
 	$self->watch_read(1);
 	$self;
 }
@@ -113,8 +115,8 @@ sub app_dispatch ($) {
 	my ($self) = @_;
 	$self->watch_read(0);
 	my $env = $self->{env};
-	$env->{REMOTE_ADDR} = $self->peer_ip_string; # Danga::Socket
-	$env->{REMOTE_PORT} = $self->{peer_port}; # set by peer_ip_string
+	$env->{REMOTE_ADDR} = $self->{remote_addr};
+	$env->{REMOTE_PORT} = $self->{remote_port};
 	if (my $host = $env->{HTTP_HOST}) {
 		$host =~ s/:(\d+)\z// and $env->{SERVER_PORT} = $1;
 		$env->{SERVER_NAME} = $host;
diff --git a/script/public-inbox-httpd b/script/public-inbox-httpd
index d867b47..b6c4e67 100755
--- a/script/public-inbox-httpd
+++ b/script/public-inbox-httpd
@@ -99,18 +99,10 @@ sub pi_httpd_async {
 sub new {
 	my ($class, $sock, $app) = @_;
 	my $n = getsockname($sock) or die "not a socket: $sock $!\n";
-	my ($port, $addr);
-	if (length($n) >= 28) {
-		require Socket6;
-		($port, $addr) = Socket6::unpack_sockaddr_in6($n);
-	} else {
-		($port, $addr) = Socket::unpack_sockaddr_in($n);
-	}
+	my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
 
 	my %env = (
-		REMOTE_HOST => '',
-		REMOTE_PORT => 0,
-		SERVER_NAME => $addr,
+		SERVER_NAME => $host,
 		SERVER_PORT => $port,
 		SCRIPT_NAME => '',
 		'psgi.version' => [ 1, 1 ],
diff --git a/t/httpd-corner.psgi b/t/httpd-corner.psgi
index c3bf523..349b35d 100644
--- a/t/httpd-corner.psgi
+++ b/t/httpd-corner.psgi
@@ -44,6 +44,9 @@ my $app = sub {
 				$fh->close;
 			};
 		}
+	} elsif ($path eq '/host-port') {
+		$code = 200;
+		push @$body, "$env->{REMOTE_ADDR}:$env->{REMOTE_PORT}";
 	}
 
 	[ $code, $h, $body ]
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index e73ebd5..198a7e9 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -76,6 +76,16 @@ sub conn_for {
 	return $conn;
 }
 
+{
+	my $conn = conn_for($sock, 'host-port');
+	$conn->write("GET /host-port HTTP/1.0\r\n\r\n");
+	$conn->read(my $buf, 4096);
+	my ($head, $body) = split(/\r\n\r\n/, $buf);
+	my ($addr, $port) = split(/:/, $body);
+	is($addr, $conn->sockhost, 'host matches addr');
+	is($port, $conn->sockport, 'port matches');
+}
+
 # graceful termination
 {
 	my $conn = conn_for($sock, 'graceful termination via slow header');
-- 
EW


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-03-03  9:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-03  9:32 [PATCH 0/2] daemon updates Eric Wong
2016-03-03  9:32 ` [PATCH 1/2] daemon: avoid polluting the main package Eric Wong
2016-03-03  9:32 ` [PATCH 2/2] daemon: introduce host_with_port for identifying sockets Eric Wong

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