]> Cypherpunks repositories - gostls13.git/commitdiff
mime: skip globs2 entries that are not simple file extensions
authorVille Skyttä <ville.skytta@iki.fi>
Sat, 7 May 2022 06:37:07 +0000 (06:37 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 13 May 2022 21:01:01 +0000 (21:01 +0000)
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: f725a910547ec52d12605760563f158dfb72e4b0
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>

src/mime/testdata/test.types.globs2
src/mime/type_unix.go
src/mime/type_unix_test.go

index fd9df7078bd877d02ed00629a9f882ac461f795c..4606d98f13a53a242160dadea442793c7baa196c 100644 (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]
index e297ecf5c1aa78f6d8dc9f441daeeb90dfbf403e..649d9001e3be04eb3622e68eb1d3ad8b48a87c56 100644 (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
index ab14ae6f80cc42b2b8f4e1cd62ead94054f6d53e..2e8f273fad636a921dc58fb62d044701aac0ca60 100644 (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 {