mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-19 15:17:43 +01:00
modify httpclient
This commit is contained in:
parent
e7f48783f2
commit
de29bd4468
@ -16,6 +16,7 @@
|
|||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
//"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/vmware/harbor/utils/log"
|
"github.com/vmware/harbor/utils/log"
|
||||||
@ -70,6 +71,8 @@ func NewAuthTransport(transport http.RoundTripper, handlers []auth.Handler) http
|
|||||||
|
|
||||||
// RoundTrip ...
|
// RoundTrip ...
|
||||||
func (a *authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
func (a *authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
// TODO remove duplicate codes
|
||||||
|
if req.Body == nil {
|
||||||
originResp, originErr := a.transport.RoundTrip(req)
|
originResp, originErr := a.transport.RoundTrip(req)
|
||||||
|
|
||||||
if originErr != nil {
|
if originErr != nil {
|
||||||
@ -113,4 +116,55 @@ func (a *authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return resp, err
|
return resp, err
|
||||||
|
} else {
|
||||||
|
originReqBody := req.Body
|
||||||
|
|
||||||
|
originReqContentLength := req.ContentLength
|
||||||
|
req.Body = nil
|
||||||
|
req.ContentLength = 0
|
||||||
|
|
||||||
|
originResp, originErr := a.transport.RoundTrip(req)
|
||||||
|
if originErr == nil {
|
||||||
|
log.Debugf("%d | %s %s", originResp.StatusCode, req.Method, req.URL)
|
||||||
|
} else {
|
||||||
|
log.Error(originErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if originErr == nil && originResp.StatusCode == http.StatusUnauthorized {
|
||||||
|
challenges := auth.ParseChallengeFromResponse(originResp)
|
||||||
|
|
||||||
|
reqChanged := false
|
||||||
|
for _, challenge := range challenges {
|
||||||
|
|
||||||
|
scheme := challenge.Scheme
|
||||||
|
|
||||||
|
for _, handler := range a.handlers {
|
||||||
|
if scheme != handler.Schema() {
|
||||||
|
log.Debugf("scheme not match: %s %s, skip", scheme, handler.Schema())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := handler.AuthorizeRequest(req, challenge.Parameters); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
reqChanged = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reqChanged {
|
||||||
|
log.Warning("no handler match scheme")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
req.ContentLength = originReqContentLength
|
||||||
|
req.Body = originReqBody
|
||||||
|
|
||||||
|
resp, err := a.transport.RoundTrip(req)
|
||||||
|
if err == nil {
|
||||||
|
log.Debugf("%d | %s %s", resp.StatusCode, req.Method, req.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user