summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal3
-rw-r--r--src/Network/BitTorrent/Tracker/Message.hs15
-rw-r--r--tests/Network/BitTorrent/Tracker/MessageSpec.hs27
3 files changed, 35 insertions, 10 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index dbc9d96a..6d2c7896 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -63,7 +63,7 @@ library
63-- , Network.BitTorrent.Tracker.HTTP 63-- , Network.BitTorrent.Tracker.HTTP
64-- , Network.BitTorrent.Tracker.UDP 64-- , Network.BitTorrent.Tracker.UDP
65-- , Network.BitTorrent.Exchange 65-- , Network.BitTorrent.Exchange
66-- , Network.BitTorrent.Exchange.Protocol 66 , Network.BitTorrent.Exchange.Protocol
67-- , Network.BitTorrent.DHT 67-- , Network.BitTorrent.DHT
68-- , Network.BitTorrent.DHT.Protocol 68-- , Network.BitTorrent.DHT.Protocol
69-- , Network.BitTorrent.Sessions 69-- , Network.BitTorrent.Sessions
@@ -155,6 +155,7 @@ test-suite spec
155 , directory 155 , directory
156 , filepath 156 , filepath
157 , time 157 , time
158 , random
158 159
159 , aeson 160 , aeson
160 , cereal 161 , cereal
diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs
index 53d7a946..3447a367 100644
--- a/src/Network/BitTorrent/Tracker/Message.hs
+++ b/src/Network/BitTorrent/Tracker/Message.hs
@@ -155,7 +155,7 @@ data AnnounceQuery = AnnounceQuery
155 155
156 -- | If not specified, the request is regular periodic request. 156 -- | If not specified, the request is regular periodic request.
157 , reqEvent :: Maybe Event 157 , reqEvent :: Maybe Event
158 } deriving (Show, Typeable) 158 } deriving (Show, Eq, Typeable)
159 159
160$(deriveJSON (L.map toLower . L.dropWhile isLower) ''AnnounceQuery) 160$(deriveJSON (L.map toLower . L.dropWhile isLower) ''AnnounceQuery)
161 161
@@ -233,8 +233,9 @@ data QueryParam
233 deriving (Show, Eq, Ord, Enum) 233 deriving (Show, Eq, Ord, Enum)
234 234
235data ParamParseFailure 235data ParamParseFailure
236 = Missing QueryParam -- ^ param not found in query string; 236 = Missing QueryParam -- ^ param not found in query string;
237 | Invalid QueryParam -- ^ param present but not valid. 237 | Invalid QueryParam Text -- ^ param present but not valid.
238 deriving (Show, Eq)
238 239
239type ParamResult = Either ParamParseFailure 240type ParamResult = Either ParamParseFailure
240 241
@@ -271,11 +272,11 @@ parseAnnounceQuery params = AnnounceQuery
271 where 272 where
272 withError e = maybe (Left e) Right 273 withError e = maybe (Left e) Right
273 reqParam param p = withError (Missing param) . L.lookup (paramName param) 274 reqParam param p = withError (Missing param) . L.lookup (paramName param)
274 >=> withError (Invalid param) . p 275 >=> \x -> withError (Invalid param x) (p x)
275 276
276 optParam param p ps 277 optParam param p ps
277 | Just x <- L.lookup (paramName param) ps 278 | Just x <- L.lookup (paramName param) ps
278 = pure <$> withError (Invalid param) (p x) 279 = pure <$> withError (Invalid param x) (p x)
279 | otherwise = pure Nothing 280 | otherwise = pure Nothing
280 281
281 progress = undefined 282 progress = undefined
@@ -426,8 +427,8 @@ invalidOffset = 150
426-- <https://wiki.theory.org/BitTorrent_Tracker_Protocol#Response_Codes> 427-- <https://wiki.theory.org/BitTorrent_Tracker_Protocol#Response_Codes>
427-- 428--
428paramFailureCode :: ParamParseFailure -> Int 429paramFailureCode :: ParamParseFailure -> Int
429paramFailureCode (Missing param) = missingOffset + fromEnum param 430paramFailureCode (Missing param ) = missingOffset + fromEnum param
430paramFailureCode (Invalid param) = invalidOffset + fromEnum param 431paramFailureCode (Invalid param _) = invalidOffset + fromEnum param
431 432
432{----------------------------------------------------------------------- 433{-----------------------------------------------------------------------
433 Scrape message 434 Scrape message
diff --git a/tests/Network/BitTorrent/Tracker/MessageSpec.hs b/tests/Network/BitTorrent/Tracker/MessageSpec.hs
index 99518067..8a06aaba 100644
--- a/tests/Network/BitTorrent/Tracker/MessageSpec.hs
+++ b/tests/Network/BitTorrent/Tracker/MessageSpec.hs
@@ -3,14 +3,19 @@ module Network.BitTorrent.Tracker.MessageSpec (spec) where
3 3
4import Control.Applicative 4import Control.Applicative
5import Data.BEncode as BE 5import Data.BEncode as BE
6import Data.ByteString.Char8 as BC
6import Data.ByteString.Lazy as BL 7import Data.ByteString.Lazy as BL
8import Data.List as L
7import Data.Maybe 9import Data.Maybe
8import Data.Word 10import Data.Word
11import Data.Text
9import Network 12import Network
10import Network.URI 13import Network.URI
11import Test.Hspec 14import Test.Hspec
12import Test.QuickCheck 15import Test.QuickCheck
13--import Network.HTTP.Types.URI 16import Test.QuickCheck.Gen
17import Network.HTTP.Types.URI
18import System.Random
14 19
15import Data.Torrent.InfoHashSpec () 20import Data.Torrent.InfoHashSpec ()
16import Data.Torrent.ProgressSpec () 21import Data.Torrent.ProgressSpec ()
@@ -39,9 +44,27 @@ instance Arbitrary AnnounceQuery where
39baseURI :: URI 44baseURI :: URI
40baseURI = fromJust $ parseURI "http://a" 45baseURI = fromJust $ parseURI "http://a"
41 46
47parseUriQuery :: URI -> [(Text, Text)]
48parseUriQuery = filterMaybes . parseQueryText . BC.pack . uriQuery
49 where
50 filterMaybes :: [(a, Maybe b)] -> [(a, b)]
51 filterMaybes = catMaybes . L.map f
52 where
53 f (a, Nothing) = Nothing
54 f (a, Just b ) = Just (a, b)
55
56test = do
57 let q = unGen arbitrary (mkStdGen 0) 0
58 print $ renderAnnounceQuery baseURI q
59 print $ parseUriQuery $ renderAnnounceQuery baseURI q
60
42spec :: Spec 61spec :: Spec
43spec = do 62spec = do
44 describe "Announce" $ do 63 describe "Announce" $ do
45 return () 64 before test $
65 it "properly url encoded" $ property $ \ q ->
66 parseAnnounceQuery (parseUriQuery (renderAnnounceQuery baseURI q))
67 `shouldBe` Right q
68
46 describe "Scrape" $ do 69 describe "Scrape" $ do
47 return () 70 return ()