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 len(subpath) > 0 && 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 }