]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
repobrowse: remove Plack::Request dependency
authorEric Wong <e@80x24.org>
Thu, 22 Dec 2016 04:38:13 +0000 (04:38 +0000)
committerEric Wong <e@80x24.org>
Thu, 22 Dec 2016 04:38:13 +0000 (04:38 +0000)
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
lib/PublicInbox/RepobrowseBase.pm
lib/PublicInbox/RepobrowseGitAtom.pm
t/repobrowse_common_git.perl

index cdd708e96aea14cbb430c3145b078a521b0ed82e..2c758ca8c0ebd5f8e02037459932dc6ad96dedb4 100644 (file)
@@ -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");
index 33647fca529cf3481b26511c31bf1537eee28383..caec5ced6da3db39a7a1fd985697168d20bcf4c3 100644 (file)
@@ -84,12 +84,11 @@ sub r {
        if ($status == 301 || $status == 302) {
                # The goal is to be able to make redirects like we make
                # <a href=> 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 '..') {
index 063cd2e42a5989a3a482cad1251912b220c509de..7c4082a696d89e2e75f1301eed2ca161b75b33a3 100644 (file)
@@ -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 {
index cd8ce582ef044dc4d76fef813916a7f6bd389c85..de61efe68d858c370849e413eafdf361e63629bd 100644 (file)
@@ -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 $@;