1 module Network.HTTP.Lucu.DefaultPage
9 import Control.Arrow.ArrowList
10 import Control.Concurrent.STM
12 import qualified Data.ByteString.Char8 as C8
13 import qualified Data.ByteString.Lazy.Char8 as L8
15 import Network.HTTP.Lucu.Config
16 import Network.HTTP.Lucu.Format
17 import Network.HTTP.Lucu.Headers
18 import Network.HTTP.Lucu.Interaction
19 import Network.HTTP.Lucu.Request
20 import Network.HTTP.Lucu.Response
21 import Network.URI hiding (path)
22 import System.IO.Unsafe
23 import Text.XML.HXT.Arrow.WriteDocument
24 import Text.XML.HXT.Arrow.XmlArrow
25 import Text.XML.HXT.Arrow.XmlIOStateArrow
26 import Text.XML.HXT.DOM.TypeDefs
27 import Text.XML.HXT.DOM.XmlKeywords
30 getDefaultPage :: Config -> Maybe Request -> Response -> String
31 getDefaultPage !conf !req !res
32 = let msgA = getMsg req res
35 do [xmlStr] <- runX ( mkDefaultPage conf (resStatus res) msgA
37 writeDocumentToString [ (a_indent, v_1) ]
42 writeDefaultPage :: Interaction -> STM ()
44 -- Content-Type が正しくなければ補完できない。
45 = do res <- readItr itr itrResponse id
46 when (getHeader (C8.pack "Content-Type") res == Just defaultPageContentType)
47 $ do reqM <- readItr itr itrRequest id
49 let conf = itrConfig itr
50 page = L8.pack $ getDefaultPage conf reqM res
52 writeTVar (itrBodyToSend itr)
56 mkDefaultPage :: (ArrowXml a) => Config -> StatusCode -> a b XmlTree -> a b XmlTree
57 mkDefaultPage !conf !status !msgA
58 = let (# sCode, sMsg #) = statusCode status
59 sig = C8.unpack (cnfServerSoftware conf)
61 ++ C8.unpack (cnfServerHost conf)
64 += sattr "xmlns" "http://www.w3.org/1999/xhtml"
67 += txt (fmtDec 3 sCode ++ " " ++ C8.unpack sMsg)
71 += txt (C8.unpack sMsg)
73 += ( eelem "p" += msgA )
75 += ( eelem "address" += txt sig ))))
76 {-# SPECIALIZE mkDefaultPage :: Config -> StatusCode -> IOSArrow b XmlTree -> IOSArrow b XmlTree #-}
78 getMsg :: (ArrowXml a) => Maybe Request -> Response -> a b XmlTree
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 big to accept.")
137 -> txt "The request URI you sent was too big 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."
151 path = let uri = reqURI $! fromJust req
156 loc = C8.unpack $! fromJust $! getHeader (C8.pack "Location") res
158 {-# SPECIALIZE getMsg :: Maybe Request -> Response -> IOSArrow b XmlTree #-}