15 safetemp "github.com/hashicorp/go-safetemp"
18 // HttpGetter is a Getter implementation that will download from an HTTP
21 // For file downloads, HTTP is used directly.
23 // The protocol for downloading a directory from an HTTP endpoint is as follows:
25 // An HTTP GET request is made to the URL with the additional GET parameter
26 // "terraform-get=1". This lets you handle that scenario specially if you
27 // wish. The response must be a 2xx.
29 // First, a header is looked for "X-Terraform-Get" which should contain
30 // a source URL to download.
32 // If the header is not present, then a meta tag is searched for named
33 // "terraform-get" and the content should be a source URL.
35 // The source URL, whether from the header or meta tag, must be a fully
36 // formed URL. The shorthand syntax of "github.com/foo/bar" or relative
37 // paths are not allowed.
38 type HttpGetter struct {
41 // Netrc, if true, will lookup and use auth information found
42 // in the user's netrc file if available.
45 // Client is the http.Client to use for Get requests.
46 // This defaults to a cleanhttp.DefaultClient if left unset.
49 // Header contains optional request header fields that should be included
50 // with every HTTP request. Note that the zero value of this field is nil,
51 // and as such it needs to be initialized before use, via something like
56 func (g *HttpGetter) ClientMode(u *url.URL) (ClientMode, error) {
57 if strings.HasSuffix(u.Path, "/") {
58 return ClientModeDir, nil
60 return ClientModeFile, nil
63 func (g *HttpGetter) Get(dst string, u *url.URL) error {
65 // Copy the URL so we can modify it
70 // Add auth from netrc if we can
71 if err := addAuthFromNetrc(u); err != nil {
80 // Add terraform-get to the parameter.
82 q.Add("terraform-get", "1")
83 u.RawQuery = q.Encode()
86 req, err := http.NewRequest("GET", u.String(), nil)
92 resp, err := g.Client.Do(req)
97 defer resp.Body.Close()
98 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
99 return fmt.Errorf("bad response code: %d", resp.StatusCode)
102 // Extract the source URL
104 if v := resp.Header.Get("X-Terraform-Get"); v != "" {
107 source, err = g.parseMeta(resp.Body)
113 return fmt.Errorf("no source URL was returned")
116 // If there is a subdir component, then we download the root separately
117 // into a temporary directory, then copy over the proper subdir.
118 source, subDir := SourceDirSubdir(source)
120 var opts []ClientOption
122 opts = g.client.Options
124 return Get(dst, source, opts...)
127 // We have a subdir, time to jump some hoops
128 return g.getSubdir(ctx, dst, source, subDir)
131 func (g *HttpGetter) GetFile(dst string, src *url.URL) error {
134 // Add auth from netrc if we can
135 if err := addAuthFromNetrc(src); err != nil {
140 // Create all the parent directories if needed
141 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
145 f, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, os.FileMode(0666))
152 g.Client = httpClient
155 var currentFileSize int64
157 // We first make a HEAD request so we can check
158 // if the server supports range queries. If the server/URL doesn't
159 // support HEAD requests, we just fall back to GET.
160 req, err := http.NewRequest("HEAD", src.String(), nil)
165 req.Header = g.Header
167 headResp, err := g.Client.Do(req)
168 if err == nil && headResp != nil {
169 headResp.Body.Close()
170 if headResp.StatusCode == 200 {
171 // If the HEAD request succeeded, then attempt to set the range
173 if headResp.Header.Get("Accept-Ranges") == "bytes" {
174 if fi, err := f.Stat(); err == nil {
175 if _, err = f.Seek(0, os.SEEK_END); err == nil {
176 req.Header.Set("Range", fmt.Sprintf("bytes=%d-", fi.Size()))
177 currentFileSize = fi.Size()
178 totalFileSize, _ := strconv.ParseInt(headResp.Header.Get("Content-Length"), 10, 64)
179 if currentFileSize >= totalFileSize {
180 // file already present
190 resp, err := g.Client.Do(req)
194 switch resp.StatusCode {
195 case http.StatusOK, http.StatusPartialContent:
199 return fmt.Errorf("bad response code: %d", resp.StatusCode)
204 if g.client != nil && g.client.ProgressListener != nil {
206 fn := filepath.Base(src.EscapedPath())
207 body = g.client.ProgressListener.TrackProgress(fn, currentFileSize, currentFileSize+resp.ContentLength, resp.Body)
211 n, err := Copy(ctx, f, body)
212 if err == nil && n < resp.ContentLength {
213 err = io.ErrShortWrite
218 // getSubdir downloads the source into the destination, but with
219 // the proper subdir.
220 func (g *HttpGetter) getSubdir(ctx context.Context, dst, source, subDir string) error {
221 // Create a temporary directory to store the full source. This has to be
222 // a non-existent directory.
223 td, tdcloser, err := safetemp.Dir("", "getter")
227 defer tdcloser.Close()
229 var opts []ClientOption
231 opts = g.client.Options
233 // Download that into the given directory
234 if err := Get(td, source, opts...); err != nil {
238 // Process any globbing
239 sourcePath, err := SubdirGlob(td, subDir)
244 // Make sure the subdir path actually exists
245 if _, err := os.Stat(sourcePath); err != nil {
247 "Error downloading %s: %s", source, err)
250 // Copy the subdirectory into our actual destination.
251 if err := os.RemoveAll(dst); err != nil {
255 // Make the final destination
256 if err := os.MkdirAll(dst, 0755); err != nil {
260 return copyDir(ctx, dst, sourcePath, false)
263 // parseMeta looks for the first meta tag in the given reader that
264 // will give us the source URL.
265 func (g *HttpGetter) parseMeta(r io.Reader) (string, error) {
266 d := xml.NewDecoder(r)
267 d.CharsetReader = charsetReader
279 if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
282 if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
285 e, ok := t.(xml.StartElement)
286 if !ok || !strings.EqualFold(e.Name.Local, "meta") {
289 if attrValue(e.Attr, "name") != "terraform-get" {
292 if f := attrValue(e.Attr, "content"); f != "" {
298 // attrValue returns the attribute value for the case-insensitive key
299 // `name', or the empty string if nothing is found.
300 func attrValue(attrs []xml.Attr, name string) string {
301 for _, a := range attrs {
302 if strings.EqualFold(a.Name.Local, name) {
309 // charsetReader returns a reader for the given charset. Currently
310 // it only supports UTF-8 and ASCII. Otherwise, it returns a meaningful
311 // error which is printed by go get, so the user can find why the package
312 // wasn't downloaded if the encoding is not supported. Note that, in
313 // order to reduce potential errors, ASCII is treated as UTF-8 (i.e. characters
314 // greater than 0x7f are not rejected).
315 func charsetReader(charset string, input io.Reader) (io.Reader, error) {
316 switch strings.ToLower(charset) {
320 return nil, fmt.Errorf("can't decode XML document using charset %q", charset)