From d0c65b4c5ee2615d35a8727d9a80536cfbe3d0ed Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 5 Aug 2019 19:20:50 +0100 Subject: [PATCH] copyurl: fix copying files that return HTTP errors --- fs/operations/operations.go | 4 +++- fs/operations/operations_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/fs/operations/operations.go b/fs/operations/operations.go index f5d46ba8e..ad5d20b0a 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -1575,11 +1575,13 @@ func RcatSize(ctx context.Context, fdst fs.Fs, dstFileName string, in io.ReadClo func CopyURL(ctx context.Context, fdst fs.Fs, dstFileName string, url string) (dst fs.Object, err error) { client := fshttp.NewClient(fs.Config) resp, err := client.Get(url) - if err != nil { return nil, err } defer fs.CheckClose(resp.Body, &err) + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, errors.Errorf("CopyURL failed: %s", resp.Status) + } return RcatSize(ctx, fdst, dstFileName, resp.Body, resp.ContentLength, time.Now()) } diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index 5aabca1e9..61fa00f98 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -695,7 +695,11 @@ func TestCopyURL(t *testing.T) { fstest.CheckItems(t, r.Fremote) // check when reading from regular HTTP server + status := 0 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if status != 0 { + http.Error(w, "an error ocurred", status) + } _, err := w.Write([]byte(contents)) assert.NoError(t, err) }) @@ -708,6 +712,14 @@ func TestCopyURL(t *testing.T) { fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, nil, fs.ModTimeNotSupported) + // Check an error is returned for a 404 + status = http.StatusNotFound + o, err = operations.CopyURL(context.Background(), r.Fremote, "file1", ts.URL) + require.Error(t, err) + assert.Contains(t, err.Error(), "Not Found") + assert.Nil(t, o) + status = 0 + // check when reading from unverified HTTPS server fs.Config.InsecureSkipVerify = true fshttp.ResetTransport()