func()

in internal/beater/request/context.go [267:315]


func (c *Context) decodeRequestBody() error {
	if c.Request.ContentLength == 0 {
		return nil
	}

	var reader io.ReadCloser
	var err error
	switch c.Request.Header.Get("Content-Encoding") {
	case "deflate":
		reader, err = c.resetZlib(c.Request.Body)
	case "gzip":
		reader, err = c.resetGzip(c.Request.Body)
	default:
		// Sniff encoding from payload by looking at the first two bytes.
		// This produces much less garbage than opportunistically calling
		// gzip.NewReader, zlib.NewReader, etc.
		//
		// Portions of code based on compress/zlib and compress/gzip.
		const (
			zlibDeflate = 8
			gzipID1     = 0x1f
			gzipID2     = 0x8b
		)
		rc := &c.compressedRequestReadCloser
		rc.ReadCloser = c.Request.Body
		if _, err := c.Request.Body.Read(rc.magic[:]); err != nil {
			if err == io.EOF {
				// Leave the original request body in place,
				// which should continue returning io.EOF.
				return nil
			}
			return err
		}
		if rc.magic[0] == gzipID1 && rc.magic[1] == gzipID2 {
			reader, err = c.resetGzip(rc)
		} else if rc.magic[0]&0x0f == zlibDeflate {
			reader, err = c.resetZlib(rc)
		} else {
			reader = rc
		}
	}
	if err != nil {
		return err
	}

	c.Request.ContentLength = -1
	c.Request.Body = reader
	return nil
}