From 96d8be417cc8927b2a40477443d041bff9c0d1c6 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Sat, 19 Mar 2016 15:07:44 +0100 Subject: [PATCH] Fix handling of compressed data --- respond/collector.go | 23 ++++++++++++++--- respond/collector_test.go | 42 +++++++++++++++++++++++++++++++ respond/testdata/nodeinfo.flated | Bin 0 -> 465 bytes 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 respond/collector_test.go create mode 100644 respond/testdata/nodeinfo.flated diff --git a/respond/collector.go b/respond/collector.go index c48103d..86eea69 100644 --- a/respond/collector.go +++ b/respond/collector.go @@ -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 } diff --git a/respond/collector_test.go b/respond/collector_test.go new file mode 100644 index 0000000..7ae2812 --- /dev/null +++ b/respond/collector_test.go @@ -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) +} diff --git a/respond/testdata/nodeinfo.flated b/respond/testdata/nodeinfo.flated new file mode 100644 index 0000000000000000000000000000000000000000..6db53fee0c0969cf8db10242397800e1ad20bb07 GIT binary patch literal 465 zcmV;?0WSV^QcZ8$Fbw@Gg|15s%b$u)JM7rQFa+3P7z)~^60NorNOXcB$bTgdsh<0Ioj@EeIo7cMuza4!RKsp()IG z+lUTm0=I?XoCs$O(R#;>B9~=WWjRxNKb?StmKEn4h{Wvosb|@KQrOrq-P`IQ@+l7E z?A`XvrJGDLCE$oFvWC8O0Vi})Fg+5`9JePptb?5_H*jOXAzgfUUV_6AFh-0*{}Bb` zjaIEHv@u<$Wm&e`fCSyN#%P03YuxqeTZ1asUtvRGbS>pho1)kGr&)hDV#3I>rokvr zr**8jfj+fz6&IT(i%VMuv>bG^(+gX#pR6(U7UPmMJBQ<)g8bP@6(iqh9$3`6%%rSUwQGt}ma1y&l<5wyu*%w6Db+TWs#Q_d zZON7V#jVcjy(8$^6oP`a<7mNs|Hmf7K-g!@Pb>V-?`g59C4B1Hv#CRK>=K$R{AAfL z+_QwHh5B^`M<2<-lydiBEuEqehP$06rm|9E|8x6j-Sfl#ar^tnuWFa)59d_Doq}gE H*e