Skip to content

Commit

Permalink
Support Remote Asset API HTTP headers, supported in bazel 7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhenglun authored and mostynb committed Jun 23, 2024
1 parent 2dcfbcf commit 6cc2a13
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions server/grpc_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (s *grpcServer) FetchBlob(ctx context.Context, req *asset.FetchBlobRequest)
return nil, errNilFetchBlobRequest
}

headers := http.Header{}

for _, q := range req.GetQualifiers() {
if q == nil {
return &asset.FetchBlobResponse{
Expand All @@ -66,6 +68,14 @@ func (s *grpcServer) FetchBlob(ctx context.Context, req *asset.FetchBlobRequest)
}, nil
}

const QualifierHTTPHeaderPrefix = "http_header:"
if strings.HasPrefix(q.Name, QualifierHTTPHeaderPrefix) {
key := q.Name[len(QualifierHTTPHeaderPrefix):]

headers[key] = strings.Split(q.Value, ",")
continue
}

if q.Name == "checksum.sri" && strings.HasPrefix(q.Value, "sha256-") {
// Ref: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

Expand Down Expand Up @@ -114,7 +124,7 @@ func (s *grpcServer) FetchBlob(ctx context.Context, req *asset.FetchBlobRequest)
// See if we can download one of the URIs.

for _, uri := range req.GetUris() {
ok, actualHash, size := s.fetchItem(ctx, uri, sha256Str)
ok, actualHash, size := s.fetchItem(ctx, uri, headers, sha256Str)
if ok {
return &asset.FetchBlobResponse{
Status: &status.Status{Code: int32(codes.OK)},
Expand All @@ -134,7 +144,7 @@ func (s *grpcServer) FetchBlob(ctx context.Context, req *asset.FetchBlobRequest)
}, nil
}

func (s *grpcServer) fetchItem(ctx context.Context, uri string, expectedHash string) (bool, string, int64) {
func (s *grpcServer) fetchItem(ctx context.Context, uri string, headers http.Header, expectedHash string) (bool, string, int64) {
u, err := url.Parse(uri)
if err != nil {
s.errorLogger.Printf("unable to parse URI: %s err: %v", uri, err)
Expand All @@ -146,7 +156,15 @@ func (s *grpcServer) fetchItem(ctx context.Context, uri string, expectedHash str
return false, "", int64(-1)
}

resp, err := http.Get(uri)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
s.errorLogger.Printf("failed to new Request: %s err: %v", uri, err)
return false, "", int64(-1)
}

req.Header = headers

resp, err := http.DefaultClient.Do(req)
if err != nil {
s.errorLogger.Printf("failed to get URI: %s err: %v", uri, err)
return false, "", int64(-1)
Expand Down

0 comments on commit 6cc2a13

Please sign in to comment.