fix(externalIpProvider/plain): when reading response body (#54)
Fixing an error where the service crashes reading the response body, when no content-length header was provided in the response. Reviewed-on: #54 Reviewed-by: branch-buddy <branch-buddy@t00n.de> Co-authored-by: Timo Behrendt <t.behrendt@t00n.de> Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #54.
This commit is contained in:
@@ -2,6 +2,7 @@ package externalIpProvider
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -40,12 +41,15 @@ func (p *ExternalIpProviderImplPlain) GetExternalIp() (net.IP, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
|
res.Body.Close()
|
||||||
return nil, errors.New("unexpected status code")
|
return nil, errors.New("unexpected status code")
|
||||||
}
|
}
|
||||||
|
|
||||||
responseBody := make([]byte, res.ContentLength)
|
responseBody, err := io.ReadAll(res.Body)
|
||||||
res.Body.Read(responseBody)
|
res.Body.Close()
|
||||||
defer res.Body.Close()
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
parsedIp := net.ParseIP(string(responseBody))
|
parsedIp := net.ParseIP(string(responseBody))
|
||||||
if parsedIp == nil {
|
if parsedIp == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user