Merge pull request #13557 from heww/fix-issue-13556

fix: remove auto re-readable body in BeforeRequest
This commit is contained in:
Daniel Jiang 2020-11-23 01:08:52 +08:00 committed by GitHub
commit 00c941a413
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -15,10 +15,11 @@
package blob
import (
"github.com/goharbor/harbor/src/lib/errors"
"io/ioutil"
"net/http"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/distribution"
"github.com/goharbor/harbor/src/server/middleware"
@ -32,6 +33,7 @@ func PutManifestMiddleware() func(http.Handler) http.Handler {
ctx := r.Context()
logger := log.G(ctx)
lib.NopCloseRequest(r) // make the r.Body re-readable
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err

View File

@ -15,10 +15,10 @@
package middleware
import (
lib_http "github.com/goharbor/harbor/src/lib/http"
"net/http"
"github.com/goharbor/harbor/src/lib"
lib_http "github.com/goharbor/harbor/src/lib/http"
)
// Middleware receives a handler and returns another handler.
@ -62,7 +62,7 @@ func New(fn func(http.ResponseWriter, *http.Request, http.Handler), skippers ...
// BeforeRequest make a middleware which will call hook before the next handler
func BeforeRequest(hook func(*http.Request) error, skippers ...Skipper) func(http.Handler) http.Handler {
return New(func(w http.ResponseWriter, r *http.Request, next http.Handler) {
if err := hook(lib.NopCloseRequest(r)); err != nil {
if err := hook(r); err != nil {
lib_http.SendError(w, err)
return
}