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 <- readItr itr itrResponse id
50 when (getHeader "Content-Type" res == Just defaultPageContentType)
51 $ do reqM <- readItr itr itrRequest id
53 let conf = itrConfig itr
54 page = B.pack $ getDefaultPage conf reqM res
56 writeTVar (itrBodyToSend itr)
60 mkDefaultPage :: (ArrowXml a) => Config -> StatusCode -> a b XmlTree -> a b XmlTree
61 mkDefaultPage conf status msgA
62 = let (sCode, sMsg) = statusCode status
63 sig = cnfServerSoftware conf
66 ++ ( case cnfServerPort conf of
67 Service serv -> ", service " ++ serv
68 PortNumber num -> ", port " ++ show num
69 UnixSocket path -> ", unix socket " ++ show path
73 += sattr "xmlns" "http://www.w3.org/1999/xhtml"
76 += txt (printf "%03d %s" sCode sMsg)
82 += ( eelem "p" += msgA )
84 += ( eelem "address" += txt sig ))))
87 getMsg :: (ArrowXml a) => Maybe Request -> Response -> a b XmlTree
89 = case resStatus res of
95 -> txt (printf "The resource at %s has been moved to " path)
97 eelem "a" += sattr "href" loc
103 -> txt (printf "The resource at %s is currently located at " path)
105 eelem "a" += sattr "href" loc
108 txt ". This is not a permanent relocation."
111 -> txt (printf "The resource at %s can be found at " path)
113 eelem "a" += sattr "href" loc
119 -> txt (printf "The resource at %s is temporarily located at " path)
121 eelem "a" += sattr "href" loc
128 -> txt "The server could not understand the request you sent."
131 -> txt (printf "You need a valid authentication to access %s" path)
134 -> txt (printf "You don't have permission to access %s" path)
137 -> txt (printf "The requested URL %s was not found on this server." path)
140 -> txt (printf "The resource at %s was here in past times, but has gone permanently." path)
142 RequestEntityTooLarge
143 -> txt (printf "The request entity you sent for %s was too big to accept." path)
146 -> txt "The request URI you sent was too big to accept."
150 -> txt (printf "An internal server error has occured during the process of your request to %s" path)
153 -> txt "The service is temporarily unavailable. Try later."
160 path = let uri = reqURI $ fromJust req
165 loc = fromJust $ getHeader "Location" res