diff options
Diffstat (limited to 'vendor/github.com/hashicorp/go-getter/get_http.go')
-rw-r--r-- | vendor/github.com/hashicorp/go-getter/get_http.go | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/vendor/github.com/hashicorp/go-getter/get_http.go b/vendor/github.com/hashicorp/go-getter/get_http.go index 3c02034..d2e2879 100644 --- a/vendor/github.com/hashicorp/go-getter/get_http.go +++ b/vendor/github.com/hashicorp/go-getter/get_http.go | |||
@@ -4,12 +4,13 @@ import ( | |||
4 | "encoding/xml" | 4 | "encoding/xml" |
5 | "fmt" | 5 | "fmt" |
6 | "io" | 6 | "io" |
7 | "io/ioutil" | ||
8 | "net/http" | 7 | "net/http" |
9 | "net/url" | 8 | "net/url" |
10 | "os" | 9 | "os" |
11 | "path/filepath" | 10 | "path/filepath" |
12 | "strings" | 11 | "strings" |
12 | |||
13 | "github.com/hashicorp/go-safetemp" | ||
13 | ) | 14 | ) |
14 | 15 | ||
15 | // HttpGetter is a Getter implementation that will download from an HTTP | 16 | // HttpGetter is a Getter implementation that will download from an HTTP |
@@ -36,6 +37,10 @@ type HttpGetter struct { | |||
36 | // Netrc, if true, will lookup and use auth information found | 37 | // Netrc, if true, will lookup and use auth information found |
37 | // in the user's netrc file if available. | 38 | // in the user's netrc file if available. |
38 | Netrc bool | 39 | Netrc bool |
40 | |||
41 | // Client is the http.Client to use for Get requests. | ||
42 | // This defaults to a cleanhttp.DefaultClient if left unset. | ||
43 | Client *http.Client | ||
39 | } | 44 | } |
40 | 45 | ||
41 | func (g *HttpGetter) ClientMode(u *url.URL) (ClientMode, error) { | 46 | func (g *HttpGetter) ClientMode(u *url.URL) (ClientMode, error) { |
@@ -57,13 +62,17 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error { | |||
57 | } | 62 | } |
58 | } | 63 | } |
59 | 64 | ||
65 | if g.Client == nil { | ||
66 | g.Client = httpClient | ||
67 | } | ||
68 | |||
60 | // Add terraform-get to the parameter. | 69 | // Add terraform-get to the parameter. |
61 | q := u.Query() | 70 | q := u.Query() |
62 | q.Add("terraform-get", "1") | 71 | q.Add("terraform-get", "1") |
63 | u.RawQuery = q.Encode() | 72 | u.RawQuery = q.Encode() |
64 | 73 | ||
65 | // Get the URL | 74 | // Get the URL |
66 | resp, err := http.Get(u.String()) | 75 | resp, err := g.Client.Get(u.String()) |
67 | if err != nil { | 76 | if err != nil { |
68 | return err | 77 | return err |
69 | } | 78 | } |
@@ -98,7 +107,18 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error { | |||
98 | } | 107 | } |
99 | 108 | ||
100 | func (g *HttpGetter) GetFile(dst string, u *url.URL) error { | 109 | func (g *HttpGetter) GetFile(dst string, u *url.URL) error { |
101 | resp, err := http.Get(u.String()) | 110 | if g.Netrc { |
111 | // Add auth from netrc if we can | ||
112 | if err := addAuthFromNetrc(u); err != nil { | ||
113 | return err | ||
114 | } | ||
115 | } | ||
116 | |||
117 | if g.Client == nil { | ||
118 | g.Client = httpClient | ||
119 | } | ||
120 | |||
121 | resp, err := g.Client.Get(u.String()) | ||
102 | if err != nil { | 122 | if err != nil { |
103 | return err | 123 | return err |
104 | } | 124 | } |
@@ -116,29 +136,40 @@ func (g *HttpGetter) GetFile(dst string, u *url.URL) error { | |||
116 | if err != nil { | 136 | if err != nil { |
117 | return err | 137 | return err |
118 | } | 138 | } |
119 | defer f.Close() | ||
120 | 139 | ||
121 | _, err = io.Copy(f, resp.Body) | 140 | n, err := io.Copy(f, resp.Body) |
141 | if err == nil && n < resp.ContentLength { | ||
142 | err = io.ErrShortWrite | ||
143 | } | ||
144 | if err1 := f.Close(); err == nil { | ||
145 | err = err1 | ||
146 | } | ||
122 | return err | 147 | return err |
123 | } | 148 | } |
124 | 149 | ||
125 | // getSubdir downloads the source into the destination, but with | 150 | // getSubdir downloads the source into the destination, but with |
126 | // the proper subdir. | 151 | // the proper subdir. |
127 | func (g *HttpGetter) getSubdir(dst, source, subDir string) error { | 152 | func (g *HttpGetter) getSubdir(dst, source, subDir string) error { |
128 | // Create a temporary directory to store the full source | 153 | // Create a temporary directory to store the full source. This has to be |
129 | td, err := ioutil.TempDir("", "tf") | 154 | // a non-existent directory. |
155 | td, tdcloser, err := safetemp.Dir("", "getter") | ||
130 | if err != nil { | 156 | if err != nil { |
131 | return err | 157 | return err |
132 | } | 158 | } |
133 | defer os.RemoveAll(td) | 159 | defer tdcloser.Close() |
134 | 160 | ||
135 | // Download that into the given directory | 161 | // Download that into the given directory |
136 | if err := Get(td, source); err != nil { | 162 | if err := Get(td, source); err != nil { |
137 | return err | 163 | return err |
138 | } | 164 | } |
139 | 165 | ||
166 | // Process any globbing | ||
167 | sourcePath, err := SubdirGlob(td, subDir) | ||
168 | if err != nil { | ||
169 | return err | ||
170 | } | ||
171 | |||
140 | // Make sure the subdir path actually exists | 172 | // Make sure the subdir path actually exists |
141 | sourcePath := filepath.Join(td, subDir) | ||
142 | if _, err := os.Stat(sourcePath); err != nil { | 173 | if _, err := os.Stat(sourcePath); err != nil { |
143 | return fmt.Errorf( | 174 | return fmt.Errorf( |
144 | "Error downloading %s: %s", source, err) | 175 | "Error downloading %s: %s", source, err) |