Fix handling of compressed data
This commit is contained in:
parent
9c09d80da9
commit
96d8be417c
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"reflect"
|
||||
@ -117,11 +118,27 @@ func (coll *Collector) parse(response *Response) (err error) {
|
||||
reader := flate.NewReader(bytes.NewReader(response.Raw))
|
||||
defer reader.Close()
|
||||
|
||||
decoder := json.NewDecoder(reader)
|
||||
if err = decoder.Decode(data); err == nil {
|
||||
coll.onReceive(response.Address, data)
|
||||
decompressed, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove useless wrapper element that only exists in compressed data.
|
||||
// Who introduced this !?
|
||||
if bytes.HasPrefix(decompressed, []byte(`{"neighbours":`)) ||
|
||||
bytes.HasPrefix(decompressed, []byte(`{"statistics":`)) {
|
||||
decompressed = decompressed[14 : len(decompressed)-1]
|
||||
} else if bytes.HasPrefix(decompressed, []byte(`{"nodeinfo":`)) {
|
||||
decompressed = decompressed[12 : len(decompressed)-1]
|
||||
}
|
||||
|
||||
err = json.Unmarshal(decompressed, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
coll.onReceive(response.Address, data)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
42
respond/collector_test.go
Normal file
42
respond/collector_test.go
Normal file
@ -0,0 +1,42 @@
|
||||
package respond
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/FreifunkBremen/respond-collector/data"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
var decompressed *data.NodeInfo
|
||||
|
||||
// callback function
|
||||
onReceive := func(addr net.UDPAddr, msg interface{}) {
|
||||
switch msg := msg.(type) {
|
||||
case *data.NodeInfo:
|
||||
decompressed = msg
|
||||
default:
|
||||
t.Error("unexpected message:", msg)
|
||||
}
|
||||
}
|
||||
|
||||
collector := &Collector{
|
||||
msgType: reflect.TypeOf(data.NodeInfo{}),
|
||||
onReceive: onReceive,
|
||||
}
|
||||
|
||||
// read testdata
|
||||
compressed, err := ioutil.ReadFile("testdata/nodeinfo.flated")
|
||||
assert.Nil(err)
|
||||
|
||||
collector.parse(&Response{
|
||||
Raw: compressed,
|
||||
})
|
||||
|
||||
assert.NotNil(decompressed)
|
||||
assert.Equal("f81a67a5e9c1", decompressed.NodeId)
|
||||
}
|
BIN
respond/testdata/nodeinfo.flated
vendored
Normal file
BIN
respond/testdata/nodeinfo.flated
vendored
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user