mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-19 22:48:10 -07:00
transition to httprouter
This commit is contained in:
+9
-20
@@ -10,12 +10,12 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/chihaya/chihaya/config"
|
||||
"github.com/chihaya/chihaya/models/query"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -133,7 +133,7 @@ type Announce struct {
|
||||
}
|
||||
|
||||
// NewAnnounce parses an HTTP request and generates an Announce.
|
||||
func NewAnnounce(r *http.Request, conf *config.Config) (*Announce, error) {
|
||||
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
|
||||
@@ -144,8 +144,7 @@ func NewAnnounce(r *http.Request, conf *config.Config) (*Announce, error) {
|
||||
infohash, _ := q.Params["info_hash"]
|
||||
peerID, _ := q.Params["peer_id"]
|
||||
|
||||
dir, _ := path.Split(r.URL.Path)
|
||||
numWant := q.RequestedPeerCount(conf.NumWantFallback)
|
||||
numWant := q.RequestedPeerCount(cfg.NumWantFallback)
|
||||
|
||||
ip, ipErr := q.RequestedIP(r)
|
||||
port, portErr := q.Uint64("port")
|
||||
@@ -160,13 +159,12 @@ func NewAnnounce(r *http.Request, conf *config.Config) (*Announce, error) {
|
||||
peerID == "" ||
|
||||
portErr != nil ||
|
||||
uploadedErr != nil ||
|
||||
ipErr != nil ||
|
||||
len(dir) != 34 {
|
||||
ipErr != nil {
|
||||
return nil, ErrMalformedRequest
|
||||
}
|
||||
|
||||
return &Announce{
|
||||
Config: conf,
|
||||
Config: cfg,
|
||||
Request: r,
|
||||
Compact: compact,
|
||||
Downloaded: downloaded,
|
||||
@@ -175,7 +173,7 @@ func NewAnnounce(r *http.Request, conf *config.Config) (*Announce, error) {
|
||||
Infohash: infohash,
|
||||
Left: left,
|
||||
NumWant: numWant,
|
||||
Passkey: dir[1:33],
|
||||
Passkey: p.ByName("passkey"),
|
||||
PeerID: peerID,
|
||||
Port: port,
|
||||
Uploaded: uploaded,
|
||||
@@ -261,21 +259,12 @@ type Scrape struct {
|
||||
}
|
||||
|
||||
// NewScrape parses an HTTP request and generates a Scrape.
|
||||
func NewScrape(r *http.Request, c *config.Config) (*Scrape, error) {
|
||||
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
|
||||
}
|
||||
|
||||
var passkey string
|
||||
if c.Private {
|
||||
dir, _ := path.Split(r.URL.Path)
|
||||
if len(dir) != 34 {
|
||||
return nil, ErrMalformedRequest
|
||||
}
|
||||
passkey = dir[1:34]
|
||||
}
|
||||
|
||||
if q.Infohashes == nil {
|
||||
if _, exists := q.Params["infohash"]; !exists {
|
||||
// There aren't any infohashes.
|
||||
@@ -285,10 +274,10 @@ func NewScrape(r *http.Request, c *config.Config) (*Scrape, error) {
|
||||
}
|
||||
|
||||
return &Scrape{
|
||||
Config: c,
|
||||
Config: cfg,
|
||||
Request: r,
|
||||
|
||||
Passkey: passkey,
|
||||
Passkey: p.ByName("passkey"),
|
||||
Infohashes: q.Infohashes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Query represents a parsed URL.Query.
|
||||
@@ -60,7 +61,7 @@ func New(query string) (*Query, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q.Params[keyStr] = valStr
|
||||
q.Params[strings.ToLower(keyStr)] = valStr
|
||||
|
||||
if keyStr == "info_hash" {
|
||||
if hasInfohash {
|
||||
@@ -109,7 +110,7 @@ func (q *Query) Uint64(key string) (uint64, error) {
|
||||
|
||||
// RequestedPeerCount returns the request peer count or the provided fallback.
|
||||
func (q Query) RequestedPeerCount(fallback int) int {
|
||||
if numWantStr, exists := q.Params["numWant"]; exists {
|
||||
if numWantStr, exists := q.Params["numwant"]; exists {
|
||||
numWant, err := strconv.Atoi(numWantStr)
|
||||
if err != nil {
|
||||
return fallback
|
||||
@@ -140,7 +141,7 @@ func (q Query) RequestedIP(r *http.Request) (net.IP, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if xRealIPs, ok := q.Params["X-Real-Ip"]; ok {
|
||||
if xRealIPs, ok := q.Params["x-real-ip"]; ok {
|
||||
if ip := net.ParseIP(string(xRealIPs[0])); ip != nil {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user