gitweb: use correct mime type even if filename has multiple dots.
PATCH 1/1 gitweb: use correct mime type even if filename has multiple dots.
Match the last part of the filename agains the extention from the mime database instead of insisting that it starts at the first dot.
--- gitweb/gitweb.perl | 9 +++++---- 1 files changed, 5 insertions(+), 4 deletions(-)
gitweb/gitweb.perl +5 -4
@@ -1106,7 +1106,6 @@ sub mimetype_guess_file { my $mimemap = shift; -r $mimemap or return undef; - my %mimemap; open(MIME, $mimemap) or return undef; while (<MIME>) { next if m/^#/; # skip comments
@@ -1114,14 +1113,16 @@ sub mimetype_guess_file { if (defined $exts) { my @exts = split(/\s+/, $exts); foreach my $ext (@exts) { - $mimemap{$ext} = $mime; + if ($filename =~ /\.$ext$/) { + close(MIME); + return $mime; + } } } } close(MIME); - $filename =~ /\.(.*?)$/; - return $mimemap{$1}; + return undef; } sub mimetype_guess {
Signature · 4 lines
1.4.2.gb8b6b -- Martin Waitz
Quoted 2 lines · Martin Waitz
Match the last part of the filename agains the extention from the mime database instead of insisting that it starts at the first dot.
[...]
gitweb/gitweb.perl sub mimetype_guess_file { · line 1124
Quoted 2 lines
gitweb/gitweb.perl +0
-2- $filename =~ /\.(.*?)$/; - return $mimemap{$1};
Actually, that is non-greedy match, so the above code insist that extension starts at the _last_ dot.
Signature · 3 lines
Jakub Narebski Warsaw, Poland ShadeHawk on #git
hoi :)
Quoted 10 lines · Jakub Narebski
Quoted 2 lines · Martin Waitz
Match the last part of the filename agains the extention from the mime database instead of insisting that it starts at the first dot.
[...]
gitweb/gitweb.perl sub mimetype_guess_file { · line 1124
Quoted 2 lines
gitweb/gitweb.perl +0
-2- $filename =~ /\.(.*?)$/; - return $mimemap{$1};Actually, that is non-greedy match, so the above code insist that extension starts at the _last_ dot.
hmm, but it didn't work for me.
I had filenames like "man/program.8.html" which got served as "text/html" with the old code.
Besides, the new code would cope with extentions that contain a dot itself.
Looking at /etc/mime.types, it only contains pcf.Z but perhaps it should also contain tar.gz or similiar.
Quoted 11 lines
Signature · 10 lines
-- Jakub Narebski Warsaw, Poland ShadeHawk on #git - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 1 line
Martin Waitz
Quoted 9 lines · Martin Waitz
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +0
-2- $filename =~ /\.(.*?)$/; - return $mimemap{$1};Actually, that is non-greedy match, so the above code insist that extension starts at the _last_ dot.
hmm, but it didn't work for me.
I had filenames like "man/program.8.html" which got served as "text/html" with the old code.
It based its decision on '.html' part, which is expected from non-greedy match (if I were writing the pattern, I would have written /\.[^.]+$/ instead, though). Are you trying to have it behave differently between "x.8.html" and "x.html"?
Quoted 2 lines
Looking at
/etc/mime.types, it only contains pcf.Z but perhaps it should also contain tar.gz or similiar.
Probably. But that makes me think it might be better to:
- read in mime.types, sort the entries with length of the suffixes (longer first);
- try matching the suffixes from longer to shorter and pick the first match.
Without that, you would not be able to cope with a /etc/mime.types that looks like this, no?
application/a a
application/b b.a
Perhaps something like the attached.
---
gitweb/gitweb.perl +30 -11
@@ -1140,23 +1140,42 @@ sub mimetype_guess_file { my $filename = shift; my $mimemap = shift; -r $mimemap or return undef; + local($_); - my %mimemap; - open(MIME, $mimemap) or return undef; + open MIME, $mimemap + or return undef; + + # Under mod_perl caching this may make a lot of sense... + my @mime = (); + my $maxlen = 0; while (<MIME>) { - next if m/^#/; # skip comments - my ($mime, $exts) = split(/\t+/); - if (defined $exts) { - my @exts = split(/\s+/, $exts); - foreach my $ext (@exts) { - $mimemap{$ext} = $mime; + next if /^#/; + chomp; + my ($mimetype, @ext) = split(/\s+/); + for (@ext) { + my $len = length; + my $map = $mime[$len]; + if (!$map) { + $mime[$len] = $map = {}; + $maxlen = $len if ($maxlen < $len); } + # We could detect duplicate definition here... i.e. + # onetype ext + # anothertype ext + $map->{$_} = $mimetype; } } - close(MIME); + close MIME; - $filename =~ /\.(.*?)$/; - return $mimemap{$1}; + for ($filename) { + for (my $len = $maxlen; 0 < $len; $len--) { + my $map = $mime[$len]; + while (my ($ext, $type) = each %$map) { + return $type if (/\.\Q$ext\E$/); + } + } + } + return undef; } sub mimetype_guess {
Quoted 11 lines · Junio C Hamano
Quoted 2 lines
Looking at
/etc/mime.types, it only contains pcf.Z but perhaps it should also contain tar.gz or similiar.Probably. But that makes me think it might be better to:
- read in mime.types, sort the entries with length of the suffixes (longer first);
- try matching the suffixes from longer to shorter and pick the first match.
Quoted 7 lines
Without that, you would not be able to cope with a
/etc/mime.typesthat looks like this, no?application/a a
application/b b.aPerhaps something like the attached.
Is it really useful? Usually the suffix in mime.types doesn't contain dot itself. Besides, to be said we need correct mimetype only for files which can be displayed in browser (HTML, XHTML, images: png, gif, jpeg, perhaps XML). All other can get generic mimetype, i.e. application/octet-stream for binary files (to be saved) and text/plain for text file (to be displayer as-is in browser).
Besides, performance will suffer for "blob_plain" view. One hash lookup vs. nested loops.
Signature · 3 lines
Jakub Narebski Warsaw, Poland ShadeHawk on #git
Quoted 6 lines · Jakub Narebski
Is it really useful? Usually the suffix in mime.types doesn't contain dot itself. Besides, to be said we need correct mimetype only for files which can be displayed in browser (HTML, XHTML, images: png, gif, jpeg, perhaps XML). All other can get generic mimetype, i.e. application/octet-stream for binary files (to be saved) and text/plain for text file (to be displayer as-is in browser).
Sorry, I do not think that is quite correct. The browser can launch an appropriate application on the downloaded file, as long as you send the data labelled with proper mimetype. If you send out application/octet-stream, of course that would not work.
Having said that, I am not sure what special things Martin wanted to do with x.8.html that is different from y.html, so maybe discussing the patch does not have merit at all.
hoi :)
Quoted 6 lines · Junio C Hamano
- read in mime.types, sort the entries with length of the suffixes (longer first);
- try matching the suffixes from longer to shorter and pick the first match.
Quoted 7 lines
Without that, you would not be able to cope with a
/etc/mime.typesthat looks like this, no?application/a a
application/b b.aPerhaps something like the attached.
works perfectly, thanks.
Signature · 1 line
Martin Waitz
Quoted 11 lines · Martin Waitz
hoi :)
Quoted 8 lines · Jakub Narebski
Quoted 2 lines · Martin Waitz
Match the last part of the filename agains the extention from the mime database instead of insisting that it starts at the first dot.
[...]
Quoted 2 lines
gitweb/gitweb.perl +0
-2- $filename =~ /\.(.*?)$/; - return $mimemap{$1};
Quoted 6 lines
Quoted 2 lines
Actually, that is non-greedy match, so the above code insist that extension starts at the _last_ dot.
hmm, but it didn't work for me.
I had filenames like "man/program.8.html" which got served as "text/html" with the old code.
And why it shouldn't? From the extension it is HTML page, I would guess manpage converted to HTML (pretty-printed manpage). And it should be served with text/html mimetype.
Quoted 2 lines
Besides, the new code would cope with extentions that contain a dot itself.
But is unnecessary complicated, and I guess performance suffers a bit.
Quoted 1 line
Looking at
/etc/mime.types, it only contains pcf.Z but perhaps
So the *.pcf.Z file wouldn't get correct mimetype. No big.
Quoted 1 line
it should also contain tar.gz or similiar.
You can't properly serve tar.gz as something other than with application/x-gzip mimetype. If you want to serve it as/with application/x-tar mimetype, you would need to add
Content-Encoding: x-gzip
in addition to
Content-Type: application/x-tar
And your code doesn't do that.
The _last_ extension defines the type.
Besides, with the exception of files which can be displayed in browser, i.e. HTML files and images it doesn't matter what the mimetype is, if only binary files get binary mimetype (e.g. generic application/octet-stream).
Signature · 3 lines
Jakub Narebski Warsaw, Poland ShadeHawk on #git
Quoted 11 lines · Jakub Narebski
Quoted 5 lines · Martin Waitz
hoi :)
hmm, but it didn't work for me.
I had filenames like "man/program.8.html" which got served as "text/html" with the old code.And why it shouldn't? From the extension it is HTML page, I would guess manpage converted to HTML (pretty-printed manpage). And it should be served with text/html mimetype.
arg, typo, it got served as "text/plain".
Signature · 1 line
Martin Waitz
PATCH 1/1 gitweb: Fix mimetype_guess_file for files with multiple extensions
Fix getting correct mimetype for "blob_plain" view for files which have multiple extensions, e.g. foo.1.html; now only the last extension is used to find mimetype.
Noticed by Martin Waitz.
---
This is much simpler (and faster!) correction to the mentioned problem. I just don't grok regular expressions, not completly.
gitweb/gitweb.perl | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
gitweb/gitweb.perl +1 -1
@@ -1199,7 +1199,7 @@ sub mimetype_guess_file { } close(MIME); - $filename =~ /\.(.*?)$/; + $filename =~ /\.([^.]*)$/; return $mimemap{$1}; }
Signature · 1 line
1.4.2.1