set abspath to be calculated at RequestPath creation

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
master
kim (grufwub) 4 years ago
parent ac20b9d6b3
commit 9e3dfe50ad

@ -4,7 +4,6 @@ import (
"os"
"sync"
"time"
"strings"
)
type FileType int
@ -148,7 +147,7 @@ func (fs *FileSystem) FetchFile(request *FileSystemRequest) ([]byte, *GophorErro
/* Create new file contents object using supplied function */
var contents FileContents
if strings.HasSuffix(request.Path.RelativePath(), "/"+GophermapFileStr) {
if request.Path.HasAbsoluteSuffix("/"+GophermapFileStr) {
contents = &GophermapContents{ request.Path, nil }
} else {
contents = &RegularFileContents{ request.Path, nil }

@ -21,9 +21,10 @@ type FileSystemRequest struct {
type RequestPath struct {
Root string
Path string
AbsPath string /* Cache the absolute path */
}
func NewRequestPath(root, request string) *RequestPath {
func NewSanitizedRequestPath(root, request string) *RequestPath {
/* Here we must sanitize the request path. Start with a clean :) */
requestPath := path.Clean(request)
@ -37,7 +38,11 @@ func NewRequestPath(root, request string) *RequestPath {
}
}
return &RequestPath{ root, requestPath }
return NewRequestPath(root, requestPath)
}
func NewRequestPath(root, relative string) *RequestPath {
return &RequestPath{ root, relative, path.Join(root, relative) }
}
func (rp *RequestPath) SelectorPath() string {
@ -49,7 +54,7 @@ func (rp *RequestPath) SelectorPath() string {
}
func (rp *RequestPath) AbsolutePath() string {
return path.Join(rp.Root, rp.Path)
return rp.AbsPath
}
func (rp *RequestPath) RelativePath() string {
@ -90,14 +95,15 @@ func (rp *RequestPath) TrimAbsoluteSuffix(suffix string) string {
func (rp *RequestPath) NewJoinPathFromCurrent(extPath string) *RequestPath {
/* DANGER THIS DOES NOT CHECK FOR BACK-DIR TRAVERSALS */
return &RequestPath{ rp.Root, rp.JoinRelativePath(extPath) }
return NewRequestPath(rp.Root, rp.JoinRelativePath(extPath))
}
func (rp *RequestPath) NewTrimPathFromCurrent(trimSuffix string) *RequestPath {
return &RequestPath{ rp.Root, rp.TrimRelativeSuffix(trimSuffix) }
/* DANGER THIS DOES NOT CHECK FOR BACK-DIR TRAVERSALS */
return NewRequestPath(rp.Root, rp.TrimRelativeSuffix(trimSuffix))
}
func (rp *RequestPath) NewPathAtRoot(extPath string) *RequestPath {
/* Sanitized and safe (hopefully) */
return NewRequestPath(rp.Root, extPath)
return NewSanitizedRequestPath(rp.Root, extPath)
}

@ -106,7 +106,7 @@ func (worker *Worker) RespondGopher(data []byte) *GophorError {
}
/* Get request path from data string */
requestPath := NewRequestPath(worker.Conn.Host.RootDir, dataStr)
requestPath := NewSanitizedRequestPath(worker.Conn.Host.RootDir, dataStr)
/* Append lastline */
response, gophorErr := Config.FileSystem.HandleRequest(requestPath, worker.Conn.Host)

Loading…
Cancel
Save