From: Eric Wong Date: Thu, 10 Apr 2014 03:02:01 +0000 (+0000) Subject: cgi: implement get_mid_txt X-Git-Tag: v1.0.0~1280 X-Git-Url: http://www.git.stargrave.org/?p=public-inbox.git;a=commitdiff_plain;h=6e1589df66857f4dd75e08e8444da2a5db8cd098 cgi: implement get_mid_txt This is essential when telling people to use something like: curl $URL | git am --- diff --git a/public-inbox-cgi b/public-inbox-cgi index 912bb191..91314f06 100755 --- a/public-inbox-cgi +++ b/public-inbox-cgi @@ -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 3ba74f30..a19f2cf1 100644 --- 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: /, + "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 {