module Network.HTTP
( module Network.HTTP.Base
, module Network.HTTP.Headers
, simpleHTTP
, simpleHTTP_
, sendHTTP
, sendHTTP_notify
, receiveHTTP
, respondHTTP
, module Network.TCP
, getRequest
, headRequest
, postRequest
, postRequestWithBody
, getResponseBody
, getResponseCode
) where
import Network.HTTP.Headers
import Network.HTTP.Base
import qualified Network.HTTP.HandleStream as S
import Network.TCP
import Network.Stream ( Result )
import Network.URI ( parseURI )
import Data.Maybe ( fromMaybe )
simpleHTTP :: (HStream ty) => Request ty -> IO (Result (Response ty))
simpleHTTP r = do
auth <- getAuth r
failHTTPS (rqURI r)
c <- openStream (host auth) (fromMaybe 80 (port auth))
let norm_r = normalizeRequest defaultNormalizeRequestOptions{normDoClose=True} r
simpleHTTP_ c norm_r
simpleHTTP_ :: HStream ty => HandleStream ty -> Request ty -> IO (Result (Response ty))
simpleHTTP_ s r = do
let norm_r = normalizeRequest defaultNormalizeRequestOptions{normDoClose=True} r
S.sendHTTP s norm_r
sendHTTP :: HStream ty => HandleStream ty -> Request ty -> IO (Result (Response ty))
sendHTTP conn rq = do
let norm_r = normalizeRequest defaultNormalizeRequestOptions rq
S.sendHTTP conn norm_r
sendHTTP_notify :: HStream ty
=> HandleStream ty
-> Request ty
-> IO ()
-> IO (Result (Response ty))
sendHTTP_notify conn rq onSendComplete = do
let norm_r = normalizeRequest defaultNormalizeRequestOptions rq
S.sendHTTP_notify conn norm_r onSendComplete
receiveHTTP :: HStream ty => HandleStream ty -> IO (Result (Request ty))
receiveHTTP conn = S.receiveHTTP conn
respondHTTP :: HStream ty => HandleStream ty -> Response ty -> IO ()
respondHTTP conn rsp = S.respondHTTP conn rsp
getRequest
:: String
-> Request_String
getRequest urlString =
case parseURI urlString of
Nothing -> error ("getRequest: Not a valid URL - " ++ urlString)
Just u -> mkRequest GET u
headRequest
:: String
-> Request_String
headRequest urlString =
case parseURI urlString of
Nothing -> error ("headRequest: Not a valid URL - " ++ urlString)
Just u -> mkRequest HEAD u
postRequest
:: String
-> Request_String
postRequest urlString =
case parseURI urlString of
Nothing -> error ("postRequest: Not a valid URL - " ++ urlString)
Just u -> mkRequest POST u
postRequestWithBody
:: String
-> String
-> String
-> Request_String
postRequestWithBody urlString typ body =
case parseURI urlString of
Nothing -> error ("postRequestWithBody: Not a valid URL - " ++ urlString)
Just u -> setRequestBody (mkRequest POST u) (typ, body)
getResponseBody :: Result (Response ty) -> IO ty
getResponseBody (Left err) = fail (show err)
getResponseBody (Right r) = return (rspBody r)
getResponseCode :: Result (Response ty) -> IO ResponseCode
getResponseCode (Left err) = fail (show err)
getResponseCode (Right r) = return (rspCode r)