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

Fix support resumable + fix incorrect handling of paths which start with a / #918

Merged
merged 15 commits into from
Sep 19, 2022
1 change: 1 addition & 0 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (s *Server) buildMuxer() {
s.mux.Host(bucketHost).Path("/{objectName:.+}").Methods(http.MethodGet, http.MethodHead).HandlerFunc(s.downloadObject)
s.mux.Path("/download/storage/v1/b/{bucketName}/o/{objectName:.+}").Methods(http.MethodGet).HandlerFunc(s.downloadObject)
s.mux.Path("/upload/storage/v1/b/{bucketName}/o").Methods(http.MethodPost).HandlerFunc(jsonToHTTPHandler(s.insertObject))
s.mux.Path("/upload/storage/v1/b/{bucketName}/o").Methods(http.MethodPut).HandlerFunc(jsonToHTTPHandler(s.uploadFileContent))
s.mux.Path("/upload/resumable/{uploadId}").Methods(http.MethodPut, http.MethodPost).HandlerFunc(jsonToHTTPHandler(s.uploadFileContent))

// Batch endpoint
Expand Down
27 changes: 21 additions & 6 deletions fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,19 @@ func (s *Server) checkUploadPreconditions(r *http.Request, bucketName string, ob
return nil
}

func normalizeFileName(name string) string {
for {
name = strings.Replace(name, "/", "", 1)
if !strings.HasPrefix(name, "/") {
break
}
}
return name
le0pard marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *Server) simpleUpload(bucketName string, r *http.Request) jsonResponse {
defer r.Body.Close()
name := r.URL.Query().Get("name")
name := normalizeFileName(r.URL.Query().Get("name"))
predefinedACL := r.URL.Query().Get("predefinedAcl")
contentEncoding := r.URL.Query().Get("contentEncoding")
if name == "" {
Expand Down Expand Up @@ -332,7 +342,7 @@ func (s *Server) multipartUpload(bucketName string, r *http.Request) jsonRespons
return jsonResponse{errorMessage: err.Error()}
}

objName := r.URL.Query().Get("name")
objName := normalizeFileName(r.URL.Query().Get("name"))
predefinedACL := r.URL.Query().Get("predefinedAcl")
if objName == "" {
objName = metadata.Name
Expand Down Expand Up @@ -366,13 +376,17 @@ func (s *Server) resumableUpload(bucketName string, r *http.Request) jsonRespons
contentEncoding := r.URL.Query().Get("contentEncoding")
metadata := new(multipartMetadata)
if r.Body != http.NoBody {
if r.URL.Query().Has("upload_id") {
return s.uploadFileContent(r)
}

var err error
metadata, err = loadMetadata(r.Body)
if err != nil {
return jsonResponse{errorMessage: err.Error()}
}
}
objName := r.URL.Query().Get("name")
objName := normalizeFileName(r.URL.Query().Get("name"))
if objName == "" {
objName = metadata.Name
}
Expand All @@ -391,9 +405,10 @@ func (s *Server) resumableUpload(bucketName string, r *http.Request) jsonRespons
}
s.uploads.Store(uploadID, obj)
header := make(http.Header)
header.Set("Location", s.URL()+"/upload/resumable/"+uploadID)
location := s.URL() + "/upload/storage/v1/b/" + bucketName + "/o?uploadType=resumable&name=" + objName + "&upload_id=" + uploadID
header.Set("Location", location)
if r.Header.Get("X-Goog-Upload-Command") == "start" {
header.Set("X-Goog-Upload-URL", s.URL()+"/upload/resumable/"+uploadID)
header.Set("X-Goog-Upload-URL", location)
header.Set("X-Goog-Upload-Status", "active")
}
return jsonResponse{
Expand Down Expand Up @@ -438,7 +453,7 @@ func (s *Server) resumableUpload(bucketName string, r *http.Request) jsonRespons
// then has a status of "200 OK", with a header "X-Http-Status-Code-Override"
// set to "308".
func (s *Server) uploadFileContent(r *http.Request) jsonResponse {
uploadID := mux.Vars(r)["uploadId"]
uploadID := r.URL.Query().Get("upload_id")
rawObj, ok := s.uploads.Load(uploadID)
if !ok {
return jsonResponse{status: http.StatusNotFound}
Expand Down