diff options
Diffstat (limited to 'bittorrent/src/Network/BitTorrent/Tracker/RPC/HTTP.hs')
-rw-r--r-- | bittorrent/src/Network/BitTorrent/Tracker/RPC/HTTP.hs | 191 |
1 files changed, 0 insertions, 191 deletions
diff --git a/bittorrent/src/Network/BitTorrent/Tracker/RPC/HTTP.hs b/bittorrent/src/Network/BitTorrent/Tracker/RPC/HTTP.hs deleted file mode 100644 index 6f7a53bf..00000000 --- a/bittorrent/src/Network/BitTorrent/Tracker/RPC/HTTP.hs +++ /dev/null | |||
@@ -1,191 +0,0 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam Truzjan 2013 | ||
3 | -- License : BSD | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : provisional | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- This module implement HTTP tracker protocol. | ||
9 | -- | ||
10 | -- For more information see: | ||
11 | -- <https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol> | ||
12 | -- | ||
13 | {-# LANGUAGE DeriveDataTypeable #-} | ||
14 | module Network.BitTorrent.Tracker.RPC.HTTP | ||
15 | ( -- * Manager | ||
16 | Options (..) | ||
17 | , Manager | ||
18 | , newManager | ||
19 | , closeManager | ||
20 | , withManager | ||
21 | |||
22 | -- * RPC | ||
23 | , RpcException (..) | ||
24 | , announce | ||
25 | , scrape | ||
26 | , scrapeOne | ||
27 | ) where | ||
28 | |||
29 | import Control.Applicative | ||
30 | import Control.Exception | ||
31 | import Control.Monad | ||
32 | import Control.Monad.Trans.Resource | ||
33 | import Data.BEncode as BE | ||
34 | import Data.ByteString as BS | ||
35 | import Data.ByteString.Char8 as BC | ||
36 | import Data.ByteString.Lazy as BL | ||
37 | import Data.Default | ||
38 | import Data.List as L | ||
39 | import Data.Monoid | ||
40 | import Data.Typeable hiding (Proxy) | ||
41 | import Network.URI | ||
42 | import Network.HTTP.Conduit hiding | ||
43 | (Manager, newManager, closeManager, withManager) | ||
44 | import Network.HTTP.Client (defaultManagerSettings) | ||
45 | import Network.HTTP.Client.Internal (setUri) | ||
46 | import qualified Network.HTTP.Conduit as HTTP | ||
47 | import Network.HTTP.Types.Header (hUserAgent) | ||
48 | import Network.HTTP.Types.URI (SimpleQuery, renderSimpleQuery) | ||
49 | |||
50 | import Data.Torrent (InfoHash) | ||
51 | import Network.Address (libUserAgent) | ||
52 | import Network.BitTorrent.Tracker.Message hiding (Request, Response) | ||
53 | |||
54 | {----------------------------------------------------------------------- | ||
55 | -- Exceptions | ||
56 | -----------------------------------------------------------------------} | ||
57 | |||
58 | data RpcException | ||
59 | = RequestFailed HttpException -- ^ failed HTTP request. | ||
60 | | ParserFailure String -- ^ unable to decode tracker response; | ||
61 | | ScrapelessTracker -- ^ tracker do not support scraping; | ||
62 | | BadScrape -- ^ unable to find info hash in response dict; | ||
63 | deriving (Show, Typeable) | ||
64 | |||
65 | instance Exception RpcException | ||
66 | |||
67 | packHttpException :: IO a -> IO a | ||
68 | packHttpException m = try m >>= either (throwIO . RequestFailed) return | ||
69 | |||
70 | {----------------------------------------------------------------------- | ||
71 | -- Manager | ||
72 | -----------------------------------------------------------------------} | ||
73 | |||
74 | -- | HTTP tracker specific RPC options. | ||
75 | data Options = Options | ||
76 | { -- | Global HTTP announce query preferences. | ||
77 | optAnnouncePrefs :: !AnnouncePrefs | ||
78 | |||
79 | -- | Whether to use HTTP proxy for HTTP tracker requests. | ||
80 | , optHttpProxy :: !(Maybe Proxy) | ||
81 | |||
82 | -- | Value to put in HTTP user agent header. | ||
83 | , optUserAgent :: !BS.ByteString | ||
84 | |||
85 | -- | HTTP manager options. | ||
86 | , optHttpOptions :: !ManagerSettings | ||
87 | } | ||
88 | |||
89 | instance Default Options where | ||
90 | def = Options | ||
91 | { optAnnouncePrefs = def | ||
92 | , optHttpProxy = Nothing | ||
93 | , optUserAgent = BC.pack libUserAgent | ||
94 | , optHttpOptions = defaultManagerSettings | ||
95 | } | ||
96 | |||
97 | -- | HTTP tracker manager. | ||
98 | data Manager = Manager | ||
99 | { options :: !Options | ||
100 | , httpMgr :: !HTTP.Manager | ||
101 | } | ||
102 | |||
103 | -- | | ||
104 | newManager :: Options -> IO Manager | ||
105 | newManager opts = Manager opts <$> HTTP.newManager (optHttpOptions opts) | ||
106 | |||
107 | -- | | ||
108 | closeManager :: Manager -> IO () | ||
109 | closeManager Manager {..} = HTTP.closeManager httpMgr | ||
110 | |||
111 | -- | Normally you need to use 'Control.Monad.Trans.Resource.allocate'. | ||
112 | withManager :: Options -> (Manager -> IO a) -> IO a | ||
113 | withManager opts = bracket (newManager opts) closeManager | ||
114 | |||
115 | {----------------------------------------------------------------------- | ||
116 | -- Queries | ||
117 | -----------------------------------------------------------------------} | ||
118 | |||
119 | fillRequest :: Options -> SimpleQuery -> Request -> Request | ||
120 | fillRequest Options {..} q r = r | ||
121 | { queryString = joinQuery (queryString r) (renderSimpleQuery False q) | ||
122 | , requestHeaders = (hUserAgent, optUserAgent) : requestHeaders r | ||
123 | , proxy = optHttpProxy | ||
124 | } | ||
125 | where | ||
126 | joinQuery a b | ||
127 | | BS.null a = b | ||
128 | | otherwise = a <> "&" <> b | ||
129 | |||
130 | httpTracker :: BEncode a => Manager -> URI -> SimpleQuery -> IO a | ||
131 | httpTracker Manager {..} uri q = packHttpException $ do | ||
132 | request <- fillRequest options q <$> setUri defaultRequest uri | ||
133 | response <- runResourceT $ httpLbs request httpMgr | ||
134 | case BE.decode $ BL.toStrict $ responseBody response of | ||
135 | Left msg -> throwIO (ParserFailure msg) | ||
136 | Right info -> return info | ||
137 | |||
138 | {----------------------------------------------------------------------- | ||
139 | -- RPC | ||
140 | -----------------------------------------------------------------------} | ||
141 | |||
142 | -- | Send request and receive response from the tracker specified in | ||
143 | -- announce list. | ||
144 | -- | ||
145 | -- This function can throw 'RpcException'. | ||
146 | -- | ||
147 | announce :: Manager -> URI -> AnnounceQuery -> IO AnnounceInfo | ||
148 | announce mgr uri q = httpTracker mgr uri (renderAnnounceRequest uriQ) | ||
149 | where | ||
150 | uriQ = AnnounceRequest | ||
151 | { announceQuery = q | ||
152 | , announcePrefs = optAnnouncePrefs (options mgr) | ||
153 | } | ||
154 | |||
155 | -- | Trying to convert /announce/ URL to /scrape/ URL. If 'scrapeURL' | ||
156 | -- gives 'Nothing' then tracker do not support scraping. | ||
157 | -- | ||
158 | scrapeURL :: URI -> Maybe URI | ||
159 | scrapeURL uri = do | ||
160 | newPath <- replace (BC.pack (uriPath uri)) | ||
161 | return uri { uriPath = BC.unpack newPath } | ||
162 | where | ||
163 | replace p = do | ||
164 | let ps = BC.splitWith (== '/') p | ||
165 | guard (not (L.null ps)) | ||
166 | guard ("announce" `BS.isPrefixOf` L.last ps) | ||
167 | let newSuff = "scrape" <> BS.drop (BS.length "announce") (L.last ps) | ||
168 | return (BS.intercalate "/" (L.init ps ++ [newSuff])) | ||
169 | |||
170 | -- | For each 'InfoHash' of torrents request scrape info from the tracker. | ||
171 | -- However if the info hash list is 'null', the tracker should list | ||
172 | -- all available torrents. | ||
173 | -- | ||
174 | -- This function can throw 'RpcException'. | ||
175 | -- | ||
176 | scrape :: Manager -> URI -> ScrapeQuery -> IO ScrapeInfo | ||
177 | scrape m u q = do | ||
178 | case scrapeURL u of | ||
179 | Nothing -> throwIO ScrapelessTracker | ||
180 | Just uri -> httpTracker m uri (renderScrapeQuery q) | ||
181 | |||
182 | -- | More particular version of 'scrape', just for one torrent. | ||
183 | -- | ||
184 | -- This function can throw 'RpcException'. | ||
185 | -- | ||
186 | scrapeOne :: Manager -> URI -> InfoHash -> IO ScrapeEntry | ||
187 | scrapeOne m uri ih = do | ||
188 | xs <- scrape m uri [ih] | ||
189 | case L.lookup ih xs of | ||
190 | Nothing -> throwIO BadScrape | ||
191 | Just a -> return a | ||