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 2/6] inbox: base_url method takes PSGI env hashref instead
Date: Sat,  2 Jul 2016 07:56:34 +0000	[thread overview]
Message-ID: <20160702075638.31017-3-e@80x24.org> (raw)
In-Reply-To: <20160702075638.31017-1-e@80x24.org>

This is lighter and we can work further towards eliminating
our Plack::Request dependency entirely.
---
 lib/PublicInbox/Feed.pm      |  4 +---
 lib/PublicInbox/Inbox.pm     | 12 ++++++++----
 lib/PublicInbox/Mbox.pm      |  2 +-
 lib/PublicInbox/WWW.pm       |  2 +-
 lib/PublicInbox/WwwStream.pm |  9 ++-------
 t/view.t                     |  1 +
 6 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index 2f141c4..ffbf5c8 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -297,13 +297,11 @@ sub get_feedopts {
 	my $pi_config = $ctx->{pi_config};
 	my $inbox = $ctx->{inbox};
 	my $obj = $ctx->{-inbox};
-	my $cgi = $ctx->{cgi};
 	my %rv = ( description => $obj->description );
 
 	$rv{address} = $obj->{address};
 	$rv{id_addr} = $obj->{-primary_address};
-	my $url_base;
-	$url_base = $obj->base_url($cgi); # CGI may be undef
+	my $url_base = $obj->base_url($ctx->{env});
 	if (my $mid = $ctx->{mid}) { # per-thread feed:
 		$rv{atomurl} = "$url_base$mid/t.atom";
 	} else {
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index ada713c..96c9265 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -71,10 +71,14 @@ sub cloneurl {
 }
 
 sub base_url {
-	my ($self, $prq) = @_; # Plack::Request
-	if (defined $prq) {
-		my $url = $prq->base->as_string;
-		$url .= '/' if $url !~ m!/\z!; # for mount in Plack::Builder
+	my ($self, $env) = @_;
+	if ($env) { # PSGI env
+		my $scheme = $env->{'psgi.url_scheme'};
+		my $host_port = $env->{HTTP_HOST} ||
+			"$env->{SERVER_NAME}:$env->{SERVER_PORT}";
+		my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
+		# for mount in Plack::Builder
+		$url .= '/' if $url !~ m!/\z!;
 		$url .= $self->{name} . '/';
 	} else {
 		# either called from a non-PSGI environment (e.g. NNTP/POP3)
diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm
index 1c97f95..9dad0f6 100644
--- a/lib/PublicInbox/Mbox.pm
+++ b/lib/PublicInbox/Mbox.pm
@@ -28,7 +28,7 @@ sub msg_str {
 		$header_obj->header_set($d);
 	}
 	my $ibx = $ctx->{-inbox};
-	my $base = $ibx->base_url($ctx->{cgi});
+	my $base = $ibx->base_url($ctx->{env});
 	my $mid = mid_clean($header_obj->header('Message-ID'));
 	$mid = uri_escape_utf8($mid);
 	my @append = (
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index c4509bd..1e23c43 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -401,7 +401,7 @@ sub r301 {
 		return $r404 if $r404;
 		$obj = $ctx->{-inbox};
 	}
-	my $url = $obj->base_url($cgi);
+	my $url = $obj->base_url($ctx->{env});
 	my $qs = $ctx->{env}->{QUERY_STRING};
 	$url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
 	$url .= $suffix if (defined $suffix);
diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm
index fdab4da..be6ce2e 100644
--- a/lib/PublicInbox/WwwStream.pm
+++ b/lib/PublicInbox/WwwStream.pm
@@ -62,13 +62,8 @@ sub _html_end {
 	my $obj = $ctx->{-inbox};
 	my $desc = ascii_html($obj->description);
 
-	# FIXME: cleanup
-	my $env = $ctx->{env};
-	my $scheme = $env->{'psgi.url_scheme'};
-	my $host_port = $env->{HTTP_HOST} ||
-			"$env->{SERVER_NAME}:$env->{SERVER_PORT}";
-	my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
-	$http = URI->new($http . $obj->{name})->canonical->as_string;
+	my $http = $obj->base_url($ctx->{env});
+	chop $http;
 	my %seen = ( $http => 1 );
 	my @urls = ($http);
 	foreach my $u (@{$obj->cloneurl}) {
diff --git a/t/view.t b/t/view.t
index 4fdd151..4cee439 100644
--- a/t/view.t
+++ b/t/view.t
@@ -25,6 +25,7 @@ my $ctx = {
 	-inbox => Plack::Util::inline_object(
 		name => 'test',
 		search => sub { undef },
+		base_url => sub { 'http://example.com/' },
 		cloneurl => sub {[]},
 		description => sub { '' }),
 };
-- 
EW


  parent reply	other threads:[~2016-07-02  7:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-02  7:56 [PATCH 0/6] misc cleanups Eric Wong
2016-07-02  7:56 ` [PATCH 1/6] TODO: clarify streaming Email::MIME replacement Eric Wong
2016-07-02  7:56 ` Eric Wong [this message]
2016-07-02  7:56 ` [PATCH 3/6] extmsg: rework to use Inbox objects Eric Wong
2016-07-02  7:56 ` [PATCH 4/6] www: use PSGI env directly Eric Wong
2016-07-02  7:56 ` [PATCH 5/6] view: rely on internal query parser for 'o' param Eric Wong
2016-07-02  7:56 ` [PATCH 6/6] www: remove Plack::Request dependency entirely 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=20160702075638.31017-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).