Skip to content

Commit

Permalink
x-pack/filebeat/input/http_endpoint: ensure request body is closed wh…
Browse files Browse the repository at this point in the history
…en content is gzipped (#37091)
  • Loading branch information
efd6 authored Nov 13, 2023
1 parent a473880 commit 9f93f93
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Fix merging of array fields(processors, paths, parsers) in configurations generated from hints and default config. {issue}36838[36838] {pull}36857[36857]
- Fix handling of response errors in HTTPJSON and CEL request trace logging. {pull}36956[36956]
- Do not error when Okta API returns no data. {pull}37092[37092]
- Fix request body close behaviour in HTTP_Endpoint when handling GZIP compressed content. {pull}37091[37091]

*Heartbeat*

Expand Down
9 changes: 6 additions & 3 deletions x-pack/filebeat/input/http_endpoint/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package http_endpoint

import (
"compress/gzip"
"errors"
"io"
"sync"
)
Expand All @@ -18,29 +19,31 @@ var gzipDecoderPool = sync.Pool{

type pooledGzipReader struct {
Reader *gzip.Reader
closer io.Closer
}

func newPooledGzipReader(r io.Reader) (*pooledGzipReader, error) {
func newPooledGzipReader(r io.ReadCloser) (*pooledGzipReader, error) {
gzipReader := gzipDecoderPool.Get().(*gzip.Reader)
if err := gzipReader.Reset(r); err != nil {
gzipDecoderPool.Put(gzipReader)
return nil, err
}
return &pooledGzipReader{Reader: gzipReader}, nil
return &pooledGzipReader{Reader: gzipReader, closer: r}, nil
}

// Read implements io.Reader, reading uncompressed bytes from its underlying Reader.
func (r *pooledGzipReader) Read(b []byte) (int, error) {
return r.Reader.Read(b)
}

// Close closes the Reader. It does not close the underlying io.Reader.
// Close closes the Reader and the underlying source.
// In order for the GZIP checksum to be verified, the reader must be
// fully consumed until the io.EOF.
//
// After this call the reader should not be reused because it is returned to the pool.
func (r *pooledGzipReader) Close() error {
err := r.Reader.Close()
err = errors.Join(err, r.closer.Close())
gzipDecoderPool.Put(r.Reader)
r.Reader = nil
return err
Expand Down

0 comments on commit 9f93f93

Please sign in to comment.