summaryrefslogtreecommitdiff
path: root/contrib/cgi/cgi.go
diff options
context:
space:
mode:
authortjp <tjp@ctrl-c.club>2023-11-13 07:25:39 -0700
committertjp <tjp@ctrl-c.club>2023-11-13 07:27:16 -0700
commit1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b (patch)
tree020e5de91f2343119fed10dede9d2c8262a3cd83 /contrib/cgi/cgi.go
parenta808b4692656c10bb43e2d54a2f5ef2746d231d5 (diff)
refactor contribs to work with a Protocol interface
Diffstat (limited to 'contrib/cgi/cgi.go')
-rw-r--r--contrib/cgi/cgi.go50
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 {