-- *** MIME Type
, MIMEType(..)
- , mkMIMEType
, parseMIMEType
+ , mimeType
-- *** Authentication
, AuthChallenge(..)
import Network.HTTP.Lucu.Config
import Network.HTTP.Lucu.ETag
import Network.HTTP.Lucu.Httpd
-import Network.HTTP.Lucu.MIMEType
+import Network.HTTP.Lucu.MIMEType hiding (mimeType)
+import Network.HTTP.Lucu.MIMEType.TH
import Network.HTTP.Lucu.Resource
import Network.HTTP.Lucu.Resource.Tree
import Network.HTTP.Lucu.Response
-- (<http://tools.ietf.org/html/rfc2046>).
module Network.HTTP.Lucu.MIMEType
( MIMEType(..)
- , mkMIMEType
, parseMIMEType
, printMIMEType
}
|]
--- |@'mkMIMEType' media sub@ returns a 'MIMEType' with the given
--- @media@ and @sub@ types but without any parameters.
-mkMIMEType ∷ CIAscii → CIAscii → MIMEType
-{-# INLINE mkMIMEType #-}
-mkMIMEType = flip flip (∅) ∘ MIMEType
-
-- |Convert a 'MIMEType' to an 'AsciiBuilder'.
printMIMEType ∷ MIMEType → AsciiBuilder
{-# INLINEABLE printMIMEType #-}
UnicodeSyntax
, ViewPatterns
#-}
+-- |A module to provide 'QuasiQuoter' for 'MIMEType' literals.
module Network.HTTP.Lucu.MIMEType.TH
( mimeType
)
import Network.HTTP.Lucu.Utils
import Prelude.Unicode
--- |A 'QuasiQuoter' for 'MIMEType' literals.
+-- |'QuasiQuoter' for 'MIMEType' literals.
--
-- @
-- textPlain :: 'MIMEType'
--- textPlain = ['mimeType'| text/plain; charset="UTF-8" |]
+-- textPlain = ['mimeType'| text/plain; charset=\"UTF-8\" |]
-- @
mimeType ∷ QuasiQuoter
mimeType = QuasiQuoter {
DoAndIfThenElse
, FlexibleContexts
, OverloadedStrings
+ , QuasiQuotes
, RecordWildCards
, ScopedTypeVariables
, UnicodeSyntax
import qualified Data.Text as T
import Network.HTTP.Lucu.Headers
import Network.HTTP.Lucu.MIMEParams
-import Network.HTTP.Lucu.MIMEType
+import Network.HTTP.Lucu.MIMEType (MIMEType)
+import qualified Network.HTTP.Lucu.MIMEType as MT
+import Network.HTTP.Lucu.MIMEType.TH
import Network.HTTP.Lucu.Parser
import Network.HTTP.Lucu.Parser.Http
import Prelude.Unicode
⧺ e
where
defaultCType ∷ MIMEType
- defaultCType = parseMIMEType "text/plain"
+ defaultCType = [mimeType| text/plain |]
partHeader ∷ Parser Headers
partHeader = crlf *> headers
Nothing
→ return Nothing
Just str
- → case parseOnly (finishOff mimeType) $ A.toByteString str of
+ → case parseOnly (finishOff MT.mimeType) $ A.toByteString str of
Right d → return $ Just d
Left err → throwError $ "malformed Content-Type: "
⧺ A.toString str
, GeneralizedNewtypeDeriving
, DoAndIfThenElse
, OverloadedStrings
+ , QuasiQuotes
, RecordWildCards
, UnicodeSyntax
#-}
import Network.HTTP.Lucu.Resource.Internal
import Network.HTTP.Lucu.Response
import Network.HTTP.Lucu.MIMEParams
-import Network.HTTP.Lucu.MIMEType
+import Network.HTTP.Lucu.MIMEType (MIMEType(..))
+import qualified Network.HTTP.Lucu.MIMEType as MT
+import Network.HTTP.Lucu.MIMEType.TH
import Network.HTTP.Lucu.Utils
import Network.Socket hiding (accept)
import Network.URI hiding (path)
toPairWithFormData (name, value)
= let fd = FormData {
fdFileName = Nothing
- , fdMIMEType = parseMIMEType "text/plain"
+ , fdMIMEType = [mimeType| text/plain |]
, fdContent = Lazy.fromChunks [value]
}
in (name, fd)
Nothing
→ return []
Just accept
- → case P.parseOnly (finishOff mimeTypeList) (A.toByteString accept) of
+ → case P.parseOnly (finishOff MT.mimeTypeList) (A.toByteString accept) of
Right xs → return xs
Left _ → abort $ mkAbortion' BadRequest
$ "Unparsable Accept: " ⊕ A.toText accept
Nothing
→ return Nothing
Just cType
- → case P.parseOnly (finishOff mimeType) (A.toByteString cType) of
+ → case P.parseOnly (finishOff MT.mimeType) (A.toByteString cType) of
Right t → return $ Just t
Left _ → abort $ mkAbortion' BadRequest
$ "Unparsable Content-Type: " ⊕ A.toText cType
$ A.toText
$ A.fromAsciiBuilder
$ A.toAsciiBuilder "Unsupported media type: "
- ⊕ printMIMEType cType
+ ⊕ MT.printMIMEType cType
where
readWWWFormURLEncoded
= (map toPairWithFormData ∘ parseWWWFormURLEncoded)
-- mandatory for sending a response body.
setContentType ∷ MIMEType → Resource ()
setContentType
- = setHeader "Content-Type" ∘ A.fromAsciiBuilder ∘ printMIMEType
+ = setHeader "Content-Type" ∘ A.fromAsciiBuilder ∘ MT.printMIMEType
-- |@'setLocation' uri@ declares the response header \"Location\" as
-- @uri@. You usually don't need to call this function directly.