]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Resource/Internal.hs
Code clean-up using convertible-text.
[Lucu.git] / Network / HTTP / Lucu / Resource / Internal.hs
index f6d17b6b2f367e32d4956cdccd68431165414db2..9feca7edf6e3b6401fc7e4306705a420553fe016 100644 (file)
@@ -38,14 +38,15 @@ import Control.Concurrent
 import Control.Concurrent.STM
 import Control.Exception
 import Control.Monad hiding (mapM_)
+import Control.Monad.Fix
 import Control.Monad.IO.Class
 import Control.Monad.Reader (ReaderT, runReaderT, ask)
 import Control.Monad.Unicode
-import Data.Ascii (Ascii, CIAscii)
-import qualified Data.Ascii as A
+import Data.Ascii (Ascii, CIAscii, AsciiBuilder)
 import Data.ByteString (ByteString)
 import qualified Data.ByteString as BS
 import Data.Collections
+import Data.Convertible.Base
 import Data.List (intersperse, nub)
 import Data.Maybe
 import Data.Monoid
@@ -75,7 +76,7 @@ newtype Rsrc a
     = Rsrc {
         unRsrc ∷ ReaderT NormalInteraction IO a
       }
-    deriving (Applicative, Functor, Monad, MonadIO)
+    deriving (Applicative, Functor, Monad, MonadFix, MonadIO)
 
 runRsrc ∷ Rsrc a → NormalInteraction → IO a
 runRsrc = runReaderT ∘ unRsrc
@@ -179,10 +180,10 @@ spawnRsrc (Resource {..}) ni@(NI {..})
       notAllowed ∷ Rsrc ()
       notAllowed = do setStatus MethodNotAllowed
                       setHeader "Allow"
-                          $ A.fromAsciiBuilder
+                          $ cs
                           $ mconcat
-                          $ intersperse (A.toAsciiBuilder ", ")
-                          $ map A.toAsciiBuilder allowedMethods
+                          $ intersperse (cs (", " ∷ Ascii) ∷ AsciiBuilder)
+                          $ map cs allowedMethods
 
       allowedMethods ∷ [Ascii]
       allowedMethods = nub $ concat [ methods resGet    ["GET"]
@@ -261,25 +262,30 @@ getRequest ∷ Rsrc Request
 getRequest = niRequest <$> getInteraction
 
 -- |Get the path of this 'Rsrc' (to be exact, 'Resource') in the
--- 'Network.HTTP.Lucu.Resource.Tree.ResTree'. The result of this
+-- corresponding 'Network.HTTP.Lucu.ResourceTree'. The result of this
 -- action is the exact path in the tree even when the 'Resource' is
--- greedy.
+-- 'Network.HTTP.Lucu.greedy'.
 --
 -- Example:
 --
--- > main = let tree = mkResTree [ (["foo"], resFoo) ]
--- >        in runHttpd defaultConfig tree []
--- >
--- > resFoo = emptyResource {
--- >     resIsGreedy = True
--- >   , resGet = Just $ do requestURI   <- getRequestURI
--- >                        resourcePath <- getResourcePath
--- >                        pathInfo     <- getPathInfo
--- >                        -- uriPath requestURI == "/foo/bar/baz"
--- >                        -- resourcePath       == ["foo"]
--- >                        -- pathInfo           == ["bar", "baz"]
--- >                        ...
--- >   }
+-- @
+--   main :: 'IO' ()
+--   main = let tree :: 'Network.HTTP.Lucu.ResourceTree'
+--              tree = 'fromList' [ (["foo"], 'Network.HTTP.Lucu.greedy' resFoo) ]
+--          in 'Network.withSocketsDo' '.' 'Network.HTTP.Lucu.runHttpd' 'defaultConfig' $ 'Network.HTTP.Lucu.resourceMap' tree
+--
+--   resFoo :: 'Resource'
+--   resFoo = 'singleton'
+--            ( 'GET'
+--            , do requestURI   <- 'getRequestURI'
+--                 resourcePath <- 'getResourcePath'
+--                 pathInfo     <- 'getPathInfo'
+--                 -- 'Network.URI.uriPath' requestURI '==' \"/foo/bar/baz\"
+--                 -- resourcePath       == ["foo"]
+--                 -- pathInfo           == ["bar", "baz"]
+--                 ...
+--            )
+-- @
 getResourcePath ∷ Rsrc Path
 getResourcePath = niResourcePath <$> getInteraction
 
@@ -330,7 +336,7 @@ setStatus sc
 -- |@'setHeader' name value@ declares the value of the response header
 -- @name@ as @value@. Note that this function is not intended to be
 -- used so frequently: there should be specialised functions like
--- 'setContentType' for every common headers.
+-- 'Network.HTTP.Lucu.setContentType' for every common headers.
 --
 -- Some important headers (especially \"Content-Length\" and
 -- \"Transfer-Encoding\") may be silently dropped or overwritten by
@@ -375,8 +381,8 @@ deleteHeader name = liftIO ∘ atomically ∘ go =≪ getInteraction
 -- infinitely long stream of octets.
 --
 -- Note that you must first declare the response header
--- \"Content-Type\" before applying this function. See:
--- 'setContentType'
+-- \"Content-Type\" before applying this function. See
+-- 'Network.HTTP.Lucu.setContentType'.
 putBuilder ∷ Builder → Rsrc ()
 putBuilder b = liftIO ∘ atomically ∘ go =≪ getInteraction
     where