mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json: fix EOF bug decoding HTTP stream
Fixes bug referenced in this thread on golang-dev: https://groups.google.com/d/topic/golang-dev/U4LSpMzL82c/discussion Change-Id: If01a2644863f9e5625dd2f95f9d344bda772e12c Reviewed-on: https://go-review.googlesource.com/12726 Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
a01d90744f
commit
7e70c2468b
2 changed files with 48 additions and 1 deletions
|
|
@ -437,6 +437,7 @@ func (dec *Decoder) More() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dec *Decoder) peek() (byte, error) {
|
func (dec *Decoder) peek() (byte, error) {
|
||||||
|
var err error
|
||||||
for {
|
for {
|
||||||
for i := dec.scanp; i < len(dec.buf); i++ {
|
for i := dec.scanp; i < len(dec.buf); i++ {
|
||||||
c := dec.buf[i]
|
c := dec.buf[i]
|
||||||
|
|
@ -446,9 +447,11 @@ func (dec *Decoder) peek() (byte, error) {
|
||||||
dec.scanp = i
|
dec.scanp = i
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
if err := dec.refill(); err != nil {
|
// buffer has been scanned, now report any error
|
||||||
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
err = dec.refill()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,10 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -315,3 +318,44 @@ func TestDecodeInStream(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const raw = `{ "foo": "bar" }`
|
||||||
|
|
||||||
|
func makeHTTP() io.ReadCloser {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte(raw))
|
||||||
|
})
|
||||||
|
ts := httptest.NewServer(mux)
|
||||||
|
defer ts.Close()
|
||||||
|
res, err := http.Get(ts.URL)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("GET failed: %v", err)
|
||||||
|
}
|
||||||
|
return res.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHttpDecoding(t *testing.T) {
|
||||||
|
|
||||||
|
foo := struct {
|
||||||
|
Foo string
|
||||||
|
}{}
|
||||||
|
|
||||||
|
rc := makeHTTP()
|
||||||
|
defer rc.Close()
|
||||||
|
|
||||||
|
d := NewDecoder(rc)
|
||||||
|
err := d.Decode(&foo)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error %v", err)
|
||||||
|
}
|
||||||
|
if foo.Foo != "bar" {
|
||||||
|
t.Errorf("Expected \"bar\", was %v", foo.Foo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure we get the EOF the second time
|
||||||
|
err = d.Decode(&foo)
|
||||||
|
if err != io.EOF {
|
||||||
|
t.Errorf("Expected io.EOF, was %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue