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