diff options
author | Jason T. Greene <jason.greene@redhat.com> | 2022-07-28 21:55:16 -0500 |
---|---|---|
committer | Jason T. Greene <jason.greene@redhat.com> | 2022-07-29 00:51:44 -0500 |
commit | 7a909a3e233d9948bdf3973d7894d70c2bba4ae9 (patch) | |
tree | 4e9a2e7efb024c8f9d0bc732b1a384bb7f267849 /pkg/api/server/handler_api.go | |
parent | a43cfc1b1adb91ef448bcbb8a4aab65477749567 (diff) | |
download | podman-7a909a3e233d9948bdf3973d7894d70c2bba4ae9.tar.gz podman-7a909a3e233d9948bdf3973d7894d70c2bba4ae9.tar.bz2 podman-7a909a3e233d9948bdf3973d7894d70c2bba4ae9.zip |
Use 8k buffer to help clients w/ broken parsing
Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
Diffstat (limited to 'pkg/api/server/handler_api.go')
-rw-r--r-- | pkg/api/server/handler_api.go | 95 |
1 files changed, 76 insertions, 19 deletions
diff --git a/pkg/api/server/handler_api.go b/pkg/api/server/handler_api.go index a3aa681d0..57cafa5f6 100644 --- a/pkg/api/server/handler_api.go +++ b/pkg/api/server/handler_api.go @@ -1,7 +1,10 @@ package server import ( + "bufio" + "errors" "fmt" + "net" "net/http" "runtime" @@ -9,34 +12,54 @@ import ( "github.com/sirupsen/logrus" ) +type BufferedResponseWriter struct { + b *bufio.Writer + w http.ResponseWriter +} + // APIHandler is a wrapper to enhance HandlerFunc's and remove redundant code func (s *APIServer) APIHandler(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Wrapper to hide some boilerplate - fn := func(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - logrus.WithFields(logrus.Fields{ - "X-Reference-Id": r.Header.Get("X-Reference-Id"), - }).Info("Failed Request: unable to parse form: " + err.Error()) - } + s.apiWrapper(h, w, r, false) + } +} + +// An API Handler to help historical clients with broken parsing that expect +// streaming JSON payloads to be reliably messaged framed (full JSON record +// always fits in each read()) +func (s *APIServer) StreamBufferedAPIHandler(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // Wrapper to hide some boilerplate + s.apiWrapper(h, w, r, true) + } +} - cv := version.APIVersion[version.Compat][version.CurrentAPI] - w.Header().Set("API-Version", fmt.Sprintf("%d.%d", cv.Major, cv.Minor)) +func (s *APIServer) apiWrapper(h http.HandlerFunc, w http.ResponseWriter, r *http.Request, buffer bool) { + if err := r.ParseForm(); err != nil { + logrus.WithFields(logrus.Fields{ + "X-Reference-Id": r.Header.Get("X-Reference-Id"), + }).Info("Failed Request: unable to parse form: " + err.Error()) + } - lv := version.APIVersion[version.Libpod][version.CurrentAPI].String() - w.Header().Set("Libpod-API-Version", lv) - w.Header().Set("Server", "Libpod/"+lv+" ("+runtime.GOOS+")") + cv := version.APIVersion[version.Compat][version.CurrentAPI] + w.Header().Set("API-Version", fmt.Sprintf("%d.%d", cv.Major, cv.Minor)) - if s.CorsHeaders != "" { - w.Header().Set("Access-Control-Allow-Origin", s.CorsHeaders) - w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth, Connection, Upgrade, X-Registry-Config") - w.Header().Set("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE, PUT, OPTIONS") - } + lv := version.APIVersion[version.Libpod][version.CurrentAPI].String() + w.Header().Set("Libpod-API-Version", lv) + w.Header().Set("Server", "Libpod/"+lv+" ("+runtime.GOOS+")") - h(w, r) - } - fn(w, r) + if s.CorsHeaders != "" { + w.Header().Set("Access-Control-Allow-Origin", s.CorsHeaders) + w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth, Connection, Upgrade, X-Registry-Config") + w.Header().Set("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE, PUT, OPTIONS") } + + if buffer { + w = newBufferedResponseWriter(w) + } + + h(w, r) } // VersionedPath prepends the version parsing code @@ -44,3 +67,37 @@ func (s *APIServer) APIHandler(h http.HandlerFunc) http.HandlerFunc { func VersionedPath(p string) string { return "/v{version:[0-9][0-9A-Za-z.-]*}" + p } + +func (w *BufferedResponseWriter) Header() http.Header { + return w.w.Header() +} + +func (w *BufferedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { + _ = w.b.Flush() + if wrapped, ok := w.w.(http.Hijacker); ok { + return wrapped.Hijack() + } + + return nil, nil, errors.New("ResponseWriter does not support hijacking") +} + +func (w *BufferedResponseWriter) Write(b []byte) (int, error) { + return w.b.Write(b) +} + +func (w *BufferedResponseWriter) WriteHeader(statusCode int) { + w.w.WriteHeader(statusCode) +} + +func (w *BufferedResponseWriter) Flush() { + _ = w.b.Flush() + if wrapped, ok := w.w.(http.Flusher); ok { + wrapped.Flush() + } +} +func newBufferedResponseWriter(rw http.ResponseWriter) *BufferedResponseWriter { + return &BufferedResponseWriter{ + bufio.NewWriterSize(rw, 8192), + rw, + } +} |