2018-01-22 19:59:34 -05:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package ctxhttp provides helper functions for performing context-aware HTTP requests.
|
|
|
|
package ctxhttp // import "golang.org/x/net/context/ctxhttp"
|
|
|
|
|
|
|
|
import (
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
"context"
|
2018-01-22 19:59:34 -05:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Do sends an HTTP request with the provided http.Client and returns
|
|
|
|
// an HTTP response.
|
|
|
|
//
|
|
|
|
// If the client is nil, http.DefaultClient is used.
|
|
|
|
//
|
|
|
|
// The provided ctx must be non-nil. If it is canceled or times out,
|
|
|
|
// ctx.Err() will be returned.
|
|
|
|
func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
|
|
|
|
if client == nil {
|
|
|
|
client = http.DefaultClient
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req.WithContext(ctx))
|
|
|
|
// If we got an error, and the context has been canceled,
|
|
|
|
// the context's error is probably more useful.
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get issues a GET request via the Do function.
|
|
|
|
func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Do(ctx, client, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head issues a HEAD request via the Do function.
|
|
|
|
func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest("HEAD", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Do(ctx, client, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post issues a POST request via the Do function.
|
|
|
|
func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequest("POST", url, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", bodyType)
|
|
|
|
return Do(ctx, client, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostForm issues a POST request via the Do function.
|
|
|
|
func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) {
|
|
|
|
return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
|
|
|
}
|