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
16 import Network.HTTP.Lucu.Config
17 import Network.HTTP.Lucu.Format
18 import Network.HTTP.Lucu.Headers
19 import Network.HTTP.Lucu.Interaction
20 import Network.HTTP.Lucu.Request
21 import Network.HTTP.Lucu.Response
22 import Network.URI hiding (path)
23 import System.IO.Unsafe
24 import Text.XML.HXT.Arrow.WriteDocument
25 import Text.XML.HXT.Arrow.XmlArrow
26 import Text.XML.HXT.Arrow.XmlIOStateArrow
27 import Text.XML.HXT.DOM.TypeDefs
28 import Text.XML.HXT.DOM.XmlKeywords
31 getDefaultPage :: Config -> Maybe Request -> Response -> String
32 getDefaultPage conf req res
33 = conf `seq` req `seq` res `seq`
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 ()
47 -- Content-Type が正しくなければ補完できない。
48 do res <- readItr itr itrResponse id
49 when (getHeader (C8.pack "Content-Type") res == Just defaultPageContentType)
50 $ do reqM <- readItr itr itrRequest id
52 let conf = itrConfig itr
53 page = L8.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 = conf `seq` status `seq` msgA `seq`
62 let (# sCode, sMsg #) = statusCode status
63 sig = C8.unpack (cnfServerSoftware conf)
65 ++ C8.unpack (cnfServerHost 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 (fmtDec 3 sCode ++ " " ++ C8.unpack sMsg)
80 += txt (C8.unpack sMsg)
82 += ( eelem "p" += msgA )
84 += ( eelem "address" += txt sig ))))
85 {-# SPECIALIZE mkDefaultPage :: Config -> StatusCode -> IOSArrow b XmlTree -> IOSArrow b XmlTree #-}
87 getMsg :: (ArrowXml a) => Maybe Request -> Response -> a b XmlTree
96 -> txt ("The resource at " ++ path ++ " has been moved to ")
98 eelem "a" += sattr "href" loc
104 -> txt ("The resource at " ++ path ++ " is currently located at ")
106 eelem "a" += sattr "href" loc
109 txt ". This is not a permanent relocation."
112 -> txt ("The resource at " ++ path ++ " can be found at ")
114 eelem "a" += sattr "href" loc
120 -> txt ("The resource at " ++ path ++ " is temporarily located at ")
122 eelem "a" += sattr "href" loc
129 -> txt "The server could not understand the request you sent."
132 -> txt ("You need a valid authentication to access " ++ path)
135 -> txt ("You don't have permission to access " ++ path)
138 -> txt ("The requested URL " ++ path ++ " was not found on this server.")
141 -> txt ("The resource at " ++ path ++ " was here in past times, but has gone permanently.")
143 RequestEntityTooLarge
144 -> txt ("The request entity you sent for " ++ path ++ " was too big to accept.")
147 -> txt "The request URI you sent was too big to accept."
151 -> txt ("An internal server error has occured during the process of your request to " ++ path)
154 -> txt "The service is temporarily unavailable. Try later."
161 path = let uri = reqURI $! fromJust req
166 loc = C8.unpack $! fromJust $! getHeader (C8.pack "Location") res
168 {-# SPECIALIZE getMsg :: Maybe Request -> Response -> IOSArrow b XmlTree #-}