From b9b12ac92230cc6807623293b93cc22e5eb33684 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 3 Mar 2017 00:55:07 +0000 Subject: repobrowse: consistently set text charset For everything with relevant content, we'll try to set UTF-8 charset and reduce duplication when generating response headers. --- lib/PublicInbox/RepoBase.pm | 9 ++++++++- lib/PublicInbox/RepoGitAtom.pm | 3 +-- lib/PublicInbox/RepoGitCommit.pm | 11 +++++------ lib/PublicInbox/RepoGitDiff.pm | 9 ++++----- lib/PublicInbox/RepoGitLog.pm | 6 +++--- lib/PublicInbox/RepoGitPatch.pm | 4 ++-- lib/PublicInbox/RepoGitRaw.pm | 10 +++++----- lib/PublicInbox/RepoGitSrc.pm | 31 ++++++++++++------------------- lib/PublicInbox/RepoGitSummary.pm | 2 +- lib/PublicInbox/RepoGitTag.pm | 5 ++--- t/repobrowse_git_log.t | 2 +- 11 files changed, 44 insertions(+), 48 deletions(-) diff --git a/lib/PublicInbox/RepoBase.pm b/lib/PublicInbox/RepoBase.pm index 9876cf2a..e600b1df 100644 --- a/lib/PublicInbox/RepoBase.pm +++ b/lib/PublicInbox/RepoBase.pm @@ -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; diff --git a/lib/PublicInbox/RepoGitAtom.pm b/lib/PublicInbox/RepoGitAtom.pm index c30c6184..c2393b38 100644 --- a/lib/PublicInbox/RepoGitAtom.pm +++ b/lib/PublicInbox/RepoGitAtom.pm @@ -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' ] ]; diff --git a/lib/PublicInbox/RepoGitCommit.pm b/lib/PublicInbox/RepoGitCommit.pm index 8add3006..acac000e 100644 --- a/lib/PublicInbox/RepoGitCommit.pm +++ b/lib/PublicInbox/RepoGitCommit.pm @@ -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 .= "$try the latest commit in HEAD\n"; $x .= ''; - [404, ['Content-Type', 'text/html'], [ $x ]]; + $self->rt(404, 'html', $x); } # FIXME: horrifically expensive... diff --git a/lib/PublicInbox/RepoGitDiff.pm b/lib/PublicInbox/RepoGitDiff.pm index 8d3b97a0..35bcb2d9 100644 --- a/lib/PublicInbox/RepoGitDiff.pm +++ b/lib/PublicInbox/RepoGitDiff.pm @@ -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' ] - ] + 'No differences'); } else { $env->{'qspawn.filter'} = git_diff_sed($self, $req); - [ 200, [ 'Content-Type', 'text/html' ] ]; + $self->rt(200, 'html'); } }); } diff --git a/lib/PublicInbox/RepoGitLog.pm b/lib/PublicInbox/RepoGitLog.pm index 1ad83fc0..73319633 100644 --- a/lib/PublicInbox/RepoGitLog.pm +++ b/lib/PublicInbox/RepoGitLog.pm @@ -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'); } }); } diff --git a/lib/PublicInbox/RepoGitPatch.pm b/lib/PublicInbox/RepoGitPatch.pm index cd5bf13c..1c9ec45f 100644 --- a/lib/PublicInbox/RepoGitPatch.pm +++ b/lib/PublicInbox/RepoGitPatch.pm @@ -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"); }); } diff --git a/lib/PublicInbox/RepoGitRaw.pm b/lib/PublicInbox/RepoGitRaw.pm index f02439ad..7d2c0d22 100644 --- a/lib/PublicInbox/RepoGitRaw.pm +++ b/lib/PublicInbox/RepoGitRaw.pm @@ -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
 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');
 		}
 	});
 }
diff --git a/lib/PublicInbox/RepoGitSrc.pm b/lib/PublicInbox/RepoGitSrc.pm
index 1546830f..de068940 100644
--- a/lib/PublicInbox/RepoGitSrc.pm
+++ b/lib/PublicInbox/RepoGitSrc.pm
@@ -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);
 		}
 	});
 }
diff --git a/lib/PublicInbox/RepoGitSummary.pm b/lib/PublicInbox/RepoGitSummary.pm
index 96ae9be9..ee9436f3 100644
--- a/lib/PublicInbox/RepoGitSummary.pm
+++ b/lib/PublicInbox/RepoGitSummary.pm
@@ -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") .
diff --git a/lib/PublicInbox/RepoGitTag.pm b/lib/PublicInbox/RepoGitTag.pm
index 785de6b1..2a281ed6 100644
--- a/lib/PublicInbox/RepoGitTag.pm
+++ b/lib/PublicInbox/RepoGitTag.pm
@@ -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('
'); diff --git a/t/repobrowse_git_log.t b/t/repobrowse_git_log.t index 86338698..2caba546 100644 --- a/t/repobrowse_git_log.t +++ b/t/repobrowse_git_log.t @@ -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!!, 'valid HTML :)'); -- cgit v1.2.3-24-ge0c7