@@ -6,8 +6,7 @@ package providercache
66import (
77 "context"
88 "fmt"
9- "io/ioutil"
10- "net/http"
9+ "net/url"
1110 "os"
1211 "path/filepath"
1312
@@ -26,54 +25,41 @@ import (
2625var unzip = getter.ZipDecompressor {}
2726
2827func installFromHTTPURL (ctx context.Context , meta getproviders.PackageMeta , targetDir string , allowedHashes []getproviders.Hash ) (* getproviders.PackageAuthenticationResult , error ) {
29- url := meta .Location .String ()
28+ urlStr := meta .Location .String ()
3029
3130 // When we're installing from an HTTP URL we expect the URL to refer to
3231 // a zip file. We'll fetch that into a temporary file here and then
3332 // delegate to installFromLocalArchive below to actually extract it.
34- // (We're not using go-getter here because its HTTP getter has a bunch
35- // of extraneous functionality we don't need or want, like indirection
36- // through X-Terraform-Get header, attempting partial fetches for
37- // files that already exist, etc.)
33+ httpGetter := getter.HttpGetter {
34+ Client : httpclient .New (),
35+ Netrc : true ,
36+ XTerraformGetDisabled : true ,
37+ }
3838
39- httpClient := httpclient .New ()
40- req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
39+ urlObj , err := url .Parse (urlStr )
4140 if err != nil {
41+ // We don't expect to get non-HTTP locations here because we're
42+ // using the registry source, so this seems like a bug in the
43+ // registry source.
4244 return nil , fmt .Errorf ("invalid provider download request: %s" , err )
4345 }
44- resp , err := httpClient .Do (req )
45- if err != nil {
46- if ctx .Err () == context .Canceled {
47- // "context canceled" is not a user-friendly error message,
48- // so we'll return a more appropriate one here.
49- return nil , fmt .Errorf ("provider download was interrupted" )
50- }
51- return nil , fmt .Errorf ("%s: %w" , getproviders .HostFromRequest (req ), err )
52- }
53- defer resp .Body .Close ()
54-
55- if resp .StatusCode != http .StatusOK {
56- return nil , fmt .Errorf ("unsuccessful request to %s: %s" , url , resp .Status )
57- }
58-
59- f , err := ioutil .TempFile ("" , "terraform-provider" )
46+ f , err := os .CreateTemp ("" , "terraform-provider" )
6047 if err != nil {
61- return nil , fmt .Errorf ("failed to open temporary file to download from %s: %w" , url , err )
48+ return nil , fmt .Errorf ("failed to open temporary file to download from %s: %w" , urlStr , err )
6249 }
6350 defer f .Close ()
6451 defer os .Remove (f .Name ())
6552
66- // We'll borrow go-getter's "cancelable copy" implementation here so that
67- // the download can potentially be interrupted partway through.
68- n , err := getter .Copy (ctx , f , resp .Body )
69- if err == nil && n < resp .ContentLength {
70- err = fmt .Errorf ("incorrect response size: expected %d bytes, but got %d bytes" , resp .ContentLength , n )
71- }
53+ archiveFilename := f .Name ()
54+ err = httpGetter .GetFile (archiveFilename , urlObj )
7255 if err != nil {
73- return nil , err
56+ if ctx .Err () == context .Canceled {
57+ // "context canceled" is not a user-friendly error message,
58+ // so we'll return a more appropriate one here.
59+ return nil , fmt .Errorf ("provider download was interrupted" )
60+ }
61+ return nil , fmt .Errorf ("%s: %w" , urlObj .Host , err )
7462 }
75-
76- archiveFilename := f .Name ()
7763 localLocation := getproviders .PackageLocalArchive (archiveFilename )
7864
7965 var authResult * getproviders.PackageAuthenticationResult
0 commit comments