Skip to content

Commit

Permalink
Always respond with HTTP 206 for range requests (#511)
Browse files Browse the repository at this point in the history
This matches the GCS behavior.
  • Loading branch information
gaul authored Jun 14, 2021
1 parent a755b8f commit 790a045
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ func (s *Server) downloadObject(w http.ResponseWriter, r *http.Request) {
}

status := http.StatusOK
start, end, content := s.handleRange(obj, r)
if len(content) != len(obj.Content) {
ranged, start, end, content := s.handleRange(obj, r)
if ranged {
status = http.StatusPartialContent
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, len(obj.Content)))
}
Expand All @@ -535,7 +535,7 @@ func (s *Server) downloadObject(w http.ResponseWriter, r *http.Request) {
}
}

func (s *Server) handleRange(obj Object, r *http.Request) (start, end int, content []byte) {
func (s *Server) handleRange(obj Object, r *http.Request) (ranged bool, start, end int, content []byte) {
if reqRange := r.Header.Get("Range"); reqRange != "" {
parts := strings.SplitN(reqRange, "=", 2)
if len(parts) == 2 && parts[0] == "bytes" {
Expand All @@ -551,11 +551,11 @@ func (s *Server) handleRange(obj Object, r *http.Request) (start, end int, conte
if end > len(obj.Content) {
end = len(obj.Content)
}
return start, end, obj.Content[start:end]
return true, start, end, obj.Content[start:end]
}
}
}
return 0, 0, obj.Content
return false, 0, 0, obj.Content
}

func (s *Server) patchObject(r *http.Request) jsonResponse {
Expand Down
4 changes: 2 additions & 2 deletions fakestorage/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ func testDownloadObjectRange(t *testing.T, server *Server) {
}{
{"No range specified", map[string]string{}, http.StatusOK, "something"},
{"Partial range specified", map[string]string{"Range": "bytes=1-4"}, http.StatusPartialContent, "omet"},
{"Exact range specified", map[string]string{"Range": "bytes=0-8"}, http.StatusOK, "something"},
{"Too-long range specified", map[string]string{"Range": "bytes=0-100"}, http.StatusOK, "something"},
{"Exact range specified", map[string]string{"Range": "bytes=0-8"}, http.StatusPartialContent, "something"},
{"Too-long range specified", map[string]string{"Range": "bytes=0-100"}, http.StatusPartialContent, "something"},
}
for _, test := range tests {
test := test
Expand Down

0 comments on commit 790a045

Please sign in to comment.