Browse Source

Merge pull request #286 from titanous/285-close-bodies

Close HTTP response bodies
Guillaume J. Charmes 12 năm trước cách đây
mục cha
commit
dea4194f8b
1 tập tin đã thay đổi với 13 bổ sung6 xóa
  1. 13 6
      registry.go

+ 13 - 6
registry.go

@@ -184,10 +184,10 @@ func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, re
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
+	defer res.Body.Close()
 	if res.StatusCode != 200 {
 	if res.StatusCode != 200 {
 		return fmt.Errorf("HTTP code: %d", res.StatusCode)
 		return fmt.Errorf("HTTP code: %d", res.StatusCode)
 	}
 	}
-	defer res.Body.Close()
 	rawJson, err := ioutil.ReadAll(res.Body)
 	rawJson, err := ioutil.ReadAll(res.Body)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
@@ -238,6 +238,7 @@ func (graph *Graph) PushImage(stdout io.Writer, imgOrig *Image, authConfig *auth
 		if err != nil {
 		if err != nil {
 			return fmt.Errorf("Failed to upload metadata: %s", err)
 			return fmt.Errorf("Failed to upload metadata: %s", err)
 		}
 		}
+		defer res.Body.Close()
 		if res.StatusCode != 200 {
 		if res.StatusCode != 200 {
 			switch res.StatusCode {
 			switch res.StatusCode {
 			case 204:
 			case 204:
@@ -257,9 +258,13 @@ func (graph *Graph) PushImage(stdout io.Writer, imgOrig *Image, authConfig *auth
 		req2, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/images/"+img.Id+"/layer", nil)
 		req2, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/images/"+img.Id+"/layer", nil)
 		req2.SetBasicAuth(authConfig.Username, authConfig.Password)
 		req2.SetBasicAuth(authConfig.Username, authConfig.Password)
 		res2, err := client.Do(req2)
 		res2, err := client.Do(req2)
-		if err != nil || res2.StatusCode != 307 {
+		if err != nil {
 			return fmt.Errorf("Registry returned error: %s", err)
 			return fmt.Errorf("Registry returned error: %s", err)
 		}
 		}
+		res2.Body.Close()
+		if res2.StatusCode != 307 {
+			return fmt.Errorf("Registry returned unexpected HTTP status code %d, expected 307", res2.StatusCode)
+		}
 		url, err := res2.Location()
 		url, err := res2.Location()
 		if err != nil || url == nil {
 		if err != nil || url == nil {
 			return fmt.Errorf("Failed to retrieve layer upload location: %s", err)
 			return fmt.Errorf("Failed to retrieve layer upload location: %s", err)
@@ -289,6 +294,7 @@ func (graph *Graph) PushImage(stdout io.Writer, imgOrig *Image, authConfig *auth
 		if err != nil {
 		if err != nil {
 			return fmt.Errorf("Failed to upload layer: %s", err)
 			return fmt.Errorf("Failed to upload layer: %s", err)
 		}
 		}
+		res3.Body.Close()
 		if res3.StatusCode != 200 {
 		if res3.StatusCode != 200 {
 			return fmt.Errorf("Received HTTP code %d while uploading layer", res3.StatusCode)
 			return fmt.Errorf("Received HTTP code %d while uploading layer", res3.StatusCode)
 		}
 		}
@@ -318,12 +324,13 @@ func (graph *Graph) pushTag(remote, revision, tag string, authConfig *auth.AuthC
 	req.Header.Add("Content-type", "application/json")
 	req.Header.Add("Content-type", "application/json")
 	req.SetBasicAuth(authConfig.Username, authConfig.Password)
 	req.SetBasicAuth(authConfig.Username, authConfig.Password)
 	res, err := client.Do(req)
 	res, err := client.Do(req)
-	if err != nil || (res.StatusCode != 200 && res.StatusCode != 201) {
-		if res != nil {
-			return fmt.Errorf("Internal server error: %d trying to push tag %s on %s", res.StatusCode, tag, remote)
-		}
+	if err != nil {
 		return err
 		return err
 	}
 	}
+	res.Body.Close()
+	if res.StatusCode != 200 && res.StatusCode != 201 {
+		return fmt.Errorf("Internal server error: %d trying to push tag %s on %s", res.StatusCode, tag, remote)
+	}
 	Debugf("Result of push tag: %d\n", res.StatusCode)
 	Debugf("Result of push tag: %d\n", res.StatusCode)
 	switch res.StatusCode {
 	switch res.StatusCode {
 	default:
 	default: