mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #38006 from AkihiroSuda/limit-client-readall
client: use io.LimitedReader for reading HTTP error
This commit is contained in:
commit
1f48759ad1
2 changed files with 26 additions and 1 deletions
|
@ -195,10 +195,18 @@ func (cli *Client) checkResponseErr(serverResp serverResponse) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(serverResp.body)
|
bodyMax := 1 * 1024 * 1024 // 1 MiB
|
||||||
|
bodyR := &io.LimitedReader{
|
||||||
|
R: serverResp.body,
|
||||||
|
N: int64(bodyMax),
|
||||||
|
}
|
||||||
|
body, err := ioutil.ReadAll(bodyR)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if bodyR.N == 0 {
|
||||||
|
return fmt.Errorf("request returned %s with a message (> %d bytes) for API route and version %s, check if the server supports the requested API version", http.StatusText(serverResp.statusCode), bodyMax, serverResp.reqURL)
|
||||||
|
}
|
||||||
if len(body) == 0 {
|
if len(body) == 0 {
|
||||||
return fmt.Errorf("request returned %s for API route and version %s, check if the server supports the requested API version", http.StatusText(serverResp.statusCode), serverResp.reqURL)
|
return fmt.Errorf("request returned %s for API route and version %s, check if the server supports the requested API version", http.StatusText(serverResp.statusCode), serverResp.reqURL)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,14 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
|
is "gotest.tools/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestSetHostHeader should set fake host for local communications, set real host
|
// TestSetHostHeader should set fake host for local communications, set real host
|
||||||
|
@ -87,3 +89,18 @@ func TestPlainTextError(t *testing.T) {
|
||||||
t.Fatalf("expected a Server Error, got %v", err)
|
t.Fatalf("expected a Server Error, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInfiniteError(t *testing.T) {
|
||||||
|
infinitR := rand.New(rand.NewSource(42))
|
||||||
|
client := &Client{
|
||||||
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||||
|
resp := &http.Response{StatusCode: http.StatusInternalServerError}
|
||||||
|
resp.Header = http.Header{}
|
||||||
|
resp.Body = ioutil.NopCloser(infinitR)
|
||||||
|
return resp, nil
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := client.Ping(context.Background())
|
||||||
|
assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue