Fix file descriptor leaks

This commit is contained in:
Matthew Holt 2015-10-21 22:16:29 -06:00
parent 01c629d834
commit 5efb56a1d4
3 changed files with 22 additions and 8 deletions

View file

@ -73,6 +73,8 @@ func NewClient(caURL string, usr User, keyBits int, optPort string, devMode bool
if err != nil {
logger().Fatalf("Could not get directory from CA URL. Please check the URL.\n\t%v", err)
}
defer dirResp.Body.Close()
var dir directory
decoder := json.NewDecoder(dirResp.Body)
err = decoder.Decode(&dir)
@ -98,6 +100,7 @@ func (c *Client) Register() (*RegistrationResource, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusConflict {
// REVIEW: should this return an error?
@ -142,6 +145,7 @@ func (c *Client) AgreeToTos() error {
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("The server returned %d but we expected %d", resp.StatusCode, http.StatusAccepted)
@ -184,6 +188,7 @@ func (c *Client) RevokeCertificate(certificate []byte) error {
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := ioutil.ReadAll(resp.Body)
@ -213,6 +218,10 @@ func (c *Client) RenewCertificate(cert CertificateResource, revokeOld bool) (Cer
// The first step of renewal is to check if we get a renewed cert
// directly from the cert URL.
resp, err := http.Get(cert.CertURL)
if err != nil {
return CertificateResource{}, err
}
defer resp.Body.Close()
serverCertBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return CertificateResource{}, err
@ -323,9 +332,9 @@ func (c *Client) getChallenges(domains []string) []*authorizationResource {
if err != nil {
errc <- err
}
resp.Body.Close()
resc <- &authorizationResource{Body: authz, NewCertURL: links["next"], AuthURL: resp.Header.Get("Location"), Domain: domain}
}(domain)
}
@ -409,6 +418,7 @@ func (c *Client) requestCertificate(authz *authorizationResource, result chan Ce
case 201:
cert, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
errc <- err
return

View file

@ -29,6 +29,7 @@ func (s *simpleHTTPChallenge) CanSolve(domain string) bool {
logger().Printf("Could not get public IP -> %v", err)
return false
}
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
if err != nil {
@ -66,6 +67,7 @@ func (s *simpleHTTPChallenge) Solve(chlng challenge, domain string) error {
if err != nil {
return fmt.Errorf("Could not start HTTPS server for challenge -> %v", err)
}
defer listener.Close()
// Tell the server about the generated random path
jsonBytes, err := json.Marshal(challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token})
@ -81,24 +83,24 @@ func (s *simpleHTTPChallenge) Solve(chlng challenge, domain string) error {
// After the path is sent, the ACME server will access our server.
// Repeatedly check the server for an updated status on our request.
var challengeResponse challenge
loop:
Loop:
for {
decoder := json.NewDecoder(resp.Body)
decoder.Decode(&challengeResponse)
err = json.NewDecoder(resp.Body).Decode(&challengeResponse)
resp.Body.Close()
if err != nil {
return err
}
switch challengeResponse.Status {
case "valid":
logger().Print("The server validated our request")
listener.Close()
break loop
break Loop
case "pending":
break
case "invalid":
listener.Close()
logger().Print("The server could not validate our request.")
return errors.New("The server could not validate our request.")
default:
listener.Close()
logger().Print("The server returned an unexpected state.")
return errors.New("The server returned an unexpected state.")
}

View file

@ -20,6 +20,7 @@ func TestSimpleHTTPCanSolve(t *testing.T) {
if err != nil {
t.Errorf("Could not get public IP -> %v", err)
}
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
if err != nil {
@ -119,6 +120,7 @@ func TestSimpleHTTP(t *testing.T) {
if err != nil {
t.Errorf("Expected the solver to listen on port 23456 -> %v", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
bodyStr := string(body)