From 96ee5f0f64972f0ce3143538437d4de1febe3c51 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 22 Dec 2016 04:38:13 +0000 Subject: repobrowse: remove Plack::Request dependency This does not make installation easier, but lightens runtime a bit. Plack::Request is unnecessary bloat and indirection which does things behind our back. $env has all the stuff we need. --- lib/PublicInbox/Repobrowse.pm | 35 ++++++++++++++++++++++++----------- lib/PublicInbox/RepobrowseBase.pm | 7 +++---- lib/PublicInbox/RepobrowseGitAtom.pm | 6 +++--- t/repobrowse_common_git.perl | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/PublicInbox/Repobrowse.pm b/lib/PublicInbox/Repobrowse.pm index cdd708e9..2c758ca8 100644 --- a/lib/PublicInbox/Repobrowse.pm +++ b/lib/PublicInbox/Repobrowse.pm @@ -20,7 +20,6 @@ package PublicInbox::Repobrowse; use strict; use warnings; -use Plack::Request; use URI::Escape qw(uri_escape_utf8 uri_unescape); use PublicInbox::RepobrowseConfig; @@ -38,6 +37,24 @@ sub new { # simple response for errors sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] } +sub base_url ($) { + my ($env) = @_; + my $scheme = $env->{'psgi.url_scheme'} || 'http'; + my $host = $env->{HTTP_HOST}; + my $base = "$scheme://"; + if (defined $host) { + $base .= $host; + } else { + $base .= $env->{SERVER_NAME}; + my $port = $env->{SERVER_PORT} || 80; + if (($scheme eq 'http' && $port != 80) || + ($scheme eq 'https' && $port != 443)) { + $base.= ":$port"; + } + } + $base .= $env->{SCRIPT_NAME}; +} + # Remove trailing slash in URLs which regular humans are likely to read # in an attempt to improve cache hit ratios. Do not redirect # plain|patch|blob|fallback endpoints since those could be using @@ -45,11 +62,9 @@ sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] } # (e.g. curl does not follow 301 unless given "-L") my %NO_TSLASH = map { $_ => 1 } qw(Log Commit Tree Summary Tag); sub no_tslash { - my ($cgi) = @_; # Plack::Request - my ($base, $uri); - $base = $cgi->base; - $base =~ s!/+\z!!; - $uri = $cgi->request_uri; + my ($env) = @_; + my $base = base_url($env); + my $uri = $env->{REQUEST_URI}; my $qs = ''; if ($uri =~ s/(\?.+)\z//) { $qs = $1; @@ -71,14 +86,13 @@ sub root_index { sub call { my ($self, $env) = @_; - my $cgi = Plack::Request->new($env); - my $method = $cgi->method; + my $method = $env->{REQUEST_METHOD}; return r(405, 'Method Not Allowed') if ($method !~ /\AGET|HEAD|POST\z/); # URL syntax: / repo [ / cmd [ / path ] ] # cmd: log | commit | diff | tree | view | blob | snapshot # repo and path (@extra) may both contain '/' - my $path_info = uri_unescape($cgi->path_info); + my $path_info = uri_unescape($env->{PATH_INFO}); my (undef, $repo_path, @extra) = split(m{/+}, $path_info, -1); return $self->root_index($self) unless length($repo_path); @@ -94,7 +108,6 @@ sub call { my $req = { repo_info => $repo_info, extra => \@extra, # path - cgi => $cgi, rconfig => $rconfig, env => $env, }; @@ -125,7 +138,7 @@ sub call { ++$tslash; } - return no_tslash($cgi) if ($tslash && $NO_TSLASH{$mod}); + return no_tslash($env) if ($tslash && $NO_TSLASH{$mod}); $req->{tslash} = $tslash; $mod = load_once("PublicInbox::Repobrowse$vcs$mod"); diff --git a/lib/PublicInbox/RepobrowseBase.pm b/lib/PublicInbox/RepobrowseBase.pm index 33647fca..caec5ced 100644 --- a/lib/PublicInbox/RepobrowseBase.pm +++ b/lib/PublicInbox/RepobrowseBase.pm @@ -84,12 +84,11 @@ sub r { if ($status == 301 || $status == 302) { # The goal is to be able to make redirects like we make # tags with '../' - my $cgi = $req->{cgi}; - my $base = $cgi->base; + my $env = $req->{env}; + my $base = PublicInbox::Repobrowse::base_url($env); my ($redir) = @extra; if ($redir =~ m!\A\.\./!) { # relative redirect - my @orig = split(m!/+!, $cgi->path_info, -1); - shift @orig; # drop leading '/' + my @orig = split(m!/+!, $env->{PATH_INFO}); my @dest = split(m!/+!, $redir); while ($dest[0] eq '..') { diff --git a/lib/PublicInbox/RepobrowseGitAtom.pm b/lib/PublicInbox/RepobrowseGitAtom.pm index 063cd2e4..7c4082a6 100644 --- a/lib/PublicInbox/RepobrowseGitAtom.pm +++ b/lib/PublicInbox/RepobrowseGitAtom.pm @@ -41,8 +41,8 @@ sub call_git_atom { sub repo_root_url { my ($self, $req) = @_; - my $cgi = $req->{cgi}; - my $uri = $cgi->request_uri; + my $env = $req->{env}; + my $uri = $env->{REQUEST_URI}; $uri =~ s/\?.+\z//; # no query string my @uri = split(m!/+!, $uri); shift @uri; # leading slash @@ -52,7 +52,7 @@ sub repo_root_url { pop @extra; } pop @uri if $uri[-1] eq 'atom'; # warn if not equal? - $cgi->base . join('/', @uri); + PublicInbox::Repobrowse::base_url($env) . join('/', @uri); } sub git_atom_stream { diff --git a/t/repobrowse_common_git.perl b/t/repobrowse_common_git.perl index cd8ce582..de61efe6 100644 --- a/t/repobrowse_common_git.perl +++ b/t/repobrowse_common_git.perl @@ -6,7 +6,7 @@ use warnings; use Test::More; use File::Temp qw/tempdir/; use Cwd qw/getcwd/; -my @mods = qw(HTTP::Request::Common Plack::Request Plack::Test URI::Escape); +my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape); foreach my $mod (@mods) { eval "require $mod"; plan skip_all => "$mod missing for $0" if $@; -- cgit v1.2.3-24-ge0c7