Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mime: wrong extensions list for content type "image/jpeg" when using built-in types #36524

Closed
junedev opened this issue Jan 13, 2020 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@junedev
Copy link

junedev commented Jan 13, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.6 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="xyz/.cache/go-build"
GOENV="xyz/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY="xyz"
GONOSUMDB="xyz"
GOOS="linux"
GOPATH="xyz/go"
GOPRIVATE="xyz"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build284189108=/tmp/go-build -gno-record-gcc-switches"
(Some values sanitized with "xyz".)

What did you do?

I was trying to retrieve file extensions for the content type "image/jpeg" with mime.ExtensionsByType("image/jpeg").
https://play.golang.org/p/gfVzW9oAdiq

What did you expect to see?

On systems where none of the following files are present, the built-in types list is used according to the documentation.

/etc/mime.types
/etc/apache2/mime.types
/etc/apache/mime.types

The built-in list can be viewed here: https://golang.org/src/mime/type.go#L59
It contains two entries for "image/jpeg". So I would expect to get these two entries as result of mime.ExtensionsByType("image/jpeg"): []string{".jpg", ".jpeg"}

There is also a code comment in that mentions this exact case: https://golang.org/src/mime/type.go#L19

What did you see instead?

On my machine either []string{".jpg"} or []string{".jpeg"} at random. On the playground always []string{".jpeg"}.

Here is a test case for the issue that can be run in "mime/type_test.go":

func TestExtensionsByType2(t *testing.T) {
	cleanup := setMimeInit(func() {
		clearMimeTypes()
                // Initialize built-in types like it is done in "type.go"
                // https://golang.org/src/mime/type.go#L84
		setMimeTypes(builtinTypesLower, builtinTypesLower)
	})
	defer cleanup()

	tests := []struct {
		typ     string
		want    []string
	}{
		{typ: "image/jpeg", want: []string{".jpg", ".jpeg"}},
	}

	for _, tt := range tests {
		got, err := ExtensionsByType(tt.typ)
		if err != nil {
			t.Error("error")
		}
		if !reflect.DeepEqual(got, tt.want) {
			t.Errorf("ExtensionsByType(%q) = %q; want %q", tt.typ, got, tt.want)
		}
	}
}
@cagedmantis cagedmantis added this to the Backlog milestone Jan 14, 2020
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 14, 2020
@cagedmantis
Copy link
Contributor

/cc @bradfitz

@bradfitz bradfitz modified the milestones: Backlog, Go1.15 Jan 14, 2020
@gopherbot
Copy link

Change https://golang.org/cl/214680 mentions this issue: mime: fix ExtensionsByType bug when there are duplicates

@bradfitz
Copy link
Contributor

We're assuming this is Go 1.15 material at this point (per https://github.com/golang/go/wiki/Go-Release-Cycle).

But if it's urgent we could backport to earlier releases. It seems like there are workarounds, though, and this doesn't seem like a regression (we've had this problem for all time?)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants