Skip to content

Commit

Permalink
Replace json.NewDecocode(body).Decode() calls with json.Unmarshal to …
Browse files Browse the repository at this point in the history
…avoid release of network connections
  • Loading branch information
vkuznet committed Nov 30, 2020
1 parent 407d425 commit 426b188
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
13 changes: 8 additions & 5 deletions client/auth-token.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ func call(aurl string, formData url.Values, verbose bool) map[string]interface{}
}
}
defer resp.Body.Close()
var data map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
d, _ := ioutil.ReadAll(resp.Body)
log.Printf("Error parsing the response body: %s, %+v\n", err, string(d))
var rec map[string]interface{}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("unable to read incoming request body %s error %v", string(data), err)
}
if err := json.Unmarshal(data, &rec); err != nil {
log.Fatalf("Error parsing the response body %s error %v\n", string(data), err)
}
return data
return rec
}

func credentials() (string, string) {
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ func settingsHandler(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()
var s = ServerSettings{}
err := json.NewDecoder(r.Body).Decode(&s)
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("unable to read incoming request body %s error %v", string(data), err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.Unmarshal(data, &s)
if err != nil {
log.Printf("unable to unmarshal incoming request, error %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down
19 changes: 15 additions & 4 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -108,9 +109,14 @@ func introspectToken(token string) (TokenAttributes, error) {
}
defer resp.Body.Close()
var tokenAttributes TokenAttributes
err = json.NewDecoder(resp.Body).Decode(&tokenAttributes)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg := fmt.Sprintf("unable to decode response body: %+v", err)
msg := fmt.Sprintf("unable to read response body %s error %v", string(data), err)
return TokenAttributes{}, errors.New(msg)
}
err = json.Unmarshal(data, &tokenAttributes)
if err != nil {
msg := fmt.Sprintf("unable to decode response body, error %v", err)
return TokenAttributes{}, errors.New(msg)
}
return tokenAttributes, nil
Expand Down Expand Up @@ -151,9 +157,14 @@ func renewToken(token string, r *http.Request) (TokenInfo, error) {
}
defer resp.Body.Close()
var tokenInfo TokenInfo
err = json.NewDecoder(resp.Body).Decode(&tokenInfo)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg := fmt.Sprintf("unable to read response body %s error %v", string(data), err)
return TokenInfo{}, errors.New(msg)
}
err = json.Unmarshal(data, &tokenInfo)
if err != nil {
msg := fmt.Sprintf("unable to decode response body: %+v", err)
msg := fmt.Sprintf("unable to decode response body, error %v", err)
return TokenInfo{}, errors.New(msg)
}
return tokenInfo, nil
Expand Down

0 comments on commit 426b188

Please sign in to comment.