diff options
author | Eric Wong <e@80x24.org> | 2017-02-09 01:37:03 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2017-02-09 01:37:03 +0000 |
commit | d9563ea5516e8e786debf223e10ec11695aee9d7 (patch) | |
tree | a0842b1ee0953bc8e65d21d5d96432f41973adb7 /lib/PublicInbox/RepobrowseBase.pm | |
parent | fb9ed5324ec7de9420956840ba9a6585b81e8231 (diff) | |
download | public-inbox-d9563ea5516e8e786debf223e10ec11695aee9d7.tar.gz |
We'll still be keeping "repobrowse" for the public API for use with .psgi files, but shortening the name means less typing and we may have command-line tools, too.
Diffstat (limited to 'lib/PublicInbox/RepobrowseBase.pm')
-rw-r--r-- | lib/PublicInbox/RepobrowseBase.pm | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/lib/PublicInbox/RepobrowseBase.pm b/lib/PublicInbox/RepobrowseBase.pm deleted file mode 100644 index 44f422df..00000000 --- a/lib/PublicInbox/RepobrowseBase.pm +++ /dev/null @@ -1,115 +0,0 @@ -# Copyright (C) 2015 all contributors <meta@public-inbox.org> -# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> -package PublicInbox::RepobrowseBase; -use strict; -use warnings; -require PublicInbox::RepobrowseGitQuery; -use PublicInbox::Hval; -our %MIME_TYPE_WHITELIST = ('application/pdf' => 1); - -sub new { bless {}, shift } - -sub call { - my ($self, $cmd, $req) = @_; - my $vcs = $req->{repo_info}->{vcs}; - my $rv = eval { - no strict 'refs'; - my $sub = "call_${vcs}_$cmd"; - $self->$sub($req); - }; - $@ ? [ 500, ['Content-Type'=>'text/plain'], [] ] : $rv; -} - -sub mime_load { - my ($self, $file) = @_; - my %rv; - open my $fh, '<', $file or return \%rv; - while (<$fh>) { - next if /^#/; # no comments - my ($type, @ext) = split(/\s+/); - - if (defined $type) { - $rv{$_} = $type foreach @ext; - } - } - \%rv; -} - -# returns undef if missing, so users can scan the blob if needed -sub mime_type_unsafe { - my ($self, $fn) = @_; - $fn =~ /\.([^\.]+)\z/ or return; - my $ext = $1; - my $m = $self->{mime_types} ||= $self->mime_load('/etc/mime.types'); - $m->{$ext}; -} - -sub mime_type { - my ($self, $fn) = @_; - my $ct = $self->mime_type_unsafe($fn); - return unless defined $ct; - - # XSS protection. Assume the browser knows what to do - # with images/audio/video; but don't allow random HTML from - # a repository to be served - ($ct =~ m!\A(?:image|audio|video)/! || $MIME_TYPE_WHITELIST{$ct}) ? - $ct : undef; -} - -# starts an HTML page for Repobrowse in a consistent way -sub html_start { - my ($self, $req, $title_html, $opts) = @_; - my $desc = $req->{repo_info}->{desc_html}; - my $meta = ''; - - if ($opts) { - my @robots; - foreach (qw(nofollow noindex)) { - push @robots, $_ if $opts->{$_}; - } - $meta = qq(<meta\nname=robots\ncontent=") . - join(',', @robots) . '" />'; - } - - "<html><head><title>$title_html</title>" . - PublicInbox::Hval::STYLE . $meta . - "</head><body><pre><b>$desc</b>"; -} - -sub r { - my ($self, $status, $req, @extra) = @_; - my @h; - - my $body = ''; - if ($status == 301 || $status == 302) { - # The goal is to be able to make redirects like we make - # <a href=> tags with '../' - my $env = $req->{env}; - my $base = PublicInbox::Repobrowse::base_url($env); - my ($redir) = @extra; - if ($redir =~ m!\A\.\./!) { # relative redirect - my @orig = split(m!/+!, $env->{PATH_INFO}); - my @dest = split(m!/+!, $redir); - - while ($dest[0] eq '..') { - pop @orig; - shift @dest; - } - my $end = ''; - $end = pop @dest if $dest[-1] =~ /\A[#\?]/; - $redir = $base . join('/', @orig, @dest) . $end; - } else { - $redir = $base . '/' . $redir; - } - push @h, qw(Content-Type text/plain Location), $redir; - - # mainly for curl (no-'-L') users: - $body = "Redirecting to $redir\n"; - } else { - push @h, qw(Content-Type text/plain); - } - - [ $status, \@h, [ $body ] ] -} - -1; |