diff --git a/cdn.go b/cdn.go index e5698ca..b2d16b2 100644 --- a/cdn.go +++ b/cdn.go @@ -18,7 +18,7 @@ type BunnyCdn struct { var bunnyCdnUrl = "https://bunnycdn.com" func (cdn *BunnyCdn) Purge(url string) { - client := &http.Client{} + client := http.DefaultClient req, _ := http.NewRequest("POST", bunnyCdnUrl+"/api/purge?url="+url, nil) req.Header.Add("Content-Type", "application/json") req.Header.Add("Accept", "application/json") diff --git a/imagecompression.go b/imagecompression.go index cf6702c..ce6f1c3 100644 --- a/imagecompression.go +++ b/imagecompression.go @@ -51,6 +51,7 @@ func (t *Tinify) Compress(url string) (location string, err error) { return } defer func() { + _ = file.Close() _ = os.Remove(file.Name()) }() e = s.ToFile(file.Name()) diff --git a/mediaendpoint.go b/mediaendpoint.go index a1e0b03..9c21656 100644 --- a/mediaendpoint.go +++ b/mediaendpoint.go @@ -40,6 +40,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("Failed to get file")) return } + defer func() { _ = file.Close() }() hashFile, _, _ := r.FormFile("file") defer func() { _ = hashFile.Close() }() fileName, err := getSHA256(hashFile) diff --git a/mediastorage.go b/mediastorage.go index 6bd4702..4ac0ba9 100644 --- a/mediastorage.go +++ b/mediastorage.go @@ -27,7 +27,7 @@ type BunnyCdnStorage struct { var bunnyCdnStorageUrl = "https://storage.bunnycdn.com" func (b *BunnyCdnStorage) Upload(fileName string, file multipart.File) (location string, err error) { - client := &http.Client{} + client := http.DefaultClient req, _ := http.NewRequest(http.MethodPut, bunnyCdnStorageUrl+"/"+url.PathEscape(b.storageZoneName)+"/"+url.PathEscape("/"+fileName), file) req.Header.Add("AccessKey", b.key) resp, err := client.Do(req) diff --git a/validation.go b/validation.go index 5556a3d..db8d354 100644 --- a/validation.go +++ b/validation.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "errors" - "io/ioutil" "net/http" "strings" ) @@ -34,7 +33,7 @@ func checkAccess(token string) (bool, error) { return false, errors.New("token string is empty") } // form the request to check the token - client := &http.Client{} + client := http.DefaultClient req, err := http.NewRequest("GET", indieAuthTokenUrl, nil) if err != nil { return false, errors.New("error making the request for checking token access") @@ -46,14 +45,10 @@ func checkAccess(token string) (bool, error) { if err != nil { return false, errors.New("error sending the request for checking token access") } - defer res.Body.Close() // parse the response - body, err := ioutil.ReadAll(res.Body) - if err != nil { - return false, errors.New("error parsing the response for checking token access") - } - var indieAuthRes = new(IndieAuthRes) - err = json.Unmarshal(body, &indieAuthRes) + indieAuthRes := &IndieAuthRes{} + err = json.NewDecoder(res.Body).Decode(&indieAuthRes) + res.Body.Close() if err != nil { return false, errors.New("Error parsing the response into json for checking token access " + err.Error()) } diff --git a/webmention.go b/webmention.go index 53032cc..a1138be 100644 --- a/webmention.go +++ b/webmention.go @@ -154,7 +154,7 @@ func HandleWebmention(w http.ResponseWriter, r *http.Request) { } func responseCodeForSource(source string) (int, error) { - client := &http.Client{} + client := http.DefaultClient resp, err := client.Get(source) if err != nil || resp == nil { return 0, err @@ -208,4 +208,4 @@ func returnSuccess(target string, w http.ResponseWriter, r *http.Request) { } }() return -} \ No newline at end of file +}