module Network.HTTP.Lucu.Response
( Response(..)
, emptyResponse
, setStatusCode
, resCanHaveBody
, isInformational
, isSuccessful
, isRedirection
, isError
, isClientError
, isServerError
)
where
import Data.Ascii (Ascii, AsciiBuilder)
import Data.Convertible.Base
import Data.Convertible.Instances.Ascii ()
import Data.Convertible.Utils
import Data.Monoid.Unicode
import Network.HTTP.Lucu.Headers
import Network.HTTP.Lucu.HttpVersion
import Network.HTTP.Lucu.Response.StatusCode
import Prelude.Unicode
data Response = Response {
resVersion ∷ !HttpVersion
, resStatus ∷ !SomeStatusCode
, resHeaders ∷ !Headers
} deriving (Show, Eq)
instance HasHeaders Response where
getHeaders = resHeaders
setHeaders res hdr = res { resHeaders = hdr }
instance ConvertSuccess Response Ascii where
convertSuccess = convertSuccessVia ((⊥) ∷ AsciiBuilder)
instance ConvertSuccess Response AsciiBuilder where
convertSuccess (Response {..})
= cs resVersion ⊕
cs (" " ∷ Ascii) ⊕
cs resStatus ⊕
cs ("\x0D\x0A" ∷ Ascii) ⊕
cs resHeaders
deriveAttempts [ ([t| Response |], [t| Ascii |])
, ([t| Response |], [t| AsciiBuilder |])
]
emptyResponse ∷ StatusCode sc ⇒ sc → Response
emptyResponse sc
= Response {
resVersion = HttpVersion 1 1
, resStatus = fromStatusCode sc
, resHeaders = (∅)
}
setStatusCode ∷ StatusCode sc ⇒ sc → Response → Response
setStatusCode sc res
= res {
resStatus = fromStatusCode sc
}
resCanHaveBody ∷ Response → Bool
resCanHaveBody (Response {..})
| isInformational resStatus = False
| resStatus ≡ cs NoContent = False
| resStatus ≡ cs ResetContent = False
| resStatus ≡ cs NotModified = False
| otherwise = True
isInformational ∷ StatusCode sc ⇒ sc → Bool
isInformational = satisfy (< 200)
isSuccessful ∷ StatusCode sc ⇒ sc → Bool
isSuccessful = satisfy (\ n → n ≥ 200 ∧ n < 300)
isRedirection ∷ StatusCode sc ⇒ sc → Bool
isRedirection = satisfy (\ n → n ≥ 300 ∧ n < 400)
isError ∷ StatusCode sc ⇒ sc → Bool
isError = satisfy (≥ 400)
isClientError ∷ StatusCode sc ⇒ sc → Bool
isClientError = satisfy (\ n → n ≥ 400 ∧ n < 500)
isServerError ∷ StatusCode sc ⇒ sc → Bool
isServerError = satisfy (≥ 500)
satisfy ∷ StatusCode sc ⇒ (Int → Bool) → sc → Bool
satisfy p (numericCode → num) = p num