func()

in internal/conn/http/http.go [71:119]


func (t *zlibTransport) Do(req *policy.Request) (*http.Response, error) {
	// Get the underlying http.Request
	httpReq := req.Raw()

	// If the request has a body, apply Deflate compression.
	if httpReq.Body != nil && httpReq.ContentLength > 0 {
		// Read the original body content.
		var buf bytes.Buffer
		_, err := io.Copy(&buf, httpReq.Body)
		if err != nil {
			return nil, err
		}

		// Compress the content using Deflate at the specified level.
		compressedBuffer := flatePool.Get().(*bytes.Buffer)
		defer func() {
			compressedBuffer.Reset()
			flatePool.Put(compressedBuffer)
		}()

		var writer *zlib.Writer
		select {
		case writer = <-t.flatePool:
		default:
			writer, err = zlib.NewWriterLevel(compressedBuffer, 5)
			if err != nil {
				return nil, err
			}
		}
		writer.Reset(compressedBuffer)
		_, err = writer.Write(buf.Bytes())
		if err != nil {
			return nil, err
		}
		writer.Close()
		select {
		case t.flatePool <- writer:
		default:
		}

		// Update the request with the compressed body.
		httpReq.Body = io.NopCloser(compressedBuffer)
		httpReq.ContentLength = int64(compressedBuffer.Len())
		httpReq.Header.Set("Content-Encoding", "deflate")
	}

	// Use the base RoundTripper to perform the actual request.
	return req.Next()
}