mime: skip globs2 entries that are not simple file extensions

The entries in globs2 can be globs beyond simple *.ext ones. We support only simple extension based matching, so skip entries that do not represent them.

Change-Id: Id5d089cb4067e53beb2471a5e67a59c13880a017
GitHub-Last-Rev: f725a91054
GitHub-Pull-Request: golang/go#51156
Reviewed-on: https://go-review.googlesource.com/c/go/+/385256
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Ville Skyttä 2022-05-07 06:37:07 +00:00 committed by Gopher Robot
parent e755a5649a
commit 92998cde5a
3 changed files with 26 additions and 8 deletions

View file

@ -9,3 +9,6 @@
50:text/plain:*,v
50:application/x-trash:*~
30:example/do-not-use:*.t4
10:example/glob-question-mark:*.foo?ar
10:example/glob-asterisk:*.foo*r
10:example/glob-range:*.foo[1-3]

View file

@ -40,7 +40,7 @@ func loadMimeGlobsFile(filename string) error {
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// Each line should be of format: weight:mimetype:*.ext[:morefields...]
// Each line should be of format: weight:mimetype:glob[:morefields...]
fields := strings.Split(scanner.Text(), ":")
if len(fields) < 3 || len(fields[0]) < 1 || len(fields[2]) < 3 {
continue
@ -49,6 +49,18 @@ func loadMimeGlobsFile(filename string) error {
}
extension := fields[2][1:]
if strings.ContainsAny(extension, "?*[") {
// Not a bare extension, but a glob. Ignore for now:
// - we do not have an implementation for this glob
// syntax (translation to path/filepath.Match could
// be possible)
// - support for globs with weight ordering would have
// performance impact to all lookups to support the
// rarely seen glob entries
// - trying to match glob metacharacters literally is
// not useful
continue
}
if _, ok := mimeTypes.Load(extension); ok {
// We've already seen this extension.
// The file is in weight order, so we keep

View file

@ -22,13 +22,16 @@ func initMimeUnixTest(t *testing.T) {
func TestTypeByExtensionUNIX(t *testing.T) {
initMimeUnixTest(t)
typeTests := map[string]string{
".T1": "application/test",
".t2": "text/test; charset=utf-8",
".t3": "document/test",
".t4": "example/test",
".png": "image/png",
",v": "",
"~": "",
".T1": "application/test",
".t2": "text/test; charset=utf-8",
".t3": "document/test",
".t4": "example/test",
".png": "image/png",
",v": "",
"~": "",
".foo?ar": "",
".foo*r": "",
".foo[1-3]": "",
}
for ext, want := range typeTests {