package acceptencoding
import (
smithyhttp
)
const acceptEncodingHeaderKey = "Accept-Encoding"
const contentEncodingHeaderKey = "Content-Encoding"
type AddAcceptEncodingGzipOptions struct {
Enable bool
}
func ( *middleware.Stack, AddAcceptEncodingGzipOptions) error {
if .Enable {
if := .Finalize.Add(&EnableGzip{}, middleware.Before); != nil {
return
}
if := .Deserialize.Insert(&DecompressGzip{}, "OperationDeserializer", middleware.After); != nil {
return
}
return nil
}
return .Finalize.Add(&DisableGzip{}, middleware.Before)
}
type DisableGzip struct{}
func (*DisableGzip) () string {
return "DisableAcceptEncodingGzip"
}
func (*DisableGzip) (
context.Context, middleware.FinalizeInput, middleware.FinalizeHandler,
) (
middleware.FinalizeOutput, middleware.Metadata, error,
) {
, := .Request.(*smithyhttp.Request)
if ! {
return , , &smithy.SerializationError{
Err: fmt.Errorf("unknown request type %T", .Request),
}
}
.Header.Set(acceptEncodingHeaderKey, "identity")
return .HandleFinalize(, )
}
type EnableGzip struct{}
func (*EnableGzip) () string {
return "AcceptEncodingGzip"
}
func (*EnableGzip) (
context.Context, middleware.FinalizeInput, middleware.FinalizeHandler,
) (
middleware.FinalizeOutput, middleware.Metadata, error,
) {
, := .Request.(*smithyhttp.Request)
if ! {
return , , &smithy.SerializationError{
Err: fmt.Errorf("unknown request type %T", .Request),
}
}
.Header.Set(acceptEncodingHeaderKey, "gzip")
return .HandleFinalize(, )
}
type DecompressGzip struct{}
func (*DecompressGzip) () string {
return "DecompressGzip"
}
func (*DecompressGzip) (
context.Context, middleware.DeserializeInput, middleware.DeserializeHandler,
) (
middleware.DeserializeOutput, middleware.Metadata, error,
) {
, , = .HandleDeserialize(, )
if != nil {
return , ,
}
, := .RawResponse.(*smithyhttp.Response)
if ! {
return , , &smithy.DeserializationError{
Err: fmt.Errorf("unknown response type %T", .RawResponse),
}
}
if := .Header.Get(contentEncodingHeaderKey); != "gzip" {
return , ,
}
.Header.Del("Content-Length")
.ContentLength = -1
.Body = wrapGzipReader(.Body)
return , ,
}
type gzipReader struct {
reader io.ReadCloser
gzip *gzip.Reader
}
func ( io.ReadCloser) *gzipReader {
return &gzipReader{
reader: ,
}
}
func ( *gzipReader) ( []byte) ( int, error) {
if .gzip == nil {
.gzip, = gzip.NewReader(.reader)
if != nil {
.gzip = nil
return 0, fmt.Errorf("failed to decompress gzip response, %w", )
}
}
return .gzip.Read()
}
func ( *gzipReader) () error {
if .gzip == nil {
return nil
}
if := .gzip.Close(); != nil {
.reader.Close()
return fmt.Errorf("failed to decompress gzip response, %w", )
}
return .reader.Close()
}