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

net/http: ServeContent / FileServer mime issue #9156

Closed
gopherbot opened this issue Nov 24, 2014 · 5 comments
Closed

net/http: ServeContent / FileServer mime issue #9156

gopherbot opened this issue Nov 24, 2014 · 5 comments
Milestone

Comments

@gopherbot
Copy link

by neven.jacmenovic:

I am experiencing two different behaviors of the same code compiling and running on
Vista vs. Windows 7. 

When static css file is being served via ServeContent or FileServe on Windows 7, the
mime type sent to browser is set and send correctly (text/css). However, when I execute
same code on Vista it gets transferred as text/plain. As I side effect Chrome won't show
css file and reports in console:  "Resource interpreted as Stylesheet but
transferred with MIME type text/plain: "http://localhost:8080/css/styles.css";.

The only way to make it work is to force mime type via Header().Set

pseudo example1:
http.Handle("/css/", http.StripPrefix("/css/",
http.FileServer(http.Dir(config.FILES.TEMPLATES+"/css/"))))


pseudo example2:
http.HandleFunc("/css/", ServeStatic)
...
func ServeStatic(w http.ResponseWriter, r *http.Request) {
    dir := http.Dir(config.FILES.TEMPLATES)
    file := r.URL.Path
    f, err := dir.Open(file)
    if err != nil {
        log.Error("HTTP error: %s", err.Error())
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
    defer f.Close()

    fi, err := f.Stat()
    if err != nil {
        log.Error("HTTP error: %s", err.Error())
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
//  w.Header().Set("Content-Type", "text/css; charset=utf-8")
    http.ServeContent(w, r, file, fi.ModTime(), f)
}



Windows 7 - Request URL: http://localhost:8080/css/styles.css
Response headers:
1. Accept-Ranges:bytes
2. Content-Length: 2829
3. Content-Type: text/css; charset=utf-8
4. Date: Sun, 23 Nov 2014 14:26:40 GMT
5. Last-Modified: Sun, 12 Oct 2014 08:53:41 GMT

Vista - Request URL: http://localhost:8080/css/styles.css
1. Accept-Ranges:bytes
2. Content-Length:7658
3. Content-Type:text/plain; charset=utf-8
4. Date:Mon, 24 Nov 2014 12:33:23 GMT
5. Last-Modified:Sat, 17 May 2014 09:36:02 GMT

I would be glad to provide more information if needed, at this point I am clueless where
to search for evidence if this is 1.4RC related or Vista related.
@gopherbot
Copy link
Author

Comment 1 by neven.jacmenovic:

Windows 7 x64 (Microsoft Windows [Version 6.1.7601])
Chrome Version 39.0.2171.65 m
go version go1.4rc1 windows/amd64
Windows Vista x64 (Microsoft Windows [Version 6.0.6002])
Chrome Version 39.0.2171.65 m
go version go1.4rc1 windows/amd64

@ianlancetaylor
Copy link
Contributor

Comment 2:

Labels changed: added repo-main, release-go1.5.

@minux
Copy link
Member

minux commented Nov 24, 2014

Comment 3:

Go has no(t many) builtin mime mapping rules, and it relies on the registry.
it's probably because HKEY_CLASSES_ROOT\.css\Content Type is text/plain
on your Vista machine.

@bradfitz
Copy link
Contributor

Comment 4:

Go does set:
                ".css":  "text/css; charset=utf-8",
But I think it prefers what Windows says. If you want "text/css; charset=utf-8", best is
to set it explicitly.
I don't think we can make any change safely here (without breaking backwards
compatibility with current behavior), even if we wanted to. Some people might be relying
on their operating system's selection of mime type the Go-known file extensions.

Status changed to WorkingAsIntended.

@gopherbot
Copy link
Author

Comment 5 by neven.jacmenovic:

Hi Brad,
Thank you very much for detailed explanation, and yes you are 100% correct - my old
Vista machine has text/plain for HKEY_CLASSES_ROOT\.css
I was mistakenly lead to believe that "HKEY_CLASSES_ROOT\Mime\Database\Content Type"
holds correct server side mime types (there is text/css definition for .css), and
HKEY_CLASSES_ROOT just client side (eg what happens when user double clicks .css file in
windows explorer) but since this is Windows, nothing can surprise me anymore. 
For further reference in case somebody stumbles on the same issue, this blog post
explains windows mime mayhem the best:
http://codelog.climens.net/2009/10/28/getting-mime-type-in-net-from-file-extension/
Best regards
Neven

@bradfitz bradfitz modified the milestone: Go1.5 Dec 16, 2014
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants