Skip to content

Commit b5424e4

Browse files
MarkusFreitagfrezbo
authored andcommitted
fix: correctly handle status-code 204
As the status-code 204 is part of the 2xx family, it should also be considered successful signaling that there is no body to read. This fixes #11755 Signed-off-by: Markus Freitag <fmarkus@mailbox.org> Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com> (cherry picked from commit 43b5b9d)
1 parent 71de2e2 commit b5424e4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pkg/download/download.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ func download(req *http.Request, options *downloadOptions) (data []byte, err err
262262
return data, options.ErrorOnBadRequest
263263
}
264264

265+
// 204 - StatusNoContent is also a successful response, signaling that there is no body
266+
if resp.StatusCode == http.StatusNoContent {
267+
return data, options.ErrorOnEmptyResponse
268+
}
269+
265270
if resp.StatusCode != http.StatusOK {
266271
// try to read first 32 bytes of the response body
267272
// to provide more context in case of error

pkg/download/download_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func TestDownload(t *testing.T) {
5959
case "/404":
6060
w.WriteHeader(http.StatusNotFound)
6161
fmt.Fprintln(w, "not found")
62+
case "/204":
63+
w.WriteHeader(http.StatusNoContent)
6264
default:
6365
w.WriteHeader(http.StatusInternalServerError)
6466
}
@@ -87,6 +89,11 @@ func TestDownload(t *testing.T) {
8789
path: "/empty",
8890
expected: "",
8991
},
92+
{
93+
name: "empty download with 204",
94+
path: "/204",
95+
expected: "",
96+
},
9097
{
9198
name: "some data",
9299
path: "/data",
@@ -104,6 +111,12 @@ func TestDownload(t *testing.T) {
104111
opts: []download.Option{download.WithErrorOnEmptyResponse(errors.New("empty response"))},
105112
expectedError: "empty response",
106113
},
114+
{
115+
name: "empty error by 204",
116+
path: "/204",
117+
opts: []download.Option{download.WithErrorOnEmptyResponse(errors.New("empty response"))},
118+
expectedError: "empty response",
119+
},
107120
{
108121
name: "not found error",
109122
path: "/404",

0 commit comments

Comments
 (0)