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
865 B

  1. package wrouter
  2. import (
  3. "net/http"
  4. "os"
  5. "path"
  6. "strings"
  7. "git.aiterp.net/gisle/wrouter/response"
  8. "git.aiterp.net/gisle/wrouter/auth"
  9. )
  10. type Static struct {
  11. path string
  12. }
  13. func NewStatic(path string) *Static {
  14. return &Static{path}
  15. }
  16. func (static *Static) Handle(urlPath string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
  17. // Get the subpath out of the path
  18. subpath := req.URL.Path[len(urlPath):]
  19. if len(subpath) > 0 && subpath[0] == '/' {
  20. subpath = subpath[1:]
  21. }
  22. // Disallow breaking out of the folder
  23. if strings.Contains(subpath, "..") {
  24. response.Text(w, 403, "No .. in paths allowed")
  25. return true
  26. }
  27. // Look for the file
  28. filepath := path.Join(static.path, subpath)
  29. info, err := os.Stat(filepath)
  30. if err != nil || info.IsDir() {
  31. return false
  32. }
  33. // Serve it
  34. http.ServeFile(w, req, filepath)
  35. return true
  36. }