Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support HTTP headers supported in bazel 7.1 #760

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading