diff --git a/bencode/bencode.go b/bencode/bencode.go deleted file mode 100644 index e28764b..0000000 --- a/bencode/bencode.go +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright 2014 The Chihaya Authors. All rights reserved. -// Use of this source code is governed by the BSD 2-Clause license, -// which can be found in the LICENSE file. - -// Package bencode implements bencoding of objects as defined in BEP 3 using -// type assertion over reflection for performance. -package bencode - -import ( - "bytes" - "errors" - "fmt" - "io" - "strconv" - "time" -) - -// An Encoder writes Bencoded objects to an output stream. -type Encoder struct { - w io.Writer -} - -// NewEncoder returns a new encoder that writes to w. -func NewEncoder(w io.Writer) *Encoder { - return &Encoder{w: w} -} - -// Encode writes the bencoding of v to the stream. -func (enc *Encoder) Encode(v interface{}) error { - return marshal(enc.w, v) -} - -// Marshal returns the bencoding of v. -func Marshal(v interface{}) ([]byte, error) { - buf := &bytes.Buffer{} - err := marshal(buf, v) - return buf.Bytes(), err -} - -// Marshaler is the interface implemented by objects that can marshal -// themselves. -type Marshaler interface { - MarshalBencode() ([]byte, error) -} - -// Marshal writes types bencoded to an io.Writer -func marshal(w io.Writer, data interface{}) error { - switch v := data.(type) { - case Marshaler: - bencoded, err := v.MarshalBencode() - if err != nil { - return err - } - _, err = w.Write(bencoded) - if err != nil { - return err - } - - case string: - fmt.Fprintf(w, "%d:%s", len(v), v) - - case int: - fmt.Fprintf(w, "i%de", v) - - case uint: - fmt.Fprintf(w, "i%se", strconv.FormatUint(uint64(v), 10)) - - case int64: - fmt.Fprintf(w, "i%se", strconv.FormatInt(v, 10)) - - case uint64: - fmt.Fprintf(w, "i%se", strconv.FormatUint(v, 10)) - - case time.Duration: // Assume seconds - fmt.Fprintf(w, "i%se", strconv.FormatInt(int64(v/time.Second), 10)) - - case map[string]interface{}: - fmt.Fprintf(w, "d") - for key, val := range v { - fmt.Fprintf(w, "%s:%s", strconv.Itoa(len(key)), key) - err := marshal(w, val) - if err != nil { - return err - } - } - fmt.Fprintf(w, "e") - - case []string: - fmt.Fprintf(w, "l") - for _, val := range v { - err := marshal(w, val) - if err != nil { - return err - } - } - fmt.Fprintf(w, "e") - - default: - // Although not currently necessary, - // should handle []interface{} manually; Go can't do it implicitly - return errors.New("bencode: attempted to marshal unsupported type") - } - - return nil -} diff --git a/bencode/bencode_test.go b/bencode/bencode_test.go deleted file mode 100644 index 95e45b3..0000000 --- a/bencode/bencode_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2014 The Chihaya Authors. All rights reserved. -// Use of this source code is governed by the BSD 2-Clause license, -// which can be found in the LICENSE file. - -package bencode - -import ( - "testing" - "time" -) - -var tests = []struct { - input interface{} - expected string -}{ - {int(42), "i42e"}, - {int(-42), "i-42e"}, - {uint(43), "i43e"}, - {int64(44), "i44e"}, - {uint64(45), "i45e"}, - - {"example", "7:example"}, - {30 * time.Minute, "i1800e"}, - - {[]string{"one", "two"}, "l3:one3:twoe"}, - {[]string{}, "le"}, - - {map[string]interface{}{"one": "aa", "two": "bb"}, "d3:one2:aa3:two2:bbe"}, - {map[string]interface{}{}, "de"}, -} - -func TestMarshal(t *testing.T) { - for _, test := range tests { - got, err := Marshal(test.input) - if err != nil { - t.Error(err) - } else if string(got) != test.expected { - t.Errorf("\ngot: %s\nexpected: %s", got, test.expected) - } - } -} diff --git a/http/announce.go b/http/announce.go index eb7b28f..16280e0 100644 --- a/http/announce.go +++ b/http/announce.go @@ -11,7 +11,7 @@ import ( "github.com/julienschmidt/httprouter" - "github.com/chihaya/chihaya/bencode" + "github.com/chihaya/bencode" "github.com/chihaya/chihaya/drivers/tracker" "github.com/chihaya/chihaya/models" ) diff --git a/http/scrape.go b/http/scrape.go index 0351272..6b49155 100644 --- a/http/scrape.go +++ b/http/scrape.go @@ -11,7 +11,7 @@ import ( "github.com/julienschmidt/httprouter" - "github.com/chihaya/chihaya/bencode" + "github.com/chihaya/bencode" "github.com/chihaya/chihaya/drivers/tracker" "github.com/chihaya/chihaya/models" )