blob: a512bbfb42f1111515e0d9fc110a1a1bcdb51b27 [file] [log] [blame]
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"io/fs"
"path"
"strings"
"unicode/utf8"
)
// isText reports whether a significant prefix of s looks like correct UTF-8;
// that is, if it is likely that s is human-readable text.
func isText(s []byte) bool {
const max = 1024 // at least utf8.UTFMax
if len(s) > max {
s = s[0:max]
}
for i, c := range string(s) {
if i+utf8.UTFMax > len(s) {
// last char may be incomplete - ignore
break
}
if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' {
// decoding error or control character - not a text file
return false
}
}
return true
}
// isTextFile reports whether the file has a known extension indicating
// a text file, or if a significant chunk of the specified file looks like
// correct UTF-8; that is, if it is likely that the file contains human-
// readable text.
func isTextFile(fsys fs.FS, filename string) bool {
// Various special cases must be served raw, not converted to nice HTML.
if filename == "robots.txt" || strings.HasPrefix(filename, "doc/play/") {
return false
}
switch path.Ext(filename) {
case ".css", ".js", ".svg", ".ts":
return false
}
// the extension is not known; read an initial chunk
// of the file and check if it looks like text
f, err := fsys.Open(filename)
if err != nil {
return false
}
defer f.Close()
var buf [1024]byte
n, err := f.Read(buf[0:])
if err != nil {
return false
}
return isText(buf[0:n])
}