]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
repobrowse: consistently set text charset
authorEric Wong <e@80x24.org>
Fri, 3 Mar 2017 00:55:07 +0000 (00:55 +0000)
committerEric Wong <e@80x24.org>
Fri, 3 Mar 2017 00:55:07 +0000 (00:55 +0000)
For everything with relevant content, we'll try to set
UTF-8 charset and reduce duplication when generating
response headers.

lib/PublicInbox/RepoBase.pm
lib/PublicInbox/RepoGitAtom.pm
lib/PublicInbox/RepoGitCommit.pm
lib/PublicInbox/RepoGitDiff.pm
lib/PublicInbox/RepoGitLog.pm
lib/PublicInbox/RepoGitPatch.pm
lib/PublicInbox/RepoGitRaw.pm
lib/PublicInbox/RepoGitSrc.pm
lib/PublicInbox/RepoGitSummary.pm
lib/PublicInbox/RepoGitTag.pm
t/repobrowse_git_log.t

index 9876cf2a01e15648edbf3ad32f5a3b880d5a7884..e600b1dfd7408b1a98110465a0a495cbd38c25e5 100644 (file)
@@ -105,10 +105,17 @@ sub r {
                # mainly for curl (no-'-L') users:
                $body = "Redirecting to $redir\n";
        } else {
-               push @h, qw(Content-Type text/plain);
+               push @h, 'Content-Type', 'text/plain; charset=UTF-8';
        }
 
        [ $status, \@h, [ $body ] ]
 }
 
+sub rt {
+       my ($self, $status, $t) = @_;
+       my $res = [ $status, [ 'Content-Type', "text/$t; charset=UTF-8" ] ];
+       $res->[2] = [ $_[3] ] if defined $_[3];
+       $res;
+}
+
 1;
