shim-client: ensure we check resp status for Put/Post

Without this, potential errors are silently dropped. Let's ensure we
return the error code as well as potenial data from the response.

Signed-off-by: Eric Ernst <eric_ernst@apple.com>
This commit is contained in:
Eric Ernst 2022-05-25 10:10:28 -07:00
parent 0706fb28ac
commit 3201ad0830

View File

@ -9,6 +9,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"time"
@ -79,12 +80,22 @@ func DoPut(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTyp
req.Header.Set("Content-Type", contentType)
resp, err := client.Do(req)
if err != nil {
return err
}
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
return err
if resp.StatusCode != http.StatusOK {
data, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("error sending put: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data))
}
return nil
}
// DoPost will make a POST request to the shim endpoint that handles the given sandbox ID
@ -95,10 +106,20 @@ func DoPost(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTy
}
resp, err := client.Post(fmt.Sprintf("http://shim%s", urlPath), contentType, bytes.NewBuffer(payload))
if err != nil {
return err
}
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
return err
if resp.StatusCode != http.StatusOK {
data, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("error sending post: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data))
}
return nil
}