]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Response.hs
many 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     , Response(..)
14     , printStatusCode
15     , printResponse
16     , isInformational
17     , isSuccessful
18     , isRedirection
19     , isError
20     , isClientError
21     , isServerError
22     , statusCode
23     )
24     where
25 import Data.Ascii (Ascii, AsciiBuilder)
26 import qualified Data.Ascii as A
27 import Data.Monoid.Unicode
28 import Data.Typeable
29 import Network.HTTP.Lucu.Headers
30 import Network.HTTP.Lucu.HttpVersion
31 import Network.HTTP.Lucu.Utils
32 import Prelude.Unicode
33
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
37 -- Timeout\" is 504.
38 data StatusCode = Continue
39                 | SwitchingProtocols
40                 | Processing
41                 -- 
42                 | Ok
43                 | Created
44                 | Accepted
45                 | NonAuthoritativeInformation
46                 | NoContent
47                 | ResetContent
48                 | PartialContent
49                 | MultiStatus
50                 --
51                 | MultipleChoices
52                 | MovedPermanently
53                 | Found
54                 | SeeOther
55                 | NotModified
56                 | UseProxy
57                 | TemporaryRedirect
58                 --
59                 | BadRequest
60                 | Unauthorized
61                 | PaymentRequired
62                 | Forbidden
63                 | NotFound
64                 | MethodNotAllowed
65                 | NotAcceptable
66                 | ProxyAuthenticationRequired
67                 | RequestTimeout
68                 | Conflict
69                 | Gone
70                 | LengthRequired
71                 | PreconditionFailed
72                 | RequestEntityTooLarge
73                 | RequestURITooLarge
74                 | UnsupportedMediaType
75                 | RequestRangeNotSatisfiable
76                 | ExpectationFailed
77                 | UnprocessableEntitiy
78                 | Locked
79                 | FailedDependency
80                 --
81                 | InternalServerError
82                 | NotImplemented
83                 | BadGateway
84                 | ServiceUnavailable
85                 | GatewayTimeout
86                 | HttpVersionNotSupported
87                 | InsufficientStorage
88                   deriving (Eq, Show, Typeable)
89
90 -- |Convert a 'StatusCode' to 'AsciiBuilder'.
91 printStatusCode ∷ StatusCode → AsciiBuilder
92 printStatusCode (statusCode → (# num, msg #))
93     = ( show3 num            ⊕
94         A.toAsciiBuilder " " ⊕
95         A.toAsciiBuilder msg
96       )
97
98 data Response = Response {
99       resVersion ∷ !HttpVersion
100     , resStatus  ∷ !StatusCode
101     , resHeaders ∷ !Headers
102     } deriving (Show, Eq)
103
104 instance HasHeaders Response where
105     getHeaders = resHeaders
106     setHeaders res hdr = res { resHeaders = hdr }
107
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
116
117 -- |@'isInformational' sc@ is 'Prelude.True' iff @sc < 200@.
118 isInformational ∷ StatusCode → Bool
119 isInformational = doesMeet (< 200)
120
121 -- |@'isSuccessful' sc@ is 'Prelude.True' iff @200 <= sc < 300@.
122 isSuccessful ∷ StatusCode → Bool
123 isSuccessful = doesMeet (\ n → n ≥ 200 ∧ n < 300)
124
125 -- |@'isRedirection' sc@ is 'Prelude.True' iff @300 <= sc < 400@.
126 isRedirection ∷ StatusCode → Bool
127 isRedirection = doesMeet (\ n → n ≥ 300 ∧ n < 400)
128
129 -- |@'isError' sc@ is 'Prelude.True' iff @400 <= sc@
130 isError ∷ StatusCode → Bool
131 isError = doesMeet (≥ 400)
132
133 -- |@'isClientError' sc@ is 'Prelude.True' iff @400 <= sc < 500@.
134 isClientError ∷ StatusCode → Bool
135 isClientError = doesMeet (\ n → n ≥ 400 ∧ n < 500)
136
137 -- |@'isServerError' sc@ is 'Prelude.True' iff @500 <= sc@.
138 isServerError ∷ StatusCode → Bool
139 isServerError = doesMeet (≥ 500)
140
141 doesMeet ∷ (Int → Bool) → StatusCode → Bool
142 {-# INLINE doesMeet #-}
143 doesMeet p (statusCode → (# num, _ #)) = p num
144
145 -- |@'statusCode' sc@ returns an unboxed tuple of numeric and textual
146 -- representation of @sc@.
147 statusCode ∷ StatusCode → (# Int, Ascii #)
148
149 statusCode Continue                    = (# 100, "Continue"                      #)
150 statusCode SwitchingProtocols          = (# 101, "Switching Protocols"           #)
151 statusCode Processing                  = (# 102, "Processing"                    #)
152
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"                  #)
161
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"            #)
169
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"             #)
191
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"          #)