httputil

package
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2022 License: Apache-2.0 Imports: 16 Imported by: 2

Documentation

Overview

Package httputil offers functions to read and download files via HTTP.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultTransport specifies the http.RoundTripper that is used for any network traffic, and may be replaced with a dummy implementation for unit testing.
	DefaultTransport = http.DefaultTransport
	// UserAgent is passed to every HTTP request as part of the 'User-Agent' header.
	UserAgent = "Bazelisk"

	// RetryClock is used for waiting between HTTP request retries.
	RetryClock = Clock(&realClock{})
	// MaxRetries specifies how often non-fatally failing HTTP requests should be retried.
	MaxRetries = 4
	// MaxRequestDuration defines the maximum amount of time that a request and its retries may take in total
	MaxRequestDuration = time.Second * 30
)

Functions

func DownloadBinary

func DownloadBinary(originURL, destDir, destFile string) (string, error)

DownloadBinary downloads a file from the given URL into the specified location, marks it executable and returns its full path.

func MaybeDownload

func MaybeDownload(bazeliskHome, url, filename, description, auth string, merger ContentMerger) ([]byte, error)

MaybeDownload downloads a file from the given url and caches the result under bazeliskHome. It skips the download if the file already exists and is not outdated. Parameter ´description´ is only used to provide better error messages. Parameter `auth` is a value of "Authorization" HTTP header.

func ReadRemoteFile

func ReadRemoteFile(url string, auth string) ([]byte, http.Header, error)

ReadRemoteFile returns the contents of the given file, using the supplied Authorization header value, if set. It also returns the HTTP headers. If the request fails with a transient error it will retry the request for at most MaxRetries times. It obeys HTTP headers such as "Retry-After" when calculating the start time of the next attempt. If no such header is present, it uses an exponential backoff strategy.

Types

type Clock added in v1.10.1

type Clock interface {
	Sleep(time.Duration)
	Now() time.Time
}

Clock keeps track of time. It can return the current time, as well as move forward by sleeping for a certain period.

type ContentMerger added in v1.9.0

type ContentMerger func([][]byte) ([]byte, error)

ContentMerger is a function that merges multiple HTTP payloads into a single message.

type FakeTransport added in v1.10.1

type FakeTransport struct {
	// contains filtered or unexported fields
}

FakeTransport represents a fake http.Transport that returns prerecorded responses.

func NewFakeTransport added in v1.10.1

func NewFakeTransport() *FakeTransport

NewFakeTransport creates a new FakeTransport instance without any responses.

func (*FakeTransport) AddResponse added in v1.10.1

func (ft *FakeTransport) AddResponse(url string, status int, body string, headers map[string]string)

AddResponse stores a fake HTTP response for the given URL.

func (*FakeTransport) RoundTrip added in v1.10.1

func (ft *FakeTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip returns a prerecorded response to the given request, if one exists. Otherwise its response indicates 404 - not found.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL