]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Network.hs
wrap network calls with withSocketsDo for multiplatform initialization
[functorrent.git] / src / FuncTorrent / Network.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 module FuncTorrent.Network
3     (
4      get,
5      mkParams
6     ) where
7
8 import Prelude hiding (concat)
9
10 import Data.ByteString (ByteString)
11 import Data.ByteString.Char8 as BC (pack, unpack, concat, intercalate)
12 import Network.HTTP (simpleHTTP, defaultGETRequest_, getResponseBody)
13 import Network.URI (parseURI)
14 import Network.Socket (withSocketsDo)
15
16 -- | Make a query string from a alist of k, v
17 -- TODO: Url encode each argument
18 mkParams :: [(String, ByteString)] -> ByteString
19 mkParams params = BC.intercalate "&" [concat [pack f, "=", s] | (f,s) <- params]
20
21 get :: String -> [(String, ByteString)] -> IO ByteString
22 get url args = withSocketsDo $ simpleHTTP (defaultGETRequest_ url') >>= getResponseBody
23     where url' = case parseURI $ unpack $ concat [pack url, "?", qstr] of
24                    Just x -> x
25                    _ -> error "Bad tracker URL"
26           qstr = mkParams args