]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
remove redundant NewsGroup class
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 94ab032f145cb06fed15deadf08c8ecf3c450b75..cf370afa750fd5c832ab29a661f1d58bc762252d 100644 (file)
@@ -14,7 +14,7 @@ use 5.008;
 use strict;
 use warnings;
 use Plack::Request;
-use PublicInbox::Config qw(try_cat);
+use PublicInbox::Config;
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
 use constant PI_URL => '//public-inbox.org/';
@@ -23,6 +23,7 @@ use PublicInbox::GitHTTPBackend;
 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
 our $MID_RE = qr!([^/]+)!;
 our $END_RE = qr!(T/|t/|R/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
+our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
 
 sub new {
        my ($class, $pi_config) = @_;
@@ -39,7 +40,7 @@ sub run {
 sub call {
        my ($self, $env) = @_;
        my $cgi = Plack::Request->new($env);
-       my $ctx = { cgi => $cgi, pi_config => $self->{pi_config} };
+       my $ctx = {cgi => $cgi, pi_config => $self->{pi_config}, www => $self};
        my $path_info = $cgi->path_info;
 
        my $method = $cgi->method;
@@ -73,6 +74,10 @@ sub call {
        } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
                msg_page($self, $ctx, $1, $2, $3);
 
+       } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
+               my ($idx, $fn) = ($3, $4);
+               invalid_inbox_mid($self, $ctx, $1, $2) ||
+                       get_attach($ctx, $idx, $fn);
        # in case people leave off the trailing slash:
        } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
                my ($inbox, $mid, $suffix) = ($1, $2, $3);
@@ -102,7 +107,7 @@ sub preload {
 
        foreach (qw(PublicInbox::Search PublicInbox::SearchView
                        PublicInbox::Mbox IO::Compress::Gzip
-                       PublicInbox::NewsWWW PublicInbox::NewsGroup)) {
+                       PublicInbox::NewsWWW)) {
                eval "require $_;";
        }
 }
@@ -124,11 +129,13 @@ sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
 
 # returns undef if valid, array ref response if invalid
 sub invalid_inbox {
-       my ($self, $ctx, $inbox, $mid) = @_;
+       my ($self, $ctx, $inbox) = @_;
        my $obj = $ctx->{pi_config}->lookup_name($inbox);
        if (defined $obj) {
                $ctx->{git_dir} = $obj->{mainrepo};
                $ctx->{git} = $obj->git;
+               # for PublicInbox::HTTP::weaken_task:
+               $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
                $ctx->{-inbox} = $obj;
                $ctx->{inbox} = $inbox;
                return;
@@ -144,7 +151,7 @@ sub invalid_inbox {
 # returns undef if valid, array ref response if invalid
 sub invalid_inbox_mid {
        my ($self, $ctx, $inbox, $mid) = @_;
-       my $ret = invalid_inbox($self, $ctx, $inbox, $mid);
+       my $ret = invalid_inbox($self, $ctx, $inbox);
        return $ret if $ret;
 
        $ctx->{mid} = $mid = uri_unescape($mid);
@@ -245,8 +252,6 @@ sub footer {
        my ($ctx) = @_;
        return '' unless $ctx;
        my $obj = $ctx->{-inbox} or return '';
-       my $footer = $obj->footer_html;
-       return $ctx->{footer} = $footer if $footer;
 
        # auto-generate a footer
        chomp(my $desc = $obj->description);
@@ -382,9 +387,14 @@ sub legacy_redirects {
 sub r301 {
        my ($ctx, $inbox, $mid, $suffix) = @_;
        my $cgi = $ctx->{cgi};
-       my $url;
+       my $obj = $ctx->{-inbox};
+       unless ($obj) {
+               my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
+               return $r404 if $r404;
+               $obj = $ctx->{-inbox};
+       }
+       my $url = $obj->base_url($cgi);
        my $qs = $cgi->env->{QUERY_STRING};
-       $url = $cgi->base->as_string . $inbox . '/';
        $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
        $url .= $suffix if (defined $suffix);
        $url .= "?$qs" if $qs ne '';
@@ -435,4 +445,10 @@ sub news_www {
        $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
 }
 
+sub get_attach {
+       my ($ctx, $idx, $fn) = @_;
+       require PublicInbox::WwwAttach;
+       PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
+}
+
 1;