mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-05-20 08:04:52 +00:00
Some checks failed
Post / coverage (push) Failing after 26m56s
Post / images (amd64) (push) Failing after 6m52s
Post / images (arm64) (push) Failing after 6m50s
Post / image manifest (push) Has been skipped
Post / trigger clusteradm e2e (push) Has been skipped
Close stale issues and PRs / stale (push) Successful in 54s
Scorecard supply-chain security / Scorecard analysis (push) Failing after 1m19s
* Acceping AWS IRSA registraion request Signed-off-by: Gaurav Jaswal <jaswalkiranavtar@gmail.com> * Addressing comments Signed-off-by: Gaurav Jaswal <jaswalkiranavtar@gmail.com> * Addressing comments Signed-off-by: Gaurav Jaswal <jaswalkiranavtar@gmail.com> * Making csr as a default enabled driver if no other driver is explicitly enabled Signed-off-by: Gaurav Jaswal <jaswalkiranavtar@gmail.com> --------- Signed-off-by: Gaurav Jaswal <jaswalkiranavtar@gmail.com> Co-authored-by: “Jeffrey <jeffreywong0417@gmail.com> Co-authored-by: Gaurav Jaswal <jaswalkiranavtar@gmail.com>
104 lines
3.3 KiB
Go
104 lines
3.3 KiB
Go
// Package requestcompression implements runtime support for smithy-modeled
|
|
// request compression.
|
|
//
|
|
// This package is designated as private and is intended for use only by the
|
|
// smithy client runtime. The exported API therein is not considered stable and
|
|
// is subject to breaking changes without notice.
|
|
package requestcompression
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"github.com/aws/smithy-go/middleware"
|
|
"github.com/aws/smithy-go/transport/http"
|
|
"io"
|
|
)
|
|
|
|
const MaxRequestMinCompressSizeBytes = 10485760
|
|
|
|
// Enumeration values for supported compress Algorithms.
|
|
const (
|
|
GZIP = "gzip"
|
|
)
|
|
|
|
type compressFunc func(io.Reader) ([]byte, error)
|
|
|
|
var allowedAlgorithms = map[string]compressFunc{
|
|
GZIP: gzipCompress,
|
|
}
|
|
|
|
// AddRequestCompression add requestCompression middleware to op stack
|
|
func AddRequestCompression(stack *middleware.Stack, disabled bool, minBytes int64, algorithms []string) error {
|
|
return stack.Serialize.Add(&requestCompression{
|
|
disableRequestCompression: disabled,
|
|
requestMinCompressSizeBytes: minBytes,
|
|
compressAlgorithms: algorithms,
|
|
}, middleware.After)
|
|
}
|
|
|
|
type requestCompression struct {
|
|
disableRequestCompression bool
|
|
requestMinCompressSizeBytes int64
|
|
compressAlgorithms []string
|
|
}
|
|
|
|
// ID returns the ID of the middleware
|
|
func (m requestCompression) ID() string {
|
|
return "RequestCompression"
|
|
}
|
|
|
|
// HandleSerialize gzip compress the request's stream/body if enabled by config fields
|
|
func (m requestCompression) HandleSerialize(
|
|
ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
|
|
) (
|
|
out middleware.SerializeOutput, metadata middleware.Metadata, err error,
|
|
) {
|
|
if m.disableRequestCompression {
|
|
return next.HandleSerialize(ctx, in)
|
|
}
|
|
// still need to check requestMinCompressSizeBytes in case it is out of range after service client config
|
|
if m.requestMinCompressSizeBytes < 0 || m.requestMinCompressSizeBytes > MaxRequestMinCompressSizeBytes {
|
|
return out, metadata, fmt.Errorf("invalid range for min request compression size bytes %d, must be within 0 and 10485760 inclusively", m.requestMinCompressSizeBytes)
|
|
}
|
|
|
|
req, ok := in.Request.(*http.Request)
|
|
if !ok {
|
|
return out, metadata, fmt.Errorf("unknown request type %T", req)
|
|
}
|
|
|
|
for _, algorithm := range m.compressAlgorithms {
|
|
compressFunc := allowedAlgorithms[algorithm]
|
|
if compressFunc != nil {
|
|
if stream := req.GetStream(); stream != nil {
|
|
size, found, err := req.StreamLength()
|
|
if err != nil {
|
|
return out, metadata, fmt.Errorf("error while finding request stream length, %v", err)
|
|
} else if !found || size < m.requestMinCompressSizeBytes {
|
|
return next.HandleSerialize(ctx, in)
|
|
}
|
|
|
|
compressedBytes, err := compressFunc(stream)
|
|
if err != nil {
|
|
return out, metadata, fmt.Errorf("failed to compress request stream, %v", err)
|
|
}
|
|
|
|
var newReq *http.Request
|
|
if newReq, err = req.SetStream(bytes.NewReader(compressedBytes)); err != nil {
|
|
return out, metadata, fmt.Errorf("failed to set request stream, %v", err)
|
|
}
|
|
*req = *newReq
|
|
|
|
if val := req.Header.Get("Content-Encoding"); val != "" {
|
|
req.Header.Set("Content-Encoding", fmt.Sprintf("%s, %s", val, algorithm))
|
|
} else {
|
|
req.Header.Set("Content-Encoding", algorithm)
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
return next.HandleSerialize(ctx, in)
|
|
}
|