where
import Blaze.ByteString.Builder (Builder)
import Data.Ascii (Ascii)
-import qualified Data.Ascii as A
import qualified Data.CaseInsensitive as CI
+import Data.Convertible.Base
import Data.Convertible.Utils
import Data.Maybe
import Data.Monoid.Unicode
body $ do h1 status
p msg
hr
- address $ do toHtml $ A.toText cnfServerSoftware
+ address $ do toHtml (cs cnfServerSoftware ∷ Text)
unsafeByteString " at "
toHtml $ CI.original cnfServerHost
where
path = toHtml ∘ uriPath ∘ reqURI $ fromJust req
loc ∷ Text
- loc = A.toText ∘ fromJust $ getHeader "Location" res
+ loc = cs ∘ fromJust $ getHeader "Location" res
hr ∷ Html
{-# INLINE hr #-}
import qualified Data.ByteString.Char8 as C8
import Data.CaseInsensitive (CI)
import qualified Data.CaseInsensitive as CI
+import Data.Convertible.Base
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
parseHost ∷ Ascii → (CI Text, Ascii)
parseHost hp
- = let (h, p) = C8.break (≡ ':') $ A.toByteString hp
+ = let (h, p) = C8.break (≡ ':') $ cs hp
-- FIXME: should decode punycode here.
hText = CI.mk $ T.decodeUtf8 h
pAscii = A.unsafeFromByteString p
uriAuthority = Just URIAuth {
uriUserInfo = ""
, uriRegName = T.unpack $ CI.original host
- , uriPort = A.toString port
+ , uriPort = cs port
}
}
in
| otherwise
→ setStatus NotImplemented
- case A.toByteString <$> getHeader "Content-Length" req of
+ case cs <$> getHeader "Content-Length" req of
Nothing → return ()
Just value → case C8.readInt value of
Just (len, garbage)
{-# LANGUAGE
CPP
, BangPatterns
+ , FlexibleContexts
, GeneralizedNewtypeDeriving
, DoAndIfThenElse
, OverloadedStrings
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Unicode
-import Data.Ascii (Ascii, CIAscii)
-import qualified Data.Ascii as A
+import Data.Ascii (Ascii, CIAscii, AsciiBuilder)
import Data.Attempt
import qualified Data.Attoparsec.Char8 as P
import Data.ByteString (ByteString)
-- @uri@. You usually don't need to call this function directly.
setLocation ∷ URI → Rsrc ()
setLocation uri
- = case A.fromChars uriStr of
- Just a → setHeader "Location" a
- Nothing → abort $ mkAbortion' InternalServerError
- $ "Malformed URI: " ⊕ T.pack uriStr
+ = case ca uriStr of
+ Success a → setHeader "Location" a
+ Failure e → abort $ mkAbortion' InternalServerError
+ $ cs (show e)
where
uriStr = uriToString id uri ""
_ → abort $ mkAbortion' InternalServerError
"setContentEncoding: Unknown HTTP version"
setHeader "Content-Encoding"
- $ A.fromAsciiBuilder
+ $ cs
$ mconcat
- $ intersperse (A.toAsciiBuilder ", ")
+ $ intersperse (cs (", " ∷ Ascii))
$ map tr codings
where
- toAB = A.toAsciiBuilder ∘ A.fromCIAscii
+ toAB ∷ ConvertSuccess α AsciiBuilder ⇒ α → AsciiBuilder
+ toAB = cs
-- |@'setWWWAuthenticate' challenge@ declares the response header
-- \"WWW-Authenticate\" as @challenge@.
import Control.Monad.IO.Class
import Control.Monad.Reader (ReaderT, runReaderT, ask)
import Control.Monad.Unicode
-import Data.Ascii (Ascii, CIAscii)
-import qualified Data.Ascii as A
+import Data.Ascii (Ascii, CIAscii, AsciiBuilder)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Collections
+import Data.Convertible.Base
import Data.List (intersperse, nub)
import Data.Maybe
import Data.Monoid
notAllowed ∷ Rsrc ()
notAllowed = do setStatus MethodNotAllowed
setHeader "Allow"
- $ A.fromAsciiBuilder
+ $ cs
$ mconcat
- $ intersperse (A.toAsciiBuilder ", ")
- $ map A.toAsciiBuilder allowedMethods
+ $ intersperse (cs (", " ∷ Ascii) ∷ AsciiBuilder)
+ $ map cs allowedMethods
allowedMethods ∷ [Ascii]
allowedMethods = nub $ concat [ methods resGet ["GET"]
{-# LANGUAGE
- QuasiQuotes
+ OverloadedStrings
+ , QuasiQuotes
#-}
-- |Definition of HTTP status code.
-- 'Network.HTTP.Lucu.Resource.setStatus' accepts these named status
import Control.Applicative
import Data.Ascii (Ascii, AsciiBuilder)
import qualified Data.Ascii as A
-import Data.Attoparsec.Char8 as P
-import Data.Attoparsec.Lazy as LP
+import Data.Attoparsec.Char8
+import qualified Data.Attoparsec.Lazy as LP
import qualified Data.ByteString.Lazy.Char8 as Lazy
import Data.Convertible.Base
import Data.Convertible.Instances.Ascii ()
numericCode ∷ sc → Int
-- |Return the combination of 3-digit integer and reason phrase
-- for this status e.g. @200 OK@
- textualStatus ∷ sc → Ascii
+ textualStatus ∷ sc → AsciiBuilder
-- |Wrap the status code into 'SomeStatusCode'.
fromStatusCode ∷ sc → SomeStatusCode
fromStatusCode = SomeStatusCode
instance StatusCode sc ⇒ ConvertSuccess sc AsciiBuilder where
{-# INLINE convertSuccess #-}
- convertSuccess = cs ∘ textualStatus
+ convertSuccess = textualStatus
instance StatusCode sc ⇒ ConvertAttempt sc Ascii where
{-# INLINE convertAttempt #-}
-- data OK = OK deriving ('Show')
-- instance OK where
-- 'numericCode' _ = 200
--- 'textualStatus' _ = 'A.unsafeFromString' \"200 OK\"
+-- 'textualStatus' _ = 'cs' (\"200 OK\" ∷ Ascii)
--
-- data BadRequest = BadRequest deriving ('Show')
-- instance BadRequest where
-- 'numericCode' _ = 400
--- 'textualStatus' _ = 'A.unsafeFromString' \"400 Bad Request\"
+-- 'textualStatus' _ = 'cs' (\"400 Bad Request\" ∷ Ascii)
--
-- data MethodNotAllowed = MethodNotAllowed deriving ('Show')
-- instance MethodNotAllowed where
-- 'numericCode' _ = 405
--- 'textualStatus' _ = 'A.unsafeFromString' \"405 Method Not Allowed\"
+-- 'textualStatus' _ = 'cs' (\"405 Method Not Allowed\" ∷ Ascii)
-- @
statusCodes ∷ QuasiQuoter
statusCodes = QuasiQuoter {
"pair"
word ∷ Parser Ascii
- word = A.unsafeFromByteString <$> P.takeWhile1 isAlpha_ascii
+ word = A.unsafeFromByteString <$> takeWhile1 isAlpha_ascii
statusDecl ∷ (Int, [Ascii]) → Q [Dec]
statusDecl (num, phrase)
return (a:bs)
where
name ∷ Name
- name = mkName $ concatMap A.toString phrase
+ name = mkName $ concatMap cs phrase
dataDecl ∷ Q Dec
dataDecl = dataD (cxt []) name [] [con] [''Show]
con = return $ NormalC name []
txt ∷ Q Exp
- txt = [| A.unsafeFromString $(lift txt') |]
+ txt = [| cs ($(lift txt') ∷ Ascii) |]
txt' ∷ String
txt' = concat $ intersperse "\x20"
- $ show num : map A.toString phrase
+ $ show num : map cs phrase
import Data.Char
import Data.Collections
import Data.Collections.BaseInstances ()
+import Data.Convertible.Base
+import Data.Convertible.Instances.Time ()
import Data.Maybe
import Data.Monoid.Unicode
-import Data.Ratio
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time
-import Data.Time.Clock.POSIX
import Network.URI
import Prelude hiding (last, mapM, null, reverse)
import Prelude.Unicode
import System.Directory
-import System.Time (ClockTime(..))
-- |'Host' represents an IP address or a host name in an URI
-- authority.
-- |Get the modification time of a given file.
getLastModified ∷ FilePath → IO UTCTime
-getLastModified = (clockTimeToUTC <$>) ∘ getModificationTime
- where
- clockTimeToUTC ∷ ClockTime → UTCTime
- clockTimeToUTC (TOD sec picoSec)
- = posixSecondsToUTCTime ∘ fromRational
- $ sec % 1 + picoSec % (1000 ⋅ 1000 ⋅ 1000 ⋅ 1000)
+getLastModified = (cs <$>) ∘ getModificationTime