From 137cf692c4ec4fd9ab95f642290ee064b2393844 Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 14 May 2021 13:47:44 -0500 Subject: [PATCH] switch to master go-pkgz/rest #71 --- app/proxy/proxy.go | 4 +- go.mod | 2 +- go.sum | 2 + vendor/github.com/go-pkgz/rest/.golangci.yml | 2 +- vendor/github.com/go-pkgz/rest/file_server.go | 43 ++++++++++++++++++- vendor/github.com/go-pkgz/rest/sizelimit.go | 2 + vendor/modules.txt | 4 +- 7 files changed, 50 insertions(+), 9 deletions(-) diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index d9c3e77..c38e2a8 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -228,7 +228,7 @@ func (h *Http) proxyHandler() http.HandlerFunc { h.Reporter.Report(w, http.StatusInternalServerError) return } - fs, err := R.FileServer(ae[0], ae[1]) + fs, err := R.FileServer(ae[0], ae[1], nil) if err != nil { h.Reporter.Report(w, http.StatusInternalServerError) return @@ -242,7 +242,7 @@ func (h *Http) assetsHandler() http.HandlerFunc { if h.AssetsLocation == "" || h.AssetsWebRoot == "" { return func(writer http.ResponseWriter, request *http.Request) {} } - fs, err := R.FileServer(h.AssetsWebRoot, h.AssetsLocation) + fs, err := R.FileServer(h.AssetsWebRoot, h.AssetsLocation, nil) if err != nil { log.Printf("[WARN] can't initialize assets server, %v", err) return func(writer http.ResponseWriter, request *http.Request) {} diff --git a/go.mod b/go.mod index abf02a9..03b8cd0 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/go-pkgz/lgr v0.10.4 - github.com/go-pkgz/rest v1.9.2 + github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db github.com/gorilla/handlers v1.5.1 github.com/prometheus/client_golang v1.10.0 github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 2d72ee0..a1b6c41 100644 --- a/go.sum +++ b/go.sum @@ -69,6 +69,8 @@ github.com/go-pkgz/lgr v0.10.4 h1:l7qyFjqEZgwRgaQQSEp6tve4A3OU80VrfzpvtEX8ngw= github.com/go-pkgz/lgr v0.10.4/go.mod h1:CD0s1z6EFpIUplV067gitF77tn25JItzwHNKAPqeCF0= github.com/go-pkgz/rest v1.9.2 h1:RyBBRXBYY6eBgTW3UGYOyT4VQPDiBBFh/tesELWsryQ= github.com/go-pkgz/rest v1.9.2/go.mod h1:wZ/dGipZUaF9to0vIQl7PwDHgWQDB0jsrFg1xnAKLDw= +github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db h1:PoIO+kDPc0A6m5xlRao4No1P9Ew4hdyZ4UFnX9fbanc= +github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db/go.mod h1:wZ/dGipZUaF9to0vIQl7PwDHgWQDB0jsrFg1xnAKLDw= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= diff --git a/vendor/github.com/go-pkgz/rest/.golangci.yml b/vendor/github.com/go-pkgz/rest/.golangci.yml index 831557a..4cd6d42 100644 --- a/vendor/github.com/go-pkgz/rest/.golangci.yml +++ b/vendor/github.com/go-pkgz/rest/.golangci.yml @@ -44,7 +44,7 @@ linters: - varcheck - stylecheck - gochecknoinits - - scopelint + - exportloopref - gocritic - nakedret - gosimple diff --git a/vendor/github.com/go-pkgz/rest/file_server.go b/vendor/github.com/go-pkgz/rest/file_server.go index 8c981a0..3edf7e3 100644 --- a/vendor/github.com/go-pkgz/rest/file_server.go +++ b/vendor/github.com/go-pkgz/rest/file_server.go @@ -2,6 +2,8 @@ package rest import ( "fmt" + "io" + "io/ioutil" "net/http" "os" "path/filepath" @@ -12,7 +14,8 @@ import ( // prevents directory listing. // - public defines base path of the url, i.e. for http://example.com/static/* it should be /static // - local for the local path to the root of the served directory -func FileServer(public, local string) (http.Handler, error) { +// - notFound is the reader for the custom 404 html, can be nil for default +func FileServer(public, local string, notFound io.Reader) (http.Handler, error) { root, err := filepath.Abs(local) if err != nil { @@ -22,7 +25,8 @@ func FileServer(public, local string) (http.Handler, error) { return nil, fmt.Errorf("local path %s doesn't exist: %w", root, err) } - return http.StripPrefix(public, http.FileServer(noDirListingFS{http.Dir(root)})), nil + fs := http.StripPrefix(public, http.FileServer(noDirListingFS{http.Dir(root)})) + return custom404Handler(fs, notFound) } type noDirListingFS struct{ fs http.FileSystem } @@ -47,3 +51,38 @@ func (fs noDirListingFS) Open(name string) (http.File, error) { } return f, nil } + +// respWriter404 intercept Write to provide custom 404 response +type respWriter404 struct { + http.ResponseWriter + status int + msg []byte +} + +func (w *respWriter404) WriteHeader(status int) { + w.status = status + w.ResponseWriter.WriteHeader(status) +} + +func (w *respWriter404) Write(p []byte) (n int, err error) { + if w.status != http.StatusNotFound || w.msg == nil { + return w.ResponseWriter.Write(p) + } + _, err = w.ResponseWriter.Write(w.msg) + return len(p), err +} + +func custom404Handler(next http.Handler, notFound io.Reader) (http.Handler, error) { + if notFound == nil { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) }), nil + } + + body, err := ioutil.ReadAll(notFound) + if err != nil { + return nil, err + } + + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + next.ServeHTTP(&respWriter404{ResponseWriter: w, msg: body}, r) + }), nil +} diff --git a/vendor/github.com/go-pkgz/rest/sizelimit.go b/vendor/github.com/go-pkgz/rest/sizelimit.go index 1b6be67..6770e88 100644 --- a/vendor/github.com/go-pkgz/rest/sizelimit.go +++ b/vendor/github.com/go-pkgz/rest/sizelimit.go @@ -26,6 +26,8 @@ func SizeLimit(size int64) func(http.Handler) http.Handler { w.WriteHeader(http.StatusServiceUnavailable) return } + _ = r.Body.Close() // the original body already consumed + if int64(len(content)) > size { w.WriteHeader(http.StatusRequestEntityTooLarge) return diff --git a/vendor/modules.txt b/vendor/modules.txt index 7ccf68a..2b360e2 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,5 +1,3 @@ -# github.com/BurntSushi/toml v0.3.1 -## explicit # github.com/beorn7/perks v1.0.1 github.com/beorn7/perks/quantile # github.com/cespare/xxhash/v2 v2.1.1 @@ -11,7 +9,7 @@ github.com/felixge/httpsnoop # github.com/go-pkgz/lgr v0.10.4 ## explicit github.com/go-pkgz/lgr -# github.com/go-pkgz/rest v1.9.2 +# github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db ## explicit github.com/go-pkgz/rest github.com/go-pkgz/rest/logger