36489ab70a
Add a new auth function, `DoWithAuthNoRetry()`, which sends an HTTP request with authorization but won't retry if authorization failed. This commit also removes the `allowRetry` parameter from `DoWithAuth()` as a result, and that function will now retry requests where authorization failed as before.
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package tq
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/git-lfs/git-lfs/lfsapi"
|
|
"github.com/git-lfs/git-lfs/tools"
|
|
"github.com/rubyist/tracerx"
|
|
)
|
|
|
|
const (
|
|
maxVerifiesConfigKey = "lfs.transfer.maxverifies"
|
|
defaultMaxVerifyAttempts = 3
|
|
)
|
|
|
|
func verifyUpload(c *lfsapi.Client, remote string, t *Transfer) error {
|
|
action, err := t.Actions.Get("verify")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if action == nil {
|
|
return nil
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", action.Href, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = lfsapi.MarshalToRequest(req, struct {
|
|
Oid string `json:"oid"`
|
|
Size int64 `json:"size"`
|
|
}{Oid: t.Oid, Size: t.Size})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for key, value := range action.Header {
|
|
req.Header.Set(key, value)
|
|
}
|
|
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
|
|
|
|
mv := c.GitEnv().Int(maxVerifiesConfigKey, defaultMaxVerifyAttempts)
|
|
mv = tools.MaxInt(defaultMaxVerifyAttempts, mv)
|
|
req = c.LogRequest(req, "lfs.verify")
|
|
|
|
for i := 1; i <= mv; i++ {
|
|
tracerx.Printf("tq: verify %s attempt #%d (max: %d)", t.Oid[:7], i, mv)
|
|
|
|
var res *http.Response
|
|
if t.Authenticated {
|
|
res, err = c.Do(req)
|
|
} else {
|
|
res, err = c.DoWithAuth(remote, c.Endpoints.AccessFor(action.Href), req)
|
|
}
|
|
|
|
if err != nil {
|
|
tracerx.Printf("tq: verify err: %+v", err.Error())
|
|
} else {
|
|
err = res.Body.Close()
|
|
break
|
|
}
|
|
}
|
|
return err
|
|
}
|