diff --git a/internal/gui/api.go b/internal/gui/api.go index 6db9785fd..52ee7b020 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -17,7 +17,6 @@ import ( "io" "io/fs" "maps" - "mime" "net/http" "net/url" "os" @@ -1055,19 +1054,29 @@ var onDock func(settings.Settings) // assetTypes are the page's own files' types. The file server takes them // from Windows' registry, which another program may have changed — an SVG // served as something else draws no icon. -var assetTypes = map[string]string{".svg": "image/svg+xml", ".png": "image/png", ".css": "text/css; charset=utf-8", - ".js": "text/javascript; charset=utf-8", ".html": "text/html; charset=utf-8"} +var assetTypes = map[string]string{ + ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", + ".mjs": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", + ".json": "application/json", ".svg": "image/svg+xml", ".png": "image/png", + ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", + ".ico": "image/x-icon", ".webm": "video/webm", ".mp4": "video/mp4", + ".woff": "font/woff", ".woff2": "font/woff2", ".ttf": "font/ttf", + ".txt": "text/plain; charset=utf-8", ".map": "application/json", ".wasm": "application/wasm", +} -// addAssetTypes is called by Handler, not init: the mime package reads -// and parses the system's whole type table (/etc/apache2/mime.types, -// ~18,000 allocations) the first time a type is added, and a launch that -// serves no page (`magpie statusline` on every Claude Code message) never -// needs it. -var addAssetTypes = sync.OnceFunc(registerAssetTypes) +// assetType is the type a file of magpie's own (or a plugin page's) is +// served as, from assetTypes: "" for one it doesn't name. +func assetType(name string) string { return assetTypes[strings.ToLower(path.Ext(name))] } -func registerAssetTypes() { - for ext, t := range assetTypes { - mime.AddExtensionType(ext, t) +// servedAs sets name's type on rw before http.ServeContent looks at the name. +// Asked for a type, the mime package reads and parses the system's whole +// table (/etc/apache2/mime.types, ~18,000 allocations) and keeps it for +// good: ~2 MB of an idle tray magpie's live heap, from its first page. +// And the system's table (Windows' registry, which another program may +// have changed) never decides how the page's own files are read. +func servedAs(rw http.ResponseWriter, name string) { + if t := assetType(name); t != "" && rw.Header().Get("Content-Type") == "" { + rw.Header().Set("Content-Type", t) } } @@ -1092,6 +1101,7 @@ func revalidated(next http.Handler) http.Handler { if name == "" { name = "index.html" } + servedAs(rw, name) sum, page, ok := staticSum(name, r.URL.Path == "/") if !ok { next.ServeHTTP(rw, r) @@ -1178,7 +1188,6 @@ func versionedPage(page []byte) []byte { // gw is the gateway this process serves, or nil when another magpie has it // (for now: see startBackend). func Handler(w Windows, gw *gateway.Server) http.Handler { - addAssetTypes() if gw != nil { served.Store(gw) } diff --git a/internal/gui/assets_test.go b/internal/gui/assets_test.go index 52bcf701e..4c7b989e0 100644 --- a/internal/gui/assets_test.go +++ b/internal/gui/assets_test.go @@ -1,24 +1,53 @@ package gui import ( + "io/fs" "mime" - "sync" + "net/http" + "net/http/httptest" + "path" "testing" ) -// Handler sets the page's own types even where the system's table says -// otherwise (Windows' registry, which another program may have changed). +// The page's own files go out as assetTypes names them, whatever the +// system's table says (Windows' registry, which another program may have +// changed): their type is set before http.ServeContent would ask the mime +// package, which reads the system's whole table into the heap for good. func TestAssetTypesHoldAgainstTheRegistry(t *testing.T) { for ext := range assetTypes { mime.AddExtensionType(ext, "application/x-other") } - was := addAssetTypes - addAssetTypes = sync.OnceFunc(registerAssetTypes) - t.Cleanup(func() { addAssetTypes = was; registerAssetTypes() }) - Handler(nil, nil) - for ext, want := range assetTypes { - if got := mime.TypeByExtension(ext); got != want { - t.Errorf("%s served as %q, not %q", ext, got, want) + t.Cleanup(func() { + for ext, typ := range assetTypes { + mime.AddExtensionType(ext, typ) + } + }) + h := Handler(nil, nil) + seen := map[string]bool{} + fs.WalkDir(staticFS(), ".", func(name string, d fs.DirEntry, err error) error { + ext := path.Ext(name) + // index.html is served as / (below); FileServer sends its own name there + if err != nil || d.IsDir() || seen[ext] || assetTypes[ext] == "" || name == "index.html" { + return err + } + seen[ext] = true + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/"+name, nil)) + if got := rec.Header().Get("Content-Type"); rec.Code == http.StatusOK && got != assetTypes[ext] { + t.Errorf("%s served as %q, not %q", name, got, assetTypes[ext]) + } else if rec.Code != http.StatusOK { + t.Errorf("%s: %d", name, rec.Code) + } + return nil + }) + for _, ext := range []string{".js", ".css", ".svg", ".png", ".jpg", ".json", ".webm"} { + if !seen[ext] { + t.Errorf("no %s file served", ext) } } + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil)) + if got := rec.Header().Get("Content-Type"); got != "text/html; charset=utf-8" { + t.Errorf("/ served as %q", got) + } } diff --git a/internal/gui/pluginpage.go b/internal/gui/pluginpage.go index 5af03c897..1da95a207 100644 --- a/internal/gui/pluginpage.go +++ b/internal/gui/pluginpage.go @@ -439,6 +439,7 @@ func servePluginPage(rw http.ResponseWriter, r *http.Request, web bool) { http.NotFound(rw, r) return } + servedAs(rw, file) http.ServeContent(rw, r, filepath.Base(file), st.ModTime(), f) } diff --git a/internal/gui/providers.go b/internal/gui/providers.go index b14b32331..3ba85c73b 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -1775,6 +1775,9 @@ func providerRoutes(mux *http.ServeMux, w Windows) { http.NotFound(rw, r) return } + if ctype == "" { + ctype = assetType(f) + } if ctype != "" { rw.Header().Set("Content-Type", ctype) }