8 module Network.HTTP.Lucu.DefaultPage
9 ( defaultPageContentType
10 , defaultPageForResponse
11 , defaultPageWithMessage
14 import Blaze.ByteString.Builder (Builder)
15 import Data.Ascii (Ascii)
16 import qualified Data.Ascii as A
17 import qualified Data.CaseInsensitive as CI
18 import Data.Convertible.Utils
20 import Data.Monoid.Unicode
21 import Data.Text (Text)
22 import Network.HTTP.Lucu.Config
23 import Network.HTTP.Lucu.Headers
24 import Network.HTTP.Lucu.Request
25 import Network.HTTP.Lucu.Response
26 import Network.URI hiding (path)
27 import Prelude hiding (head)
28 import Prelude.Unicode
30 import Text.Blaze.Html5 hiding (hr)
31 import Text.Blaze.Html5.Attributes hiding (title)
32 import Text.Blaze.Renderer.Utf8
34 defaultPageContentType ∷ Ascii
35 {-# INLINE defaultPageContentType #-}
36 defaultPageContentType = "application/xhtml+xml; charset=\"UTF-8\""
38 defaultPageForResponse ∷ Config → Maybe Request → Response → Builder
39 {-# INLINEABLE defaultPageForResponse #-}
40 defaultPageForResponse conf req res
41 = defaultPageWithMessage conf (resStatus res) $ defaultMessage req res
43 defaultPageWithMessage ∷ ∀sc. StatusCode sc ⇒ Config → sc → Html → Builder
44 {-# INLINEABLE defaultPageWithMessage #-}
45 defaultPageWithMessage (Config {..}) sc msg
47 do unsafeByteString "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
49 html ! xmlns "http://www.w3.org/1999/xhtml" $
50 do let status = toHtml $ scText sc
55 address $ do toHtml $ A.toText cnfServerSoftware
56 unsafeByteString " at "
57 toHtml $ CI.original cnfServerHost
60 scText = convertSuccessVia ((⊥) ∷ Ascii) ∘ fromStatusCode
62 defaultMessage ∷ Maybe Request → Response → Html
63 {-# INLINEABLE defaultMessage #-}
64 defaultMessage req res@(Response {..})
65 -- 1xx responses don't have a body.
66 -- 2xx responses don't need a body to be completed.
68 | resStatus ≈ MovedPermanently
69 = do unsafeByteString "The resource at "
71 unsafeByteString " has been moved to "
72 a ! href (toValue loc) $ toHtml loc
73 unsafeByteString " permanently."
76 = do unsafeByteString "The resource at "
78 unsafeByteString " is currently located at "
79 a ! href (toValue loc) $ toHtml loc
80 unsafeByteString ". This is not a permanent relocation."
82 | resStatus ≈ SeeOther
83 = do unsafeByteString "The resource at "
85 unsafeByteString " can be found at "
86 a ! href (toValue loc) $ toHtml loc
89 | resStatus ≈ TemporaryRedirect
90 = do unsafeByteString "The resource at "
92 unsafeByteString " is temporarily located at "
93 a ! href (toValue loc) $ toHtml loc
97 | resStatus ≈ BadRequest
98 = unsafeByteString "The server could not understand the request you sent."
99 | resStatus ≈ Unauthorized
100 = unsafeByteString "You need a valid authentication to access " ⊕ path
101 | resStatus ≈ Forbidden
102 = unsafeByteString "You don't have permission to access " ⊕ path
103 | resStatus ≈ NotFound
104 = do unsafeByteString "The requested URL "
106 unsafeByteString " was not found on this server."
108 = do unsafeByteString "The resource at "
110 unsafeByteString " was here in past times, but has gone permanently."
111 | resStatus ≈ RequestEntityTooLarge
112 = do unsafeByteString "The request entity you sent for "
114 unsafeByteString " was too large to accept."
115 | resStatus ≈ RequestURITooLarge
116 = unsafeByteString "The request URI you sent was too large to accept."
119 | resStatus ≈ InternalServerError
120 = unsafeByteString "An internal server error has occured during the process of your request to " ⊕ path
121 | resStatus ≈ ServiceUnavailable
122 = unsafeByteString "The service is temporarily unavailable. Try later."
128 path = toHtml ∘ uriPath ∘ reqURI $ fromJust req
131 loc = A.toText ∘ fromJust $ getHeader "Location" res
135 hr = unsafeByteString "<hr/>"