{"thread":{"id":"5581","subject":"[PATCH] gitweb: use correct mime type even if filename has multiple dots.","startedAt":"2006-09-16T21:09:33Z","lastAt":"2006-09-19T11:57:03Z","messageCount":10,"participants":["Martin Waitz","Jakub Narebski","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"27019","messageId":"20060916210933.GX17042@admingilde.org","threadId":"5581","inReplyTo":null,"subject":"[PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Martin Waitz","fromEmail":"tali@admingilde.org","sentAt":"2006-09-16T21:09:33Z","receivedAt":"2006-09-16T21:09:33Z","isPatch":true,"sender":{"key":"tali@admingilde.org","avatar":"https://gravatar.com/avatar/3f89b03eee362187effabe257898735b475673a12265c398ea9161259ae91553?d=mp&s=160"},"body":"Match the last part of the filename agains the extention from the\nmime database instead of insisting that it starts at the first dot.\n\nSigned-off-by: Martin Waitz <tali@admingilde.org>\n---\n gitweb/gitweb.perl |    9 +++++----\n 1 files changed, 5 insertions(+), 4 deletions(-)\n\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex ee561c6..7501251 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -1106,7 +1106,6 @@ sub mimetype_guess_file {\n \tmy $mimemap = shift;\n \t-r $mimemap or return undef;\n \n-\tmy %mimemap;\n \topen(MIME, $mimemap) or return undef;\n \twhile (<MIME>) {\n \t\tnext if m/^#/; # skip comments\n@@ -1114,14 +1113,16 @@ sub mimetype_guess_file {\n \t\tif (defined $exts) {\n \t\t\tmy @exts = split(/\\s+/, $exts);\n \t\t\tforeach my $ext (@exts) {\n-\t\t\t\t$mimemap{$ext} = $mime;\n+\t\t\t\tif ($filename =~ /\\.$ext$/) {\n+\t\t\t\t\tclose(MIME);\n+\t\t\t\t\treturn $mime;\n+\t\t\t\t}\n \t\t\t}\n \t\t}\n \t}\n \tclose(MIME);\n \n-\t$filename =~ /\\.(.*?)$/;\n-\treturn $mimemap{$1};\n+\treturn undef;\n }\n \n sub mimetype_guess {\n-- \n1.4.2.gb8b6b\n\n-- \nMartin Waitz\n"},{"id":"27021","messageId":"eehr7e$5i0$1@sea.gmane.org","threadId":"5581","inReplyTo":"20060916210933.GX17042@admingilde.org","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-09-16T21:44:52Z","receivedAt":"2006-09-16T21:44:52Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Martin Waitz wrote:\n\n> Match the last part of the filename agains the extention from the\n> mime database instead of insisting that it starts at the first dot.\n[...]\n> -     $filename =~ /\\.(.*?)$/;\n> -     return $mimemap{$1};\n\nActually, that is non-greedy match, so the above code insist that \nextension starts at the _last_ dot.\n\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"},{"id":"27045","messageId":"20060917075157.GY17042@admingilde.org","threadId":"5581","inReplyTo":"eehr7e$5i0$1@sea.gmane.org","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Martin Waitz","fromEmail":"tali@admingilde.org","sentAt":"2006-09-17T07:51:57Z","receivedAt":"2006-09-17T07:51:57Z","isPatch":true,"sender":{"key":"tali@admingilde.org","avatar":"https://gravatar.com/avatar/3f89b03eee362187effabe257898735b475673a12265c398ea9161259ae91553?d=mp&s=160"},"body":"hoi :)\n\nOn Sat, Sep 16, 2006 at 11:44:52PM +0200, Jakub Narebski wrote:\n> Martin Waitz wrote:\n> \n> > Match the last part of the filename agains the extention from the\n> > mime database instead of insisting that it starts at the first dot.\n> [...]\n> > -     $filename =~ /\\.(.*?)$/;\n> > -     return $mimemap{$1};\n> \n> Actually, that is non-greedy match, so the above code insist that \n> extension starts at the _last_ dot.\n\nhmm, but it didn't work for me.\nI had filenames like \"man/program.8.html\" which got served as\n\"text/html\" with the old code.\n\nBesides, the new code would cope with extentions that contain a\ndot itself.\n\nLooking at /etc/mime.types, it only contains pcf.Z but perhaps\nit should also contain tar.gz or similiar.\n> \n> -- \n> Jakub Narebski\n> Warsaw, Poland\n> ShadeHawk on #git\n> \n> \n> -\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n\n-- \nMartin Waitz\n"},{"id":"27047","messageId":"eej0l8$36t$1@sea.gmane.org","threadId":"5581","inReplyTo":"20060917075157.GY17042@admingilde.org","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-09-17T08:23:45Z","receivedAt":"2006-09-17T08:23:45Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Martin Waitz wrote:\n\n> hoi :)\n> \n> On Sat, Sep 16, 2006 at 11:44:52PM +0200, Jakub Narebski wrote:\n>> Martin Waitz wrote:\n>> \n>> > Match the last part of the filename agains the extention from the\n>> > mime database instead of insisting that it starts at the first dot.\n>> [...]\n>> > -     $filename =~ /\\.(.*?)$/;\n>> > -     return $mimemap{$1};\n>> \n>> Actually, that is non-greedy match, so the above code insist that \n>> extension starts at the _last_ dot.\n> \n> hmm, but it didn't work for me.\n> I had filenames like \"man/program.8.html\" which got served as\n> \"text/html\" with the old code.\n\nAnd why it shouldn't? From the extension it is HTML page, I would guess\nmanpage converted to HTML (pretty-printed manpage). And it should be served\nwith text/html mimetype.\n\n> Besides, the new code would cope with extentions that contain a\n> dot itself.\n\nBut is unnecessary complicated, and I guess performance suffers a bit.\n\n> Looking at /etc/mime.types, it only contains pcf.Z but perhaps\n\nSo the *.pcf.Z file wouldn't get correct mimetype. No big.\n\n> it should also contain tar.gz or similiar.\n\nYou can't properly serve tar.gz as something other than with\napplication/x-gzip mimetype. If you want to serve it as/with \napplication/x-tar mimetype, you would need to add \n        Content-Encoding: x-gzip\nin addition to\n        Content-Type: application/x-tar\nAnd your code doesn't do that.\n\nThe _last_ extension defines the type.\n\n\nBesides, with the exception of files which can be displayed in browser, \ni.e. HTML files and images it doesn't matter what the mimetype is, if only\nbinary files get binary mimetype (e.g. generic application/octet-stream).\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"},{"id":"27049","messageId":"7vejua7um3.fsf@assigned-by-dhcp.cox.net","threadId":"5581","inReplyTo":"20060917075157.GY17042@admingilde.org","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-09-17T08:41:40Z","receivedAt":"2006-09-17T08:41:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Martin Waitz <tali@admingilde.org> writes:\n\n>> > -     $filename =~ /\\.(.*?)$/;\n>> > -     return $mimemap{$1};\n>> \n>> Actually, that is non-greedy match, so the above code insist that \n>> extension starts at the _last_ dot.\n>\n> hmm, but it didn't work for me.\n> I had filenames like \"man/program.8.html\" which got served as\n> \"text/html\" with the old code.\n\nIt based its decision on '.html' part, which is expected from\nnon-greedy match (if I were writing the pattern, I would have\nwritten /\\.[^.]+$/ instead, though).  Are you trying to have it\nbehave differently between \"x.8.html\" and \"x.html\"?\n\n> Looking at /etc/mime.types, it only contains pcf.Z but perhaps\n> it should also contain tar.gz or similiar.\n\nProbably.  But that makes me think it might be better to:\n\n - read in mime.types, sort the entries with length of the\n   suffixes (longer first);\n\n - try matching the suffixes from longer to shorter and pick the\n   first match.\n\nWithout that, you would not be able to cope with a /etc/mime.types\nthat looks like this, no?\n\n        application/a\ta\n        application/b\tb.a\n\nPerhaps something like the attached.\n\n---\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex a81c8d4..4994a33 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -1140,23 +1140,42 @@ sub mimetype_guess_file {\n \tmy $filename = shift;\n \tmy $mimemap = shift;\n \t-r $mimemap or return undef;\n+\tlocal($_);\n \n-\tmy %mimemap;\n-\topen(MIME, $mimemap) or return undef;\n+\topen MIME, $mimemap\n+\t    or return undef;\n+\n+\t# Under mod_perl caching this may make a lot of sense...\n+\tmy @mime = ();\n+\tmy $maxlen = 0;\n \twhile (<MIME>) {\n-\t\tnext if m/^#/; # skip comments\n-\t\tmy ($mime, $exts) = split(/\\t+/);\n-\t\tif (defined $exts) {\n-\t\t\tmy @exts = split(/\\s+/, $exts);\n-\t\t\tforeach my $ext (@exts) {\n-\t\t\t\t$mimemap{$ext} = $mime;\n+\t\tnext if /^#/;\n+\t\tchomp;\n+\t\tmy ($mimetype, @ext) = split(/\\s+/);\n+\t\tfor (@ext) {\n+\t\t\tmy $len = length;\n+\t\t\tmy $map = $mime[$len];\n+\t\t\tif (!$map) {\n+\t\t\t\t$mime[$len] = $map = {};\n+\t\t\t\t$maxlen = $len if ($maxlen < $len);\n \t\t\t}\n+\t\t\t# We could detect duplicate definition here... i.e.\n+\t\t\t# onetype\text\n+\t\t\t# anothertype\text\n+\t\t\t$map->{$_} = $mimetype;\n \t\t}\n \t}\n-\tclose(MIME);\n+\tclose MIME;\n \n-\t$filename =~ /\\.(.*?)$/;\n-\treturn $mimemap{$1};\n+\tfor ($filename) {\n+\t\tfor (my $len = $maxlen; 0 < $len; $len--) {\n+\t\t\tmy $map = $mime[$len];\n+\t\t\twhile (my ($ext, $type) = each %$map) {\n+\t\t\t\treturn $type if (/\\.\\Q$ext\\E$/);\n+\t\t\t}\n+\t\t}\n+\t}\n+\treturn undef;\n }\n \n sub mimetype_guess {\n"},{"id":"27053","messageId":"eej2i1$97c$1@sea.gmane.org","threadId":"5581","inReplyTo":"7vejua7um3.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-09-17T08:56:10Z","receivedAt":"2006-09-17T08:56:10Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Junio C Hamano wrote:\n\n>> Looking at /etc/mime.types, it only contains pcf.Z but perhaps\n>> it should also contain tar.gz or similiar.\n> \n> Probably.  But that makes me think it might be better to:\n> \n>  - read in mime.types, sort the entries with length of the\n>    suffixes (longer first);\n> \n>  - try matching the suffixes from longer to shorter and pick the\n>    first match.\n> \n> Without that, you would not be able to cope with a /etc/mime.types\n> that looks like this, no?\n> \n>         application/a   a\n>         application/b   b.a\n> \n> Perhaps something like the attached.\n\nIs it really useful? Usually the suffix in mime.types doesn't contain dot\nitself. Besides, to be said we need correct mimetype only for files which\ncan be displayed in browser (HTML, XHTML, images: png, gif, jpeg, perhaps\nXML). All other can get generic mimetype, i.e. application/octet-stream for\nbinary files (to be saved) and text/plain for text file (to be displayer\nas-is in browser).\n\nBesides, performance will suffer for \"blob_plain\" view. One hash lookup \nvs. nested loops.\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"},{"id":"27057","messageId":"7vvenm6euq.fsf@assigned-by-dhcp.cox.net","threadId":"5581","inReplyTo":"eej2i1$97c$1@sea.gmane.org","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-09-17T09:07:25Z","receivedAt":"2006-09-17T09:07:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> writes:\n\n> Is it really useful? Usually the suffix in mime.types doesn't contain dot\n> itself. Besides, to be said we need correct mimetype only for files which\n> can be displayed in browser (HTML, XHTML, images: png, gif, jpeg, perhaps\n> XML). All other can get generic mimetype, i.e. application/octet-stream for\n> binary files (to be saved) and text/plain for text file (to be displayer\n> as-is in browser).\n\nSorry, I do not think that is quite correct.  The browser can\nlaunch an appropriate application on the downloaded file, as\nlong as you send the data labelled with proper mimetype.  If you\nsend out application/octet-stream, of course that would not work.\n\nHaving said that, I am not sure what special things Martin\nwanted to do with x.8.html that is different from y.html, so\nmaybe discussing the patch does not have merit at all.\n"},{"id":"27151","messageId":"20060919082925.GC31940@admingilde.org","threadId":"5581","inReplyTo":"eej0l8$36t$1@sea.gmane.org","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Martin Waitz","fromEmail":"tali@admingilde.org","sentAt":"2006-09-19T08:29:25Z","receivedAt":"2006-09-19T08:29:25Z","isPatch":true,"sender":{"key":"tali@admingilde.org","avatar":"https://gravatar.com/avatar/3f89b03eee362187effabe257898735b475673a12265c398ea9161259ae91553?d=mp&s=160"},"body":"On Sun, Sep 17, 2006 at 10:23:45AM +0200, Jakub Narebski wrote:\n> Martin Waitz wrote:\n> \n> > hoi :)\n> > \n> > hmm, but it didn't work for me.\n> > I had filenames like \"man/program.8.html\" which got served as\n> > \"text/html\" with the old code.\n> \n> And why it shouldn't? From the extension it is HTML page, I would guess\n> manpage converted to HTML (pretty-printed manpage). And it should be served\n> with text/html mimetype.\n\narg, typo, it got served as \"text/plain\".\n\n-- \nMartin Waitz\n"},{"id":"27153","messageId":"20060919092324.GD31940@admingilde.org","threadId":"5581","inReplyTo":"7vejua7um3.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] gitweb: use correct mime type even if filename has multiple dots.","fromName":"Martin Waitz","fromEmail":"tali@admingilde.org","sentAt":"2006-09-19T09:23:24Z","receivedAt":"2006-09-19T09:23:24Z","isPatch":true,"sender":{"key":"tali@admingilde.org","avatar":"https://gravatar.com/avatar/3f89b03eee362187effabe257898735b475673a12265c398ea9161259ae91553?d=mp&s=160"},"body":"hoi :)\n\nOn Sun, Sep 17, 2006 at 01:41:40AM -0700, Junio C Hamano wrote:\n>  - read in mime.types, sort the entries with length of the\n>    suffixes (longer first);\n> \n>  - try matching the suffixes from longer to shorter and pick the\n>    first match.\n> \n> Without that, you would not be able to cope with a /etc/mime.types\n> that looks like this, no?\n> \n>         application/a\ta\n>         application/b\tb.a\n> \n> Perhaps something like the attached.\n\nworks perfectly, thanks.\n\n-- \nMartin Waitz\n"},{"id":"27155","messageId":"200609191357.03796.jnareb@gmail.com","threadId":"5581","inReplyTo":"20060919082925.GC31940@admingilde.org","subject":"[PATCH] gitweb: Fix mimetype_guess_file for files with multiple extensions","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-09-19T11:57:03Z","receivedAt":"2006-09-19T11:57:03Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Fix getting correct mimetype for \"blob_plain\" view for files which have\nmultiple extensions, e.g. foo.1.html; now only the last extension\nis used to find mimetype.\n\nNoticed by Martin Waitz.\n\nSigned-off-by: Jakub Narebski <jnareb@gmail.com>\n---\nThis is much simpler (and faster!) correction to the mentioned problem.\nI just don't grok regular expressions, not completly.\n\n gitweb/gitweb.perl |    2 +-\n 1 files changed, 1 insertions(+), 1 deletions(-)\n\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex 01fae94..034cdf1 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -1199,7 +1199,7 @@ sub mimetype_guess_file {\n \t}\n \tclose(MIME);\n \n-\t$filename =~ /\\.(.*?)$/;\n+\t$filename =~ /\\.([^.]*)$/;\n \treturn $mimemap{$1};\n }\n \n-- \n1.4.2.1\n"}]}