summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal12
-rw-r--r--src/Data/BEncode/Pretty.hs15
-rw-r--r--src/Network/KRPC/Manager.hs5
3 files changed, 19 insertions, 13 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index fbd17397..8db80f50 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -159,6 +159,7 @@ library
159 , data-default-class 159 , data-default-class
160-- , data-dword 160-- , data-dword
161 , intset >= 0.1 161 , intset >= 0.1
162-- patched build: , intset == 0.1.1.10000
162 , PSQueue >= 1.1 163 , PSQueue >= 1.1
163 , split >= 0.2 164 , split >= 0.2
164 , text >= 0.11.0 165 , text >= 0.11.0
@@ -342,7 +343,7 @@ executable dht
342 hs-source-dirs: examples 343 hs-source-dirs: examples
343 main-is: dht.hs 344 main-is: dht.hs
344 default-language: Haskell2010 345 default-language: Haskell2010
345 build-depends: base, haskeline, network, bytestring 346 build-depends: base, haskeline, network, bytestring, transformers
346 347
347executable dhtd 348executable dhtd
348 hs-source-dirs: examples 349 hs-source-dirs: examples
@@ -380,11 +381,12 @@ executable mktorrent
380 , filepath 381 , filepath
381 , optparse-applicative 382 , optparse-applicative
382 , hslogger 383 , hslogger
383 if flag(network-uri) 384-- if flag(network-uri)
384 Build-depends: network >= 2.6 385-- Build-depends:
386 , network >= 2.6
385 , network-uri >= 2.6 387 , network-uri >= 2.6
386 else 388-- else
387 Build-depends: network >= 2.4 && < 2.6 389-- Build-depends: network >= 2.4 && < 2.6
388 ghc-options: -Wall -O2 -threaded 390 ghc-options: -Wall -O2 -threaded
389 391
390-- nonfunctioning example of very basic bittorrent client 392-- nonfunctioning example of very basic bittorrent client
diff --git a/src/Data/BEncode/Pretty.hs b/src/Data/BEncode/Pretty.hs
index 63efc61c..89fcb489 100644
--- a/src/Data/BEncode/Pretty.hs
+++ b/src/Data/BEncode/Pretty.hs
@@ -7,6 +7,9 @@ import qualified Data.ByteString.Lazy as BL
7import qualified Data.ByteString.Lazy.Char8 as BL8 7import qualified Data.ByteString.Lazy.Char8 as BL8
8import Data.Text (Text) 8import Data.Text (Text)
9import qualified Data.Text as T 9import qualified Data.Text as T
10import Data.Text.Encoding
11import Text.Printf
12import qualified Data.ByteString.Base16 as Base16
10#ifdef BENCODE_AESON 13#ifdef BENCODE_AESON
11import Data.BEncode.BDict hiding (map) 14import Data.BEncode.BDict hiding (map)
12import Data.Aeson.Types hiding (parse) 15import Data.Aeson.Types hiding (parse)
@@ -14,12 +17,9 @@ import Data.Aeson.Encode.Pretty
14import qualified Data.HashMap.Strict as HashMap 17import qualified Data.HashMap.Strict as HashMap
15import qualified Data.Vector as Vector 18import qualified Data.Vector as Vector
16import Data.Foldable as Foldable 19import Data.Foldable as Foldable
17import Data.Text.Encoding
18import Text.Printf
19#endif 20#endif
20 21
21#ifdef BENCODE_AESON 22{-
22
23unhex :: Text -> BS.ByteString 23unhex :: Text -> BS.ByteString
24unhex t = BS.pack $ map unhex1 [0 .. BS.length nibs `div` 2] 24unhex t = BS.pack $ map unhex1 [0 .. BS.length nibs `div` 2]
25 where 25 where
@@ -31,6 +31,9 @@ unhex t = BS.pack $ map unhex1 [0 .. BS.length nibs `div` 2]
31 31
32hex :: BS.ByteString -> Text 32hex :: BS.ByteString -> Text
33hex bs = T.concat $ map (T.pack . printf "%02X") $ BS.unpack bs 33hex bs = T.concat $ map (T.pack . printf "%02X") $ BS.unpack bs
34-}
35
36#ifdef BENCODE_AESON
34 37
35quote_chr :: Char 38quote_chr :: Char
36quote_chr = ' ' 39quote_chr = ' '
@@ -39,12 +42,12 @@ quote :: Text -> Text
39quote t = quote_chr `T.cons` t `T.snoc` quote_chr 42quote t = quote_chr `T.cons` t `T.snoc` quote_chr
40 43
41encodeByteString :: BS.ByteString -> Text 44encodeByteString :: BS.ByteString -> Text
42encodeByteString s = either (const $ hex s) quote $ decodeUtf8' s 45encodeByteString s = either (const . decodeUtf8 $ Base16.encode s) quote $ decodeUtf8' s
43 46
44decodeByteString :: Text -> BS.ByteString 47decodeByteString :: Text -> BS.ByteString
45decodeByteString s 48decodeByteString s
46 | T.head s==quote_chr = encodeUtf8 (T.takeWhile (/=quote_chr) $ T.drop 1 s) 49 | T.head s==quote_chr = encodeUtf8 (T.takeWhile (/=quote_chr) $ T.drop 1 s)
47 | otherwise = unhex s 50 | otherwise = fst (Base16.decode (encodeUtf8 s))
48 51
49instance ToJSON BValue where 52instance ToJSON BValue where
50 toJSON (BInteger x) = Number $ fromIntegral x 53 toJSON (BInteger x) = Number $ fromIntegral x
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs
index 7a2120cb..4852eb38 100644
--- a/src/Network/KRPC/Manager.hs
+++ b/src/Network/KRPC/Manager.hs
@@ -52,7 +52,8 @@ import Control.Monad.Reader
52import Control.Monad.Trans.Control 52import Control.Monad.Trans.Control
53import Data.BEncode as BE 53import Data.BEncode as BE
54import Data.BEncode.Internal as BE 54import Data.BEncode.Internal as BE
55import Data.BEncode.Pretty (showBEncode, hex) 55import Data.BEncode.Pretty (showBEncode)
56import qualified Data.ByteString.Base16 as Base16
56import Data.ByteString as BS 57import Data.ByteString as BS
57import Data.ByteString.Char8 as BC 58import Data.ByteString.Char8 as BC
58import Data.ByteString.Lazy as BL 59import Data.ByteString.Lazy as BL
@@ -218,7 +219,7 @@ withManager opts addr hs = bracket (newManager opts addr hs) closeManager
218querySignature :: MethodName -> TransactionId -> SockAddr -> Text 219querySignature :: MethodName -> TransactionId -> SockAddr -> Text
219querySignature name transaction addr = T.concat 220querySignature name transaction addr = T.concat
220 [ "&", T.decodeUtf8 name 221 [ "&", T.decodeUtf8 name
221 , " #", hex transaction -- T.decodeUtf8 transaction 222 , " #", T.decodeUtf8 (Base16.encode transaction) -- T.decodeUtf8 transaction
222 , " @", T.pack (show addr) 223 , " @", T.pack (show addr)
223 ] 224 ]
224 225