Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 2dee5c0

Browse files
author
慕陶
committed
fix: checkStatusCode after download
Signed-off-by: 慕陶 <[email protected]>
1 parent 0f1a3a5 commit 2dee5c0

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

supernode/daemon/mgr/cdn/downloader.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
// Body which the caller is expected to close.
3636
func (cm *Manager) download(ctx context.Context, taskID, url string, headers map[string]string,
3737
startPieceNum int, httpFileLength int64, pieceContSize int32) (*http.Response, error) {
38-
var checkCode = http.StatusOK | http.StatusPartialContent
38+
checkCode := []int{http.StatusOK, http.StatusPartialContent}
3939

4040
if startPieceNum > 0 {
4141
breakRange, err := util.CalculateBreakRange(startPieceNum, int(pieceContSize), httpFileLength)
@@ -50,9 +50,20 @@ func (cm *Manager) download(ctx context.Context, taskID, url string, headers map
5050
if _, ok := headers["Range"]; !ok {
5151
headers["Range"] = httputils.ConstructRangeStr(breakRange)
5252
}
53-
checkCode = http.StatusPartialContent
53+
checkCode = []int{http.StatusPartialContent}
5454
}
5555

5656
logrus.Infof("start to download for taskId(%s) with fileUrl: %s header: %v checkCode: %d", taskID, url, headers, checkCode)
57-
return cm.originClient.Download(url, headers, checkCode)
57+
return cm.originClient.Download(url, headers, checkStatusCode(checkCode))
58+
}
59+
60+
func checkStatusCode(statusCode []int) func(int) bool {
61+
return func(status int) bool {
62+
for _, s := range statusCode {
63+
if status == s {
64+
return true
65+
}
66+
}
67+
return false
68+
}
5869
}

supernode/daemon/mgr/cdn/downloader_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/ioutil"
2323
"net/http"
2424
"net/http/httptest"
25+
"reflect"
2526
"testing"
2627

2728
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
@@ -114,3 +115,48 @@ func (s *CDNDownloadTestSuite) TestDownload(c *check.C) {
114115
c.Check(string(result), check.Equals, string(v.exceptedBody))
115116
}
116117
}
118+
119+
func Test_checkStatusCode(t *testing.T) {
120+
type args struct {
121+
statusCode []int
122+
targetStatusCode int
123+
}
124+
tests := []struct {
125+
name string
126+
args args
127+
statusCode int
128+
want bool
129+
}{
130+
{
131+
name: "200",
132+
args: args{
133+
statusCode: []int{http.StatusOK},
134+
targetStatusCode: 200,
135+
},
136+
want: true,
137+
},
138+
{
139+
name: "200|206",
140+
args: args{
141+
statusCode: []int{http.StatusOK, http.StatusPartialContent},
142+
targetStatusCode: 206,
143+
},
144+
want: true,
145+
},
146+
{
147+
name: "204",
148+
args: args{
149+
statusCode: []int{http.StatusOK, http.StatusPartialContent},
150+
targetStatusCode: 204,
151+
},
152+
want: false,
153+
},
154+
}
155+
for _, tt := range tests {
156+
t.Run(tt.name, func(t *testing.T) {
157+
if got := checkStatusCode(tt.args.statusCode)(tt.args.targetStatusCode); !reflect.DeepEqual(got, tt.want) {
158+
t.Errorf("checkStatusCode() = %v, want %v", got, tt.want)
159+
}
160+
})
161+
}
162+
}

supernode/httpclient/mock/mock_origin_http_client.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

supernode/httpclient/origin_http_client.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ import (
3535
"github.com/pkg/errors"
3636
)
3737

38+
type StatusCodeChecker func(int) bool
39+
3840
// OriginHTTPClient supply apis that interact with the source.
3941
type OriginHTTPClient interface {
4042
RegisterTLSConfig(rawURL string, insecure bool, caBlock []strfmt.Base64)
4143
GetContentLength(url string, headers map[string]string) (int64, int, error)
4244
IsSupportRange(url string, headers map[string]string) (bool, error)
4345
IsExpired(url string, headers map[string]string, lastModified int64, eTag string) (bool, error)
44-
Download(url string, headers map[string]string, checkCode int) (*http.Response, error)
46+
Download(url string, headers map[string]string, checkCode StatusCodeChecker) (*http.Response, error)
4547
}
4648

4749
// OriginClient is an implementation of the interface of OriginHTTPClient.
@@ -156,14 +158,14 @@ func (client *OriginClient) IsExpired(url string, headers map[string]string, las
156158
}
157159

158160
// Download downloads the file from the original address
159-
func (client *OriginClient) Download(url string, headers map[string]string, checkCode int) (*http.Response, error) {
161+
func (client *OriginClient) Download(url string, headers map[string]string, checkCode StatusCodeChecker) (*http.Response, error) {
160162
// TODO: add timeout
161163
resp, err := client.HTTPWithHeaders("GET", url, headers, 0)
162164
if err != nil {
163165
return nil, err
164166
}
165167

166-
if (resp.StatusCode & checkCode) == resp.StatusCode {
168+
if checkCode(resp.StatusCode) {
167169
return resp, nil
168170
}
169171
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)

0 commit comments

Comments
 (0)