6 module Network.HTTP.Lucu.DefaultPage
12 import qualified Blaze.ByteString.Builder.Char.Utf8 as BB
14 import Control.Arrow.ArrowList
15 import Control.Arrow.ListArrow
16 import Control.Arrow.Unicode
17 import Control.Concurrent.STM
19 import qualified Data.Ascii as A
21 import qualified Data.Text as T
22 import qualified Data.Text.Lazy as Lazy
23 import Network.HTTP.Lucu.Config
24 import Network.HTTP.Lucu.Headers
25 import Network.HTTP.Lucu.Interaction
26 import Network.HTTP.Lucu.Request
27 import Network.HTTP.Lucu.Response
28 import Network.URI hiding (path)
29 import Prelude.Unicode
30 import Text.XML.HXT.Arrow.WriteDocument
31 import Text.XML.HXT.Arrow.XmlArrow
32 import Text.XML.HXT.Arrow.XmlState
33 import Text.XML.HXT.DOM.TypeDefs
35 getDefaultPage ∷ Config → Maybe Request → Response → Lazy.Text
36 {-# INLINEABLE getDefaultPage #-}
37 getDefaultPage conf req res
38 = let msgA = getMsg req res
39 [xmlStr] = runLA ( mkDefaultPage conf (resStatus res) msgA
41 writeDocumentToString [ withIndent True ]
46 writeDefaultPage ∷ Interaction → STM ()
47 writeDefaultPage (Interaction {..})
48 -- Content-Type が正しくなければ補完できない。
49 = do res ← readTVar itrResponse
50 when (getHeader "Content-Type" res ≡ Just defaultPageContentType)
51 $ do let page = getDefaultPage itrConfig itrRequest res
52 putTMVar itrBodyToSend (BB.fromLazyText page)
54 mkDefaultPage ∷ (ArrowXml a) ⇒ Config → StatusCode → a b XmlTree → a b XmlTree
55 {-# INLINEABLE mkDefaultPage #-}
56 mkDefaultPage conf status msgA
57 = let sStr = A.toString $ A.fromAsciiBuilder $ printStatusCode status
58 sig = concat [ A.toString (cnfServerSoftware conf)
60 , T.unpack (cnfServerHost conf)
64 += sattr "xmlns" "http://www.w3.org/1999/xhtml"
73 += ( eelem "p" += msgA )
75 += ( eelem "address" += txt sig ))))
77 getMsg ∷ (ArrowXml a) ⇒ Maybe Request → Response → a b XmlTree
78 {-# INLINEABLE getMsg #-}
80 = case resStatus res of
86 → txt ("The resource at " ⧺ path ⧺ " has been moved to ")
88 eelem "a" += sattr "href" loc
94 → txt ("The resource at " ⧺ path ⧺ " is currently located at ")
96 eelem "a" += sattr "href" loc
99 txt ". This is not a permanent relocation."
102 → txt ("The resource at " ⧺ path ⧺ " can be found at ")
104 eelem "a" += sattr "href" loc
110 → txt ("The resource at " ⧺ path ⧺ " is temporarily located at ")
112 eelem "a" += sattr "href" loc
119 → txt "The server could not understand the request you sent."
122 → txt ("You need a valid authentication to access " ⧺ path)
125 → txt ("You don't have permission to access " ⧺ path)
128 → txt ("The requested URL " ⧺ path ⧺ " was not found on this server.")
131 → txt ("The resource at " ⧺ path ⧺ " was here in past times, but has gone permanently.")
133 RequestEntityTooLarge
134 → txt ("The request entity you sent for " ⧺ path ⧺ " was too large to accept.")
137 → txt "The request URI you sent was too large to accept."
141 → txt ("An internal server error has occured during the process of your request to " ⧺ path)
144 → txt "The service is temporarily unavailable. Try later."
150 path = let uri = reqURI $ fromJust req
155 loc = A.toString $ fromJust $ getHeader "Location" res