1
1
mirror of https://github.com/OJ/gobuster.git synced 2025-09-09 09:32:02 +02:00
gobuster/vhs/server.go
2025-08-26 23:26:14 +02:00

51 lines
1.3 KiB
Go

package main
import (
"log" // nolint:depguard
"net/http"
"regexp"
)
type route struct {
pattern *regexp.Regexp
handler http.Handler
}
type RegexpHandler struct {
routes []*route
}
func (h *RegexpHandler) Handler(pattern *regexp.Regexp, handler http.Handler) {
h.routes = append(h.routes, &route{pattern, handler})
}
func (h *RegexpHandler) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request)) {
h.routes = append(h.routes, &route{pattern, http.HandlerFunc(handler)})
}
func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, route := range h.routes {
if route.pattern.MatchString(r.URL.Path) {
route.handler.ServeHTTP(w, r)
return
}
}
// no pattern matched; send 404 response
http.NotFound(w, r)
}
func main() {
x := RegexpHandler{}
x.routes = append(x.routes, &route{regexp.MustCompile(`^/\w+-\w+-\w+-\w+-\w+$`), http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
})})
x.routes = append(x.routes, &route{regexp.MustCompile(`^/`), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(r.URL.Path)); err != nil {
log.Fatal(err.Error())
}
})})
log.Fatal(http.ListenAndServe("127.0.0.1:8081", &x)) // nolint:gosec
}