diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Tracker/HTTP.hs | 22 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/Protocol.hs | 27 |
2 files changed, 39 insertions, 10 deletions
diff --git a/src/Network/BitTorrent/Tracker/HTTP.hs b/src/Network/BitTorrent/Tracker/HTTP.hs index 4fef5e56..8d3a6412 100644 --- a/src/Network/BitTorrent/Tracker/HTTP.hs +++ b/src/Network/BitTorrent/Tracker/HTTP.hs | |||
@@ -33,6 +33,17 @@ import Network.HTTP | |||
33 | import Data.Torrent.Metainfo hiding (announce) | 33 | import Data.Torrent.Metainfo hiding (announce) |
34 | import Network.BitTorrent.Tracker.Protocol | 34 | import Network.BitTorrent.Tracker.Protocol |
35 | 35 | ||
36 | |||
37 | data HTTPTracker = HTTPTracker URI | ||
38 | |||
39 | instance Tracker URI where | ||
40 | announce = askTracker | ||
41 | scrape_ uri ihs = do | ||
42 | e <- scrape uri ihs | ||
43 | case e of | ||
44 | Left str -> error str | ||
45 | Right si -> return si | ||
46 | |||
36 | {----------------------------------------------------------------------- | 47 | {----------------------------------------------------------------------- |
37 | Announce | 48 | Announce |
38 | -----------------------------------------------------------------------} | 49 | -----------------------------------------------------------------------} |
@@ -42,8 +53,8 @@ encodeRequest announce req = URL.urlEncode req | |||
42 | `addToURI` announce | 53 | `addToURI` announce |
43 | `addHashToURI` reqInfoHash req | 54 | `addHashToURI` reqInfoHash req |
44 | 55 | ||
45 | mkHTTPRequest :: URI -> Request ByteString | 56 | mkGET :: URI -> Request ByteString |
46 | mkHTTPRequest uri = Request uri GET [] "" | 57 | mkGET uri = Request uri GET [] "" |
47 | 58 | ||
48 | -- TODO rename to something like "announceBlahBlah" | 59 | -- TODO rename to something like "announceBlahBlah" |
49 | 60 | ||
@@ -53,7 +64,7 @@ mkHTTPRequest uri = Request uri GET [] "" | |||
53 | -- | 64 | -- |
54 | askTracker :: URI -> AnnounceQuery -> IO AnnounceInfo | 65 | askTracker :: URI -> AnnounceQuery -> IO AnnounceInfo |
55 | askTracker announce req = do | 66 | askTracker announce req = do |
56 | let r = mkHTTPRequest (encodeRequest announce req) | 67 | let r = mkGET (encodeRequest announce req) |
57 | 68 | ||
58 | rawResp <- simpleHTTP r | 69 | rawResp <- simpleHTTP r |
59 | respBody <- getResponseBody rawResp | 70 | respBody <- getResponseBody rawResp |
@@ -69,16 +80,13 @@ askTracker announce req = do | |||
69 | -- conjunction with 'Stopped'. | 80 | -- conjunction with 'Stopped'. |
70 | leaveTracker :: URI -> AnnounceQuery -> IO () | 81 | leaveTracker :: URI -> AnnounceQuery -> IO () |
71 | leaveTracker announce req = do | 82 | leaveTracker announce req = do |
72 | let r = mkHTTPRequest (encodeRequest announce req) | 83 | let r = mkGET (encodeRequest announce req) |
73 | void $ simpleHTTP r >>= getResponseBody | 84 | void $ simpleHTTP r >>= getResponseBody |
74 | 85 | ||
75 | {----------------------------------------------------------------------- | 86 | {----------------------------------------------------------------------- |
76 | Scrape | 87 | Scrape |
77 | -----------------------------------------------------------------------} | 88 | -----------------------------------------------------------------------} |
78 | 89 | ||
79 | -- | Scrape info about a set of torrents. | ||
80 | type Scrape = Map InfoHash ScrapeInfo | ||
81 | |||
82 | -- | Trying to convert /announce/ URL to /scrape/ URL. If 'scrapeURL' | 90 | -- | Trying to convert /announce/ URL to /scrape/ URL. If 'scrapeURL' |
83 | -- gives 'Nothing' then tracker do not support scraping. The info hash | 91 | -- gives 'Nothing' then tracker do not support scraping. The info hash |
84 | -- list is used to restrict the tracker's report to that particular | 92 | -- list is used to restrict the tracker's report to that particular |
diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs index 4e1262ed..b67b856d 100644 --- a/src/Network/BitTorrent/Tracker/Protocol.hs +++ b/src/Network/BitTorrent/Tracker/Protocol.hs | |||
@@ -23,9 +23,22 @@ | |||
23 | {-# LANGUAGE TemplateHaskell #-} | 23 | {-# LANGUAGE TemplateHaskell #-} |
24 | {-# OPTIONS -fno-warn-orphans #-} | 24 | {-# OPTIONS -fno-warn-orphans #-} |
25 | module Network.BitTorrent.Tracker.Protocol | 25 | module Network.BitTorrent.Tracker.Protocol |
26 | ( Event(..), AnnounceQuery(..), AnnounceInfo(..) | 26 | ( -- * Announce |
27 | , defaultNumWant , defaultPorts | 27 | Event(..) |
28 | , ScrapeQuery, ScrapeInfo(..) | 28 | , AnnounceQuery(..) |
29 | , AnnounceInfo(..) | ||
30 | |||
31 | -- ** Defaults | ||
32 | , defaultNumWant | ||
33 | , defaultPorts | ||
34 | |||
35 | -- * Scrape | ||
36 | , ScrapeQuery | ||
37 | , ScrapeInfo(..) | ||
38 | , Scrape | ||
39 | |||
40 | -- * TODO | ||
41 | , Tracker(..) | ||
29 | ) | 42 | ) |
30 | where | 43 | where |
31 | 44 | ||
@@ -325,6 +338,9 @@ data ScrapeInfo = ScrapeInfo { | |||
325 | 338 | ||
326 | $(deriveJSON (L.map toLower . L.dropWhile isLower) ''ScrapeInfo) | 339 | $(deriveJSON (L.map toLower . L.dropWhile isLower) ''ScrapeInfo) |
327 | 340 | ||
341 | -- | Scrape info about a set of torrents. | ||
342 | type Scrape = Map InfoHash ScrapeInfo | ||
343 | |||
328 | instance BEncodable ScrapeInfo where | 344 | instance BEncodable ScrapeInfo where |
329 | toBEncode ScrapeInfo {..} = fromAssocs | 345 | toBEncode ScrapeInfo {..} = fromAssocs |
330 | [ "complete" --> siComplete | 346 | [ "complete" --> siComplete |
@@ -357,3 +373,8 @@ instance Serialize ScrapeInfo where | |||
357 | , siIncomplete = fromIntegral leechers | 373 | , siIncomplete = fromIntegral leechers |
358 | , siName = Nothing | 374 | , siName = Nothing |
359 | } | 375 | } |
376 | |||
377 | -- | Set of tracker RPCs. | ||
378 | class Tracker s where | ||
379 | announce :: s -> AnnounceQuery -> IO AnnounceInfo | ||
380 | scrape_ :: s -> ScrapeQuery -> IO Scrape | ||