Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gzip transcoding #851

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 29 additions & 7 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package fakestorage

import (
"compress/gzip"
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -647,7 +649,7 @@ func (s *Server) downloadObject(w http.ResponseWriter, r *http.Request) {
}

status := http.StatusOK
ranged, start, lastByte, content, satisfiable := s.handleRange(obj, r)
ranged, start, lastByte, content, satisfiable, transcoded := s.handleRange(obj, r)

if ranged && satisfiable {
status = http.StatusPartialContent
Expand All @@ -670,7 +672,8 @@ func (s *Server) downloadObject(w http.ResponseWriter, r *http.Request) {
if obj.ContentType != "" {
w.Header().Set(contentTypeHeader, obj.ContentType)
}
if obj.ContentEncoding != "" {
// If content was transcoded, the underlying encoding was removed so we shouldn't report it.
if obj.ContentEncoding != "" && !transcoded {
w.Header().Set("Content-Encoding", obj.ContentEncoding)
}
}
Expand All @@ -681,12 +684,31 @@ func (s *Server) downloadObject(w http.ResponseWriter, r *http.Request) {
}
}

func (s *Server) handleRange(obj Object, r *http.Request) (ranged bool, start int64, lastByte int64, content []byte, satisfiable bool) {
func (s *Server) handleRange(obj Object, r *http.Request) (ranged bool, start int64, lastByte int64, content []byte, satisfiable bool, transcoded bool) {
// This should also be false if the Cache-Control metadata field == "no-transform",
// but we don't currently support that field.
// See https://cloud.google.com/storage/docs/transcoding
if obj.ContentEncoding == "gzip" && r.Header.Get("accept-encoding") != "gzip" {
// GCS will transparently decompress gzipped content, see
// https://cloud.google.com/storage/docs/transcoding
// In this case, any Range header is ignored and the full content is returned.

// If the content is not a valid gzip file, ignore errors and continue
// without transcoding. Otherwise, return decompressed content.
gzipReader, err := gzip.NewReader(bytes.NewReader(obj.Content))
if err == nil {
content, err := io.ReadAll(gzipReader)
if err == nil {
return false, 0, 0, content, false, true
}
}
}

contentLength := int64(len(obj.Content))
start, end, err := parseRange(r.Header.Get("Range"), contentLength)
if err != nil {
// If the range isn't valid, GCS returns all content.
return false, 0, 0, obj.Content, false
return false, 0, 0, obj.Content, false, false
}
// GCS is pretty flexible when it comes to invalid ranges. A 416 http
// response is only returned when the range start is beyond the length of
Expand All @@ -698,12 +720,12 @@ func (s *Server) handleRange(obj Object, r *http.Request) (ranged bool, start in
// Length: 40, Range: bytes=50-
case start >= contentLength:
// This IS a ranged request, but it ISN'T satisfiable.
return true, 0, 0, []byte{}, false
return true, 0, 0, []byte{}, false, false
// Negative range, ignore range and return all content.
// Examples:
// Length: 40, Range: bytes=30-20
case end < start:
return false, 0, 0, obj.Content, false
return false, 0, 0, obj.Content, false, false
// Return range. Clamp start and end.
// Examples:
// Length: 40, Range: bytes=-100
Expand All @@ -715,7 +737,7 @@ func (s *Server) handleRange(obj Object, r *http.Request) (ranged bool, start in
if end >= contentLength {
end = contentLength - 1
}
return true, start, end, obj.Content[start : end+1], true
return true, start, end, obj.Content[start : end+1], true, false
}
}

Expand Down
4 changes: 4 additions & 0 deletions fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,14 @@ func (s *Server) resumableUpload(bucketName string, r *http.Request) jsonRespons
if objName == "" {
objName = metadata.Name
}
if contentEncoding == "" {
contentEncoding = metadata.ContentEncoding
}
obj := Object{
ObjectAttrs: ObjectAttrs{
BucketName: bucketName,
Name: objName,
ContentType: metadata.ContentType,
ContentEncoding: contentEncoding,
ACL: getObjectACL(predefinedACL),
Metadata: metadata.Metadata,
Expand Down