Comment detail

HTTPでGET その2 (Nested Flatten)

HTTPパッケージにあるモジュール群を使う. タイムアウトは ghc-6.8.2 で標準になったSystem.TImeout モジュールを使う

 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
module Main (main) where

import Data.Maybe
import Network.Browser
import Network.HTTP
import Network.URI
import System.Environment
import System.IO
import System.Timeout
-- import qualified System.IO.UTF8 as U

main :: IO ()
main = do { prog <- getProgName
          ; args <- getArgs
          ; case args of
              [h] -> do { mr <- timeout tolerance $ httpRequest (mkreq h)
                        ; case mr of
                            Nothing -> hPutStrLn stderr $ prog ++ ": timeout"
                            Just (Right resp)
                              -> hPutStrLn stdout $ rspBody resp
                        }
              _   -> usage prog
          }
  where 
    mkreq h = Request uri GET [Header HdrHost h'] ""
      where 
         uri = fromJust $ parseURI h
         h'  = uriRegName $ fromJust $ uriAuthority uri
         
tolerance :: Int
tolerance = 10^6 -- micro sec
                            
usage :: String -> IO ()
usage prog = hPutStrLn stderr $ "usage: "++prog++" <uri>"

httpRequest :: Request -> IO (Result Response)
httpRequest req
 = do { proxy <- catch
                   (getEnv "HTTP_PROXY" >>= return . (flip Proxy Nothing))
                   (\_ -> return NoProxy)
      ; (_, resp) <- browse (setProxy proxy >> request req)
      ; return (Right resp)
      }

補足:プロキシは環境変数 HTTP_PROXY を見るようにしてあります。

Index

Feed

Other

Link

Pathtraq

loading...