fix crash when req is called with no body

This commit is contained in:
Ringo Hoffmann 2021-11-08 23:10:07 +01:00 committed by Christoph Polcin
parent adba8dc051
commit 3f8721cd4b

View File

@ -13,6 +13,7 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
var r *http.Request var r *http.Request
var retryBuf io.Reader var retryBuf io.Reader
if body != nil {
// If the authorization fails, we will need to restart reading // If the authorization fails, we will need to restart reading
// from the passed body stream. // from the passed body stream.
// When body is seekable, use seek to reset the streams // When body is seekable, use seek to reset the streams
@ -29,11 +30,9 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
retryBuf = buff retryBuf = buff
body = io.TeeReader(body, buff) body = io.TeeReader(body, buff)
} }
if body == nil {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
} else {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), body) r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), body)
} else {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
} }
if err != nil { if err != nil {
@ -82,12 +81,9 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
return rs, newPathError("Authorize", c.root, rs.StatusCode) return rs, newPathError("Authorize", c.root, rs.StatusCode)
} }
if body == nil { // retryBuf will be nil if body was nil initially so no check
return c.req(method, path, nil, intercept) // for body == nil is required here.
} else {
return c.req(method, path, retryBuf, intercept) return c.req(method, path, retryBuf, intercept)
}
} else if rs.StatusCode == 401 { } else if rs.StatusCode == 401 {
return rs, newPathError("Authorize", c.root, rs.StatusCode) return rs, newPathError("Authorize", c.root, rs.StatusCode)
} }