diff options
Diffstat (limited to 'contrib/cgi/cgi.go')
-rw-r--r-- | contrib/cgi/cgi.go | 50 |
1 files changed, 20 insertions, 30 deletions
diff --git a/contrib/cgi/cgi.go b/contrib/cgi/cgi.go index b7dd14a..1b5bfcc 100644 --- a/contrib/cgi/cgi.go +++ b/contrib/cgi/cgi.go @@ -11,6 +11,7 @@ import ( "net" "os" "os/exec" + "path" "path/filepath" "strings" @@ -25,48 +26,37 @@ import ( // It will find executables which are just part way through the path, so for example // a request for /foo/bar/baz can run an executable found at /foo or /foo/bar. In such // a case the PATH_INFO would include the remaining portion of the URI path. -func ResolveCGI(requestPath, fsRoot string) (string, string, error) { - fsRoot = strings.TrimRight(fsRoot, "/") - segments := strings.Split(strings.TrimLeft(requestPath, "/"), "/") +func ResolveCGI(requestpath, fsroot string) (string, string, error) { + segments := append([]string{""}, strings.Split(requestpath, "/")...) - for i := range append(segments, "") { - filepath := strings.Join(append([]string{fsRoot}, segments[:i]...), "/") - isDir, isExecutable, err := executableFile(filepath) + fullpath := fsroot + for i, segment := range segments { + fullpath = filepath.Join(fullpath, segment) + + info, err := os.Stat(fullpath) + if isNotExistError(err) { + break + } if err != nil { return "", "", err } - if isExecutable { - pathinfo := "/" - if len(segments) > i+1 { - pathinfo = strings.Join(segments[i:], "/") - } - return filepath, pathinfo, nil + if info.IsDir() { + continue } - if !isDir { + if info.Mode()&5 != 5 { break } - } - - return "", "", nil -} -func executableFile(filepath string) (bool, bool, error) { - info, err := os.Stat(filepath) - if isNotExistError(err) { - return false, false, nil - } - if err != nil { - return false, false, err - } - - if info.IsDir() { - return true, false, nil + pathinfo := "/" + if len(segments) > i+1 { + pathinfo = path.Join(segments[i:]...) + } + return fullpath, pathinfo, nil } - // readable + executable by anyone - return false, info.Mode()&5 == 5, nil + return "", "", nil } func isNotExistError(err error) bool { |