diff main.go @ 2:d19133be91ba

ndex and smarter parser
author Atarwn Gard <a@qwa.su>
date Mon, 09 Mar 2026 01:55:11 +0500
parents 3e7247db5c6e
children eb705d4cdcd7
line wrap: on
line diff
--- a/main.go	Mon Mar 09 01:04:16 2026 +0500
+++ b/main.go	Mon Mar 09 01:55:11 2026 +0500
@@ -160,29 +160,23 @@
 
 func (h *handler) serve(w http.ResponseWriter, r *http.Request, dirs []icf.Directive, _ map[string]string) {
 	var (
-		rootDir   string
-		rootIndex []string
-		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":
-				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)
@@ -191,9 +185,19 @@
 			}
 		case "rprx":
 			rprxAddr = safeArg(d.Args, 0)
+		case "rdir":
+			rdirCode, _ = strconv.Atoi(safeArg(d.Args, 0))
+			rdirURL = safeArg(d.Args, 1)
 		}
 	}
 
+	if rdirURL != "" {
+		if rdirCode == 0 {
+			rdirCode = http.StatusFound
+		}
+		http.Redirect(w, r, rdirURL, rdirCode)
+		return
+	}
 	if rprxAddr != "" {
 		serveReverseProxy(w, r, rprxAddr)
 		return
@@ -206,7 +210,7 @@
 		return
 	}
 	if rootDir != "" {
-		serveStatic(w, r, rootDir, rootIndex)
+		serveStatic(w, r, rootDir, rootShow, ndex)
 		return
 	}
 
@@ -214,8 +218,7 @@
 }
 
 // --- Static -----------------------------------------------------------------
-
-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) {
 	fpath := filepath.Join(rootDir, filepath.FromSlash(path.Clean(r.URL.Path)))
 
 	info, err := os.Stat(fpath)
@@ -229,17 +232,17 @@
 	}
 
 	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 {
 				http.ServeFile(w, r, idxPath)
 				return
 			}
 		}
+		if !show {
+			http.Error(w, "forbidden", http.StatusForbidden)
+			return
+		}
 		listDir(w, r, fpath, r.URL.Path)
 		return
 	}
@@ -362,4 +365,4 @@
 	regPat := "^" + strings.ReplaceAll(regexp.QuoteMeta(pattern), regexp.QuoteMeta("*"), ".*") + "$"
 	matched, err := regexp.MatchString(regPat, s)
 	return err == nil && matched
-}
+}
\ No newline at end of file