]> gitweb @ CieloNegro.org - Rakka.git/blob - Rakka/Resource/Object.hs
Resurrection from bitrot
[Rakka.git] / Rakka / Resource / Object.hs
1 {-# LANGUAGE
2     UnicodeSyntax
3   #-}
4 module Rakka.Resource.Object
5     ( resObject
6     )
7     where
8 import qualified Codec.Binary.UTF8.String as UTF8
9 import Control.Monad.Unicode
10 import qualified Data.Text as T
11 import           Network.HTTP.Lucu
12 import Prelude.Unicode
13 import           Rakka.Environment
14 import           Rakka.Page
15 import           Rakka.Storage
16 import           Rakka.SystemConfig
17 import           System.FilePath.Posix
18
19 resObject ∷ Environment → ResourceDef
20 resObject env
21     = ResourceDef {
22         resUsesNativeThread = False
23       , resIsGreedy         = True
24       , resGet              = Just $ getPathInfo ≫= handleGet env ∘ toPageName
25       , resHead             = Nothing
26       , resPost             = Nothing
27       , resPut              = Nothing
28       , resDelete           = Nothing
29       }
30     where
31       toPageName ∷ [String] → PageName
32       toPageName = T.pack ∘ UTF8.decodeString . joinPath
33
34 handleGet :: Environment -> PageName -> Resource ()
35 handleGet env name
36     = do pageM <- getPage (envStorage env) name Nothing
37          case pageM of
38            Nothing   -> foundNoEntity Nothing
39            Just page -> if isEntity page then
40                             handleGetEntity page
41                         else
42                             handleRedirect env page
43
44
45 {-
46   HTTP/1.1 302 Found
47   Location: http://example.org/object/Destination
48 -}
49 handleRedirect :: Environment -> Page -> Resource ()
50 handleRedirect env redir
51     = do BaseURI baseURI <- getSysConf (envSysConf env)
52          redirect Found (mkObjectURI baseURI $ redirName redir)
53
54
55 {-
56   HTTP/1.1 200 OK
57   Content-Type: image/png
58   
59   ...
60 -}
61 handleGetEntity :: Page -> Resource ()
62 handleGetEntity page
63     = do case entityRevision page of
64            0   -> foundTimeStamp (entityLastMod page) -- 0 はデフォルトページ
65            rev -> foundEntity (strongETag $ show rev) (entityLastMod page)
66
67          setContentType (entityType page)
68          outputLBS (entityContent page)