github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/server/middleware/debug.go (about) 1 package middleware 2 3 import ( 4 "bufio" 5 "encoding/json" 6 "io" 7 "net/http" 8 9 "github.com/Sirupsen/logrus" 10 "github.com/docker/docker/api/server/httputils" 11 "github.com/docker/docker/pkg/ioutils" 12 "golang.org/x/net/context" 13 ) 14 15 // DebugRequestMiddleware dumps the request to logger 16 func DebugRequestMiddleware(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 17 return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 18 logrus.Debugf("Calling %s %s", r.Method, r.RequestURI) 19 20 if r.Method != "POST" { 21 return handler(ctx, w, r, vars) 22 } 23 if err := httputils.CheckForJSON(r); err != nil { 24 return handler(ctx, w, r, vars) 25 } 26 maxBodySize := 4096 // 4KB 27 if r.ContentLength > int64(maxBodySize) { 28 return handler(ctx, w, r, vars) 29 } 30 31 body := r.Body 32 bufReader := bufio.NewReaderSize(body, maxBodySize) 33 r.Body = ioutils.NewReadCloserWrapper(bufReader, func() error { return body.Close() }) 34 35 b, err := bufReader.Peek(maxBodySize) 36 if err != io.EOF { 37 // either there was an error reading, or the buffer is full (in which case the request is too large) 38 return handler(ctx, w, r, vars) 39 } 40 41 var postForm map[string]interface{} 42 if err := json.Unmarshal(b, &postForm); err == nil { 43 if _, exists := postForm["password"]; exists { 44 postForm["password"] = "*****" 45 } 46 formStr, errMarshal := json.Marshal(postForm) 47 if errMarshal == nil { 48 logrus.Debugf("form data: %s", string(formStr)) 49 } else { 50 logrus.Debugf("form data: %q", postForm) 51 } 52 } 53 54 return handler(ctx, w, r, vars) 55 } 56 }