]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Network.hs
*.hs: add GPLv3 License text and copyright notice
[functorrent.git] / src / FuncTorrent / Network.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 {-
3 Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
4
5 This file is part of FuncTorrent.
6
7 FuncTorrent is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 FuncTorrent is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with FuncTorrent; if not,  see <http://www.gnu.org/licenses/>
19 -}
20
21 module FuncTorrent.Network
22     (sendGetRequest
23     , mkParams
24     ) where
25
26 import Prelude hiding (concat)
27
28 import Data.ByteString (ByteString)
29 import Data.ByteString.Char8 as BC (pack, unpack, concat, intercalate)
30 import Network.HTTP (simpleHTTP, defaultGETRequest_, getResponseBody)
31 import Network.URI (parseURI)
32
33 -- | Make a query string from a alist of k, v
34 -- TODO: Url encode each argument
35 mkParams :: [(String, ByteString)] -> ByteString
36 mkParams params = BC.intercalate "&" [concat [pack f, "=", s] | (f,s) <- params]
37
38 sendGetRequest :: String -> [(String, ByteString)] -> IO ByteString
39 sendGetRequest url args = simpleHTTP (defaultGETRequest_ url') >>= getResponseBody
40     where url' = case parseURI $ unpack $ concat [pack url, "?", qstr] of
41                    Just x -> x
42                    _ -> error "Bad tracker URL"
43           qstr = mkParams args