try to set Content-Type for plain files

This commit is contained in:
mischief 2016-01-26 05:03:41 -08:00
parent c0a06bf061
commit a3148e447b
1 changed files with 16 additions and 0 deletions

16
main.go
View File

@ -8,6 +8,7 @@ import (
"html/template"
"io"
"log"
"mime"
"net"
"net/http"
"os"
@ -416,6 +417,21 @@ again:
// plain file handling
log.Printf("f %s", base+path)
// ripped from http.serveContent
ctype := mime.TypeByExtension(filepath.Ext(path))
if ctype == "" {
// read a chunk to decide between utf-8 text and binary
var buf [512]byte
n, _ := io.ReadFull(f, buf[:])
ctype = http.DetectContentType(buf[:n])
_, err := f.Seek(0, os.SEEK_SET) // rewind to output whole file
if err != nil {
http.Error(w, "seeker can't seek", http.StatusInternalServerError)
return
}
}
w.Header().Set("Content-Type", ctype)
io.Copy(w, f)
return
}