inserted setUserAgent in each HTTP request
This commit is contained in:
parent
2e95c379d1
commit
342460ed9a
1 changed files with 26 additions and 17 deletions
|
@ -105,33 +105,30 @@ type VersionChecker interface {
|
|||
Version() string
|
||||
}
|
||||
|
||||
func setUserAgentHeader(req *http.Request, baseVersions []VersionChecker, extra ...VersionChecker) error {
|
||||
if len(baseVersions)+len(extra) == 0 {
|
||||
return nil
|
||||
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)
|
||||
}
|
||||
userAgent := make(map[string]string, len(baseVersions)+len(extra))
|
||||
|
||||
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()
|
||||
}
|
||||
for _, v := range extra {
|
||||
userAgent[v.Name()] = v.Version()
|
||||
}
|
||||
|
||||
header, err := json.Marshal(userAgent)
|
||||
header, _ := json.Marshal(userAgent)
|
||||
userAgent = nil
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("User-Agent", string(header))
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := r.client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
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)
|
||||
}
|
||||
req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
|
||||
r.setUserAgent(req, nil)
|
||||
res, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
|
||||
r.setUserAgent(req, nil)
|
||||
res, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -235,6 +237,7 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
|
|||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
|
||||
r.setUserAgent(req, nil)
|
||||
res, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
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.Header.Set("X-Docker-Token", "true")
|
||||
r.setUserAgent(req, nil)
|
||||
|
||||
res, err := r.client.Do(req)
|
||||
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.Set("Authorization", "Token "+strings.Join(token, ","))
|
||||
req.Header.Set("X-Docker-Checksum", imgData.Checksum)
|
||||
r.setUserAgent(req, nil)
|
||||
|
||||
utils.Debugf("Setting checksum for %s: %s", imgData.ID, imgData.Checksum)
|
||||
res, err := doWithCookies(r.client, req)
|
||||
|
@ -370,6 +375,7 @@ func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registr
|
|||
req.ContentLength = -1
|
||||
req.TransferEncoding = []string{"chunked"}
|
||||
req.Header.Set("Authorization", "Token "+strings.Join(token, ","))
|
||||
r.setUserAgent(req, nil)
|
||||
res, err := doWithCookies(r.client, req)
|
||||
if err != nil {
|
||||
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.Set("Authorization", "Token "+strings.Join(token, ","))
|
||||
r.setUserAgent(req, nil)
|
||||
req.ContentLength = int64(len(revision))
|
||||
res, err := doWithCookies(r.client, req)
|
||||
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.ContentLength = int64(len(imgListJSON))
|
||||
req.Header.Set("X-Docker-Token", "true")
|
||||
r.setUserAgent(req, nil)
|
||||
if validate {
|
||||
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.ContentLength = int64(len(imgListJSON))
|
||||
req.Header.Set("X-Docker-Token", "true")
|
||||
r.setUserAgent(req, nil)
|
||||
if validate {
|
||||
req.Header["X-Docker-Endpoints"] = regs
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue