1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Refactor put image function's redirect loop

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2014-12-10 18:08:40 -08:00
parent 3b4de1070f
commit 9a7a1e5be0

View file

@ -462,7 +462,6 @@ func (r *Session) PushRegistryTag(remote, revision, tag, registry string, token
func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, validate bool, regs []string) (*RepositoryData, error) { func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, validate bool, regs []string) (*RepositoryData, error) {
cleanImgList := []*ImgData{} cleanImgList := []*ImgData{}
if validate { if validate {
for _, elem := range imgList { for _, elem := range imgList {
if elem.Checksum != "" { if elem.Checksum != "" {
@ -484,44 +483,28 @@ func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, validate
u := fmt.Sprintf("%srepositories/%s/%s", r.indexEndpoint.VersionString(1), remote, suffix) u := fmt.Sprintf("%srepositories/%s/%s", r.indexEndpoint.VersionString(1), remote, suffix)
log.Debugf("[registry] PUT %s", u) log.Debugf("[registry] PUT %s", u)
log.Debugf("Image list pushed to index:\n%s", imgListJSON) log.Debugf("Image list pushed to index:\n%s", imgListJSON)
req, err := r.reqFactory.NewRequest("PUT", u, bytes.NewReader(imgListJSON)) headers := map[string][]string{
if err != nil { "Content-type": {"application/json"},
return nil, err "X-Docker-Token": {"true"},
} }
req.Header.Add("Content-type", "application/json")
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
req.ContentLength = int64(len(imgListJSON))
req.Header.Set("X-Docker-Token", "true")
if validate { if validate {
req.Header["X-Docker-Endpoints"] = regs headers["X-Docker-Endpoints"] = regs
} }
res, _, err := r.doRequest(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
// Redirect if necessary // Redirect if necessary
for res.StatusCode >= 300 && res.StatusCode < 400 { var res *http.Response
log.Debugf("Redirected to %s", res.Header.Get("Location")) for {
req, err := r.reqFactory.NewRequest("PUT", res.Header.Get("Location"), bytes.NewReader(imgListJSON)) if res, err = r.putImageRequest(u, headers, imgListJSON); err != nil {
if err != nil {
return nil, err return nil, err
} }
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password) if !shouldRedirect(res) {
req.ContentLength = int64(len(imgListJSON)) break
req.Header.Set("X-Docker-Token", "true")
if validate {
req.Header["X-Docker-Endpoints"] = regs
} }
redirect, _, err := r.doRequest(req) res.Body.Close()
if err != nil { u = res.Header.Get("Location")
return nil, err log.Debugf("Redirected to %s", u)
}
res = redirect
defer res.Body.Close()
} }
defer res.Body.Close()
var tokens, endpoints []string var tokens, endpoints []string
if !validate { if !validate {
@ -564,6 +547,27 @@ func (r *Session) PushImageJSONIndex(remote string, imgList []*ImgData, validate
}, nil }, nil
} }
func (r *Session) putImageRequest(u string, headers map[string][]string, body []byte) (*http.Response, error) {
req, err := r.reqFactory.NewRequest("PUT", u, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
req.ContentLength = int64(len(body))
for k, v := range headers {
req.Header[k] = v
}
response, _, err := r.doRequest(req)
if err != nil {
return nil, err
}
return response, nil
}
func shouldRedirect(response *http.Response) bool {
return response.StatusCode >= 300 && response.StatusCode < 400
}
func (r *Session) SearchRepositories(term string) (*SearchResults, error) { func (r *Session) SearchRepositories(term string) (*SearchResults, error) {
log.Debugf("Index server: %s", r.indexEndpoint) log.Debugf("Index server: %s", r.indexEndpoint)
u := r.indexEndpoint.VersionString(1) + "search?q=" + url.QueryEscape(term) u := r.indexEndpoint.VersionString(1) + "search?q=" + url.QueryEscape(term)