]> Sergey Matveev's repositories - public-inbox.git/commitdiff
cgi: implement get_mid_txt
authorEric Wong <e@80x24.org>
Thu, 10 Apr 2014 03:02:01 +0000 (03:02 +0000)
committerEric Wong <e@80x24.org>
Thu, 10 Apr 2014 03:02:01 +0000 (03:02 +0000)
This is essential when telling people to use something like:
curl $URL | git am

public-inbox-cgi
t/cgi.t

index 912bb191bd3e1f75ad9f6e0be2c8e07de93618cb..91314f06836456414a172e7b0e47669f20a5ecb4 100755 (executable)
@@ -15,6 +15,7 @@ use warnings;
 use CGI qw(:cgi :escapeHTML -nosticky); # PSGI/FastCGI/mod_perl compat
 use Encode qw(decode_utf8);
 use PublicInbox::Config;
+use Digest::SHA qw(sha1_hex);
 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
 our $pi_config;
 BEGIN {
@@ -63,11 +64,11 @@ sub main {
                invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
 
        # single-message pages
-       } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.txt\z!o) {
-               get_mid_txt($cgi, $1, $2);
-       } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.html\z!o) {
-               get_mid_html($cgi, $1, $2);
-       } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\z!o) {
+       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
+       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
+       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
                redirect_mid_html($cgi, $1, $2);
        } else {
                r404();
@@ -126,3 +127,19 @@ sub get_index {
                })
        ];
 }
+
+sub mid2blob {
+       my ($ctx) = @_;
+       local $ENV{GIT_DIR} = $ctx->{git_dir};
+       my $hex = sha1_hex($ctx->{mid});
+       $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
+                       die "BUG: not a SHA-1 hex: $hex";
+       my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
+       $? == 0 ? \$blob : undef;
+}
+
+sub get_mid_txt {
+       my ($ctx, $cgi) = @_;
+       my $x = mid2blob($ctx);
+       $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
+}
diff --git a/t/cgi.t b/t/cgi.t
index 3ba74f30cb10d03663a943f6690990191606ce8f..a19f2cf11ab9fe8f367ce69ab942fa447846d48b 100644 (file)
--- a/t/cgi.t
+++ b/t/cgi.t
@@ -122,6 +122,16 @@ EOF
        # more checks in t/feed.t
 }
 
+
+{
+       local $ENV{HOME} = $home;
+       my $res = cgi_run("/test/m/blahblah\@example.com.txt");
+       like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
+               "mid.txt hit");
+       $res = cgi_run("/test/m/blahblah\@example.con.txt");
+       like($res->{head}, qr/Status: 404 Not Found/, "mid.txt miss");
+}
+
 done_testing();
 
 sub run_with_env {