Skip to content

Commit

Permalink
check Accept-Ranges bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Jul 11, 2024
1 parent 4193e6d commit 414010f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions storagev2/downloader/download_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestDownloadManagerDownloadDirectory(t *testing.T) {
ioMux := http.NewServeMux()
ioMux.HandleFunc("/test1/file1", func(w http.ResponseWriter, r *http.Request) {
rander := rand.New(rand.NewSource(time.Now().UnixNano()))
w.Header().Set("Accept-Ranges", "bytes")
switch r.Method {
case http.MethodHead:
w.Header().Set("Content-Type", "application/json")
Expand All @@ -83,6 +84,7 @@ func TestDownloadManagerDownloadDirectory(t *testing.T) {
})
ioMux.HandleFunc("/test2/file2", func(w http.ResponseWriter, r *http.Request) {
rander := rand.New(rand.NewSource(time.Now().UnixNano()))
w.Header().Set("Accept-Ranges", "bytes")
switch r.Method {
case http.MethodHead:
w.Header().Set("Content-Type", "application/json")
Expand Down
3 changes: 2 additions & 1 deletion storagev2/downloader/downloaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ func (downloader concurrentDownloader) Download(ctx context.Context, urlsIter UR
return 0, clientv1.ResponseError(headResponse)
}
etag := parseEtag(headResponse.Header.Get("Etag"))
if headResponse.ContentLength < 0 { // 无法确定文件实际大小,发出一个请求下载整个文件,不再使用并行下载
if headResponse.ContentLength < 0 || // 无法确定文件实际大小,发出一个请求下载整个文件,不再使用并行下载
headResponse.Header.Get("Accept-Ranges") != "bytes" { // 必须返回 Accept-Ranges 头,否则不认为可以分片下载
var progress func(uint64)
if onDownloadingProgress := options.OnDownloadingProgress; onDownloadingProgress != nil {
progress = func(downloaded uint64) {
Expand Down
3 changes: 3 additions & 0 deletions storagev2/downloader/downloaders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestConcurrentDownloaderWithSinglePart(t *testing.T) {
case 2:
w.WriteHeader(http.StatusServiceUnavailable)
case 3:
w.Header().Set("Accept-Ranges", "bytes")
switch r.Method {
case http.MethodHead:
w.Header().Set("Etag", "testetag1")
Expand Down Expand Up @@ -144,6 +145,7 @@ func TestConcurrentDownloaderWithCompression(t *testing.T) {
counts += 1
switch id {
case 1:
w.Header().Set("Accept-Ranges", "bytes")
switch r.Method {
case http.MethodHead:
if r.Header.Get("Accept-Encoding") != "" {
Expand Down Expand Up @@ -449,6 +451,7 @@ func TestConcurrentDownloaderWithResumableRecorder(t *testing.T) {
handler := func(id int, w http.ResponseWriter, r *http.Request) {
switch id {
case 1:
w.Header().Set("Accept-Ranges", "bytes")
switch r.Method {
case http.MethodHead:
w.Header().Set("Etag", "testetag1")
Expand Down

0 comments on commit 414010f

Please sign in to comment.