Skip to content

Commit

Permalink
Properly close response body on internet check
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhpedersen committed Dec 19, 2024
1 parent fc36ba5 commit 47b1573
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions internal/forms/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"net/textproto"
Expand Down Expand Up @@ -369,7 +370,13 @@ func isInternetAvailable() bool {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "HEAD", "https://www.google.com", nil)
_, err := http.DefaultClient.Do(req)
resp, err := http.DefaultClient.Do(req)
debug.Printf("Internet available: %v (%v)", err == nil, err)
return err == nil
if err != nil {
return false
}
// Be nice, read the response body and close it.
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
return true
}

0 comments on commit 47b1573

Please sign in to comment.