Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
845 B

package wrouter
import (
"net/http"
"os"
"path"
"strings"
"git.aiterp.net/gisle/wrouter/response"
"git.aiterp.net/gisle/wrouter/auth"
)
type Static struct {
path string
}
func NewStatic(path string) *Static {
return &Static{path}
}
func (static *Static) Handle(urlPath string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
// Get the subpath out of the path
subpath := req.URL.Path[len(urlPath):]
if subpath[0] == '/' {
subpath = subpath[1:]
}
// Disallow breaking out of the folder
if strings.Contains(subpath, "..") {
response.Text(w, 403, "No .. in paths allowed")
return true
}
// Look for the file
filepath := path.Join(static.path, subpath)
info, err := os.Stat(filepath)
if err != nil || info.IsDir() {
return false
}
// Serve it
http.ServeFile(w, req, filepath)
return true
}