2 module Network.HTTP.Lucu.DefaultPage
10 import Control.Arrow.ArrowList
11 import Control.Concurrent.STM
13 import qualified Data.ByteString.Lazy.Char8 as B
14 import Data.ByteString.Lazy.Char8 (ByteString)
17 import Network.HTTP.Lucu.Config
18 import Network.HTTP.Lucu.Headers
19 import Network.HTTP.Lucu.Interaction
20 import Network.HTTP.Lucu.Request
21 import Network.HTTP.Lucu.Response
23 import System.IO.Unsafe
25 import Text.XML.HXT.Arrow.WriteDocument
26 import Text.XML.HXT.Arrow.XmlArrow
27 import Text.XML.HXT.Arrow.XmlIOStateArrow
28 import Text.XML.HXT.DOM.TypeDefs
29 import Text.XML.HXT.DOM.XmlKeywords
32 getDefaultPage :: Config -> Maybe Request -> Response -> String
33 getDefaultPage conf req res
34 = let msgA = getMsg req res
37 do [xmlStr] <- runX ( mkDefaultPage conf (resStatus res) msgA
39 writeDocumentToString [ (a_indent, v_1) ]
44 writeDefaultPage :: Interaction -> STM ()
46 = do wroteHeader <- readTVar (itrWroteHeader itr)
48 -- Content-Type が正しくなければ補完できない。
49 res <- readTVar (itrResponse itr)
50 when (getHeader "Content-Type" res == Just defaultPageContentType)
51 $ do let reqM = itrRequest itr
53 page = B.pack $ getDefaultPage conf reqM res
55 writeTVar (itrBodyToSend itr)
59 mkDefaultPage :: (ArrowXml a) => Config -> StatusCode -> a b XmlTree -> a b XmlTree
60 mkDefaultPage conf status msgA
61 = let (sCode, sMsg) = statusCode status
62 sig = cnfServerSoftware conf
65 ++ ( case cnfServerPort conf of
66 Service serv -> ", service " ++ serv
67 PortNumber num -> ", port " ++ show num
68 UnixSocket path -> ", unix socket " ++ show path
72 += sattr "xmlns" "http://www.w3.org/1999/xhtml"
75 += txt (printf "%03d %s" sCode sMsg)
81 += ( eelem "p" += msgA )
83 += ( eelem "address" += txt sig ))))
86 getMsg :: (ArrowXml a) => Maybe Request -> Response -> a b XmlTree
88 = case resStatus res of
94 -> txt (printf "The resource at %s has been moved to " path)
96 eelem "a" += sattr "href" loc
102 -> txt (printf "The resource at %s is currently located at " path)
104 eelem "a" += sattr "href" loc
107 txt ". This is not a permanent relocation."
110 -> txt (printf "The resource at %s can be found at " path)
112 eelem "a" += sattr "href" loc
118 -> txt (printf "The resource at %s is temporarily located at " path)
120 eelem "a" += sattr "href" loc
127 -> txt "The server could not understand the request you sent."
130 -> txt (printf "You need a valid authentication to access %s" path)
133 -> txt (printf "You don't have permission to access %s" path)
136 -> txt (printf "The requested URL %s was not found on this server." path)
139 -> txt (printf "The resource at %s was here in past times, but has gone permanently." path)
141 RequestEntityTooLarge
142 -> txt (printf "The request entity you sent for %s was too big to accept." path)
145 -> txt "The request URI you sent was too big to accept."
149 -> txt (printf "An internal server error has occured during the process of your request to %s" path)
152 -> txt "The service is temporarily unavailable. Try later."
159 path = let uri = reqURI $ fromJust req
164 loc = fromJust $ getHeader "Location" res