try to set Content-Type for plain files
This commit is contained in:
parent
c0a06bf061
commit
a3148e447b
1 changed files with 16 additions and 0 deletions
16
main.go
16
main.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue