changeset 5:07b6f06899e0

my tired ass deleted a fix that was partially working
author Atarwn Gard <a@qwa.su>
date Mon, 09 Mar 2026 03:35:47 +0500
parents dacc92aae6d5
children 54ab94198677
files main.go
diffstat 1 files changed, 37 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/main.go	Mon Mar 09 03:07:18 2026 +0500
+++ b/main.go	Mon Mar 09 03:35:47 2026 +0500
@@ -164,31 +164,23 @@
 
 func (h *handler) serve(w http.ResponseWriter, r *http.Request, dirs []icf.Directive, _ map[string]string) {
 	var (
-		rootDir   string
-		rootIndex []string // nil = hide; non-nil = show, try these index files first
-		fcgiAddr  string
-		fcgiPat   string
-		rprxAddr  string
+		rootDir  string
+		rootShow bool
+		ndex     []string
+		fcgiAddr string
+		fcgiPat  string
+		rprxAddr string
+		rdirCode int
+		rdirURL  string
 	)
 
 	for _, d := range dirs {
 		switch d.Key {
 		case "root":
 			rootDir = safeArg(d.Args, 0)
-			switch safeArg(d.Args, 1) {
-			case "show":
-				// root /path show [index.php index.html ...]
-				// up to 12 index file candidates; default is index.html
-				if len(d.Args) >= 3 {
-					rootIndex = d.Args[2:]
-				} else {
-					rootIndex = []string{"index.html"}
-				}
-			case "hide", "":
-				rootIndex = nil
-			default:
-				log.Printf("d2o: root: unknown mode %q (want show|hide)", safeArg(d.Args, 1))
-			}
+			rootShow = safeArg(d.Args, 1) == "show"
+		case "ndex":
+			ndex = d.Args
 		case "fcgi":
 			fcgiAddr = safeArg(d.Args, 0)
 			fcgiPat = safeArg(d.Args, 1)
@@ -197,10 +189,19 @@
 			}
 		case "rprx":
 			rprxAddr = safeArg(d.Args, 0)
+		case "rdir":
+			rdirCode, _ = strconv.Atoi(safeArg(d.Args, 0))
+			rdirURL = safeArg(d.Args, 1)
 		}
 	}
 
-	// Priority: rprx > fcgi > static root
+	if rdirURL != "" {
+		if rdirCode == 0 {
+			rdirCode = http.StatusFound
+		}
+		http.Redirect(w, r, rdirURL, rdirCode)
+		return
+	}
 	if rprxAddr != "" {
 		serveReverseProxy(w, r, rprxAddr)
 		return
@@ -213,7 +214,7 @@
 		return
 	}
 	if rootDir != "" {
-		serveStatic(w, r, rootDir, rootIndex)
+		serveStatic(w, r, rootDir, rootShow, ndex, fcgiAddr, fcgiPat)
 		return
 	}
 
@@ -225,7 +226,7 @@
 // serveStatic serves files from rootDir.
 // rootIndex == nil: directory listing forbidden (hide).
 // rootIndex != nil: try each as index candidate; if none found, show listing.
-func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, rootIndex []string) {
+func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, show bool, ndex []string, fcgiAddr, fcgiPat string) {
 	fpath := filepath.Join(rootDir, filepath.FromSlash(path.Clean(r.URL.Path)))
 
 	info, err := os.Stat(fpath)
@@ -239,17 +240,26 @@
 	}
 
 	if info.IsDir() {
-		if rootIndex == nil {
-			http.Error(w, "forbidden", http.StatusForbidden)
-			return
-		}
-		for _, idx := range rootIndex {
+		for _, idx := range ndex {
 			idxPath := filepath.Join(fpath, idx)
 			if _, err := os.Stat(idxPath); err == nil {
+				if fcgiAddr != "" && matchGlob(fcgiPat, idx) {
+					r2 := r.Clone(r.Context())
+					r2.URL.Path = path.Join(r.URL.Path, idx)
+					if err := serveFCGI(w, r2, fcgiAddr, rootDir); err != nil {
+						log.Printf("d2o: fcgi error: %v", err)
+						http.Error(w, "gateway error", http.StatusBadGateway)
+					}
+					return
+				}
 				http.ServeFile(w, r, idxPath)
 				return
 			}
 		}
+		if !show {
+			http.Error(w, "forbidden", http.StatusForbidden)
+			return
+		}
 		listDir(w, r, fpath, r.URL.Path)
 		return
 	}