index c30c61840b5e0549bde18c3d2a094578642af410..c2393b38b57d4b0f99ab2f1902a808b0c10dbaa4 100644 (file)
@@ -124,8 +124,7 @@ sub git_atom_cb {
                my $env = $req->{env};
                if (!defined $r) {
                        my $git = $req->{-repo}->{git};
-                       return [ 400, [ 'Content-Type', 'text/plain' ],
-                               [ $git->err ] ];
+                       return $self->rt(400, 'plain', $git->err);
                }
                $env->{'qspawn.filter'} = git_atom_sed($self, $req);
                [ 200, [ 'Content-Type', 'application/atom+xml' ] ];
index 8add3006a1fbbb137d606228ccf6fd17692e718a..acac000e1594fb6f4ef2a638c65c80628850c77b 100644 (file)
@@ -135,19 +135,18 @@ sub call_git_commit { # RepoBase calls this
        $qsp->psgi_return($env, undef, sub { # parse header
                my ($r, $bref) = @_;
                if (!defined $r) {
-                       my $errmsg = $git->err;
-                       [ 500, [ 'Content-Type', 'text/html' ], [ $errmsg ] ];
+                       $self->rt(500, 'plain', $git->err);
                } elsif ($r == 0) {
-                       git_commit_404($req);
+                       git_commit_404($self, $req);
                } else {
                        $env->{'qspawn.filter'} = git_commit_sed($self, $req);
-                       [ 200, [ 'Content-Type', 'text/html' ] ];
+                       $self->rt(200, 'html');
                }
        });
 }
 
 sub git_commit_404 {
-       my ($req) = @_;
+       my ($self, $req) = @_;
        my $x = 'Missing commit or path';
        my $pfx = "$req->{relcmd}commit";
 
@@ -156,7 +155,7 @@ sub git_commit_404 {
        $x .= "<a\nhref=\"$pfx\">$try the latest commit in HEAD</a>\n";
        $x .= '</pre></body>';
 
-       [404, ['Content-Type', 'text/html'], [ $x ]];
+       $self->rt(404, 'html', $x);
 }
 
 # FIXME: horrifically expensive...
index 8d3b97a0da4c47e690bfbd684a41f6ff4667762b..35bcb2d9147025587283e0b77dbc68a174174df5 100644 (file)
@@ -54,15 +54,14 @@ sub call_git_diff {
        $qsp->psgi_return($env, undef, sub { # parse header
                my ($r) = @_;
                if (!defined $r) {
-                       [ 500, [ 'Content-Type', 'text/plain' ], [ $git->err ]];
+                       $self->rt(500, 'plain', $git->err);
                } elsif ($r == 0) {
-                       [ 200, [ 'Content-Type', 'text/html' ], [
+                       $self->rt(200, 'html',
                                delete($req->{dhtml}).
-                               'No differences</pre></body></html>' ]
-                       ]
+                               'No differences</pre></body></html>');
                } else {
                        $env->{'qspawn.filter'} = git_diff_sed($self, $req);
-                       [ 200, [ 'Content-Type', 'text/html' ] ];
+                       $self->rt(200, 'html');
                }
        });
 }
index 1ad83fc0536145074f1dba29d4c571ec043c563b..7331963324435044e06aaa195f8981f1b46f099d 100644 (file)
@@ -139,12 +139,12 @@ sub call_git_log {
        $qsp->psgi_return($env, undef, sub {
                my ($r) = @_;
                if (!defined $r) {
-                       [ 500, [ 'Content-Type', 'text/html' ], [ $git->err ] ];
+                       $self->rt(500, 'html', $git->err);
                } elsif ($r == 0) {
-                       [ 404, [ 'Content-Type', 'text/html' ], [ $git->err ] ];
+                       $self->rt(404, 'html', $git->err);
                } else {
                        $env->{'qspawn.filter'} = git_log_sed($self, $req);
-                       [ 200, [ 'Content-Type', 'text/html' ] ];
+                       $self->rt(200, 'html');
                }
        });
 }
index cd5bf13c898128a9f079071f1426e54522172843..1c9ec45fa24fcb732920ad3482e3e9c836e91d47 100644 (file)
@@ -31,8 +31,8 @@ sub call_git_patch {
        my $qsp = PublicInbox::Qspawn->new($cmd);
        $qsp->psgi_return($env, undef, sub {
                my ($r) = @_;
-               my $h = ['Content-Type', 'text/plain; charset=UTF-8'];
-               $r ? [ 200, $h ] : [ 500, $h, [ "format-patch error\n" ] ];
+               $r ? $self->rt(200, 'plain') :
+                       $self->rt(500, 'plain', "format-patch error\n");
        });
 }
 
index f02439ad9eb39a551ad639931bbd7621cc3efdbb..7d2c0d22edcd32d09100492f27cc5f8f9aca629e 100644 (file)
@@ -22,10 +22,10 @@ sub call_git_raw {
        if ($type eq 'blob') {
                $type = git_blob_mime_type($self, $req, $cat, \$buf, \$left);
        } elsif ($type eq 'commit' || $type eq 'tag') {
-               $type = 'text/plain';
+               $type = 'text/plain; charset=UTF-8';
        } elsif ($type eq 'tree') {
                $git->cat_file_finish($left);
-               return git_tree_raw($req, $git, $hex);
+               return git_tree_raw($self, $req, $git, $hex);
        } else {
                $type = 'application/octet-stream';
        }
@@ -61,7 +61,7 @@ sub git_tree_sed ($) {
 # This should follow the cgit DOM structure in case anybody depends on it,
 # not using <pre> here as we don't expect people to actually view it much
 sub git_tree_raw {
-       my ($req, $git, $hex) = @_;
+       my ($self, $req, $git, $hex) = @_;
 
        my @ex = @{$req->{extra}};
        my $rel = $req->{relcmd};
@@ -83,10 +83,10 @@ sub git_tree_raw {
        $qsp->psgi_return($env, undef, sub {
                my ($r) = @_;
                if (!defined $r) {
-                       [ 500, [ 'Content-Type', 'text/plain' ], [ $git->err ]];
+                       $self->rt(500, 'plain', $git->err);
                } else {
                        $env->{'qspawn.filter'} = git_tree_sed($req);
-                       [ 200, [ 'Content-Type', 'text/html' ] ];
+                       $self->rt(200, 'html');
                }
        });
 }
index 1546830f0acaf8eff8f0cdc9925e6cc9c7f93e8f..de068940cb92cf1efc9378598bcaa3db002f0717 100644 (file)
@@ -30,9 +30,7 @@ sub call_git_src {
                        my ($info) = @_;
                        my ($hex, $type, $size) = @$info;
                        unless (defined $type) {
-                               return $res->([404,
-                                       ['Content-Type','text/plain'],
-                                       ['Not Found']]);
+                               $res->($self->rt(404, 'plain', 'Not Found'));
                        }
                        show_tree($self, $req, $res, $hex, $type, $size);
                });
@@ -46,12 +44,12 @@ sub show_tree {
        $req->{thtml} = $self->html_start($req, $title, $opts) . "\n";
        if ($type eq 'tree') {
                $opts->{noindex} = 1;
-               git_tree_show($req, $res, $hex);
+               git_tree_show($self, $req, $res, $hex);
        } elsif ($type eq 'blob') {
-               git_blob_show($req, $res, $hex, $size);
+               git_blob_show($self, $req, $res, $hex, $size);
        } else {
-               $res->([404, ['Content-Type', 'text/plain; charset=UTF-8'],
-                        ["Unrecognized type ($type) for $hex\n"]]);
+               $res->($self->rt(404, 'plain',
+                       "Unrecognized type ($type) for $hex\n"));
        }
 }
 
@@ -140,7 +138,7 @@ sub git_blob_sed ($$$) {
 }
 
 sub git_blob_show {
-       my ($req, $res, $hex, $size) = @_;
+       my ($self, $req, $res, $hex, $size) = @_;
        my $sed = git_blob_sed($req, $hex, $size);
        my $git = $req->{-repo}->{git};
        if ($size <= $MAX_ASYNC) {
@@ -152,9 +150,7 @@ sub git_blob_show {
                        if ($ref eq 'SCALAR') {
                                $buf .= $$r;
                                if (bytes::length($buf) == $size) {
-                                       my $fh = $res->([200,
-                                               ['Content-Type',
-                                                'text/html; charset=UTF-8']]);
+                                       my $fh = $res->($self->rt(200, 'html'));
                                        $fh->write($sed->($buf));
                                        $fh->write($sed->(undef));
                                        $fh->close;
@@ -163,13 +159,10 @@ sub git_blob_show {
                        }
                        my $cb = $res or return;
                        $res = undef;
-                       $cb->([500,
-                               ['Content-Type', 'text/plain; charset=UTF-8'],
-                               [ 'Error' ]]);
+                       $cb->($self->rt(500, 'plain', "Error\n"));
                });
        } else {
-               $res->([200, ['Content-Type', 'text/plain; charset=UTF-8'],
-                       [ 'Too big' ]]);
+               $res->($self->rt(200, 'plain', "Too big\n"));
        }
 }
 
@@ -216,7 +209,7 @@ sub git_tree_sed ($) {
 }
 
 sub git_tree_show {
-       my ($req, $res, $hex) = @_;
+       my ($self, $req, $res, $hex) = @_;
        my $git = $req->{-repo}->{git};
        my $cmd = $git->cmd(qw(ls-tree -l -z), $git->abbrev, $hex);
        my $rdr = { 2 => $git->err_begin };
@@ -239,9 +232,9 @@ sub git_tree_show {
                my ($r) = @_;
                if (defined $r) {
                        $env->{'qspawn.filter'} = git_tree_sed($req);
-                       [ 200, [ 'Content-Type', 'text/html' ] ];
+                       $self->rt(200, 'html');
                } else {
-                       [ 500, [ 'Content-Type', 'text/plain' ], [ $git->err ]];
+                       $self->rt(500, 'plain', $git->err);
                }
        });
 }
index 96ae9be913dea768de5093ab018cad0659aebf9f..ee9436f3bb70a9d7fd5d60eed6e3e885ae46be8f 100644 (file)
@@ -32,7 +32,7 @@ sub for_each_ref {
        my $refs = $git->popen(qw(for-each-ref --sort=-creatordate),
                                EACH_REF_FMT, "--count=$count",
                                qw(refs/heads/ refs/tags/));
-       $fh = $res->([200, ['Content-Type', 'text/html; charset=UTF-8']]);
+       $fh = $res->($self->rt(200, 'html'));
        # ref names are unpredictable in length and requires tables :<
        $fh->write($self->html_start($req,
                                "$repo->{repo}: overview") .
index 785de6b1e739d9b115ba6506daafdb0cd9b2cbd3..2a281ed6677b3fea3c42baf98505852210e7ee33 100644 (file)
@@ -78,13 +78,12 @@ sub git_tag_show {
        my ($self, $req, $h, $res) = @_;
        my $git = $req->{-repo}->{git};
        my $fh;
-       my $hdr = ['Content-Type', 'text/html; charset=UTF-8'];
 
        # yes, this could still theoretically show anything,
        # but a tag could also point to anything:
        $git->cat_file("refs/tags/$h", sub {
                my ($cat, $left, $type, $hex) = @_;
-               $fh = $res->([200, $hdr]);
+               $fh = $res->($self->rt(200, 'html'));
                $h = PublicInbox::Hval->utf8($h);
                my $m = "git_show_${type}_as_tag";
 
@@ -97,7 +96,7 @@ sub git_tag_show {
                }
        });
        unless ($fh) {
-               $fh = $res->([404, $hdr]);
+               $fh = $res->($self->rt(404, 'html'));
                $fh->write(invalid_tag_start($req, $h));
        }
        $fh->write('</pre></body></html>');
index 86338698a1f4a97733b734e15bc95424afaeb25d..2caba546c3b5d6da569f080f461c34180ec6fc61 100644 (file)
@@ -10,7 +10,7 @@ test_psgi($test->{app}, sub {
        my $req = 'http://example.com/test.git/log';
        my $res = $cb->(GET($req));
        is($res->code, 200, 'got 200');
-       is($res->header('Content-Type'), 'text/html',
+       is($res->header('Content-Type'), 'text/html; charset=UTF-8',
                'got correct Content-Type');
        my $body = dechunk($res);
        like($body, qr!</html>!, 'valid HTML :)');