File: get.hs

package info (click to toggle)
haskell-http 20060707-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 220 kB
  • ctags: 7
  • sloc: haskell: 1,885; sh: 108; makefile: 82
file content (49 lines) | stat: -rw-r--r-- 1,315 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-- A simple test program which takes a url on the commandline
-- and outputs the contents to stdout.

-- ghc --make -package HTTP get.hs -o get

import Data.Char (intToDigit)
import Network.HTTP
import Network.URI
import System.Environment (getArgs)
import System.Exit (exitFailure)
import System.IO (hPutStrLn, stderr)

main = 
    do
    args <- getArgs
    case args of 
	[addr] -> case parseURI addr of
		       Nothing -> err "Could not parse URI"
		       Just uri -> do
				   cont <- get uri
			           putStr cont
	_ -> err "Usage: get <url>"

err :: String -> IO a
err msg = do 
	  hPutStrLn stderr msg
	  exitFailure

get :: URI -> IO String
get uri =
    do
    eresp <- simpleHTTP (request uri)
    resp <- handleE (err . show) eresp
    case rspCode resp of
                      (2,0,0) -> return (rspBody resp)
                      _ -> err (httpError resp)
    where
    showRspCode (a,b,c) = map intToDigit [a,b,c]
    httpError resp = showRspCode (rspCode resp) ++ " " ++ rspReason resp

request :: URI -> Request
request uri = Request{ rqURI = uri,
                       rqMethod = GET,
                       rqHeaders = [],
                       rqBody = "" }

handleE :: Monad m => (ConnError -> m a) -> Either ConnError a -> m a
handleE h (Left e) = h e
handleE _ (Right v) = return v