modify httpclient

This commit is contained in:
Wenkai Yin 2016-04-22 17:32:25 +08:00
parent e7f48783f2
commit de29bd4468

View File

@ -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
}
} }