124 lines
3.0 KiB
Go
124 lines
3.0 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func serveHomeFile(w http.ResponseWriter, r *http.Request) {
|
|
home, err := OperatorHomeDirectory()
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
serveHomeFileFrom(w, r, home)
|
|
}
|
|
|
|
func serveHomeFileFrom(w http.ResponseWriter, r *http.Request, home string) {
|
|
home = filepath.Clean(home)
|
|
realHome, err := filepath.EvalSymlinks(home)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
target, err := expandHomePath(home, r.URL.Query().Get("path"))
|
|
if err != nil {
|
|
http.Error(w, "file not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
_, realTarget, err := resolveInsideHome(realHome, target)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "outside the home directory") {
|
|
http.Error(w, "file access denied", http.StatusForbidden)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
return
|
|
}
|
|
info, err := os.Stat(realTarget)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if info.IsDir() || !info.Mode().IsRegular() {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if specialFilesystem(realTarget) {
|
|
http.Error(w, "file access denied", http.StatusForbidden)
|
|
return
|
|
}
|
|
file, err := os.Open(realTarget)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrPermission) {
|
|
http.Error(w, "file access denied", http.StatusForbidden)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
w.Header().Set("Cache-Control", "private, no-cache")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
contentType := mime.TypeByExtension(filepath.Ext(realTarget))
|
|
if contentType == "" {
|
|
var sample [512]byte
|
|
n, readErr := file.Read(sample[:])
|
|
if readErr != nil && !errors.Is(readErr, io.EOF) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
contentType = http.DetectContentType(sample[:n])
|
|
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
}
|
|
if contentType == "" {
|
|
contentType = "application/octet-stream"
|
|
}
|
|
w.Header().Set("Content-Type", contentType)
|
|
disposition := "inline"
|
|
if r.URL.Query().Get("download") == "1" {
|
|
disposition = "attachment"
|
|
}
|
|
w.Header().Set("Content-Disposition", contentDisposition(disposition, filepath.Base(realTarget)))
|
|
http.ServeContent(w, r, filepath.Base(realTarget), info.ModTime(), file)
|
|
}
|
|
|
|
func contentDisposition(kind, filename string) string {
|
|
// The ASCII fallback keeps older clients useful; filename* preserves the
|
|
// actual name for clients that implement RFC 6266/RFC 5987.
|
|
fallback := strings.Map(func(r rune) rune {
|
|
if r < 0x20 || r > 0x7e || r == '"' || r == '\\' {
|
|
return '_'
|
|
}
|
|
return r
|
|
}, filename)
|
|
if fallback == "" {
|
|
fallback = "file"
|
|
}
|
|
return kind + `; filename="` + fallback + `"; filename*=UTF-8''` + rfc5987(filename)
|
|
}
|
|
|
|
func rfc5987(value string) string {
|
|
const hex = "0123456789ABCDEF"
|
|
var b strings.Builder
|
|
for _, c := range []byte(value) {
|
|
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || strings.ContainsRune("!#$&+-.^_`|~", rune(c)) {
|
|
b.WriteByte(c)
|
|
} else {
|
|
b.WriteByte('%')
|
|
b.WriteByte(hex[c>>4])
|
|
b.WriteByte(hex[c&15])
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|