]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Resource.hs
Transfer-Encoding is always overwritten / foundEntity refuses POST requests / Documen...
[Lucu.git] / Network / HTTP / Lucu / Resource.hs
index ab594b096b3572eae2d5984a559b515af1ae3816..d87e509b89d479ffcd39b97d58e0c01724bcb337 100644 (file)
@@ -104,8 +104,6 @@ module Network.HTTP.Lucu.Resource
     , setStatus
     , setHeader
     , redirect
-    , setETag
-    , setLastModified
     , setContentType
 
     -- ** Writing a response body
@@ -248,6 +246,20 @@ getContentType = do cType <- getHeader "Content-Type"
 
 {- ExaminingRequest 時に使用するアクション群 -}
 
+-- |Tell the system that the 'Resource' found an entity for the
+-- request URI. If this is a GET or HEAD request, a found entity means
+-- a datum to be replied. If this is a PUT or DELETE request, it means
+-- a datum which was stored for the URI up to now. It is an error to
+-- compute 'foundEntity' if this is a POST request.
+--
+-- Computation of 'foundEntity' performs \"If-Match\" test or
+-- \"If-None-Match\" test if possible. When those tests fail, the
+-- computation of 'Resource' immediately aborts with status \"412
+-- Precondition Failed\" or \"304 Not Modified\" depending on the
+-- situation.
+--
+-- If this is a GET or HEAD request, 'foundEntity' automatically puts
+-- \"ETag\" and \"Last-Modified\" headers into the response.
 foundEntity :: ETag -> ClockTime -> Resource ()
 foundEntity tag timeStamp
     = do driftTo ExaminingRequest
@@ -255,11 +267,20 @@ foundEntity tag timeStamp
          method <- getMethod
          when (method == GET || method == HEAD)
                   $ setHeader' "Last-Modified" $ formatHTTPDateTime timeStamp
+         when (method == POST)
+                  $ abort InternalServerError []
+                        (Just "Illegal computation of foundEntity for POST request.")
          foundETag tag
 
          driftTo GettingBody
 
-
+-- |Tell the system that the 'Resource' found an entity for the
+-- request URI. The only difference from 'foundEntity' is that
+-- 'foundETag' doesn't (and can't) put \"Last-Modified\" header into
+-- the response.
+--
+-- This action is not preferred. You should use 'foundEntity' when
+-- possible.
 foundETag :: ETag -> Resource ()
 foundETag tag
     = do driftTo ExaminingRequest
@@ -267,6 +288,9 @@ foundETag tag
          method <- getMethod
          when (method == GET || method == HEAD)
                   $ setHeader' "ETag" $ show tag
+         when (method == POST)
+                  $ abort InternalServerError []
+                        (Just "Illegal computation of foundETag for POST request.")
 
          -- If-Match があればそれを見る。
          ifMatch <- getHeader "If-Match"
@@ -300,7 +324,16 @@ foundETag tag
 
          driftTo GettingBody
 
-
+-- |Tell the system that the 'Resource' found an entity for the
+-- request URI. The only difference from 'foundEntity' is that
+-- 'foundTimeStamp' performs \"If-Modified-Since\" test or
+-- \"If-Unmodified-Since\" test instead of \"If-Match\" test or
+-- \"If-None-Match\" test. Be aware that any tests based on last
+-- modification time are unsafe because it is possible to mess up such
+-- tests by modifying the entity twice in a second.
+--
+-- This action is not preferred. You should use 'foundEntity' when
+-- possible.
 foundTimeStamp :: ClockTime -> Resource ()
 foundTimeStamp timeStamp
     = do driftTo ExaminingRequest
@@ -308,6 +341,9 @@ foundTimeStamp timeStamp
          method <- getMethod
          when (method == GET || method == HEAD)
                   $ setHeader' "Last-Modified" $ formatHTTPDateTime timeStamp
+         when (method == POST)
+                  $ abort InternalServerError []
+                        (Just "Illegal computation of foundTimeStamp for POST request.")
 
          let statusForIfModSince = if method == GET || method == HEAD then
                                        NotModified
@@ -340,14 +376,25 @@ foundTimeStamp timeStamp
 
          driftTo GettingBody
 
-
+-- |Computation of @'foundNoEntity' mStr@ tell the system that the
+-- 'Resource' found no entity for the request URI. @mStr@ is an
+-- optional error message to be replied to the client.
+--
+-- If this is a PUT request, 'foundNoEntity' performs \"If-Match\"
+-- test and aborts with status \"412 Precondition Failed\" when it
+-- failed. If this is a GET, HEAD or DELETE request, 'foundNoEntity'
+-- always aborts with status \"404 Not Found\". It is an error to
+-- compute 'foundNoEntity' if this is a POST request.
 foundNoEntity :: Maybe String -> Resource ()
 foundNoEntity msgM
     = do driftTo ExaminingRequest
 
          method <- getMethod
+         when (method == POST)
+                  $ abort InternalServerError []
+                        (Just "Illegal computation of foundNoEntity for POST request.")
          when (method /= PUT)
-              $ abort NotFound [] msgM
+                  $ abort NotFound [] msgM
 
          -- エンティティが存在しないと云ふ事は、"*" も含めたどのやうな
          -- If-Match: 條件も滿たさない。
@@ -518,7 +565,18 @@ setStatus code
                                  resStatus = code
                                }
 
-
+-- | Set a value of given resource header. Comparison of header name
+-- is case-insensitive. Note that this action is not intended to be
+-- used so frequently: there should be an action like 'setContentType'
+-- for every common headers.
+--
+-- Some important headers (especially \"Content-Length\" and
+-- \"Transfer-Encoding\") may be silently deleted or overwritten by
+-- the system not to corrupt the interaction with client at the
+-- viewpoint of HTTP protocol. For instance, if we are keeping
+-- connection alive, for an obvious reason it causes a catastrophe to
+-- send header \"Content-Length: 10\" and actually sending body of 20
+-- bytes long.
 setHeader :: String -> String -> Resource ()
 setHeader name value
     = driftTo DecidingHeader >> setHeader' name value
@@ -541,16 +599,6 @@ redirect code uri
          setHeader "Location" (uriToString id uri $ "")
 
 
-setETag :: ETag -> Resource ()
-setETag tag
-    = setHeader "ETag" $ show tag
-
-
-setLastModified :: ClockTime -> Resource ()
-setLastModified lastmod
-    = setHeader "Last-Modified" $ formatHTTPDateTime lastmod
-
-
 setContentType :: MIMEType -> Resource ()
 setContentType mType
     = setHeader "Content-Type" $ show mType