Haskell で Basic 認証

Basic 認証 の必要なアクセスをAuthentication.hsを参考に動かしてみた。

 import Codec.Binary.Base64.String
 import Network.HTTP
 import Network.URI
 
 -- Sends an HTTP request to a server identified by uri.
 -- The username and password are sent with the request using HTTP basic authentication.
 connect :: String -> (String, String) -> IO String
 connect uri credentials = case parseURI uri of
                            Nothing -> error "Could not parse URI"
                            Just uri -> let request = makeRequest uri credentials in
                                          do resp <- simpleHTTP request
                                             getResponseBody resp
 
 -- makeRequest :: URI -> (String, String) -> Request
 makeRequest :: URI -> (String, String) -> Request String
 makeRequest uri credentials = Request{ rqURI = uri,
                                       rqMethod = GET,
                                       rqHeaders = [Header HdrAuthorization (makeAuth credentials)],
                                       rqBody = "" }
                                       
 -- Makes an HTTP Basic authentication string.
 makeAuth :: (String, String) -> String
 makeAuth (user, password) = "Basic " ++ encode (user ++ ":" ++ password)
 
 main = print =<< connect "http://www.example.com" ("username","password") 

Basic 認証 はユーザ名、パスワードがそのまま流れます。