replace filepath with path module

This commit is contained in:
Paweł Kierski 2023-04-10 20:59:38 +02:00 committed by GitHub
parent 0e61e22b98
commit bd813cc356
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ import (
"html/template" "html/template"
"io/fs" "io/fs"
"net/http" "net/http"
"path/filepath" "path"
"strings" "strings"
) )
@ -29,7 +29,7 @@ type uiAssetsHandler struct {
// serve the file specified by the URL path. // serve the file specified by the URL path.
func (h *uiAssetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *uiAssetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Get the absolute path to prevent directory traversal. // Get the absolute path to prevent directory traversal.
path := filepath.ToSlash(filepath.Clean(r.URL.Path)) path := path.Clean(r.URL.Path)
// Get the path relative to the root path. // Get the path relative to the root path.
if !strings.HasPrefix(path, h.rootPath) { if !strings.HasPrefix(path, h.rootPath) {
@ -45,7 +45,7 @@ func (h *uiAssetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (h *uiAssetsHandler) indexFilePath() string { func (h *uiAssetsHandler) indexFilePath() string {
return filepath.ToSlash(filepath.Join(h.staticDirPath, h.indexFileName)) return path.Join(h.staticDirPath, h.indexFileName)
} }
func (h *uiAssetsHandler) renderIndexFile(w http.ResponseWriter) error { func (h *uiAssetsHandler) renderIndexFile(w http.ResponseWriter) error {
@ -74,15 +74,15 @@ func (h *uiAssetsHandler) renderIndexFile(w http.ResponseWriter) error {
// and serves if a file is found. // and serves if a file is found.
// If a requested file is not found in the filesystem, it serves the index file to // If a requested file is not found in the filesystem, it serves the index file to
// make sure when user refreshes the page in SPA things still work. // make sure when user refreshes the page in SPA things still work.
func (h *uiAssetsHandler) serveFile(w http.ResponseWriter, path string) (code int, err error) { func (h *uiAssetsHandler) serveFile(w http.ResponseWriter, urlPath string) (code int, err error) {
if path == "/" || path == "" { if urlPath == "/" || urlPath == "" {
if err := h.renderIndexFile(w); err != nil { if err := h.renderIndexFile(w); err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
return http.StatusOK, nil return http.StatusOK, nil
} }
path = filepath.ToSlash(filepath.Join(h.staticDirPath, path)) urlPath = path.Join(h.staticDirPath, urlPath)
bytes, err := h.contents.ReadFile(path) bytes, err := h.contents.ReadFile(urlPath)
if err != nil { if err != nil {
// If path is error (e.g. file not exist, path is a directory), serve index file. // If path is error (e.g. file not exist, path is a directory), serve index file.
var pathErr *fs.PathError var pathErr *fs.PathError
@ -97,7 +97,7 @@ func (h *uiAssetsHandler) serveFile(w http.ResponseWriter, path string) (code in
// Setting the MIME type for .js files manually to application/javascript as // Setting the MIME type for .js files manually to application/javascript as
// http.DetectContentType is using https://mimesniff.spec.whatwg.org/ which // http.DetectContentType is using https://mimesniff.spec.whatwg.org/ which
// will not recognize application/javascript for security reasons. // will not recognize application/javascript for security reasons.
if strings.HasSuffix(path, ".js") { if strings.HasSuffix(urlPath, ".js") {
w.Header().Add("Content-Type", "application/javascript; charset=utf-8") w.Header().Add("Content-Type", "application/javascript; charset=utf-8")
} else { } else {
w.Header().Add("Content-Type", http.DetectContentType(bytes)) w.Header().Add("Content-Type", http.DetectContentType(bytes))