handlers: a directory under /s/ answers 404, not a listing (closes #286)
check / check (push) Waiting to run
check / check (push) Waiting to run
http.FileServer answered /s/ and /s/css/ with an HTML listing of the embedded files, and /s/css with a redirect to that listing. HandleStatic in internal/handlers now looks the name up in the embedded files first and answers 404 when it names a directory; files are served as before. The routes mount it under /s. Model: opus-5-5
This commit is contained in:
@@ -910,6 +910,7 @@ internal/
|
|||||||
templates/dashboard.html Dashboard template (embedded)
|
templates/dashboard.html Dashboard template (embedded)
|
||||||
status.go /api/v1/status
|
status.go /api/v1/status
|
||||||
healthcheck.go Health check handler
|
healthcheck.go Health check handler
|
||||||
|
static.go Static assets under /s/
|
||||||
server/
|
server/
|
||||||
server.go HTTP server lifecycle
|
server.go HTTP server lifecycle
|
||||||
routes.go Route definitions
|
routes.go Route definitions
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ milestone: https://git.eeqj.de/sneak/dnswatcher/milestone/7
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
|
- 2026-10-15: a request under `/s/` that names a directory, such as `/s/` or
|
||||||
|
`/s/css`, answers 404 instead of a listing of its files (closes #286).
|
||||||
- 2026-10-15: a notification endpoint that answers with a redirect, or any
|
- 2026-10-15: a notification endpoint that answers with a redirect, or any
|
||||||
status other than 2xx, is a failed delivery, retried and logged (closes #287).
|
status other than 2xx, is a failed delivery, retried and logged (closes #287).
|
||||||
- 2026-10-15: a nameserver is asked at each of its IPv4 addresses in turn, so it
|
- 2026-10-15: a nameserver is asked at each of its IPv4 addresses in turn, so it
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"sneak.berlin/go/dnswatcher/static"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleStatic returns the handler for the embedded static assets. It
|
||||||
|
// is mounted under /s, with that prefix stripped from the path. A
|
||||||
|
// request that names a directory answers 404: http.FileServer would
|
||||||
|
// answer it with a listing of the directory, or with a redirect to
|
||||||
|
// that listing when the trailing slash is missing.
|
||||||
|
func (h *Handlers) HandleStatic() http.HandlerFunc {
|
||||||
|
files := http.FileServer(http.FS(static.Static))
|
||||||
|
|
||||||
|
return func(
|
||||||
|
writer http.ResponseWriter,
|
||||||
|
request *http.Request,
|
||||||
|
) {
|
||||||
|
// fs.Stat refuses a name that is empty or ends in a slash,
|
||||||
|
// so /s/ and /s/css/ answer 404 here along with /s/css.
|
||||||
|
name := strings.TrimPrefix(request.URL.Path, "/")
|
||||||
|
|
||||||
|
info, err := fs.Stat(static.Static, name)
|
||||||
|
if err != nil || info.IsDir() {
|
||||||
|
http.NotFound(writer, request)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files.ServeHTTP(writer, request)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
chimw "github.com/go-chi/chi/v5/middleware"
|
chimw "github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
|
||||||
"sneak.berlin/go/dnswatcher/static"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// requestTimeout is the maximum duration for handling a request.
|
// requestTimeout is the maximum duration for handling a request.
|
||||||
@@ -47,13 +45,7 @@ func (s *Server) SetupRoutes() {
|
|||||||
public.Get("/", s.handlers.HandleDashboard())
|
public.Get("/", s.handlers.HandleDashboard())
|
||||||
|
|
||||||
// Static assets (embedded CSS/JS)
|
// Static assets (embedded CSS/JS)
|
||||||
public.Mount(
|
public.Mount("/s", http.StripPrefix("/s", s.handlers.HandleStatic()))
|
||||||
"/s",
|
|
||||||
http.StripPrefix(
|
|
||||||
"/s",
|
|
||||||
http.FileServer(http.FS(static.Static)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
// Health check (standard well-known path)
|
// Health check (standard well-known path)
|
||||||
public.Get(
|
public.Get(
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package server_test
|
package server_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -288,3 +289,42 @@ func TestMetricsRateLimitComesBeforeAuth(t *testing.T) {
|
|||||||
t.Errorf("another address: status = %d, want 200", rec.Code)
|
t.Errorf("another address: status = %d, want 200", rec.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestStaticDirectoriesAnswerNotFound checks that a request naming a
|
||||||
|
// directory of the static assets, with or without the trailing slash,
|
||||||
|
// answers 404 rather than a listing of it or a redirect to one, and
|
||||||
|
// that the stylesheet is still served as CSS.
|
||||||
|
func TestStaticDirectoriesAnswerNotFound(t *testing.T) {
|
||||||
|
viper.Reset()
|
||||||
|
t.Setenv("DNSWATCHER_TARGETS", "example.com")
|
||||||
|
|
||||||
|
srv := routedServer(t)
|
||||||
|
|
||||||
|
for _, path := range []string{"/s", "/s/", "/s/css", "/s/css/"} {
|
||||||
|
rec := serve(srv, httptest.NewRequestWithContext(
|
||||||
|
t.Context(), http.MethodGet, path, nil,
|
||||||
|
))
|
||||||
|
|
||||||
|
if rec.Code != http.StatusNotFound {
|
||||||
|
t.Errorf("GET %s: status = %d, want 404", path, rec.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rec := serve(srv, httptest.NewRequestWithContext(
|
||||||
|
t.Context(), http.MethodGet, "/s/css/tailwind.min.css", nil,
|
||||||
|
))
|
||||||
|
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Errorf("GET stylesheet: status = %d, want 200", rec.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType := rec.Header().Get("Content-Type")
|
||||||
|
|
||||||
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||||
|
if err != nil || mediaType != "text/css" {
|
||||||
|
t.Errorf(
|
||||||
|
"GET stylesheet: Content-Type = %q, want text/css",
|
||||||
|
contentType,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user