inserted setUserAgent in each HTTP request

This commit is contained in:
Nan Monnand Deng 2013-06-28 17:24:54 -04:00
parent 2e95c379d1
commit 342460ed9a

View File

@ -105,33 +105,30 @@ type VersionChecker interface {
Version() string Version() string
} }
func setUserAgentHeader(req *http.Request, baseVersions []VersionChecker, extra ...VersionChecker) error { func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) {
if len(baseVersions)+len(extra) == 0 { for _, cookie := range c.Jar.Cookies(req.URL) {
return nil req.AddCookie(cookie)
} }
userAgent := make(map[string]string, len(baseVersions)+len(extra)) return c.Do(req)
}
for _, v := range baseVersions { func (r *Registry) setUserAgent(req *http.Request, extra ...VersionChecker) {
if len(r.baseVersions)+len(extra) == 0 {
return
}
userAgent := make(map[string]string, len(r.baseVersions)+len(extra))
for _, v := range r.baseVersions {
userAgent[v.Name()] = v.Version() userAgent[v.Name()] = v.Version()
} }
for _, v := range extra { for _, v := range extra {
userAgent[v.Name()] = v.Version() userAgent[v.Name()] = v.Version()
} }
header, err := json.Marshal(userAgent) header, _ := json.Marshal(userAgent)
userAgent = nil userAgent = nil
if err != nil {
return err
}
req.Header.Set("User-Agent", string(header)) req.Header.Set("User-Agent", string(header))
return nil return
}
func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) {
for _, cookie := range c.Jar.Cookies(req.URL) {
req.AddCookie(cookie)
}
return c.Do(req)
} }
// Retrieve the history of a given image from the Registry. // Retrieve the history of a given image from the Registry.
@ -142,6 +139,9 @@ func (r *Registry) GetRemoteHistory(imgID, registry string, token []string) ([]s
return nil, err return nil, err
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
if err != nil {
return nil, err
}
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil || res.StatusCode != 200 { if err != nil || res.StatusCode != 200 {
if res != nil { if res != nil {
@ -188,6 +188,7 @@ func (r *Registry) GetRemoteImageJSON(imgID, registry string, token []string) ([
return nil, -1, fmt.Errorf("Failed to download json: %s", err) return nil, -1, fmt.Errorf("Failed to download json: %s", err)
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
r.setUserAgent(req, nil)
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil { if err != nil {
return nil, -1, fmt.Errorf("Failed to download json: %s", err) return nil, -1, fmt.Errorf("Failed to download json: %s", err)
@ -215,6 +216,7 @@ func (r *Registry) GetRemoteImageLayer(imgID, registry string, token []string) (
return nil, fmt.Errorf("Error while getting from the server: %s\n", err) return nil, fmt.Errorf("Error while getting from the server: %s\n", err)
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
r.setUserAgent(req, nil)
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
@ -235,6 +237,7 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
return nil, err return nil, err
} }
req.Header.Set("Authorization", "Token "+strings.Join(token, ", ")) req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
r.setUserAgent(req, nil)
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
@ -273,6 +276,7 @@ func (r *Registry) GetRepositoryData(indexEp, remote string) (*RepositoryData, e
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password) req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
} }
req.Header.Set("X-Docker-Token", "true") req.Header.Set("X-Docker-Token", "true")
r.setUserAgent(req, nil)
res, err := r.client.Do(req) res, err := r.client.Do(req)
if err != nil { if err != nil {
@ -336,6 +340,7 @@ func (r *Registry) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, regis
req.Header.Add("Content-type", "application/json") req.Header.Add("Content-type", "application/json")
req.Header.Set("Authorization", "Token "+strings.Join(token, ",")) req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
req.Header.Set("X-Docker-Checksum", imgData.Checksum) req.Header.Set("X-Docker-Checksum", imgData.Checksum)
r.setUserAgent(req, nil)
utils.Debugf("Setting checksum for %s: %s", imgData.ID, imgData.Checksum) utils.Debugf("Setting checksum for %s: %s", imgData.ID, imgData.Checksum)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
@ -370,6 +375,7 @@ func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registr
req.ContentLength = -1 req.ContentLength = -1
req.TransferEncoding = []string{"chunked"} req.TransferEncoding = []string{"chunked"}
req.Header.Set("Authorization", "Token "+strings.Join(token, ",")) req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
r.setUserAgent(req, nil)
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
return fmt.Errorf("Failed to upload layer: %s", err) return fmt.Errorf("Failed to upload layer: %s", err)
@ -407,6 +413,7 @@ func (r *Registry) PushRegistryTag(remote, revision, tag, registry string, token
} }
req.Header.Add("Content-type", "application/json") req.Header.Add("Content-type", "application/json")
req.Header.Set("Authorization", "Token "+strings.Join(token, ",")) req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
r.setUserAgent(req, nil)
req.ContentLength = int64(len(revision)) req.ContentLength = int64(len(revision))
res, err := doWithCookies(r.client, req) res, err := doWithCookies(r.client, req)
if err != nil { if err != nil {
@ -439,6 +446,7 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password) req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
req.ContentLength = int64(len(imgListJSON)) req.ContentLength = int64(len(imgListJSON))
req.Header.Set("X-Docker-Token", "true") req.Header.Set("X-Docker-Token", "true")
r.setUserAgent(req, nil)
if validate { if validate {
req.Header["X-Docker-Endpoints"] = regs req.Header["X-Docker-Endpoints"] = regs
} }
@ -459,6 +467,7 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password) req.SetBasicAuth(r.authConfig.Username, r.authConfig.Password)
req.ContentLength = int64(len(imgListJSON)) req.ContentLength = int64(len(imgListJSON))
req.Header.Set("X-Docker-Token", "true") req.Header.Set("X-Docker-Token", "true")
r.setUserAgent(req, nil)
if validate { if validate {
req.Header["X-Docker-Endpoints"] = regs req.Header["X-Docker-Endpoints"] = regs
} }