10 -- |Definition of things related on HTTP response.
11 module Network.HTTP.Lucu.Response
30 import Data.Ascii (Ascii, AsciiBuilder)
31 import qualified Data.Ascii as A
32 import Data.Monoid.Unicode
34 import Network.HTTP.Lucu.Headers
35 import Network.HTTP.Lucu.HttpVersion
36 import Network.HTTP.Lucu.Utils
37 import Prelude.Unicode
39 -- |This is the definition of HTTP status code.
40 -- 'Network.HTTP.Lucu.Resource.setStatus' accepts these named status
41 -- codes so you don't have to memorize, for instance, that \"Gateway
43 data StatusCode = Continue
50 | NonAuthoritativeInformation
71 | ProxyAuthenticationRequired
77 | RequestEntityTooLarge
79 | UnsupportedMediaType
80 | RequestRangeNotSatisfiable
82 | UnprocessableEntitiy
91 | HttpVersionNotSupported
93 deriving (Eq, Show, Typeable)
95 -- |Convert a 'StatusCode' to an 'AsciiBuilder'.
96 printStatusCode ∷ StatusCode → AsciiBuilder
97 {-# INLINEABLE printStatusCode #-}
98 printStatusCode (statusCode → (# num, msg #))
100 A.toAsciiBuilder " " ⊕
104 -- |This is the definition of an HTTP response.
105 data Response = Response {
106 resVersion ∷ !HttpVersion
107 , resStatus ∷ !StatusCode
108 , resHeaders ∷ !Headers
109 } deriving (Show, Eq)
111 instance HasHeaders Response where
112 getHeaders = resHeaders
113 setHeaders res hdr = res { resHeaders = hdr }
115 -- |Returns an HTTP\/1.1 'Response' with no header fields.
116 emptyResponse ∷ StatusCode → Response
119 resVersion = HttpVersion 1 1
124 -- |Returns 'True' iff a given 'Response' allows the existence of
125 -- response entity body.
126 resCanHaveBody ∷ Response → Bool
127 {-# INLINEABLE resCanHaveBody #-}
128 resCanHaveBody (Response {..})
129 | isInformational resStatus = False
130 | resStatus ≡ NoContent = False
131 | resStatus ≡ ResetContent = False
132 | resStatus ≡ NotModified = False
135 -- |Convert a 'Response' to 'AsciiBuilder'.
136 printResponse ∷ Response → AsciiBuilder
137 {-# INLINEABLE printResponse #-}
138 printResponse (Response {..})
139 = printHttpVersion resVersion ⊕
140 A.toAsciiBuilder " " ⊕
141 printStatusCode resStatus ⊕
142 A.toAsciiBuilder "\x0D\x0A" ⊕
143 printHeaders resHeaders
145 -- |@'isInformational' sc@ returns 'True' iff @sc < 200@.
146 isInformational ∷ StatusCode → Bool
147 {-# INLINE isInformational #-}
148 isInformational = satisfy (< 200)
150 -- |@'isSuccessful' sc@ returns 'True' iff @200 <= sc < 300@.
151 isSuccessful ∷ StatusCode → Bool
152 {-# INLINE isSuccessful #-}
153 isSuccessful = satisfy (\ n → n ≥ 200 ∧ n < 300)
155 -- |@'isRedirection' sc@ returns 'True' iff @300 <= sc < 400@.
156 isRedirection ∷ StatusCode → Bool
157 {-# INLINE isRedirection #-}
158 isRedirection = satisfy (\ n → n ≥ 300 ∧ n < 400)
160 -- |@'isError' sc@ returns 'True' iff @400 <= sc@
161 isError ∷ StatusCode → Bool
162 {-# INLINE isError #-}
163 isError = satisfy (≥ 400)
165 -- |@'isClientError' sc@ returns 'True' iff @400 <= sc < 500@.
166 isClientError ∷ StatusCode → Bool
167 {-# INLINE isClientError #-}
168 isClientError = satisfy (\ n → n ≥ 400 ∧ n < 500)
170 -- |@'isServerError' sc@ returns 'True' iff @500 <= sc@.
171 isServerError ∷ StatusCode → Bool
172 {-# INLINE isServerError #-}
173 isServerError = satisfy (≥ 500)
175 satisfy ∷ (Int → Bool) → StatusCode → Bool
176 {-# INLINE satisfy #-}
177 satisfy p (statusCode → (# num, _ #)) = p num
179 -- |@'statusCode' sc@ returns an unboxed tuple of numeric and textual
180 -- representation of @sc@.
181 statusCode ∷ StatusCode → (# Int, Ascii #)
182 {-# INLINEABLE statusCode #-}
184 statusCode Continue = (# 100, "Continue" #)
185 statusCode SwitchingProtocols = (# 101, "Switching Protocols" #)
186 statusCode Processing = (# 102, "Processing" #)
188 statusCode Ok = (# 200, "OK" #)
189 statusCode Created = (# 201, "Created" #)
190 statusCode Accepted = (# 202, "Accepted" #)
191 statusCode NonAuthoritativeInformation = (# 203, "Non Authoritative Information" #)
192 statusCode NoContent = (# 204, "No Content" #)
193 statusCode ResetContent = (# 205, "Reset Content" #)
194 statusCode PartialContent = (# 206, "Partial Content" #)
195 statusCode MultiStatus = (# 207, "Multi Status" #)
197 statusCode MultipleChoices = (# 300, "Multiple Choices" #)
198 statusCode MovedPermanently = (# 301, "Moved Permanently" #)
199 statusCode Found = (# 302, "Found" #)
200 statusCode SeeOther = (# 303, "See Other" #)
201 statusCode NotModified = (# 304, "Not Modified" #)
202 statusCode UseProxy = (# 305, "Use Proxy" #)
203 statusCode TemporaryRedirect = (# 306, "Temporary Redirect" #)
205 statusCode BadRequest = (# 400, "Bad Request" #)
206 statusCode Unauthorized = (# 401, "Unauthorized" #)
207 statusCode PaymentRequired = (# 402, "Payment Required" #)
208 statusCode Forbidden = (# 403, "Forbidden" #)
209 statusCode NotFound = (# 404, "Not Found" #)
210 statusCode MethodNotAllowed = (# 405, "Method Not Allowed" #)
211 statusCode NotAcceptable = (# 406, "Not Acceptable" #)
212 statusCode ProxyAuthenticationRequired = (# 407, "Proxy Authentication Required" #)
213 statusCode RequestTimeout = (# 408, "Request Timeout" #)
214 statusCode Conflict = (# 409, "Conflict" #)
215 statusCode Gone = (# 410, "Gone" #)
216 statusCode LengthRequired = (# 411, "Length Required" #)
217 statusCode PreconditionFailed = (# 412, "Precondition Failed" #)
218 statusCode RequestEntityTooLarge = (# 413, "Request Entity Too Large" #)
219 statusCode RequestURITooLarge = (# 414, "Request URI Too Large" #)
220 statusCode UnsupportedMediaType = (# 415, "Unsupported Media Type" #)
221 statusCode RequestRangeNotSatisfiable = (# 416, "Request Range Not Satisfiable" #)
222 statusCode ExpectationFailed = (# 417, "Expectation Failed" #)
223 statusCode UnprocessableEntitiy = (# 422, "Unprocessable Entity" #)
224 statusCode Locked = (# 423, "Locked" #)
225 statusCode FailedDependency = (# 424, "Failed Dependency" #)
227 statusCode InternalServerError = (# 500, "Internal Server Error" #)
228 statusCode NotImplemented = (# 501, "Not Implemented" #)
229 statusCode BadGateway = (# 502, "Bad Gateway" #)
230 statusCode ServiceUnavailable = (# 503, "Service Unavailable" #)
231 statusCode GatewayTimeout = (# 504, "Gateway Timeout" #)
232 statusCode HttpVersionNotSupported = (# 505, "HTTP Version Not Supported" #)
233 statusCode InsufficientStorage = (# 507, "Insufficient Storage" #)