make infohash and peerID byte arrays (#169)

This commit is contained in:
mrd0ll4r
2016-05-16 23:48:23 -04:00
committed by Jimmy Zelinskie
parent a081e5195b
commit 7f16c55d81
16 changed files with 113 additions and 46 deletions
+8 -1
View File
@@ -20,6 +20,10 @@ import (
// it.
var ErrKeyNotFound = errors.New("query: value for the provided key does not exist")
// ErrInvalidInfohash is returned when parsing a query encounters an infohash
// with invalid length.
var ErrInvalidInfohash = errors.New("query: invalid infohash")
// Query represents a parsed URL.Query.
type Query struct {
query string
@@ -71,7 +75,10 @@ func New(query string) (*Query, error) {
}
if keyStr == "info_hash" {
q.infoHashes = append(q.infoHashes, chihaya.InfoHash(valStr))
if len(valStr) != 20 {
return nil, ErrInvalidInfohash
}
q.infoHashes = append(q.infoHashes, chihaya.InfoHashFromString(valStr))
} else {
q.params[strings.ToLower(keyStr)] = valStr
}
+4 -1
View File
@@ -49,7 +49,10 @@ func announceRequest(r *http.Request, cfg *httpConfig) (*chihaya.AnnounceRequest
if err != nil {
return nil, tracker.ClientError("failed to parse parameter: peer_id")
}
request.PeerID = chihaya.PeerID(peerID)
if len(peerID) != 20 {
return nil, tracker.ClientError("failed to provide valid peer_id")
}
request.PeerID = chihaya.PeerIDFromString(peerID)
request.Left, err = q.Uint64("left")
if err != nil {
+2 -2
View File
@@ -71,7 +71,7 @@ func writeAnnounceResponse(w http.ResponseWriter, resp *chihaya.AnnounceResponse
func writeScrapeResponse(w http.ResponseWriter, resp *chihaya.ScrapeResponse) error {
filesDict := bencode.NewDict()
for infohash, scrape := range resp.Files {
filesDict[string(infohash)] = bencode.Dict{
filesDict[string(infohash[:])] = bencode.Dict{
"complete": scrape.Complete,
"incomplete": scrape.Incomplete,
}
@@ -91,7 +91,7 @@ func compact(peer chihaya.Peer) (buf []byte) {
func dict(peer chihaya.Peer) bencode.Dict {
return bencode.Dict{
"peer id": string(peer.ID),
"peer id": string(peer.ID[:]),
"ip": peer.IP.String(),
"port": peer.Port,
}