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: TypeByExtension should not output charset=utf8 #27078

Closed
frostyplanet opened this issue Aug 19, 2018 · 4 comments
Closed

mime: TypeByExtension should not output charset=utf8 #27078

frostyplanet opened this issue Aug 19, 2018 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@frostyplanet
Copy link

frostyplanet commented Aug 19, 2018

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

go1.10 linux/amd64

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

linux ubuntu 16.04; regardless LC_ALL env setting

complied code run on windows 2012 also has this issue;

What did you do?

package main

import "mime"
func main() {
    println(mime.TypeByExtension(".html"))
}

What did you expect to see?

expect "text/html",

What did you see instead?

"text/html; charset=utf-8"

the html content may not be utf8 after all.
using the result from mime.TypeByExtension() and return in Content-Type header of one http response will make browser believe the chraset is utf8

@josharian josharian changed the title mime.TypeByExtension should not output charset=utf8 mime: TypeByExtension should not output charset=utf8 Aug 19, 2018
@andriisoldatenko
Copy link
Contributor

andriisoldatenko commented Aug 31, 2018

For quick look in source code https://golang.org/src/mime/type.go?s=2698:2737#L63

var builtinTypesLower = map[string]string{
	".css":  "text/css; charset=utf-8",
	".gif":  "image/gif",
	".htm":  "text/html; charset=utf-8",
	".html": "text/html; charset=utf-8",
	".jpg":  "image/jpeg",
	".js":   "application/javascript",
	".wasm": "application/wasm",
	".pdf":  "application/pdf",
	".png":  "image/png",
	".svg":  "image/svg+xml",
	".xml":  "text/xml; charset=utf-8",
}

and working example https://play.golang.org/p/SD93m9K93WD.
Also in docs we can see src/mime/type.go:102

// Text types have the charset parameter set to "utf-8" by default.

@josharian Please let me know if we need to fix it.

@frostyplanet
Copy link
Author

Although the doc specify function behavior, but it's no big deal for html and xml if not to include "; charset=utf-8",
usually the content itself contain a charset declaration. Should at lease let user to opt out the behavior.

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 4, 2018
@andybons andybons added this to the Unplanned milestone Sep 4, 2018
@andybons
Copy link
Member

andybons commented Sep 4, 2018

@bradfitz

@bradfitz
Copy link
Contributor

bradfitz commented Sep 5, 2018

This was an explicit decision when these APIs were made.

If you need a different charset (increasingly rare?), you can set it explicitly and not use the package.

@andybons andybons closed this as completed Sep 5, 2018
@golang golang locked and limited conversation to collaborators Sep 5, 2019
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

5 participants