Fix #31185 try fix lfs download from bitbucket failed (#31201)

Fix #31185

(cherry picked from commit e25d6960b5749fbf7f88ebb6b27878c0459817da)
This commit is contained in:
Zoupers Zou 2024-06-12 06:22:28 +08:00 committed by Earl Warren
parent 41bea7c23a
commit baad8337f9
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
8 changed files with 16 additions and 13 deletions

View file

@ -211,7 +211,7 @@ func createRequest(ctx context.Context, method, url string, headers map[string]s
for key, value := range headers { for key, value := range headers {
req.Header.Set(key, value) req.Header.Set(key, value)
} }
req.Header.Set("Accept", MediaType) req.Header.Set("Accept", AcceptHeader)
return req, nil return req, nil
} }
@ -251,6 +251,6 @@ func handleErrorResponse(resp *http.Response) error {
return err return err
} }
log.Trace("ErrorResponse: %v", er) log.Trace("ErrorResponse(%v): %v", resp.Status, er)
return errors.New(er.Message) return errors.New(er.Message)
} }

View file

@ -155,7 +155,7 @@ func TestHTTPClientDownload(t *testing.T) {
hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
assert.Equal(t, "POST", req.Method) assert.Equal(t, "POST", req.Method)
assert.Equal(t, MediaType, req.Header.Get("Content-type")) assert.Equal(t, MediaType, req.Header.Get("Content-type"))
assert.Equal(t, MediaType, req.Header.Get("Accept")) assert.Equal(t, AcceptHeader, req.Header.Get("Accept"))
var batchRequest BatchRequest var batchRequest BatchRequest
err := json.NewDecoder(req.Body).Decode(&batchRequest) err := json.NewDecoder(req.Body).Decode(&batchRequest)
@ -263,7 +263,7 @@ func TestHTTPClientUpload(t *testing.T) {
hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
assert.Equal(t, "POST", req.Method) assert.Equal(t, "POST", req.Method)
assert.Equal(t, MediaType, req.Header.Get("Content-type")) assert.Equal(t, MediaType, req.Header.Get("Content-type"))
assert.Equal(t, MediaType, req.Header.Get("Accept")) assert.Equal(t, AcceptHeader, req.Header.Get("Accept"))
var batchRequest BatchRequest var batchRequest BatchRequest
err := json.NewDecoder(req.Body).Decode(&batchRequest) err := json.NewDecoder(req.Body).Decode(&batchRequest)

View file

@ -10,6 +10,8 @@ import (
const ( const (
// MediaType contains the media type for LFS server requests // MediaType contains the media type for LFS server requests
MediaType = "application/vnd.git-lfs+json" MediaType = "application/vnd.git-lfs+json"
// Some LFS servers offer content with other types, so fallback to '*/*' if application/vnd.git-lfs+json cannot be served
AcceptHeader = "application/vnd.git-lfs+json;q=0.9, */*;q=0.8"
) )
// BatchRequest contains multiple requests processed in one batch operation. // BatchRequest contains multiple requests processed in one batch operation.

View file

@ -37,6 +37,7 @@ func (a *BasicTransferAdapter) Download(ctx context.Context, l *Link) (io.ReadCl
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Debug("Download Request: %+v", req)
resp, err := performRequest(ctx, a.client, req) resp, err := performRequest(ctx, a.client, req)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -26,7 +26,7 @@ func TestBasicTransferAdapter(t *testing.T) {
p := Pointer{Oid: "b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259", Size: 5} p := Pointer{Oid: "b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259", Size: 5}
roundTripHandler := func(req *http.Request) *http.Response { roundTripHandler := func(req *http.Request) *http.Response {
assert.Equal(t, MediaType, req.Header.Get("Accept")) assert.Equal(t, AcceptHeader, req.Header.Get("Accept"))
assert.Equal(t, "test-value", req.Header.Get("test-header")) assert.Equal(t, "test-value", req.Header.Get("test-header"))
url := req.URL.String() url := req.URL.String()

View file

@ -477,7 +477,7 @@ func buildObjectResponse(rc *requestContext, pointer lfs_module.Pointer, downloa
} }
// This is only needed to workaround https://github.com/git-lfs/git-lfs/issues/3662 // This is only needed to workaround https://github.com/git-lfs/git-lfs/issues/3662
verifyHeader["Accept"] = lfs_module.MediaType verifyHeader["Accept"] = lfs_module.AcceptHeader
rep.Actions["verify"] = &lfs_module.Link{Href: rc.VerifyLink(pointer), Header: verifyHeader} rep.Actions["verify"] = &lfs_module.Link{Href: rc.VerifyLink(pointer), Header: verifyHeader}
} }

View file

@ -105,7 +105,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
for _, test := range tests { for _, test := range tests {
session := loginUser(t, test.user.Name) session := loginUser(t, test.user.Name)
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks", test.repo.FullName()), map[string]string{"path": test.path}) req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks", test.repo.FullName()), map[string]string{"path": test.path})
req.Header.Set("Accept", lfs.MediaType) req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType) req.Header.Set("Content-Type", lfs.MediaType)
resp := session.MakeRequest(t, req, test.httpResult) resp := session.MakeRequest(t, req, test.httpResult)
if len(test.addTime) > 0 { if len(test.addTime) > 0 {
@ -123,7 +123,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
for _, test := range resultsTests { for _, test := range resultsTests {
session := loginUser(t, test.user.Name) session := loginUser(t, test.user.Name)
req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName()) req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName())
req.Header.Set("Accept", lfs.MediaType) req.Header.Set("Accept", lfs.AcceptHeader)
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
var lfsLocks api.LFSLockList var lfsLocks api.LFSLockList
DecodeJSON(t, resp, &lfsLocks) DecodeJSON(t, resp, &lfsLocks)
@ -135,7 +135,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
} }
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/verify", test.repo.FullName()), map[string]string{}) req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/verify", test.repo.FullName()), map[string]string{})
req.Header.Set("Accept", lfs.MediaType) req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType) req.Header.Set("Content-Type", lfs.MediaType)
resp = session.MakeRequest(t, req, http.StatusOK) resp = session.MakeRequest(t, req, http.StatusOK)
var lfsLocksVerify api.LFSLockListVerify var lfsLocksVerify api.LFSLockListVerify
@ -159,7 +159,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
for _, test := range deleteTests { for _, test := range deleteTests {
session := loginUser(t, test.user.Name) session := loginUser(t, test.user.Name)
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/%s/unlock", test.repo.FullName(), test.lockID), map[string]string{}) req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/%s/unlock", test.repo.FullName(), test.lockID), map[string]string{})
req.Header.Set("Accept", lfs.MediaType) req.Header.Set("Accept", lfs.AcceptHeader)
req.Header.Set("Content-Type", lfs.MediaType) req.Header.Set("Content-Type", lfs.MediaType)
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
var lfsLockRep api.LFSLockResponse var lfsLockRep api.LFSLockResponse
@ -172,7 +172,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
for _, test := range resultsTests { for _, test := range resultsTests {
session := loginUser(t, test.user.Name) session := loginUser(t, test.user.Name)
req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName()) req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName())
req.Header.Set("Accept", lfs.MediaType) req.Header.Set("Accept", lfs.AcceptHeader)
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
var lfsLocks api.LFSLockList var lfsLocks api.LFSLockList
DecodeJSON(t, resp, &lfsLocks) DecodeJSON(t, resp, &lfsLocks)

