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 statuses
41 -- 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 'AsciiBuilder'.
96 printStatusCode ∷ StatusCode → AsciiBuilder
97 {-# INLINEABLE printStatusCode #-}
98 printStatusCode (statusCode → (# num, msg #))
100 A.toAsciiBuilder " " ⊕
104 data Response = Response {
105 resVersion ∷ !HttpVersion
106 , resStatus ∷ !StatusCode
107 , resHeaders ∷ !Headers
108 } deriving (Show, Eq)
110 instance HasHeaders Response where
111 getHeaders = resHeaders
112 setHeaders res hdr = res { resHeaders = hdr }
114 -- |Returns an HTTP\/1.1 'Response' with no header fields.
115 emptyResponse ∷ StatusCode → Response
118 resVersion = HttpVersion 1 1
123 -- |Returns 'True' iff a given 'Response' allows the existence of
124 -- response entity body.
125 resCanHaveBody ∷ Response → Bool
126 {-# INLINEABLE resCanHaveBody #-}
127 resCanHaveBody (Response {..})
128 | isInformational resStatus = False
129 | resStatus ≡ NoContent = False
130 | resStatus ≡ ResetContent = False
131 | resStatus ≡ NotModified = False
134 -- |Convert a 'Response' to 'AsciiBuilder'.
135 printResponse ∷ Response → AsciiBuilder
136 {-# INLINEABLE printResponse #-}
137 printResponse (Response {..})
138 = printHttpVersion resVersion ⊕
139 A.toAsciiBuilder " " ⊕
140 printStatusCode resStatus ⊕
141 A.toAsciiBuilder "\x0D\x0A" ⊕
142 printHeaders resHeaders
144 -- |@'isInformational' sc@ returns 'True' iff @sc < 200@.
145 isInformational ∷ StatusCode → Bool
146 {-# INLINE isInformational #-}
147 isInformational = satisfy (< 200)
149 -- |@'isSuccessful' sc@ returns 'True' iff @200 <= sc < 300@.
150 isSuccessful ∷ StatusCode → Bool
151 {-# INLINE isSuccessful #-}
152 isSuccessful = satisfy (\ n → n ≥ 200 ∧ n < 300)
154 -- |@'isRedirection' sc@ returns 'True' iff @300 <= sc < 400@.
155 isRedirection ∷ StatusCode → Bool
156 {-# INLINE isRedirection #-}
157 isRedirection = satisfy (\ n → n ≥ 300 ∧ n < 400)
159 -- |@'isError' sc@ returns 'True' iff @400 <= sc@
160 isError ∷ StatusCode → Bool
161 {-# INLINE isError #-}
162 isError = satisfy (≥ 400)
164 -- |@'isClientError' sc@ returns 'True' iff @400 <= sc < 500@.
165 isClientError ∷ StatusCode → Bool
166 {-# INLINE isClientError #-}
167 isClientError = satisfy (\ n → n ≥ 400 ∧ n < 500)
169 -- |@'isServerError' sc@ returns 'True' iff @500 <= sc@.
170 isServerError ∷ StatusCode → Bool
171 {-# INLINE isServerError #-}
172 isServerError = satisfy (≥ 500)
174 satisfy ∷ (Int → Bool) → StatusCode → Bool
175 {-# INLINE satisfy #-}
176 satisfy p (statusCode → (# num, _ #)) = p num
178 -- |@'statusCode' sc@ returns an unboxed tuple of numeric and textual
179 -- representation of @sc@.
180 statusCode ∷ StatusCode → (# Int, Ascii #)
181 {-# INLINEABLE statusCode #-}
183 statusCode Continue = (# 100, "Continue" #)
184 statusCode SwitchingProtocols = (# 101, "Switching Protocols" #)
185 statusCode Processing = (# 102, "Processing" #)
187 statusCode Ok = (# 200, "OK" #)
188 statusCode Created = (# 201, "Created" #)
189 statusCode Accepted = (# 202, "Accepted" #)
190 statusCode NonAuthoritativeInformation = (# 203, "Non Authoritative Information" #)
191 statusCode NoContent = (# 204, "No Content" #)
192 statusCode ResetContent = (# 205, "Reset Content" #)
193 statusCode PartialContent = (# 206, "Partial Content" #)
194 statusCode MultiStatus = (# 207, "Multi Status" #)
196 statusCode MultipleChoices = (# 300, "Multiple Choices" #)
197 statusCode MovedPermanently = (# 301, "Moved Permanently" #)
198 statusCode Found = (# 302, "Found" #)
199 statusCode SeeOther = (# 303, "See Other" #)
200 statusCode NotModified = (# 304, "Not Modified" #)
201 statusCode UseProxy = (# 305, "Use Proxy" #)
202 statusCode TemporaryRedirect = (# 306, "Temporary Redirect" #)
204 statusCode BadRequest = (# 400, "Bad Request" #)
205 statusCode Unauthorized = (# 401, "Unauthorized" #)
206 statusCode PaymentRequired = (# 402, "Payment Required" #)
207 statusCode Forbidden = (# 403, "Forbidden" #)
208 statusCode NotFound = (# 404, "Not Found" #)
209 statusCode MethodNotAllowed = (# 405, "Method Not Allowed" #)
210 statusCode NotAcceptable = (# 406, "Not Acceptable" #)
211 statusCode ProxyAuthenticationRequired = (# 407, "Proxy Authentication Required" #)
212 statusCode RequestTimeout = (# 408, "Request Timeout" #)
213 statusCode Conflict = (# 409, "Conflict" #)
214 statusCode Gone = (# 410, "Gone" #)
215 statusCode LengthRequired = (# 411, "Length Required" #)
216 statusCode PreconditionFailed = (# 412, "Precondition Failed" #)
217 statusCode RequestEntityTooLarge = (# 413, "Request Entity Too Large" #)
218 statusCode RequestURITooLarge = (# 414, "Request URI Too Large" #)
219 statusCode UnsupportedMediaType = (# 415, "Unsupported Media Type" #)
220 statusCode RequestRangeNotSatisfiable = (# 416, "Request Range Not Satisfiable" #)
221 statusCode ExpectationFailed = (# 417, "Expectation Failed" #)
222 statusCode UnprocessableEntitiy = (# 422, "Unprocessable Entity" #)
223 statusCode Locked = (# 423, "Locked" #)
224 statusCode FailedDependency = (# 424, "Failed Dependency" #)
226 statusCode InternalServerError = (# 500, "Internal Server Error" #)
227 statusCode NotImplemented = (# 501, "Not Implemented" #)
228 statusCode BadGateway = (# 502, "Bad Gateway" #)
229 statusCode ServiceUnavailable = (# 503, "Service Unavailable" #)
230 statusCode GatewayTimeout = (# 504, "Gateway Timeout" #)
231 statusCode HttpVersionNotSupported = (# 505, "HTTP Version Not Supported" #)
232 statusCode InsufficientStorage = (# 507, "Insufficient Storage" #)