]> source.dussan.org Git - gitea.git/commitdiff
Fix #31185 try fix lfs download from bitbucket failed (#31201) (#31329)
authorGiteabot <teabot@gitea.io>
Tue, 11 Jun 2024 23:34:37 +0000 (07:34 +0800)
committerGitHub <noreply@github.com>
Tue, 11 Jun 2024 23:34:37 +0000 (02:34 +0300)
Backport #31201 by @Zoupers

Fix #31185

Co-authored-by: Zoupers Zou <1171443643@qq.com>
modules/lfs/http_client.go
modules/lfs/http_client_test.go
modules/lfs/shared.go
modules/lfs/transferadapter.go
modules/lfs/transferadapter_test.go
services/lfs/server.go
tests/integration/api_repo_lfs_locks_test.go
tests/integration/api_repo_lfs_test.go

index e06879baea4fea8e261fe97896d2287ee98d2f1f..f5ddd38b0911df293574e74229a84f34d58f804d 100644 (file)
@@ -211,7 +211,7 @@ func createRequest(ctx context.Context, method, url string, headers map[string]s
        for key, value := range headers {
                req.Header.Set(key, value)
        }
-       req.Header.Set("Accept", MediaType)
+       req.Header.Set("Accept", AcceptHeader)
 
        return req, nil
 }
@@ -251,6 +251,6 @@ func handleErrorResponse(resp *http.Response) error {
                return err
        }
 
-       log.Trace("ErrorResponse: %v", er)
+       log.Trace("ErrorResponse(%v): %v", resp.Status, er)
        return errors.New(er.Message)
 }
index 7459d9c0c97e8d9cbc04d54beece3f1fd1058ba1..7431132f760f420a27660d559e279e8c6a71acf4 100644 (file)
@@ -155,7 +155,7 @@ func TestHTTPClientDownload(t *testing.T) {
        hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
                assert.Equal(t, "POST", req.Method)
                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
                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 {
                assert.Equal(t, "POST", req.Method)
                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
                err := json.NewDecoder(req.Body).Decode(&batchRequest)
index 6b2e55f2fbfd3f503c5d3117d8bbb9e305e58f21..80f4fed00d406810ee53c4a5776fc09baca605da 100644 (file)
@@ -10,6 +10,8 @@ import (
 const (
        // MediaType contains the media type for LFS server requests
        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.
index d425b91946d17c46f8a4e4412fa8190f44e53660..fbc3a3ad8c71685925e594947df4e31772ff053e 100644 (file)
@@ -37,6 +37,7 @@ func (a *BasicTransferAdapter) Download(ctx context.Context, l *Link) (io.ReadCl
        if err != nil {
                return nil, err
        }
+       log.Debug("Download Request: %+v", req)
        resp, err := performRequest(ctx, a.client, req)
        if err != nil {
                return nil, err
index 6023cd07d32fd9dc1fb250bf481011dfa50e5dc0..7fec137efe56911312c2219d826f64bfc4297dae 100644 (file)
@@ -26,7 +26,7 @@ func TestBasicTransferAdapter(t *testing.T) {
        p := Pointer{Oid: "b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259", Size: 5}
 
        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"))
 
                url := req.URL.String()
index 706be0d080ebc644b8182d2991332027f8d01734..ace501e15f0df07eefdacfc53dcd44e8060c08d3 100644 (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
-                       verifyHeader["Accept"] = lfs_module.MediaType
+                       verifyHeader["Accept"] = lfs_module.AcceptHeader
 
                        rep.Actions["verify"] = &lfs_module.Link{Href: rc.VerifyLink(pointer), Header: verifyHeader}
                }
index 5aa1396941dddb17d3ae15497ba01cf6fb45a9dd..427e0b9fb11dbbb4b7e949cac3b48ff704b73a5a 100644 (file)
@@ -105,7 +105,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
        for _, test := range tests {
                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.Header.Set("Accept", lfs.MediaType)
+               req.Header.Set("Accept", lfs.AcceptHeader)
                req.Header.Set("Content-Type", lfs.MediaType)
                resp := session.MakeRequest(t, req, test.httpResult)
                if len(test.addTime) > 0 {
@@ -123,7 +123,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
        for _, test := range resultsTests {
                session := loginUser(t, test.user.Name)
                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)
                var lfsLocks api.LFSLockList
                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.Header.Set("Accept", lfs.MediaType)
+               req.Header.Set("Accept", lfs.AcceptHeader)
                req.Header.Set("Content-Type", lfs.MediaType)
                resp = session.MakeRequest(t, req, http.StatusOK)
                var lfsLocksVerify api.LFSLockListVerify
@@ -159,7 +159,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
        for _, test := range deleteTests {
                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.Header.Set("Accept", lfs.MediaType)
+               req.Header.Set("Accept", lfs.AcceptHeader)
                req.Header.Set("Content-Type", lfs.MediaType)
                resp := session.MakeRequest(t, req, http.StatusOK)
                var lfsLockRep api.LFSLockResponse
@@ -172,7 +172,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
        for _, test := range resultsTests {
                session := loginUser(t, test.user.Name)
                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)
                var lfsLocks api.LFSLockList
                DecodeJSON(t, resp, &lfsLocks)
index 211dcf76c1128480ec68edb65610151a57f44159..6b42b83bc5a1e97b8dceffb5542f495a02a45e94 100644 (file)
@@ -84,7 +84,7 @@ func TestAPILFSBatch(t *testing.T) {
 
        newRequest := func(t testing.TB, br *lfs.BatchRequest) *RequestWrapper {
                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)
        }
        decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse {
@@ -447,7 +447,7 @@ func TestAPILFSVerify(t *testing.T) {
 
        newRequest := func(t testing.TB, p *lfs.Pointer) *RequestWrapper {
                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)
        }