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

incorrect mime-type for .js files on windows 10 (text/plain instead of application/javascript) #32350

Open
kjk opened this issue May 31, 2019 · 8 comments

Comments

@kjk
Copy link

commented May 31, 2019

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

$ go version
go version go1.12.5 windows/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
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\kjk\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\kjk\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\kjk\src\apps\offdocs\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\kjk\AppData\Local\Temp\go-build840166758=/tmp/go-build -gno-record-gcc-switches

What did you do?

Consider this program:

package main

import (
	"fmt"
	"mime"
)

func main() {
	ct := mime.TypeByExtension(".js")
	fmt.Printf("ct: %s\n", ct)
}

https://play.golang.org/p/JTVjWc9xMDE

What did you expect to see?

Mime-type for .js files should be application/javascript on all platforms.

What did you see instead?

On Linux it returns ct: application/javascript

On my latest Windows 10 it returns ct: text/plain; charset=utf-8

This also affects Content-Type returned by http.ServeFile() for .js files.

This is because in src\mime\type.go:

func initMime() {
	if fn := testInitMime; fn != nil {
		fn()
	} else {
		setMimeTypes(builtinTypesLower, builtinTypesLower)
		osInitMime()
	}
}

On Windows osInitMime() reads info for additional extensions from registry and over-writes mime-type for .js files.

Note: it's possible this won't repro on every version on Windows.

Reversing the initialization order should fix it:

osInitMime()
setMimeTypes(builtinTypesLower, builtinTypesLower)
@slrz

This comment has been minimized.

Copy link

commented May 31, 2019

Reversing the order would be a pretty heavy-handed fix. Local system configuration should have precedence over the builtin defaults.

Does this issue reproduce on a clean Windows install?

@hdm

This comment has been minimized.

Copy link

commented Aug 27, 2019

This issue started to appear out of the blue on a Windows 10 1903 install (go 1.12.9).

@hdm

This comment has been minimized.

Copy link

commented Aug 27, 2019

A terrible work-around:

var builtinMimeTypesLower = 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",
}

func staticFileGetMimeType(ext string) string {
	if v, ok := builtinMimeTypesLower[ext]; ok {
		return v
	}
	return mime.TypeByExtension(ext)
}
@mattn

This comment has been minimized.

Copy link
Member

commented Aug 27, 2019

On Windows7, mime type of .js is application/javascript:

Sorry, it may not be default configuration.

C:\>reg query HKCR\.js /v "Content Type"

HKEY_CLASSES_ROOT\.js
    Content Type    REG_SZ    application/javascript
@hdm

This comment has been minimized.

Copy link

commented Aug 27, 2019

Interesting, I wonder what is changing this. Thanks for the pointer.

C:\>reg query HKCR\.js /v "Content Type"

HKEY_CLASSES_ROOT\.js
    Content Type    REG_SZ    text/plain
@mattn

This comment has been minimized.

Copy link
Member

commented Aug 27, 2019

As a result that I swimed in internet for 10 minutes, I figure out this value of the registory key seems to be possibly changed by some text editor or casual confguration.

https://timothytocci.com/tag/registry/ (text/plain)

https://www.jianshu.com/p/a443991462d7 (application/x-javascript)

And it is not registered in default on Windows 10. I think this should not be set from registory. Or should be non-overwritable.

@gopherbot

This comment has been minimized.

Copy link

commented Aug 27, 2019

Change https://golang.org/cl/191917 mentions this issue: mime: do not change initial configuration for mimeTypes

@mattn

This comment has been minimized.

Copy link
Member

commented Sep 6, 2019

builtinTypesLower is safe. it is not overwritable. mime.types or registory is NOT safe. This can be added to mimeTypes easily. (ex with installing text editor on Windows). So cl191917 disable overwrite to mimeTypes from mime.types or registory. What about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.