mirror of
https://github.com/LBRYFoundation/tracker.git
synced 2025-08-23 17:47:29 +00:00
Move NewAnnounce and NewScrape to http, since they are an implementation detail
This commit is contained in:
parent
ed126dda29
commit
343b3358a1
3 changed files with 104 additions and 96 deletions
|
@ -28,7 +28,7 @@ func (s *Server) check(w http.ResponseWriter, r *http.Request, p httprouter.Para
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||||
ann, err := models.NewAnnounce(s.config, r, p)
|
ann, err := NewAnnounce(s.config, r, p)
|
||||||
writer := &Writer{w}
|
writer := &Writer{w}
|
||||||
|
|
||||||
if err == models.ErrMalformedRequest {
|
if err == models.ErrMalformedRequest {
|
||||||
|
@ -46,7 +46,7 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprou
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||||
scrape, err := models.NewScrape(s.config, r, p)
|
scrape, err := NewScrape(s.config, r, p)
|
||||||
writer := &Writer{w}
|
writer := &Writer{w}
|
||||||
|
|
||||||
if err == models.ErrMalformedRequest {
|
if err == models.ErrMalformedRequest {
|
||||||
|
|
100
http/tracker.go
Normal file
100
http/tracker.go
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
// 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 http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
||||||
|
"github.com/chihaya/chihaya/config"
|
||||||
|
"github.com/chihaya/chihaya/models/query"
|
||||||
|
"github.com/chihaya/chihaya/tracker/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewAnnounce parses an HTTP request and generates a models.Announce.
|
||||||
|
func NewAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*models.Announce, error) {
|
||||||
|
q, err := query.New(r.URL.RawQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
compact := q.Params["compact"] != "0"
|
||||||
|
event, _ := q.Params["event"]
|
||||||
|
numWant := q.RequestedPeerCount(cfg.NumWantFallback)
|
||||||
|
|
||||||
|
infohash, exists := q.Params["info_hash"]
|
||||||
|
if !exists {
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
peerID, exists := q.Params["peer_id"]
|
||||||
|
if !exists {
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
ip, err := q.RequestedIP(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := q.Uint64("port")
|
||||||
|
if err != nil {
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
left, err := q.Uint64("left")
|
||||||
|
if err != nil {
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
downloaded, err := q.Uint64("downloaded")
|
||||||
|
if err != nil {
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
uploaded, err := q.Uint64("uploaded")
|
||||||
|
if err != nil {
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.Announce{
|
||||||
|
Config: cfg,
|
||||||
|
Compact: compact,
|
||||||
|
Downloaded: downloaded,
|
||||||
|
Event: event,
|
||||||
|
IP: ip,
|
||||||
|
Infohash: infohash,
|
||||||
|
Left: left,
|
||||||
|
NumWant: numWant,
|
||||||
|
Passkey: p.ByName("passkey"),
|
||||||
|
PeerID: peerID,
|
||||||
|
Port: port,
|
||||||
|
Uploaded: uploaded,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewScrape parses an HTTP request and generates a models.Scrape.
|
||||||
|
func NewScrape(cfg *config.Config, r *http.Request, p httprouter.Params) (*models.Scrape, error) {
|
||||||
|
q, err := query.New(r.URL.RawQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if q.Infohashes == nil {
|
||||||
|
if _, exists := q.Params["info_hash"]; !exists {
|
||||||
|
// There aren't any infohashes.
|
||||||
|
return nil, models.ErrMalformedRequest
|
||||||
|
}
|
||||||
|
q.Infohashes = []string{q.Params["info_hash"]}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.Scrape{
|
||||||
|
Config: cfg,
|
||||||
|
|
||||||
|
Passkey: p.ByName("passkey"),
|
||||||
|
Infohashes: q.Infohashes,
|
||||||
|
}, nil
|
||||||
|
}
|
|
@ -7,13 +7,10 @@ package models
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya/config"
|
||||||
"github.com/chihaya/chihaya/models/query"
|
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -118,7 +115,6 @@ type User struct {
|
||||||
// Announce is an Announce by a Peer.
|
// Announce is an Announce by a Peer.
|
||||||
type Announce struct {
|
type Announce struct {
|
||||||
Config *config.Config `json:"config"`
|
Config *config.Config `json:"config"`
|
||||||
Request *http.Request `json:"request"`
|
|
||||||
|
|
||||||
Compact bool `json:"compact"`
|
Compact bool `json:"compact"`
|
||||||
Downloaded uint64 `json:"downloaded"`
|
Downloaded uint64 `json:"downloaded"`
|
||||||
|
@ -133,69 +129,6 @@ type Announce struct {
|
||||||
Uploaded uint64 `json:"uploaded"`
|
Uploaded uint64 `json:"uploaded"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAnnounce parses an HTTP request and generates an Announce.
|
|
||||||
func NewAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*Announce, error) {
|
|
||||||
q, err := query.New(r.URL.RawQuery)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
compact := q.Params["compact"] != "0"
|
|
||||||
event, _ := q.Params["event"]
|
|
||||||
numWant := q.RequestedPeerCount(cfg.NumWantFallback)
|
|
||||||
|
|
||||||
infohash, exists := q.Params["info_hash"]
|
|
||||||
if !exists {
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
peerID, exists := q.Params["peer_id"]
|
|
||||||
if !exists {
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
ip, err := q.RequestedIP(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
port, err := q.Uint64("port")
|
|
||||||
if err != nil {
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
left, err := q.Uint64("left")
|
|
||||||
if err != nil {
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
downloaded, err := q.Uint64("downloaded")
|
|
||||||
if err != nil {
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
uploaded, err := q.Uint64("uploaded")
|
|
||||||
if err != nil {
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Announce{
|
|
||||||
Config: cfg,
|
|
||||||
Request: r,
|
|
||||||
Compact: compact,
|
|
||||||
Downloaded: downloaded,
|
|
||||||
Event: event,
|
|
||||||
IP: ip,
|
|
||||||
Infohash: infohash,
|
|
||||||
Left: left,
|
|
||||||
NumWant: numWant,
|
|
||||||
Passkey: p.ByName("passkey"),
|
|
||||||
PeerID: peerID,
|
|
||||||
Port: port,
|
|
||||||
Uploaded: uploaded,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClientID returns the part of a PeerID that identifies a Peer's client
|
// ClientID returns the part of a PeerID that identifies a Peer's client
|
||||||
// software.
|
// software.
|
||||||
func (a Announce) ClientID() (clientID string) {
|
func (a Announce) ClientID() (clientID string) {
|
||||||
|
@ -269,32 +202,7 @@ func NewAnnounceDelta(a *Announce, p *Peer, u *User, t *Torrent, created, snatch
|
||||||
// Scrape is a Scrape by a Peer.
|
// Scrape is a Scrape by a Peer.
|
||||||
type Scrape struct {
|
type Scrape struct {
|
||||||
Config *config.Config `json:"config"`
|
Config *config.Config `json:"config"`
|
||||||
Request *http.Request `json:"request"`
|
|
||||||
|
|
||||||
Passkey string
|
Passkey string
|
||||||
Infohashes []string
|
Infohashes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewScrape parses an HTTP request and generates a Scrape.
|
|
||||||
func NewScrape(cfg *config.Config, r *http.Request, p httprouter.Params) (*Scrape, error) {
|
|
||||||
q, err := query.New(r.URL.RawQuery)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if q.Infohashes == nil {
|
|
||||||
if _, exists := q.Params["info_hash"]; !exists {
|
|
||||||
// There aren't any infohashes.
|
|
||||||
return nil, ErrMalformedRequest
|
|
||||||
}
|
|
||||||
q.Infohashes = []string{q.Params["info_hash"]}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Scrape{
|
|
||||||
Config: cfg,
|
|
||||||
Request: r,
|
|
||||||
|
|
||||||
Passkey: p.ByName("passkey"),
|
|
||||||
Infohashes: q.Infohashes,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue