Mercurial Hosting > d2o
comparison main.go @ 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 | eb705d4cdcd7 |
| children | 8e4813b4e509 |
comparison
equal
deleted
inserted
replaced
| 4:dacc92aae6d5 | 5:07b6f06899e0 |
|---|---|
| 162 h.serve(w, r, dirs, caps) | 162 h.serve(w, r, dirs, caps) |
| 163 } | 163 } |
| 164 | 164 |
| 165 func (h *handler) serve(w http.ResponseWriter, r *http.Request, dirs []icf.Directive, _ map[string]string) { | 165 func (h *handler) serve(w http.ResponseWriter, r *http.Request, dirs []icf.Directive, _ map[string]string) { |
| 166 var ( | 166 var ( |
| 167 rootDir string | 167 rootDir string |
| 168 rootIndex []string // nil = hide; non-nil = show, try these index files first | 168 rootShow bool |
| 169 fcgiAddr string | 169 ndex []string |
| 170 fcgiPat string | 170 fcgiAddr string |
| 171 rprxAddr string | 171 fcgiPat string |
| 172 rprxAddr string | |
| 173 rdirCode int | |
| 174 rdirURL string | |
| 172 ) | 175 ) |
| 173 | 176 |
| 174 for _, d := range dirs { | 177 for _, d := range dirs { |
| 175 switch d.Key { | 178 switch d.Key { |
| 176 case "root": | 179 case "root": |
| 177 rootDir = safeArg(d.Args, 0) | 180 rootDir = safeArg(d.Args, 0) |
| 178 switch safeArg(d.Args, 1) { | 181 rootShow = safeArg(d.Args, 1) == "show" |
| 179 case "show": | 182 case "ndex": |
| 180 // root /path show [index.php index.html ...] | 183 ndex = d.Args |
| 181 // up to 12 index file candidates; default is index.html | |
| 182 if len(d.Args) >= 3 { | |
| 183 rootIndex = d.Args[2:] | |
| 184 } else { | |
| 185 rootIndex = []string{"index.html"} | |
| 186 } | |
| 187 case "hide", "": | |
| 188 rootIndex = nil | |
| 189 default: | |
| 190 log.Printf("d2o: root: unknown mode %q (want show|hide)", safeArg(d.Args, 1)) | |
| 191 } | |
| 192 case "fcgi": | 184 case "fcgi": |
| 193 fcgiAddr = safeArg(d.Args, 0) | 185 fcgiAddr = safeArg(d.Args, 0) |
| 194 fcgiPat = safeArg(d.Args, 1) | 186 fcgiPat = safeArg(d.Args, 1) |
| 195 if fcgiPat == "" { | 187 if fcgiPat == "" { |
| 196 fcgiPat = "*" | 188 fcgiPat = "*" |
| 197 } | 189 } |
| 198 case "rprx": | 190 case "rprx": |
| 199 rprxAddr = safeArg(d.Args, 0) | 191 rprxAddr = safeArg(d.Args, 0) |
| 200 } | 192 case "rdir": |
| 201 } | 193 rdirCode, _ = strconv.Atoi(safeArg(d.Args, 0)) |
| 202 | 194 rdirURL = safeArg(d.Args, 1) |
| 203 // Priority: rprx > fcgi > static root | 195 } |
| 196 } | |
| 197 | |
| 198 if rdirURL != "" { | |
| 199 if rdirCode == 0 { | |
| 200 rdirCode = http.StatusFound | |
| 201 } | |
| 202 http.Redirect(w, r, rdirURL, rdirCode) | |
| 203 return | |
| 204 } | |
| 204 if rprxAddr != "" { | 205 if rprxAddr != "" { |
| 205 serveReverseProxy(w, r, rprxAddr) | 206 serveReverseProxy(w, r, rprxAddr) |
| 206 return | 207 return |
| 207 } | 208 } |
| 208 if fcgiAddr != "" && matchGlob(fcgiPat, r.URL.Path) { | 209 if fcgiAddr != "" && matchGlob(fcgiPat, r.URL.Path) { |
| 211 http.Error(w, "gateway error", http.StatusBadGateway) | 212 http.Error(w, "gateway error", http.StatusBadGateway) |
| 212 } | 213 } |
| 213 return | 214 return |
| 214 } | 215 } |
| 215 if rootDir != "" { | 216 if rootDir != "" { |
| 216 serveStatic(w, r, rootDir, rootIndex) | 217 serveStatic(w, r, rootDir, rootShow, ndex, fcgiAddr, fcgiPat) |
| 217 return | 218 return |
| 218 } | 219 } |
| 219 | 220 |
| 220 http.Error(w, "not found", http.StatusNotFound) | 221 http.Error(w, "not found", http.StatusNotFound) |
| 221 } | 222 } |
| 223 // --- Static ----------------------------------------------------------------- | 224 // --- Static ----------------------------------------------------------------- |
| 224 | 225 |
| 225 // serveStatic serves files from rootDir. | 226 // serveStatic serves files from rootDir. |
| 226 // rootIndex == nil: directory listing forbidden (hide). | 227 // rootIndex == nil: directory listing forbidden (hide). |
| 227 // rootIndex != nil: try each as index candidate; if none found, show listing. | 228 // rootIndex != nil: try each as index candidate; if none found, show listing. |
| 228 func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, rootIndex []string) { | 229 func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, show bool, ndex []string, fcgiAddr, fcgiPat string) { |
| 229 fpath := filepath.Join(rootDir, filepath.FromSlash(path.Clean(r.URL.Path))) | 230 fpath := filepath.Join(rootDir, filepath.FromSlash(path.Clean(r.URL.Path))) |
| 230 | 231 |
| 231 info, err := os.Stat(fpath) | 232 info, err := os.Stat(fpath) |
| 232 if os.IsNotExist(err) { | 233 if os.IsNotExist(err) { |
| 233 http.Error(w, "not found", http.StatusNotFound) | 234 http.Error(w, "not found", http.StatusNotFound) |
| 237 http.Error(w, "internal error", http.StatusInternalServerError) | 238 http.Error(w, "internal error", http.StatusInternalServerError) |
| 238 return | 239 return |
| 239 } | 240 } |
| 240 | 241 |
| 241 if info.IsDir() { | 242 if info.IsDir() { |
| 242 if rootIndex == nil { | 243 for _, idx := range ndex { |
| 243 http.Error(w, "forbidden", http.StatusForbidden) | |
| 244 return | |
| 245 } | |
| 246 for _, idx := range rootIndex { | |
| 247 idxPath := filepath.Join(fpath, idx) | 244 idxPath := filepath.Join(fpath, idx) |
| 248 if _, err := os.Stat(idxPath); err == nil { | 245 if _, err := os.Stat(idxPath); err == nil { |
| 246 if fcgiAddr != "" && matchGlob(fcgiPat, idx) { | |
| 247 r2 := r.Clone(r.Context()) | |
| 248 r2.URL.Path = path.Join(r.URL.Path, idx) | |
| 249 if err := serveFCGI(w, r2, fcgiAddr, rootDir); err != nil { | |
| 250 log.Printf("d2o: fcgi error: %v", err) | |
| 251 http.Error(w, "gateway error", http.StatusBadGateway) | |
| 252 } | |
| 253 return | |
| 254 } | |
| 249 http.ServeFile(w, r, idxPath) | 255 http.ServeFile(w, r, idxPath) |
| 250 return | 256 return |
| 251 } | 257 } |
| 258 } | |
| 259 if !show { | |
| 260 http.Error(w, "forbidden", http.StatusForbidden) | |
| 261 return | |
| 252 } | 262 } |
| 253 listDir(w, r, fpath, r.URL.Path) | 263 listDir(w, r, fpath, r.URL.Path) |
| 254 return | 264 return |
| 255 } | 265 } |
| 256 | 266 |
