mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-02 06:48:57 -07:00
Bring in more old behaviour, use types for peer_id and infohash
This commit is contained in:
committed by
Jimmy Zelinskie
parent
05b7b955a1
commit
75b4a20e56
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
// AnnounceHandler is a function that operates on an AnnounceResponse before it
|
||||
// has been delivered to a client.
|
||||
type AnnounceHandler func(*config.TrackerConfig, chihaya.AnnounceRequest, *chihaya.AnnounceResponse) error
|
||||
type AnnounceHandler func(*config.TrackerConfig, *chihaya.AnnounceRequest, *chihaya.AnnounceResponse) error
|
||||
|
||||
// AnnounceMiddleware is higher-order AnnounceHandler used to implement modular
|
||||
// behavior processing an announce.
|
||||
@@ -24,7 +24,7 @@ func (c *announceChain) Append(mw ...AnnounceMiddleware) {
|
||||
}
|
||||
|
||||
func (c *announceChain) Handler() AnnounceHandler {
|
||||
final := func(cfg *config.TrackerConfig, req chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
final := func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
return nil
|
||||
}
|
||||
for i := len(c.mw) - 1; i >= 0; i-- {
|
||||
@@ -54,7 +54,7 @@ func RegisterAnnounceMiddleware(name string, mw AnnounceMiddleware) {
|
||||
|
||||
// ScrapeHandler is a middleware function that operates on a ScrapeResponse
|
||||
// before it has been delivered to a client.
|
||||
type ScrapeHandler func(*config.TrackerConfig, chihaya.ScrapeRequest, *chihaya.ScrapeResponse) error
|
||||
type ScrapeHandler func(*config.TrackerConfig, *chihaya.ScrapeRequest, *chihaya.ScrapeResponse) error
|
||||
|
||||
// ScrapeMiddleware is higher-order ScrapeHandler used to implement modular
|
||||
// behavior processing a scrape.
|
||||
@@ -67,7 +67,7 @@ func (c *scrapeChain) Append(mw ...ScrapeMiddleware) {
|
||||
}
|
||||
|
||||
func (c *scrapeChain) Handler() ScrapeHandler {
|
||||
final := func(cfg *config.TrackerConfig, req chihaya.ScrapeRequest, resp *chihaya.ScrapeResponse) error {
|
||||
final := func(cfg *config.TrackerConfig, req *chihaya.ScrapeRequest, resp *chihaya.ScrapeResponse) error {
|
||||
return nil
|
||||
}
|
||||
for i := len(c.mw) - 1; i >= 0; i-- {
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func testAnnounceMW1(next AnnounceHandler) AnnounceHandler {
|
||||
return func(cfg *config.TrackerConfig, req chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
|
||||
Port: 1,
|
||||
})
|
||||
@@ -23,7 +23,7 @@ func testAnnounceMW1(next AnnounceHandler) AnnounceHandler {
|
||||
}
|
||||
|
||||
func testAnnounceMW2(next AnnounceHandler) AnnounceHandler {
|
||||
return func(cfg *config.TrackerConfig, req chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
|
||||
Port: 2,
|
||||
})
|
||||
@@ -32,7 +32,7 @@ func testAnnounceMW2(next AnnounceHandler) AnnounceHandler {
|
||||
}
|
||||
|
||||
func testAnnounceMW3(next AnnounceHandler) AnnounceHandler {
|
||||
return func(cfg *config.TrackerConfig, req chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
|
||||
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
|
||||
Port: 3,
|
||||
})
|
||||
@@ -47,7 +47,7 @@ func TestAnnounceChain(t *testing.T) {
|
||||
achain.Append(testAnnounceMW3)
|
||||
handler := achain.Handler()
|
||||
resp := &chihaya.AnnounceResponse{}
|
||||
err := handler(nil, chihaya.AnnounceRequest{}, resp)
|
||||
err := handler(nil, &chihaya.AnnounceRequest{}, resp)
|
||||
assert.Nil(t, err, "the handler should not return an error")
|
||||
assert.Equal(t, resp.IPv4Peers, []chihaya.Peer{chihaya.Peer{Port: 1}, chihaya.Peer{Port: 2}, chihaya.Peer{Port: 3}}, "the list of peers added from the middleware should be in the same order.")
|
||||
}
|
||||
|
||||
+2
-2
@@ -49,7 +49,7 @@ func NewTracker(cfg *config.TrackerConfig) (*Tracker, error) {
|
||||
|
||||
// HandleAnnounce runs an AnnounceRequest through a Tracker's middleware and
|
||||
// returns the result.
|
||||
func (t *Tracker) HandleAnnounce(req chihaya.AnnounceRequest) (*chihaya.AnnounceResponse, error) {
|
||||
func (t *Tracker) HandleAnnounce(req *chihaya.AnnounceRequest) (*chihaya.AnnounceResponse, error) {
|
||||
resp := &chihaya.AnnounceResponse{}
|
||||
err := t.handleAnnounce(t.cfg, req, resp)
|
||||
return resp, err
|
||||
@@ -57,7 +57,7 @@ func (t *Tracker) HandleAnnounce(req chihaya.AnnounceRequest) (*chihaya.Announce
|
||||
|
||||
// HandleScrape runs a ScrapeRequest through a Tracker's middleware and returns
|
||||
// the result.
|
||||
func (t *Tracker) HandleScrape(req chihaya.ScrapeRequest) (*chihaya.ScrapeResponse, error) {
|
||||
func (t *Tracker) HandleScrape(req *chihaya.ScrapeRequest) (*chihaya.ScrapeResponse, error) {
|
||||
resp := &chihaya.ScrapeResponse{}
|
||||
err := t.handleScrape(t.cfg, req, resp)
|
||||
return resp, err
|
||||
|
||||
Reference in New Issue
Block a user