diff --git a/weed/util/http/http_global_client_util.go b/weed/util/http/http_global_client_util.go index 24e38f8c7..1a78bd412 100644 --- a/weed/util/http/http_global_client_util.go +++ b/weed/util/http/http_global_client_util.go @@ -397,6 +397,9 @@ func ReadUrlAsStream(ctx context.Context, fileUrl, jwt string, cipherKey []byte, switch contentEncoding { case "gzip": reader, err = gzip.NewReader(r.Body) + if err != nil { + return true, err + } defer reader.Close() default: reader = r.Body diff --git a/weed/util/http/http_global_client_util_test.go b/weed/util/http/http_global_client_util_test.go index f24bd5aca..cc17301e3 100644 --- a/weed/util/http/http_global_client_util_test.go +++ b/weed/util/http/http_global_client_util_test.go @@ -1,6 +1,11 @@ package http -import "testing" +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) func TestAppendQueryParameter(t *testing.T) { testCases := []struct { @@ -70,3 +75,19 @@ func TestAppendQueryParameter(t *testing.T) { }) } } + +func TestReadUrlAsStreamReturnsGzipReaderError(t *testing.T) { + InitGlobalHttpClient() + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Encoding", "gzip") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("not gzip")) + })) + defer server.Close() + + _, err := ReadUrlAsStream(context.Background(), server.URL, "", nil, false, true, 0, 0, func(data []byte) {}) + if err == nil { + t.Fatal("ReadUrlAsStream returned nil error for invalid gzip response") + } +}