mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-12 15:50:15 -08:00
feat: prepare for config scanning (#1005)
* temp: disable config scanning
This commit is contained in:
77
rpc/cache/service.twirp.go
vendored
77
rpc/cache/service.twirp.go
vendored
@@ -17,23 +17,19 @@ import fmt "fmt"
|
||||
import ioutil "io/ioutil"
|
||||
import http "net/http"
|
||||
import strconv "strconv"
|
||||
import gzip "compress/gzip"
|
||||
|
||||
import jsonpb "github.com/golang/protobuf/jsonpb"
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import twirp "github.com/twitchtv/twirp"
|
||||
import ctxsetters "github.com/twitchtv/twirp/ctxsetters"
|
||||
|
||||
import google_protobuf1 "github.com/golang/protobuf/ptypes/empty"
|
||||
import google_protobuf1 "google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
// Imports only used by utility functions:
|
||||
import io "io"
|
||||
import json "encoding/json"
|
||||
import url "net/url"
|
||||
|
||||
// A response is compressed with gzip when the response size exceeds this threshold.
|
||||
const CompressThreshold = 10000
|
||||
|
||||
// ===============
|
||||
// Cache Interface
|
||||
// ===============
|
||||
@@ -420,16 +416,6 @@ func (s *cacheServer) servePutArtifactProtobuf(ctx context.Context, resp http.Re
|
||||
return
|
||||
}
|
||||
|
||||
// Compress the response if the size exceeds the threshold
|
||||
if len(respBytes) > CompressThreshold && isGZipAcceptable(req) {
|
||||
respBytes, err = compressWithGzip(respBytes)
|
||||
if err != nil {
|
||||
s.writeError(ctx, resp, wrapInternal(err, "failed to compress response"))
|
||||
return
|
||||
}
|
||||
resp.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
|
||||
ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK)
|
||||
resp.Header().Set("Content-Type", "application/protobuf")
|
||||
resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes)))
|
||||
@@ -559,16 +545,6 @@ func (s *cacheServer) servePutBlobProtobuf(ctx context.Context, resp http.Respon
|
||||
return
|
||||
}
|
||||
|
||||
// Compress the response if the size exceeds the threshold
|
||||
if len(respBytes) > CompressThreshold && isGZipAcceptable(req) {
|
||||
respBytes, err = compressWithGzip(respBytes)
|
||||
if err != nil {
|
||||
s.writeError(ctx, resp, wrapInternal(err, "failed to compress response"))
|
||||
return
|
||||
}
|
||||
resp.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
|
||||
ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK)
|
||||
resp.Header().Set("Content-Type", "application/protobuf")
|
||||
resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes)))
|
||||
@@ -698,16 +674,6 @@ func (s *cacheServer) serveMissingBlobsProtobuf(ctx context.Context, resp http.R
|
||||
return
|
||||
}
|
||||
|
||||
// Compress the response if the size exceeds the threshold
|
||||
if len(respBytes) > CompressThreshold && isGZipAcceptable(req) {
|
||||
respBytes, err = compressWithGzip(respBytes)
|
||||
if err != nil {
|
||||
s.writeError(ctx, resp, wrapInternal(err, "failed to compress response"))
|
||||
return
|
||||
}
|
||||
resp.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
|
||||
ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK)
|
||||
resp.Header().Set("Content-Type", "application/protobuf")
|
||||
resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes)))
|
||||
@@ -866,7 +832,6 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType
|
||||
}
|
||||
req.Header.Set("Accept", contentType)
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
req.Header.Set("Accept-Encoding", "gzip")
|
||||
req.Header.Set("Twirp-Version", "v5.10.1")
|
||||
return req, nil
|
||||
}
|
||||
@@ -1119,15 +1084,7 @@ func doProtobufRequest(ctx context.Context, client HTTPClient, hooks *twirp.Clie
|
||||
return errorFromResponse(resp)
|
||||
}
|
||||
|
||||
r := resp.Body
|
||||
if resp.Header.Get("Content-Encoding") == "gzip" {
|
||||
r, err = gzip.NewReader(r)
|
||||
if err != nil {
|
||||
return wrapInternal(err, "invalid gzip")
|
||||
}
|
||||
}
|
||||
|
||||
respBodyBytes, err := ioutil.ReadAll(r)
|
||||
respBodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return wrapInternal(err, "failed to read response body")
|
||||
}
|
||||
@@ -1232,36 +1189,6 @@ func callError(ctx context.Context, h *twirp.ServerHooks, err twirp.Error) conte
|
||||
return h.Error(ctx, err)
|
||||
}
|
||||
|
||||
// compressWithGzip compresses the data with gzip
|
||||
func compressWithGzip(data []byte) ([]byte, error) {
|
||||
var b bytes.Buffer
|
||||
gz := gzip.NewWriter(&b)
|
||||
defer gz.Close()
|
||||
|
||||
if _, err := gz.Write(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := gz.Flush(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := gz.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func isGZipAcceptable(request *http.Request) bool {
|
||||
for _, encoding := range request.Header["Accept-Encoding"] {
|
||||
if encoding == "gzip" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func callClientResponseReceived(ctx context.Context, h *twirp.ClientHooks) {
|
||||
if h == nil || h.ResponseReceived == nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user