Skip to content

Commit

Permalink
check proto for http getSubdir too
Browse files Browse the repository at this point in the history
Use the same protocol switching checks when there's a subdir, passing
the limited set of getters if there is wrapping client.
  • Loading branch information
jbardin committed Jun 8, 2022
1 parent 28c40be commit cd6aaf8
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions get_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,6 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
// If there is a subdir component, then we download the root separately
// into a temporary directory, then copy over the proper subdir.
source, subDir := SourceDirSubdir(source)
if subDir != "" {
// We have a subdir, time to jump some hoops
return g.getSubdir(ctx, dst, source, subDir)
}

var opts []ClientOption

Expand Down Expand Up @@ -333,17 +329,29 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
opts = g.client.Options
}

// If the client is nil, we know we're using the HttpGetter directly. In this case,
// we don't know exactly which protocols are configued, but we can make a good guess.
// If the client is nil, we know we're using the HttpGetter directly. In
// this case, we don't know exactly which protocols are configured, but we
// can make a good guess.
//
// This prevents all default getters from being allowed when only using the
// HttpGetter directly. To enable protocol switching, a client "wrapper" must
// be used.
if g.client == nil {
opts = append(opts, WithGetters(map[string]Getter{
"http": g,
"https": g,
}))
switch {
case subDir != "":
// If there's a subdirectory, we will also need a file getter to
// unpack it.
opts = append(opts, WithGetters(map[string]Getter{
"file": new(FileGetter),
"http": g,
"https": g,
}))
default:
opts = append(opts, WithGetters(map[string]Getter{
"http": g,
"https": g,
}))
}
}

// Ensure we pass along the context we constructed in this function.
Expand All @@ -352,6 +360,11 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
// which could be setup, if configured, at the top of this function.
opts = append(opts, WithContext(ctx))

if subDir != "" {
// We have a subdir, time to jump some hoops
return g.getSubdir(ctx, dst, source, subDir, opts...)
}

// Note: this allows the protocol to be switched to another configured getters.
return Get(dst, source, opts...)
}
Expand Down Expand Up @@ -495,7 +508,7 @@ func (g *HttpGetter) GetFile(dst string, src *url.URL) error {

// getSubdir downloads the source into the destination, but with
// the proper subdir.
func (g *HttpGetter) getSubdir(ctx context.Context, dst, source, subDir string) error {
func (g *HttpGetter) getSubdir(ctx context.Context, dst, source, subDir string, opts ...ClientOption) error {
// Create a temporary directory to store the full source. This has to be
// a non-existent directory.
td, tdcloser, err := safetemp.Dir("", "getter")
Expand All @@ -504,10 +517,6 @@ func (g *HttpGetter) getSubdir(ctx context.Context, dst, source, subDir string)
}
defer tdcloser.Close()

var opts []ClientOption
if g.client != nil {
opts = g.client.Options
}
// Download that into the given directory
if err := Get(td, source, opts...); err != nil {
return err
Expand Down

0 comments on commit cd6aaf8

Please sign in to comment.