]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - Rakka/Resource/Object.hs
Resurrection from bitrot
[Rakka.git] / Rakka / Resource / Object.hs
index 9e30d1eee78d919f8094c10ab02adb886c78b0c4..3a98b1e262810fdb403ac69c81fb7bb2fda9a7a6 100644 (file)
@@ -1,22 +1,68 @@
+{-# LANGUAGE
+    UnicodeSyntax
+  #-}
 module Rakka.Resource.Object
     ( resObject
     )
     where
-
+import qualified Codec.Binary.UTF8.String as UTF8
+import Control.Monad.Unicode
+import qualified Data.Text as T
 import           Network.HTTP.Lucu
+import Prelude.Unicode
 import           Rakka.Environment
+import           Rakka.Page
+import           Rakka.Storage
+import           Rakka.SystemConfig
+import           System.FilePath.Posix
 
-
-resObject :: Environment -> ResourceDef
+resObject ∷ Environment → ResourceDef
 resObject env
     = ResourceDef {
         resUsesNativeThread = False
-      , resIsGreedy         = False
-      , resGet              = Just $ do setContentType $ read "text/plain"
-                                        output "FIXME: not implemented"
+      , resIsGreedy         = True
+      , resGet              = Just $ getPathInfo ≫= handleGet env ∘ toPageName
       , resHead             = Nothing
       , resPost             = Nothing
       , resPut              = Nothing
       , resDelete           = Nothing
       }
+    where
+      toPageName ∷ [String] → PageName
+      toPageName = T.pack ∘ UTF8.decodeString . joinPath
+
+handleGet :: Environment -> PageName -> Resource ()
+handleGet env name
+    = do pageM <- getPage (envStorage env) name Nothing
+         case pageM of
+           Nothing   -> foundNoEntity Nothing
+           Just page -> if isEntity page then
+                            handleGetEntity page
+                        else
+                            handleRedirect env page
+
+
+{-
+  HTTP/1.1 302 Found
+  Location: http://example.org/object/Destination
+-}
+handleRedirect :: Environment -> Page -> Resource ()
+handleRedirect env redir
+    = do BaseURI baseURI <- getSysConf (envSysConf env)
+         redirect Found (mkObjectURI baseURI $ redirName redir)
+
+
+{-
+  HTTP/1.1 200 OK
+  Content-Type: image/png
+  
+  ...
+-}
+handleGetEntity :: Page -> Resource ()
+handleGetEntity page
+    = do case entityRevision page of
+           0   -> foundTimeStamp (entityLastMod page) -- 0 はデフォルトページ
+           rev -> foundEntity (strongETag $ show rev) (entityLastMod page)
 
+         setContentType (entityType page)
+         outputLBS (entityContent page)