summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker/Protocol.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-07 19:14:41 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-07 19:14:41 +0400
commit13819af69d4a99f97eaf8ae439baeb6de37a1f5f (patch)
tree25710835624effca5d87a633cecdfbc3667e9836 /src/Network/BitTorrent/Tracker/Protocol.hs
parent7f299646b8c761f28b101b6232cc183712dcfa2e (diff)
~ Throw exception in askTracker.
HTTP client functions throws an exception. If we unable to decode result we return BEncode.Result.Left. So user of this function should check both kinds of errors and this complicate usage. Instead of this we throw IOException too.
Diffstat (limited to 'src/Network/BitTorrent/Tracker/Protocol.hs')
-rw-r--r--src/Network/BitTorrent/Tracker/Protocol.hs23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs
index 0371a187..af48e3e9 100644
--- a/src/Network/BitTorrent/Tracker/Protocol.hs
+++ b/src/Network/BitTorrent/Tracker/Protocol.hs
@@ -24,7 +24,7 @@ module Network.BitTorrent.Tracker.Protocol
24 ( module Network.BitTorrent.Tracker.Scrape 24 ( module Network.BitTorrent.Tracker.Scrape
25 25
26 , Event(..), TRequest(..), TResponse(..) 26 , Event(..), TRequest(..), TResponse(..)
27 , sendRequest 27 , askTracker
28 28
29 -- * Defaults 29 -- * Defaults
30 , defaultPorts, defaultNumWant 30 , defaultPorts, defaultNumWant
@@ -220,16 +220,21 @@ defaultPorts = [6881..6889]
220defaultNumWant :: Int 220defaultNumWant :: Int
221defaultNumWant = 25 221defaultNumWant = 25
222 222
223-- | TODO rename to ask for peers 223-- | Send request and receive response from the tracker specified in
224-- announce list. This function throws 'IOException' if it couldn't
225-- send request or receive response or decode response.
224-- 226--
225sendRequest :: TRequest -> IO (Result TResponse) 227askTracker :: TRequest -> IO TResponse
226sendRequest req = do 228askTracker req = do
227 let r = mkHTTPRequest (encodeRequest req) 229 let r = mkHTTPRequest (encodeRequest req)
228
229 rawResp <- simpleHTTP r
230 respBody <- getResponseBody rawResp
231 return (decoded respBody)
232 230
231 rawResp <- simpleHTTP r
232 respBody <- getResponseBody rawResp
233 checkResult $ decoded respBody
233 where 234 where
234 mkHTTPRequest :: URI -> Request ByteString 235 mkHTTPRequest :: URI -> Request ByteString
235 mkHTTPRequest uri = Request uri GET [] "" 236 mkHTTPRequest uri = Request uri GET [] ""
237
238 checkResult (Left err) = ioError (userError err)
239 checkResult (Right (Failure err)) = ioError (userError (show err))
240 checkResult (Right resp) = return resp