mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-17 05:38:11 -07:00
make infohash and peerID byte arrays (#169)
This commit is contained in:
committed by
Jimmy Zelinskie
parent
a081e5195b
commit
7f16c55d81
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user