-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5020 from twz123/http-downloader
Replace grab with internal download function
- Loading branch information
Showing
15 changed files
with
1,213 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
Copyright 2024 k0s authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
internalio "github.com/k0sproject/k0s/internal/io" | ||
"github.com/k0sproject/k0s/pkg/build" | ||
"github.com/k0sproject/k0s/pkg/k0scontext" | ||
) | ||
|
||
type DownloadOption func(*downloadOptions) | ||
|
||
// Downloads the contents of the given URL. Writes the HTTP response body to writer. | ||
// Stalled downloads will be aborted if there's no data transfer for some time. | ||
func Download(ctx context.Context, url string, target io.Writer, options ...DownloadOption) (err error) { | ||
opts := downloadOptions{ | ||
stalenessTimeout: time.Minute, | ||
} | ||
for _, opt := range options { | ||
opt(&opts) | ||
} | ||
|
||
// Prepare the client and the request. | ||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
// This is a one-shot HTTP client which should release resources immediately. | ||
DisableKeepAlives: true, | ||
}, | ||
} | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return fmt.Errorf("invalid download request: %w", err) | ||
} | ||
req.Header.Set("User-Agent", "k0s/"+build.Version) | ||
|
||
// Create a context with an inactivity timeout to cancel the download if it stalls. | ||
ctx, cancel, keepAlive := k0scontext.WithInactivityTimeout(ctx, opts.stalenessTimeout) | ||
defer cancel(nil) | ||
|
||
// Execute the request. | ||
resp, err := client.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
if cause := context.Cause(ctx); cause != nil && !errors.Is(err, cause) { | ||
err = fmt.Errorf("%w (%w)", cause, err) | ||
} | ||
return fmt.Errorf("request failed: %w", err) | ||
} | ||
defer func() { | ||
if closeErr := resp.Body.Close(); closeErr != nil { | ||
err = errors.Join(err, closeErr) | ||
} | ||
}() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("bad status: %s", resp.Status) | ||
} | ||
|
||
if err := opts.detectRemoteFileName(resp); err != nil { | ||
return err | ||
} | ||
|
||
// Monitor writes. Keep the download context alive as long as data is flowing. | ||
writeMonitor := internalio.WriterFunc(func(p []byte) (int, error) { | ||
len := len(p) | ||
if len > 0 { | ||
keepAlive() | ||
} | ||
return len, nil | ||
}) | ||
|
||
// Run the actual data transfer. | ||
if _, err := io.Copy(io.MultiWriter(writeMonitor, target), resp.Body); err != nil { | ||
if cause := context.Cause(ctx); cause != nil && !errors.Is(err, cause) { | ||
err = fmt.Errorf("%w (%w)", cause, err) | ||
} | ||
|
||
return fmt.Errorf("while downloading: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Sets the staleness timeout for a download. | ||
// Defaults to one minute if omitted. | ||
func WithStalenessTimeout(stalenessTimeout time.Duration) DownloadOption { | ||
return func(opts *downloadOptions) { | ||
opts.stalenessTimeout = stalenessTimeout | ||
} | ||
} | ||
|
||
type downloadOptions struct { | ||
stalenessTimeout time.Duration | ||
downloadFileNameOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
Copyright 2024 k0s authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package http_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
internalhttp "github.com/k0sproject/k0s/internal/http" | ||
internalio "github.com/k0sproject/k0s/internal/io" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDownload_CancelRequest(t *testing.T) { | ||
ctx, cancel := context.WithCancelCause(context.TODO()) | ||
cancel(assert.AnError) | ||
|
||
err := internalhttp.Download(ctx, "http://404.example.com", io.Discard) | ||
assert.ErrorIs(t, err, assert.AnError) | ||
if urlErr := (*url.Error)(nil); assert.ErrorAs(t, err, &urlErr) { | ||
assert.Equal(t, "Get", urlErr.Op) | ||
assert.Equal(t, "http://404.example.com", urlErr.URL) | ||
assert.Equal(t, context.Canceled, urlErr.Err) | ||
} | ||
} | ||
|
||
func TestDownload_NoContent(t *testing.T) { | ||
baseURL := startFakeDownloadServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNoContent) | ||
})) | ||
err := internalhttp.Download(context.TODO(), baseURL, io.Discard) | ||
assert.ErrorContains(t, err, "bad status: 204 No Content") | ||
} | ||
|
||
func TestDownload_ShortDownload(t *testing.T) { | ||
baseURL := startFakeDownloadServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Content-Length", "10") | ||
_, err := w.Write([]byte("too short")) // this is only 9 bytes | ||
assert.NoError(t, err) | ||
})) | ||
|
||
err := internalhttp.Download(context.TODO(), baseURL, io.Discard) | ||
assert.ErrorContains(t, err, "while downloading: unexpected EOF") | ||
} | ||
|
||
func TestDownload_ExcessContentLength(t *testing.T) { | ||
baseURL := startFakeDownloadServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Content-Length", "4") | ||
_, err := w.Write([]byte("yolo")) | ||
assert.NoError(t, err) | ||
// Excess content length | ||
_, err = w.Write([]byte("<-stripped")) | ||
assert.ErrorIs(t, err, http.ErrContentLength) | ||
})) | ||
|
||
var downloaded strings.Builder | ||
err := internalhttp.Download(context.TODO(), baseURL, &downloaded) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "yolo", downloaded.String()) | ||
} | ||
|
||
func TestDownload_CancelDownload(t *testing.T) { | ||
ctx, cancel := context.WithCancelCause(context.TODO()) | ||
t.Cleanup(func() { cancel(nil) }) | ||
|
||
baseURL := startFakeDownloadServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
for { | ||
if _, err := w.Write([]byte(t.Name())); !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
select { | ||
case <-r.Context().Done(): | ||
return | ||
case <-time.After(time.Microsecond): | ||
} | ||
} | ||
})) | ||
|
||
err := internalhttp.Download(ctx, baseURL, internalio.WriterFunc(func(p []byte) (int, error) { | ||
cancel(assert.AnError) | ||
return len(p), nil | ||
})) | ||
|
||
assert.ErrorContains(t, err, "while downloading: ") | ||
assert.ErrorIs(t, err, assert.AnError) | ||
assert.ErrorIs(t, err, context.Canceled) | ||
} | ||
|
||
func TestDownload_RedirectLoop(t *testing.T) { | ||
// The current implementation doesn't detect loops, but it stops after 10 redirects. | ||
|
||
expectedRequests := uint32(10) | ||
var requests atomic.Uint32 | ||
var baseURL string | ||
baseURL = startFakeDownloadServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if !assert.LessOrEqual(t, requests.Add(1), expectedRequests, "More requests than anticipated") { | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
return | ||
} | ||
|
||
// Produce redirect loops: /looper-0, /looper-1, /looper-2, /looper-0, ... | ||
var seq uint8 | ||
if _, lastSeq, found := strings.Cut(r.URL.Path, "/looper-"); found { | ||
if lastSeq, err := strconv.ParseUint(lastSeq, 10, 4); err == nil && lastSeq < 3 { | ||
seq = uint8(lastSeq) + 1 | ||
} | ||
} | ||
|
||
http.Redirect(w, r, fmt.Sprintf("%s/looper-%d", baseURL, seq), http.StatusSeeOther) | ||
})) | ||
|
||
var downloaded strings.Builder | ||
err := internalhttp.Download(context.TODO(), baseURL, &downloaded) | ||
|
||
assert.Equal(t, expectedRequests, requests.Load()) | ||
assert.ErrorContains(t, err, "stopped after 10 redirects") | ||
} | ||
|
||
func startFakeDownloadServer(t *testing.T, handler http.Handler) string { | ||
server := &http.Server{Addr: "localhost:0", Handler: handler} | ||
listener, err := net.Listen("tcp", server.Addr) | ||
if err != nil { | ||
require.NoError(t, err) | ||
} | ||
|
||
serverError := make(chan error) | ||
go func() { | ||
defer close(serverError) | ||
serverError <- server.Serve(listener) | ||
}() | ||
|
||
t.Cleanup(func() { | ||
err := server.Shutdown(context.Background()) | ||
if !assert.NoError(t, err, "Couldn't shutdown HTTP server") { | ||
return | ||
} | ||
|
||
assert.ErrorIs(t, <-serverError, http.ErrServerClosed, "HTTP server terminated unexpectedly") | ||
}) | ||
|
||
return (&url.URL{Scheme: "http", Host: listener.Addr().String()}).String() | ||
} |
Oops, something went wrong.