diff options
author | Jakob Borg <jakob@nym.se> | 2016-05-06 22:01:56 +0000 |
---|---|---|
committer | Audrius Butkevicius <audrius.butkevicius@gmail.com> | 2016-05-06 22:01:56 +0000 |
commit | 39899e40bf1e0186319a614a13d2c8153e5d336d (patch) | |
tree | 2c557aaa9b0f436cb153528348cf81744b5ff495 | |
parent | 5d337bb24fdefd9c61f4cacca329a436a6152ae1 (diff) |
cmd/syncthing: Use ReadAll + json.Unmarshal in places were we care about consuming the reader
Because json.NewDecoder(r).Decode(&v) doesn't necessarily consume all
data on the reader, that means an HTTP connection can't be reused. We
don't do a lot of HTTP traffic where we read JSON responses, but the
discovery is one such place. The other two are for POSTs from the GUI,
where it's not exactly critical but still nice if the connection still
can be keep-alive'd after the request as well.
Also ensure that we call req.Body.Close() for clarity, even though this
should by all accounts not really be necessary.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3050
-rw-r--r-- | cmd/syncthing/gui.go | 10 | ||||
-rw-r--r-- | lib/config/config.go | 8 | ||||
-rw-r--r-- | lib/discover/global.go | 10 |
3 files changed, 23 insertions, 5 deletions
diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 3ae5537d97..17bd7958df 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -718,6 +718,7 @@ func (s *apiService) postSystemConfig(w http.ResponseWriter, r *http.Request) { defer s.systemConfigMut.Unlock() to, err := config.ReadJSON(r.Body, myID) + r.Body.Close() if err != nil { l.Warnln("decoding posted config:", err) http.Error(w, err.Error(), http.StatusBadRequest) @@ -940,10 +941,15 @@ func (s *apiService) getDBIgnores(w http.ResponseWriter, r *http.Request) { func (s *apiService) postDBIgnores(w http.ResponseWriter, r *http.Request) { qs := r.URL.Query() - var data map[string][]string - err := json.NewDecoder(r.Body).Decode(&data) + bs, err := ioutil.ReadAll(r.Body) r.Body.Close() + if err != nil { + http.Error(w, err.Error(), 500) + return + } + var data map[string][]string + err = json.Unmarshal(bs, &data) if err != nil { http.Error(w, err.Error(), 500) return diff --git a/lib/config/config.go b/lib/config/config.go index d981d83e74..b179e92163 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -11,6 +11,7 @@ import ( "encoding/json" "encoding/xml" "io" + "io/ioutil" "net/url" "os" "sort" @@ -92,7 +93,12 @@ func ReadJSON(r io.Reader, myID protocol.DeviceID) (Configuration, error) { util.SetDefaults(&cfg.Options) util.SetDefaults(&cfg.GUI) - err := json.NewDecoder(r).Decode(&cfg) + bs, err := ioutil.ReadAll(r) + if err != nil { + return cfg, err + } + + err = json.Unmarshal(bs, &cfg) cfg.OriginalVersion = cfg.Version cfg.prepare(myID) diff --git a/lib/discover/global.go b/lib/discover/global.go index 22a85b78e3..aa1dafe5b2 100644 --- a/lib/discover/global.go +++ b/lib/discover/global.go @@ -12,6 +12,7 @@ import ( "encoding/json" "errors" "io" + "io/ioutil" "net/http" "net/url" "strconv" @@ -154,9 +155,14 @@ func (c *globalClient) Lookup(device protocol.DeviceID) (addresses []string, err return nil, err } - var ann announcement - err = json.NewDecoder(resp.Body).Decode(&ann) + bs, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } resp.Body.Close() + + var ann announcement + err = json.Unmarshal(bs, &ann) return ann.Addresses, err } |