]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Response.hs
More documentation
[Lucu.git] / Network / HTTP / Lucu / Response.hs
index 1c19da4cc87babe36f9f407aed2fdf1615e2ff7f..9ca08be016a2c9509d5467c6e8f0111df6106358 100644 (file)
@@ -1,18 +1,30 @@
+-- #prune
+
+-- |Definition of things related on HTTP response.
 module Network.HTTP.Lucu.Response
     ( StatusCode(..)
     , Response(..)
-    , hPutResponse    -- Handle -> Response -> IO ()
-    , isInformational -- StatusCode -> Bool
-    , isError         -- StatusCode -> Bool
-    , statusCode      -- StatusCode -> (Int, String)
+    , hPutResponse
+    , isInformational
+    , isSuccessful
+    , isRedirection
+    , isError
+    , isClientError
+    , isServerError
+    , statusCode
     )
     where
 
+import           Data.Dynamic
 import           Network.HTTP.Lucu.Headers
 import           Network.HTTP.Lucu.HttpVersion
 import           System.IO
 import           Text.Printf
 
+-- |This is the definition of HTTP status code.
+-- 'Network.HTTP.Lucu.Resource.setStatus' accepts these named statuses
+-- so you don't have to memorize, for instance, that \"Gateway
+-- Timeout\" is 504.
 data StatusCode = Continue
                 | SwitchingProtocols
                 | Processing
@@ -63,7 +75,7 @@ data StatusCode = Continue
                 | GatewayTimeout
                 | HttpVersionNotSupported
                 | InsufficientStorage
-                  deriving (Eq)
+                  deriving (Typeable, Eq)
 
 instance Show StatusCode where
     show sc = let (num, msg) = statusCode sc
@@ -75,8 +87,8 @@ data Response = Response {
       resVersion :: HttpVersion
     , resStatus  :: StatusCode
     , resHeaders :: Headers
-    }
-                deriving (Show, Eq)
+    } deriving (Show, Eq)
+
 
 instance HasHeaders Response where
     getHeaders = resHeaders
@@ -95,16 +107,39 @@ hPutStatus h sc = let (num, msg) = statusCode sc
                   in
                     hPrintf h "%03d %s" num msg
 
-
+-- |@'isInformational' sc@ is True iff @sc < 200@.
 isInformational :: StatusCode -> Bool
-isInformational sc = let (num, _) = statusCode sc
-                     in num < 200
+isInformational = doesMeet (< 200)
+
+-- |@'isSuccessful' sc@ is True iff @200 <= sc < 300@.
+isSuccessful :: StatusCode -> Bool
+isSuccessful = doesMeet (\ n -> n >= 200 && n < 300)
+
+-- |@'isRedirection' sc@ is True iff @300 <= sc < 400@.
+isRedirection :: StatusCode -> Bool
+isRedirection = doesMeet (\ n -> n >= 300 && n < 400)
 
+-- |@'isError' sc@ is True iff @400 <= sc@
 isError :: StatusCode -> Bool
-isError sc = let (num, _) = statusCode sc
-             in num >= 400
+isError = doesMeet (>= 400)
+
+-- |@'isClientError' sc@ is True iff @400 <= sc < 500@.
+isClientError :: StatusCode -> Bool
+isClientError = doesMeet (\ n -> n >= 400 && n < 500)
+
+-- |@'isServerError' sc@ is True iff @500 <= sc@.
+isServerError :: StatusCode -> Bool
+isServerError = doesMeet (>= 500)
+
+
+doesMeet :: (Int -> Bool) -> StatusCode -> Bool
+doesMeet p sc = let (num, _) = statusCode sc
+                in
+                  p num
 
 
+-- |@'statusCode' sc@ returns a tuple of numeric and textual
+-- representation of @sc@.
 statusCode :: StatusCode -> (Int, String)
 statusCode Continue                    = (100, "Continue")
 statusCode SwitchingProtocols          = (101, "Switching Protocols")