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