models: Use a uint16 for port numbers

This commit is contained in:
Justin Li 2015-02-20 02:06:44 -05:00
parent d9a7611eee
commit e3aff35532
3 changed files with 11 additions and 11 deletions

View file

@ -89,7 +89,7 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprou
stats.RecordEvent(stats.Announce) stats.RecordEvent(stats.Announce)
writer := &Writer{w} writer := &Writer{w}
ann, err := newAnnounce(s.config, r, p) ann, err := s.newAnnounce(r, p)
if err != nil { if err != nil {
return handleTorrentError(err, writer) return handleTorrentError(err, writer)
} }
@ -101,7 +101,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httproute
stats.RecordEvent(stats.Scrape) stats.RecordEvent(stats.Scrape)
writer := &Writer{w} writer := &Writer{w}
scrape, err := newScrape(s.config, r, p) scrape, err := s.newScrape(r, p)
if err != nil { if err != nil {
return handleTorrentError(err, writer) return handleTorrentError(err, writer)
} }

View file

@ -18,7 +18,7 @@ import (
) )
// newAnnounce parses an HTTP request and generates a models.Announce. // newAnnounce parses an HTTP request and generates a models.Announce.
func newAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*models.Announce, error) { func (s *Server) newAnnounce(r *http.Request, p httprouter.Params) (*models.Announce, error) {
q, err := query.New(r.URL.RawQuery) q, err := query.New(r.URL.RawQuery)
if err != nil { if err != nil {
return nil, err return nil, err
@ -26,7 +26,7 @@ func newAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*mod
compact := q.Params["compact"] != "0" compact := q.Params["compact"] != "0"
event, _ := q.Params["event"] event, _ := q.Params["event"]
numWant := requestedPeerCount(q, cfg.NumWantFallback) numWant := requestedPeerCount(q, s.config.NumWantFallback)
infohash, exists := q.Params["info_hash"] infohash, exists := q.Params["info_hash"]
if !exists { if !exists {
@ -38,7 +38,7 @@ func newAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*mod
return nil, models.ErrMalformedRequest return nil, models.ErrMalformedRequest
} }
ipv4, ipv6, err := requestedIP(q, r, &cfg.NetConfig) ipv4, ipv6, err := requestedIP(q, r, &s.config.NetConfig)
if err != nil { if err != nil {
return nil, models.ErrMalformedRequest return nil, models.ErrMalformedRequest
} }
@ -64,7 +64,7 @@ func newAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*mod
} }
return &models.Announce{ return &models.Announce{
Config: cfg, Config: s.config,
Compact: compact, Compact: compact,
Downloaded: downloaded, Downloaded: downloaded,
Event: event, Event: event,
@ -75,13 +75,13 @@ func newAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*mod
NumWant: numWant, NumWant: numWant,
Passkey: p.ByName("passkey"), Passkey: p.ByName("passkey"),
PeerID: peerID, PeerID: peerID,
Port: port, Port: uint16(port),
Uploaded: uploaded, Uploaded: uploaded,
}, nil }, nil
} }
// newScrape parses an HTTP request and generates a models.Scrape. // newScrape parses an HTTP request and generates a models.Scrape.
func newScrape(cfg *config.Config, r *http.Request, p httprouter.Params) (*models.Scrape, error) { func (s *Server) newScrape(r *http.Request, p httprouter.Params) (*models.Scrape, error) {
q, err := query.New(r.URL.RawQuery) q, err := query.New(r.URL.RawQuery)
if err != nil { if err != nil {
return nil, err return nil, err
@ -96,7 +96,7 @@ func newScrape(cfg *config.Config, r *http.Request, p httprouter.Params) (*model
} }
return &models.Scrape{ return &models.Scrape{
Config: cfg, Config: s.config,
Passkey: p.ByName("passkey"), Passkey: p.ByName("passkey"),
Infohashes: q.Infohashes, Infohashes: q.Infohashes,

View file

@ -71,7 +71,7 @@ type Peer struct {
// Always has length net.IPv4len if IPv4, and net.IPv6len if IPv6 // Always has length net.IPv4len if IPv4, and net.IPv6len if IPv6
IP net.IP `json:"ip,omitempty"` IP net.IP `json:"ip,omitempty"`
Port uint64 `json:"port"` Port uint16 `json:"port"`
Uploaded uint64 `json:"uploaded"` Uploaded uint64 `json:"uploaded"`
Downloaded uint64 `json:"downloaded"` Downloaded uint64 `json:"downloaded"`
@ -133,7 +133,7 @@ type Announce struct {
NumWant int `json:"numwant"` NumWant int `json:"numwant"`
Passkey string `json:"passkey"` Passkey string `json:"passkey"`
PeerID string `json:"peer_id"` PeerID string `json:"peer_id"`
Port uint64 `json:"port"` Port uint16 `json:"port"`
Uploaded uint64 `json:"uploaded"` Uploaded uint64 `json:"uploaded"`
Torrent *Torrent `json:"-"` Torrent *Torrent `json:"-"`