]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Response.hs
Yet Another Huge Changes
[Lucu.git] / Network / HTTP / Lucu / Response.hs
1 {-# LANGUAGE
2     DeriveDataTypeable
3   , OverloadedStrings
4   , RecordWildCards
5   , UnboxedTuples
6   , UnicodeSyntax
7   , ViewPatterns
8   #-}
9
10 -- |Definition of things related on HTTP response.
11 module Network.HTTP.Lucu.Response
12     ( StatusCode(..)
13     , printStatusCode
14
15     , Response(..)
16     , emptyResponse
17     , resCanHaveBody
18     , printResponse
19
20     , isInformational
21     , isSuccessful
22     , isRedirection
23     , isError
24     , isClientError
25     , isServerError
26
27     , statusCode
28     )
29     where
30 import Data.Ascii (Ascii, AsciiBuilder)
31 import qualified Data.Ascii as A
32 import Data.Monoid.Unicode
33 import Data.Typeable
34 import Network.HTTP.Lucu.Headers
35 import Network.HTTP.Lucu.HttpVersion
36 import Network.HTTP.Lucu.Utils
37 import Prelude.Unicode
38
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
42 -- Timeout\" is 504.
43 data StatusCode = Continue
44                 | SwitchingProtocols
45                 | Processing
46                 -- 
47                 | Ok
48                 | Created
49                 | Accepted
50                 | NonAuthoritativeInformation
51                 | NoContent
52                 | ResetContent
53                 | PartialContent
54                 | MultiStatus
55                 --
56                 | MultipleChoices
57                 | MovedPermanently
58                 | Found
59                 | SeeOther
60                 | NotModified
61                 | UseProxy
62                 | TemporaryRedirect
63                 --
64                 | BadRequest
65                 | Unauthorized
66                 | PaymentRequired
67                 | Forbidden
68                 | NotFound
69                 | MethodNotAllowed
70                 | NotAcceptable
71                 | ProxyAuthenticationRequired
72                 | RequestTimeout
73                 | Conflict
74                 | Gone
75                 | LengthRequired
76                 | PreconditionFailed
77                 | RequestEntityTooLarge
78                 | RequestURITooLarge
79                 | UnsupportedMediaType
80                 | RequestRangeNotSatisfiable
81                 | ExpectationFailed
82                 | UnprocessableEntitiy
83                 | Locked
84                 | FailedDependency
85                 --
86                 | InternalServerError
87                 | NotImplemented
88                 | BadGateway
89                 | ServiceUnavailable
90                 | GatewayTimeout
91                 | HttpVersionNotSupported
92                 | InsufficientStorage
93                   deriving (Eq, Show, Typeable)
94
95 -- |Convert a 'StatusCode' to 'AsciiBuilder'.
96 printStatusCode ∷ StatusCode → AsciiBuilder
97 {-# INLINEABLE printStatusCode #-}
98 printStatusCode (statusCode → (# num, msg #))
99     = ( show3 num            ⊕
100         A.toAsciiBuilder " " ⊕
101         A.toAsciiBuilder msg
102       )
103
104 data Response = Response {
105       resVersion ∷ !HttpVersion
106     , resStatus  ∷ !StatusCode
107     , resHeaders ∷ !Headers
108     } deriving (Show, Eq)
109
110 instance HasHeaders Response where
111     getHeaders         = resHeaders
112     setHeaders res hdr = res { resHeaders = hdr }
113
114 -- |Returns an HTTP\/1.1 'Response' with no header fields.
115 emptyResponse ∷ StatusCode → Response
116 emptyResponse sc
117     = Response {
118         resVersion = HttpVersion 1 1
119       , resStatus  = sc
120       , resHeaders = (∅)
121       }
122
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
132     | otherwise                 = True
133
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
143
144 -- |@'isInformational' sc@ returns 'True' iff @sc < 200@.
145 isInformational ∷ StatusCode → Bool
146 {-# INLINE isInformational #-}
147 isInformational = satisfy (< 200)
148
149 -- |@'isSuccessful' sc@ returns 'True' iff @200 <= sc < 300@.
150 isSuccessful ∷ StatusCode → Bool
151 {-# INLINE isSuccessful #-}
152 isSuccessful = satisfy (\ n → n ≥ 200 ∧ n < 300)
153
154 -- |@'isRedirection' sc@ returns 'True' iff @300 <= sc < 400@.
155 isRedirection ∷ StatusCode → Bool
156 {-# INLINE isRedirection #-}
157 isRedirection = satisfy (\ n → n ≥ 300 ∧ n < 400)
158
159 -- |@'isError' sc@ returns 'True' iff @400 <= sc@
160 isError ∷ StatusCode → Bool
161 {-# INLINE isError #-}
162 isError = satisfy (≥ 400)
163
164 -- |@'isClientError' sc@ returns 'True' iff @400 <= sc < 500@.
165 isClientError ∷ StatusCode → Bool
166 {-# INLINE isClientError #-}
167 isClientError = satisfy (\ n → n ≥ 400 ∧ n < 500)
168
169 -- |@'isServerError' sc@ returns 'True' iff @500 <= sc@.
170 isServerError ∷ StatusCode → Bool
171 {-# INLINE isServerError #-}
172 isServerError = satisfy (≥ 500)
173
174 satisfy ∷ (Int → Bool) → StatusCode → Bool
175 {-# INLINE satisfy #-}
176 satisfy p (statusCode → (# num, _ #)) = p num
177
178 -- |@'statusCode' sc@ returns an unboxed tuple of numeric and textual
179 -- representation of @sc@.
180 statusCode ∷ StatusCode → (# Int, Ascii #)
181 {-# INLINEABLE statusCode #-}
182
183 statusCode Continue                    = (# 100, "Continue"                      #)
184 statusCode SwitchingProtocols          = (# 101, "Switching Protocols"           #)
185 statusCode Processing                  = (# 102, "Processing"                    #)
186
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"                  #)
195
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"            #)
203
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"             #)
225
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"          #)