Fix some memory leaks caused by not closing HTTP request body (#516)
This commit is contained in:
parent
2df30f64d0
commit
1bc1c45698
9 changed files with 38 additions and 9 deletions
|
|
@ -47,9 +47,13 @@ func runHealth(cmd *cobra.Command, args []string) {
|
|||
var ok bool
|
||||
resp, err := client.Get(fmt.Sprintf("http+unix://%s/health", serviceName))
|
||||
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
log.INFO.Printf("health check ok")
|
||||
ok = true
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
log.INFO.Printf("health check ok")
|
||||
ok = true
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ func (e StatusError) HasStatus(codes ...int) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx
|
||||
// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx. It closes the request body after reading.
|
||||
func ReadBody(resp *http.Response) ([]byte, error) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
|
|
|||
|
|
@ -146,7 +146,11 @@ func (v *API) getCookies() (cookieClient *request.Helper, err error) {
|
|||
v.config.CCSPServiceID,
|
||||
v.config.URI,
|
||||
)
|
||||
_, err = cookieClient.Get(uri)
|
||||
|
||||
var resp *http.Response
|
||||
if resp, err = cookieClient.Get(uri); err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return cookieClient, err
|
||||
|
|
@ -159,7 +163,10 @@ func (v *API) setLanguage(cookieClient *request.Helper) error {
|
|||
|
||||
req, err := request.New(http.MethodPost, v.config.URI+v.config.Lang, request.MarshalJSON(data), request.JSONEncoding)
|
||||
if err == nil {
|
||||
_, err = cookieClient.Do(req)
|
||||
var resp *http.Response
|
||||
if resp, err = cookieClient.Do(req); err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -108,6 +108,7 @@ func (v *BMW) login(user, password string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
query, err := url.ParseQuery(resp.Header.Get("Location"))
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ func (v *ID) authFlow() error {
|
|||
var vars vw.FormVars
|
||||
if err == nil {
|
||||
vars, err = vw.FormValues(resp.Body, "form#emailPasswordForm")
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
// POST identity.vwgroup.io/signin-service/v1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com/login/identifier
|
||||
|
|
|
|||
|
|
@ -190,6 +190,8 @@ func (v *Nissan) authFlow() error {
|
|||
v.Client.CheckRedirect = nil
|
||||
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
|
||||
var location *url.URL
|
||||
if location, err = url.Parse(resp.Header.Get("Location")); err == nil {
|
||||
if code = location.Query().Get("code"); code == "" {
|
||||
|
|
|
|||
|
|
@ -134,6 +134,7 @@ func (v *Porsche) authFlow() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
query, err := url.ParseQuery(resp.Request.URL.RawQuery)
|
||||
if err != nil {
|
||||
|
|
@ -161,9 +162,10 @@ func (v *Porsche) authFlow() error {
|
|||
}
|
||||
|
||||
// process the auth so the session is authenticated
|
||||
if _, err = client.Do(req); err != nil {
|
||||
if resp, err = client.Do(req); err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
var CodeVerifier, _ = cv.CreateCodeVerifier()
|
||||
codeChallenge := CodeVerifier.CodeChallengeS256()
|
||||
|
|
@ -188,6 +190,7 @@ func (v *Porsche) authFlow() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
query, err = url.ParseQuery(resp.Request.URL.RawQuery)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -178,7 +178,10 @@ func (v *VW) Close() error {
|
|||
})
|
||||
|
||||
if err == nil {
|
||||
_, err = v.Do(req)
|
||||
var resp *http.Response
|
||||
if resp, err = v.Do(req); err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ type Identity struct {
|
|||
*http.Client
|
||||
}
|
||||
|
||||
// redirect follows HTTP redirect header if error is nil. Request body is closed.
|
||||
func (v *Identity) redirect(resp *http.Response, err error) (*http.Response, error) {
|
||||
if err == nil {
|
||||
uri := resp.Header.Get("Location")
|
||||
|
|
@ -30,7 +31,9 @@ func (v *Identity) redirect(resp *http.Response, err error) (*http.Response, err
|
|||
return nil, errors.New("could not find expected HTTP redirect header\ngo to https://www.portal.volkswagen-we.com/ check account status")
|
||||
}
|
||||
|
||||
resp, err = v.Get(uri)
|
||||
if resp, err = v.Get(uri); err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return resp, err
|
||||
|
|
@ -44,6 +47,9 @@ func (v *Identity) Login(query url.Values, user, password string) (string, error
|
|||
// GET identity.vwgroup.io/oidc/v1/authorize?ui_locales=de&scope=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&response_type=code&state=gmiJOaB4&redirect_uri=https%3A%2F%2Fwww.portal.volkswagen-we.com%2Fportal%2Fweb%2Fguest%2Fcomplete-login&nonce=38042ee3-b7a7-43cf-a9c1-63d2f3f2d9f3&prompt=login&client_id=b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com
|
||||
uri := "https://identity.vwgroup.io/oidc/v1/authorize?" + query.Encode()
|
||||
resp, err := v.Get(uri)
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
// GET identity.vwgroup.io/signin-service/v1/signin/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com?relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa
|
||||
if err == nil {
|
||||
|
|
@ -52,6 +58,7 @@ func (v *Identity) Login(query url.Values, user, password string) (string, error
|
|||
|
||||
if err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#emailPasswordForm")
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,6 +86,7 @@ func (v *Identity) Login(query url.Values, user, password string) (string, error
|
|||
|
||||
if err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#credentialsForm")
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue