From 93e6c1b6e5ac92c7b1f15da58debbd117f72ec17 Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Tue, 27 Feb 2024 11:33:59 +0800 Subject: [PATCH 1/6] Write NotFound middleware --- .../handler/site/middleware/middleware.go | 1 + internal/handler/site/middleware/notfound.go | 41 +++++++++++++++++++ internal/handler/site/site_handler.go | 6 +-- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 internal/handler/site/middleware/notfound.go diff --git a/internal/handler/site/middleware/middleware.go b/internal/handler/site/middleware/middleware.go index 80e434d..cb62c18 100644 --- a/internal/handler/site/middleware/middleware.go +++ b/internal/handler/site/middleware/middleware.go @@ -9,5 +9,6 @@ var Default = []site.Middleware{ CanonicalizePath, RouteSPA, IndexPage, + NotFound, compression, } diff --git a/internal/handler/site/middleware/notfound.go b/internal/handler/site/middleware/notfound.go new file mode 100644 index 0000000..0069915 --- /dev/null +++ b/internal/handler/site/middleware/notfound.go @@ -0,0 +1,41 @@ +package middleware + +import ( + "fmt" + "net/http" + "os" + "path" + + "github.com/oursky/pageship/internal/site" +) + +func NotFound(site *site.Descriptor, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + const NotFoundPage = "404.html" + notFound := false + + for { + fmt.Println(r.URL.Path) + _, err := site.FS.Stat(r.URL.Path) + if os.IsNotExist(err) { + notFound = true + if path.Dir(r.URL.Path) == "/" { + http.NotFound(w, r) + return + } + if path.Base(r.URL.Path) == NotFoundPage { + r.URL.Path = path.Join(path.Dir(path.Dir(r.URL.Path)), NotFoundPage) + } else { + r.URL.Path = path.Join(path.Dir(r.URL.Path), NotFoundPage) + } + } else { + break + } + } + + if notFound { + w.WriteHeader(404) + } + next.ServeHTTP(w, r) + }) +} diff --git a/internal/handler/site/site_handler.go b/internal/handler/site/site_handler.go index 4b001c8..3af0f32 100644 --- a/internal/handler/site/site_handler.go +++ b/internal/handler/site/site_handler.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "net/http" - "os" "path" "strings" "time" @@ -60,10 +59,7 @@ func (h *SiteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *SiteHandler) serveFile(w http.ResponseWriter, r *http.Request) { info, err := h.publicFS.Stat(r.URL.Path) - if os.IsNotExist(err) { - http.NotFound(w, r) - return - } else if err != nil { + if err != nil { http.Error(w, "internal server error", http.StatusInternalServerError) return } From fbec0a0cb487734cbdd634e3fddd30a1552265df Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Tue, 27 Feb 2024 11:40:41 +0800 Subject: [PATCH 2/6] Test manually --- examples/dev/public/404.html | 14 ++++++++++++++ examples/main/public/404.html | 14 ++++++++++++++ internal/handler/site/middleware/notfound.go | 2 -- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 examples/dev/public/404.html create mode 100644 examples/main/public/404.html diff --git a/examples/dev/public/404.html b/examples/dev/public/404.html new file mode 100644 index 0000000..30b67eb --- /dev/null +++ b/examples/dev/public/404.html @@ -0,0 +1,14 @@ + + + + + + Test + + +

Hello dev 404

+ + diff --git a/examples/main/public/404.html b/examples/main/public/404.html new file mode 100644 index 0000000..02bd579 --- /dev/null +++ b/examples/main/public/404.html @@ -0,0 +1,14 @@ + + + + + + Test + + +

Hello world 404

+ + diff --git a/internal/handler/site/middleware/notfound.go b/internal/handler/site/middleware/notfound.go index 0069915..09ef4bb 100644 --- a/internal/handler/site/middleware/notfound.go +++ b/internal/handler/site/middleware/notfound.go @@ -1,7 +1,6 @@ package middleware import ( - "fmt" "net/http" "os" "path" @@ -15,7 +14,6 @@ func NotFound(site *site.Descriptor, next http.Handler) http.Handler { notFound := false for { - fmt.Println(r.URL.Path) _, err := site.FS.Stat(r.URL.Path) if os.IsNotExist(err) { notFound = true From 423415b2bdac5d6315a06563dc011c69d7cb3bb2 Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Tue, 27 Feb 2024 14:53:47 +0800 Subject: [PATCH 3/6] Write notfoundtest setup --- .../handler/site/middleware/notfound_test.go | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 internal/handler/site/middleware/notfound_test.go diff --git a/internal/handler/site/middleware/notfound_test.go b/internal/handler/site/middleware/notfound_test.go new file mode 100644 index 0000000..af9102e --- /dev/null +++ b/internal/handler/site/middleware/notfound_test.go @@ -0,0 +1,79 @@ +package middleware_test + +import ( + "bytes" + "context" + "embed" + "io" + "net/http" + "net/http/httptest" + "net/url" + "path" + "testing" + "time" + + "github.com/oursky/pageship/internal/config" + "github.com/oursky/pageship/internal/handler/site/middleware" + "github.com/oursky/pageship/internal/site" +) + +type mockHandler struct { + publicFS site.FS +} + +func (mh mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + rsc, _ := mh.publicFS.Open(r.Context(), r.URL.Path) + http.ServeContent(w, r, path.Base(r.URL.Path), time.Now(), rsc) +} + +type RSCAdapter struct { + *bytes.Reader +} + +func (rsca RSCAdapter) Close() error { + return nil +} + +type FSAdapter struct { + embed.FS +} + +func (fa FSAdapter) Open(c context.Context, s string) (io.ReadSeekCloser, error) { + b := []byte{} + f, _ := fa.FS.Open(s) + f.Read(b) + return RSCAdapter{bytes.NewReader(b)}, nil +} + +func (fa FSAdapter) Stat(string) (*site.FileInfo, error) { + return &site.FileInfo{ + IsDir: false, + ModTime: time.Now(), + Size: 0, + ContentType: "", + Hash: "", + }, nil +} + +//go:embed testdata/testrootwith404 +var mockFS embed.FS + +func TestRootWith404(t *testing.T) { + mh := mockHandler{} + sc := config.DefaultSiteConfig() + mockSiteDescriptor := site.Descriptor{ + ID: "", + Domain: "", + Config: &sc, + FS: FSAdapter{mockFS}, + } + h := middleware.NotFound(&mockSiteDescriptor, mh) + + rec := httptest.NewRecorder() + h.ServeHTTP(rec, &http.Request{ + URL: &url.URL{ + Path: "/", + }, + }) + +} From 19e2e6b12a51f44138fcf11271612441459ce20e Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Tue, 5 Mar 2024 11:44:43 +0800 Subject: [PATCH 4/6] Test notfound middleware --- .../site/middleware/compression_test.go | 12 +- internal/handler/site/middleware/notfound.go | 7 +- .../handler/site/middleware/notfound_test.go | 114 +++++++++++++++--- .../testdata/testrootno404/index.html | 1 + .../testdata/testrootwith404/404.html | 1 + .../testsubdirno404/subdir/index.html | 1 + .../testdata/testsubdirwith404/404.html | 1 + 7 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 internal/handler/site/middleware/testdata/testrootno404/index.html create mode 100644 internal/handler/site/middleware/testdata/testrootwith404/404.html create mode 100644 internal/handler/site/middleware/testdata/testsubdirno404/subdir/index.html create mode 100644 internal/handler/site/middleware/testdata/testsubdirwith404/404.html diff --git a/internal/handler/site/middleware/compression_test.go b/internal/handler/site/middleware/compression_test.go index 6b06fb6..ca6eb7e 100644 --- a/internal/handler/site/middleware/compression_test.go +++ b/internal/handler/site/middleware/compression_test.go @@ -1,22 +1,22 @@ package middleware_test import ( + "compress/gzip" "io" "net/http" "net/http/httptest" "testing" - "compress/gzip" + "github.com/andybalholm/brotli" "github.com/oursky/pageship/internal/handler/site/middleware" "github.com/stretchr/testify/assert" - "github.com/andybalholm/brotli" ) -type mockHandler struct { +type compressMockHandler struct { executeCount int } -func (mh *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (mh *compressMockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { mh.executeCount++ w.Header().Add("Content-Type", "text/plain") w.Write([]byte("hello")) @@ -24,7 +24,7 @@ func (mh *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func TestCacheGzip(t *testing.T) { //Setup - h := middleware.Compression(new(mockHandler)) + h := middleware.Compression(new(compressMockHandler)) //Act Assert req, err := http.NewRequest("GET", "endpoint", nil) @@ -47,7 +47,7 @@ func TestCacheGzip(t *testing.T) { func TestCacheBrotli(t *testing.T) { //Setup - h := middleware.Compression(new(mockHandler)) + h := middleware.Compression(new(compressMockHandler)) //Act Assert req, err := http.NewRequest("GET", "endpoint", nil) diff --git a/internal/handler/site/middleware/notfound.go b/internal/handler/site/middleware/notfound.go index 09ef4bb..e99847b 100644 --- a/internal/handler/site/middleware/notfound.go +++ b/internal/handler/site/middleware/notfound.go @@ -1,8 +1,9 @@ package middleware import ( + "errors" + "io/fs" "net/http" - "os" "path" "github.com/oursky/pageship/internal/site" @@ -15,9 +16,9 @@ func NotFound(site *site.Descriptor, next http.Handler) http.Handler { for { _, err := site.FS.Stat(r.URL.Path) - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { notFound = true - if path.Dir(r.URL.Path) == "/" { + if path.Dir(r.URL.Path) == "/" && path.Base(r.URL.Path) == NotFoundPage { http.NotFound(w, r) return } diff --git a/internal/handler/site/middleware/notfound_test.go b/internal/handler/site/middleware/notfound_test.go index af9102e..be0f27d 100644 --- a/internal/handler/site/middleware/notfound_test.go +++ b/internal/handler/site/middleware/notfound_test.go @@ -5,6 +5,7 @@ import ( "context" "embed" "io" + "io/fs" "net/http" "net/http/httptest" "net/url" @@ -15,13 +16,14 @@ import ( "github.com/oursky/pageship/internal/config" "github.com/oursky/pageship/internal/handler/site/middleware" "github.com/oursky/pageship/internal/site" + "github.com/stretchr/testify/assert" ) -type mockHandler struct { +type notFoundMockHandler struct { publicFS site.FS } -func (mh mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (mh notFoundMockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { rsc, _ := mh.publicFS.Open(r.Context(), r.URL.Path) http.ServeContent(w, r, path.Base(r.URL.Path), time.Now(), rsc) } @@ -36,44 +38,120 @@ func (rsca RSCAdapter) Close() error { type FSAdapter struct { embed.FS + subdir string } func (fa FSAdapter) Open(c context.Context, s string) (io.ReadSeekCloser, error) { - b := []byte{} - f, _ := fa.FS.Open(s) - f.Read(b) + f, _ := fa.FS.Open(path.Join(fa.subdir, s)) + b, _ := io.ReadAll(f) return RSCAdapter{bytes.NewReader(b)}, nil } -func (fa FSAdapter) Stat(string) (*site.FileInfo, error) { +func (fa FSAdapter) Stat(s string) (*site.FileInfo, error) { + st, err := fs.Stat(fa.FS, path.Join(fa.subdir, s)) + if err != nil { + return nil, err + } return &site.FileInfo{ - IsDir: false, - ModTime: time.Now(), - Size: 0, + IsDir: st.IsDir(), + ModTime: st.ModTime(), + Size: st.Size(), ContentType: "", Hash: "", }, nil } +var default404 = "404 page not found\n" + +func AssertResponse(t *testing.T, h http.Handler, p string, sc int, cont string) { + rec := httptest.NewRecorder() + h.ServeHTTP(rec, &http.Request{ + URL: &url.URL{ + Path: p, + }, + }) + res := rec.Result() + assert.Equal(t, sc, res.StatusCode) + bo, _ := io.ReadAll(res.Body) + assert.Equal(t, cont, string(bo)) +} + //go:embed testdata/testrootwith404 -var mockFS embed.FS +var testrootwith404FS embed.FS func TestRootWith404(t *testing.T) { - mh := mockHandler{} + mh := notFoundMockHandler{FSAdapter{testrootwith404FS, "testdata/testrootwith404"}} sc := config.DefaultSiteConfig() mockSiteDescriptor := site.Descriptor{ ID: "", Domain: "", Config: &sc, - FS: FSAdapter{mockFS}, + FS: FSAdapter{testrootwith404FS, "testdata/testrootwith404"}, } h := middleware.NotFound(&mockSiteDescriptor, mh) - rec := httptest.NewRecorder() - h.ServeHTTP(rec, &http.Request{ - URL: &url.URL{ - Path: "/", - }, - }) + AssertResponse(t, h, "/index.html", 404, "testrootwith404_404") + AssertResponse(t, h, "/404.html", 200, "testrootwith404_404") + AssertResponse(t, h, "/nonexistant.html", 404, "testrootwith404_404") + AssertResponse(t, h, "/nonexistant/index.html", 404, "testrootwith404_404") +} + +//go:embed testdata/testrootno404 +var testrootno404FS embed.FS + +func TestRootno404(t *testing.T) { + mh := notFoundMockHandler{FSAdapter{testrootno404FS, "testdata/testrootno404"}} + sc := config.DefaultSiteConfig() + mockSiteDescriptor := site.Descriptor{ + ID: "", + Domain: "", + Config: &sc, + FS: FSAdapter{testrootno404FS, "testdata/testrootno404"}, + } + h := middleware.NotFound(&mockSiteDescriptor, mh) + + AssertResponse(t, h, "/index.html", 200, "testrootno404_index") + AssertResponse(t, h, "/404.html", 404, default404) + AssertResponse(t, h, "/nonexistant.html", 404, default404) + AssertResponse(t, h, "/nonexistant/index.html", 404, default404) +} + +//go:embed testdata/testsubdirno404 +var testsubdirno404FS embed.FS + +func TestSubdirNo404(t *testing.T) { + mh := notFoundMockHandler{FSAdapter{testsubdirno404FS, "testdata/testsubdirno404"}} + sc := config.DefaultSiteConfig() + mockSiteDescriptor := site.Descriptor{ + ID: "", + Domain: "", + Config: &sc, + FS: FSAdapter{testsubdirno404FS, "testdata/testsubdirno404"}, + } + h := middleware.NotFound(&mockSiteDescriptor, mh) + + AssertResponse(t, h, "/subdir/index.html", 200, "testsubdirno404_index") + AssertResponse(t, h, "/subdir/404.html", 404, default404) + AssertResponse(t, h, "/subdir/nonexistant.html", 404, default404) + AssertResponse(t, h, "/subdir/nonexistant/index.html", 404, default404) +} + +//go:embed testdata/testsubdirwith404 +var testsubdirwith404FS embed.FS + +func TestSubdirWith404(t *testing.T) { + mh := notFoundMockHandler{FSAdapter{testsubdirwith404FS, "testdata/testsubdirwith404"}} + sc := config.DefaultSiteConfig() + mockSiteDescriptor := site.Descriptor{ + ID: "", + Domain: "", + Config: &sc, + FS: FSAdapter{testsubdirwith404FS, "testdata/testsubdirwith404"}, + } + h := middleware.NotFound(&mockSiteDescriptor, mh) + AssertResponse(t, h, "/subdir/index.html", 404, "testsubdirwith404_404") + AssertResponse(t, h, "/404.html", 200, "testsubdirwith404_404") + AssertResponse(t, h, "/subdir/nonexistant.html", 404, "testsubdirwith404_404") + AssertResponse(t, h, "/subdir/nonexistant/index.html", 404, "testsubdirwith404_404") } diff --git a/internal/handler/site/middleware/testdata/testrootno404/index.html b/internal/handler/site/middleware/testdata/testrootno404/index.html new file mode 100644 index 0000000..f89bcda --- /dev/null +++ b/internal/handler/site/middleware/testdata/testrootno404/index.html @@ -0,0 +1 @@ +testrootno404_index \ No newline at end of file diff --git a/internal/handler/site/middleware/testdata/testrootwith404/404.html b/internal/handler/site/middleware/testdata/testrootwith404/404.html new file mode 100644 index 0000000..d83128f --- /dev/null +++ b/internal/handler/site/middleware/testdata/testrootwith404/404.html @@ -0,0 +1 @@ +testrootwith404_404 \ No newline at end of file diff --git a/internal/handler/site/middleware/testdata/testsubdirno404/subdir/index.html b/internal/handler/site/middleware/testdata/testsubdirno404/subdir/index.html new file mode 100644 index 0000000..0d5732d --- /dev/null +++ b/internal/handler/site/middleware/testdata/testsubdirno404/subdir/index.html @@ -0,0 +1 @@ +testsubdirno404_index \ No newline at end of file diff --git a/internal/handler/site/middleware/testdata/testsubdirwith404/404.html b/internal/handler/site/middleware/testdata/testsubdirwith404/404.html new file mode 100644 index 0000000..2e60b8b --- /dev/null +++ b/internal/handler/site/middleware/testdata/testsubdirwith404/404.html @@ -0,0 +1 @@ +testsubdirwith404_404 \ No newline at end of file From fc2e6c3544ef9a7a01cd94480e3297206ea99544 Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Mon, 11 Mar 2024 11:48:46 +0800 Subject: [PATCH 5/6] Improve based on comments --- internal/handler/site/middleware/notfound.go | 13 ++++- .../handler/site/middleware/notfound_test.go | 58 ++++++++++++++++--- .../testdata/testrootwith404andindex/404.html | 1 + .../testrootwith404andindex/index.html | 1 + .../testsubdirwith404andindex/404.html | 1 + .../subdir/index.html | 1 + internal/handler/site/site_handler.go | 7 ++- 7 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 internal/handler/site/middleware/testdata/testrootwith404andindex/404.html create mode 100644 internal/handler/site/middleware/testdata/testrootwith404andindex/index.html create mode 100644 internal/handler/site/middleware/testdata/testsubdirwith404andindex/404.html create mode 100644 internal/handler/site/middleware/testdata/testsubdirwith404andindex/subdir/index.html diff --git a/internal/handler/site/middleware/notfound.go b/internal/handler/site/middleware/notfound.go index e99847b..a20609f 100644 --- a/internal/handler/site/middleware/notfound.go +++ b/internal/handler/site/middleware/notfound.go @@ -2,16 +2,20 @@ package middleware import ( "errors" + "io" "io/fs" "net/http" "path" + "time" + internalhttputil "github.com/oursky/pageship/internal/httputil" "github.com/oursky/pageship/internal/site" ) +const NotFoundPage = "404.html" + func NotFound(site *site.Descriptor, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - const NotFoundPage = "404.html" notFound := false for { @@ -34,7 +38,12 @@ func NotFound(site *site.Descriptor, next http.Handler) http.Handler { if notFound { w.WriteHeader(404) + writer := internalhttputil.NewTimeoutResponseWriter(w, 10*time.Second) + rsc, _ := site.FS.Open(r.Context(), r.URL.Path) + b, _ := io.ReadAll(rsc) + writer.Write(b) + } else { + next.ServeHTTP(w, r) } - next.ServeHTTP(w, r) }) } diff --git a/internal/handler/site/middleware/notfound_test.go b/internal/handler/site/middleware/notfound_test.go index be0f27d..9d14c00 100644 --- a/internal/handler/site/middleware/notfound_test.go +++ b/internal/handler/site/middleware/notfound_test.go @@ -76,6 +76,26 @@ func AssertResponse(t *testing.T, h http.Handler, p string, sc int, cont string) assert.Equal(t, cont, string(bo)) } +//go:embed testdata/testrootwith404andindex +var testrootwith404andindexFS embed.FS + +func TestRootWith404AndIndex(t *testing.T) { + mh := notFoundMockHandler{FSAdapter{testrootwith404andindexFS, "testdata/testrootwith404andindex"}} + sc := config.DefaultSiteConfig() + mockSiteDescriptor := site.Descriptor{ + ID: "", + Domain: "", + Config: &sc, + FS: FSAdapter{testrootwith404andindexFS, "testdata/testrootwith404andindex"}, + } + h := middleware.NotFound(&mockSiteDescriptor, mh) + + AssertResponse(t, h, "/index.html", 200, "testrootwith404andindex_index") + AssertResponse(t, h, "/404.html", 200, "testrootwith404andindex_404") + AssertResponse(t, h, "/nonexistant.html", 404, "testrootwith404andindex_404") + AssertResponse(t, h, "/nonexistant/index.html", 404, "testrootwith404andindex_404") +} + //go:embed testdata/testrootwith404 var testrootwith404FS embed.FS @@ -116,24 +136,24 @@ func TestRootno404(t *testing.T) { AssertResponse(t, h, "/nonexistant/index.html", 404, default404) } -//go:embed testdata/testsubdirno404 -var testsubdirno404FS embed.FS +//go:embed testdata/testsubdirwith404andindex +var testsubdirwith404andindexFS embed.FS -func TestSubdirNo404(t *testing.T) { - mh := notFoundMockHandler{FSAdapter{testsubdirno404FS, "testdata/testsubdirno404"}} +func TestSubdirWith404AndIndex(t *testing.T) { + mh := notFoundMockHandler{FSAdapter{testsubdirwith404andindexFS, "testdata/testsubdirwith404andindex"}} sc := config.DefaultSiteConfig() mockSiteDescriptor := site.Descriptor{ ID: "", Domain: "", Config: &sc, - FS: FSAdapter{testsubdirno404FS, "testdata/testsubdirno404"}, + FS: FSAdapter{testsubdirwith404andindexFS, "testdata/testsubdirwith404andindex"}, } h := middleware.NotFound(&mockSiteDescriptor, mh) - AssertResponse(t, h, "/subdir/index.html", 200, "testsubdirno404_index") - AssertResponse(t, h, "/subdir/404.html", 404, default404) - AssertResponse(t, h, "/subdir/nonexistant.html", 404, default404) - AssertResponse(t, h, "/subdir/nonexistant/index.html", 404, default404) + AssertResponse(t, h, "/subdir/index.html", 200, "testsubdirwith404andindex_index") + AssertResponse(t, h, "404.html", 200, "testsubdirwith404andindex_404") + AssertResponse(t, h, "/subdir/nonexistant.html", 404, "testsubdirwith404andindex_404") + AssertResponse(t, h, "/subdir/nonexistant/index.html", 404, "testsubdirwith404andindex_404") } //go:embed testdata/testsubdirwith404 @@ -155,3 +175,23 @@ func TestSubdirWith404(t *testing.T) { AssertResponse(t, h, "/subdir/nonexistant.html", 404, "testsubdirwith404_404") AssertResponse(t, h, "/subdir/nonexistant/index.html", 404, "testsubdirwith404_404") } + +//go:embed testdata/testsubdirno404 +var testsubdirno404FS embed.FS + +func TestSubdirNo404(t *testing.T) { + mh := notFoundMockHandler{FSAdapter{testsubdirno404FS, "testdata/testsubdirno404"}} + sc := config.DefaultSiteConfig() + mockSiteDescriptor := site.Descriptor{ + ID: "", + Domain: "", + Config: &sc, + FS: FSAdapter{testsubdirno404FS, "testdata/testsubdirno404"}, + } + h := middleware.NotFound(&mockSiteDescriptor, mh) + + AssertResponse(t, h, "/subdir/index.html", 200, "testsubdirno404_index") + AssertResponse(t, h, "/subdir/404.html", 404, default404) + AssertResponse(t, h, "/subdir/nonexistant.html", 404, default404) + AssertResponse(t, h, "/subdir/nonexistant/index.html", 404, default404) +} diff --git a/internal/handler/site/middleware/testdata/testrootwith404andindex/404.html b/internal/handler/site/middleware/testdata/testrootwith404andindex/404.html new file mode 100644 index 0000000..9c7527b --- /dev/null +++ b/internal/handler/site/middleware/testdata/testrootwith404andindex/404.html @@ -0,0 +1 @@ +testrootwith404andindex_404 \ No newline at end of file diff --git a/internal/handler/site/middleware/testdata/testrootwith404andindex/index.html b/internal/handler/site/middleware/testdata/testrootwith404andindex/index.html new file mode 100644 index 0000000..ca75fff --- /dev/null +++ b/internal/handler/site/middleware/testdata/testrootwith404andindex/index.html @@ -0,0 +1 @@ +testrootwith404andindex_index \ No newline at end of file diff --git a/internal/handler/site/middleware/testdata/testsubdirwith404andindex/404.html b/internal/handler/site/middleware/testdata/testsubdirwith404andindex/404.html new file mode 100644 index 0000000..78e68d4 --- /dev/null +++ b/internal/handler/site/middleware/testdata/testsubdirwith404andindex/404.html @@ -0,0 +1 @@ +testsubdirwith404andindex_404 \ No newline at end of file diff --git a/internal/handler/site/middleware/testdata/testsubdirwith404andindex/subdir/index.html b/internal/handler/site/middleware/testdata/testsubdirwith404andindex/subdir/index.html new file mode 100644 index 0000000..48f780a --- /dev/null +++ b/internal/handler/site/middleware/testdata/testsubdirwith404andindex/subdir/index.html @@ -0,0 +1 @@ +testsubdirwith404andindex_index \ No newline at end of file diff --git a/internal/handler/site/site_handler.go b/internal/handler/site/site_handler.go index 3af0f32..85b568e 100644 --- a/internal/handler/site/site_handler.go +++ b/internal/handler/site/site_handler.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "os" "path" "strings" "time" @@ -59,7 +60,11 @@ func (h *SiteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *SiteHandler) serveFile(w http.ResponseWriter, r *http.Request) { info, err := h.publicFS.Stat(r.URL.Path) - if err != nil { + if os.IsNotExist(err) { + fmt.Println("angy ", r.URL.Path) + http.NotFound(w, r) + return + } else if err != nil { http.Error(w, "internal server error", http.StatusInternalServerError) return } From e0dbda9325bef701fa38c402669c346e71754951 Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Mon, 11 Mar 2024 14:59:00 +0800 Subject: [PATCH 6/6] Use io.Copy and remove debug --- internal/handler/site/middleware/notfound.go | 3 +-- internal/handler/site/site_handler.go | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/handler/site/middleware/notfound.go b/internal/handler/site/middleware/notfound.go index a20609f..390da86 100644 --- a/internal/handler/site/middleware/notfound.go +++ b/internal/handler/site/middleware/notfound.go @@ -40,8 +40,7 @@ func NotFound(site *site.Descriptor, next http.Handler) http.Handler { w.WriteHeader(404) writer := internalhttputil.NewTimeoutResponseWriter(w, 10*time.Second) rsc, _ := site.FS.Open(r.Context(), r.URL.Path) - b, _ := io.ReadAll(rsc) - writer.Write(b) + io.Copy(writer, rsc) } else { next.ServeHTTP(w, r) } diff --git a/internal/handler/site/site_handler.go b/internal/handler/site/site_handler.go index 85b568e..4b001c8 100644 --- a/internal/handler/site/site_handler.go +++ b/internal/handler/site/site_handler.go @@ -61,7 +61,6 @@ func (h *SiteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *SiteHandler) serveFile(w http.ResponseWriter, r *http.Request) { info, err := h.publicFS.Stat(r.URL.Path) if os.IsNotExist(err) { - fmt.Println("angy ", r.URL.Path) http.NotFound(w, r) return } else if err != nil {