View file

@ -85,7 +85,7 @@ func TestAPILFSBatch(t *testing.T) {
newRequest := func(t testing.TB, br *lfs.BatchRequest) *RequestWrapper { newRequest := func(t testing.TB, br *lfs.BatchRequest) *RequestWrapper {
return NewRequestWithJSON(t, "POST", "/user2/lfs-batch-repo.git/info/lfs/objects/batch", br). return NewRequestWithJSON(t, "POST", "/user2/lfs-batch-repo.git/info/lfs/objects/batch", br).
SetHeader("Accept", lfs.MediaType). SetHeader("Accept", lfs.AcceptHeader).
SetHeader("Content-Type", lfs.MediaType) SetHeader("Content-Type", lfs.MediaType)
} }
decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse { decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse {
@ -448,7 +448,7 @@ func TestAPILFSVerify(t *testing.T) {
newRequest := func(t testing.TB, p *lfs.Pointer) *RequestWrapper { newRequest := func(t testing.TB, p *lfs.Pointer) *RequestWrapper {
return NewRequestWithJSON(t, "POST", "/user2/lfs-verify-repo.git/info/lfs/verify", p). return NewRequestWithJSON(t, "POST", "/user2/lfs-verify-repo.git/info/lfs/verify", p).
SetHeader("Accept", lfs.MediaType). SetHeader("Accept", lfs.AcceptHeader).
SetHeader("Content-Type", lfs.MediaType) SetHeader("Content-Type", lfs.MediaType)
} }