]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Response.hs
Fixed many bugs...
[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     , resCanHaveBody
17     , printResponse
18
19     , isInformational
20     , isSuccessful
21     , isRedirection
22     , isError
23     , isClientError
24     , isServerError
25
26     , statusCode
27     )
28     where
29 import Data.Ascii (Ascii, AsciiBuilder)
30 import qualified Data.Ascii as A
31 import Data.Monoid.Unicode
32 import Data.Typeable
33 import Network.HTTP.Lucu.Headers
34 import Network.HTTP.Lucu.HttpVersion
35 import Network.HTTP.Lucu.Utils
36 import Prelude.Unicode
37
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
41 -- Timeout\" is 504.
42 data StatusCode = Continue
43                 | SwitchingProtocols
44                 | Processing
45                 -- 
46                 | Ok
47                 | Created
48                 | Accepted
49                 | NonAuthoritativeInformation
50                 | NoContent
51                 | ResetContent
52                 | PartialContent
53                 | MultiStatus
54                 --
55                 | MultipleChoices
56                 | MovedPermanently
57                 | Found
58                 | SeeOther
59                 | NotModified
60                 | UseProxy
61                 | TemporaryRedirect
62                 --
63                 | BadRequest
64                 | Unauthorized
65                 | PaymentRequired
66                 | Forbidden
67                 | NotFound
68                 | MethodNotAllowed
69                 | NotAcceptable
70                 | ProxyAuthenticationRequired
71                 | RequestTimeout
72                 | Conflict
73                 | Gone
74                 | LengthRequired
75                 | PreconditionFailed
76                 | RequestEntityTooLarge
77                 | RequestURITooLarge
78                 | UnsupportedMediaType
79                 | RequestRangeNotSatisfiable
80                 | ExpectationFailed
81                 | UnprocessableEntitiy
82                 | Locked
83                 | FailedDependency
84                 --
85                 | InternalServerError
86                 | NotImplemented
87                 | BadGateway
88                 | ServiceUnavailable
89                 | GatewayTimeout
90                 | HttpVersionNotSupported
91                 | InsufficientStorage
92                   deriving (Eq, Show, Typeable)
93
94 -- |Convert a 'StatusCode' to 'AsciiBuilder'.
95 printStatusCode ∷ StatusCode → AsciiBuilder
96 {-# INLINEABLE printStatusCode #-}
97 printStatusCode (statusCode → (# num, msg #))
98     = ( show3 num            ⊕
99         A.toAsciiBuilder " " ⊕
100         A.toAsciiBuilder msg
101       )
102
103 data Response = Response {
104       resVersion ∷ !HttpVersion
105     , resStatus  ∷ !StatusCode
106     , resHeaders ∷ !Headers
107     } deriving (Show, Eq)
108
109 instance HasHeaders Response where
110     {-# INLINE getHeaders #-}
111     getHeaders = resHeaders
112     {-# INLINE setHeaders #-}
113     setHeaders res hdr = res { resHeaders = hdr }
114
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
124     | otherwise                 = True
125
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
135
136 -- |@'isInformational' sc@ returns 'True' iff @sc < 200@.
137 isInformational ∷ StatusCode → Bool
138 {-# INLINE isInformational #-}
139 isInformational = satisfy (< 200)
140
141 -- |@'isSuccessful' sc@ returns 'True' iff @200 <= sc < 300@.
142 isSuccessful ∷ StatusCode → Bool
143 {-# INLINE isSuccessful #-}
144 isSuccessful = satisfy (\ n → n ≥ 200 ∧ n < 300)
145
146 -- |@'isRedirection' sc@ returns 'True' iff @300 <= sc < 400@.
147 isRedirection ∷ StatusCode → Bool
148 {-# INLINE isRedirection #-}
149 isRedirection = satisfy (\ n → n ≥ 300 ∧ n < 400)
150
151 -- |@'isError' sc@ returns 'True' iff @400 <= sc@
152 isError ∷ StatusCode → Bool
153 {-# INLINE isError #-}
154 isError = satisfy (≥ 400)
155
156 -- |@'isClientError' sc@ returns 'True' iff @400 <= sc < 500@.
157 isClientError ∷ StatusCode → Bool
158 {-# INLINE isClientError #-}
159 isClientError = satisfy (\ n → n ≥ 400 ∧ n < 500)
160
161 -- |@'isServerError' sc@ returns 'True' iff @500 <= sc@.
162 isServerError ∷ StatusCode → Bool
163 {-# INLINE isServerError #-}
164 isServerError = satisfy (≥ 500)
165
166 satisfy ∷ (Int → Bool) → StatusCode → Bool
167 {-# INLINE satisfy #-}
168 satisfy p (statusCode → (# num, _ #)) = p num
169
170 -- |@'statusCode' sc@ returns an unboxed tuple of numeric and textual
171 -- representation of @sc@.
172 statusCode ∷ StatusCode → (# Int, Ascii #)
173 {-# INLINEABLE statusCode #-}
174
175 statusCode Continue                    = (# 100, "Continue"                      #)
176 statusCode SwitchingProtocols          = (# 101, "Switching Protocols"           #)
177 statusCode Processing                  = (# 102, "Processing"                    #)
178
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"                  #)
187
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"            #)
195
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"             #)
217
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"          #)