...

Source file src/github.com/sigstore/rekor/pkg/util/fetch.go

Documentation: github.com/sigstore/rekor/pkg/util

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package util
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  )
    25  
    26  // FileOrURLReadCloser Note: caller is responsible for closing ReadCloser returned from method!
    27  func FileOrURLReadCloser(ctx context.Context, url string, content []byte) (io.ReadCloser, error) {
    28  	var dataReader io.ReadCloser
    29  	if url != "" {
    30  		//TODO: set timeout here, SSL settings?
    31  		client := &http.Client{}
    32  		req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		resp, err := client.Do(req)
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  		if resp.StatusCode < 200 || resp.StatusCode > 299 {
    41  			return nil, fmt.Errorf("error received while fetching artifact '%v': %v", url, resp.Status)
    42  		}
    43  
    44  		dataReader = resp.Body
    45  	} else {
    46  		dataReader = io.NopCloser(bytes.NewReader(content))
    47  	}
    48  	return dataReader, nil
    49  }
    50  

View as plain text