-- |An internal module for entity tags.
module Network.HTTP.Lucu.ETag
( ETag(..)
-
, strongETag
, weakETag
, eTag
module Network.HTTP.Lucu.Headers
( Headers
, HasHeaders(..)
-
, headers
- , printHeaders
)
where
import Control.Applicative hiding (empty)
import qualified Data.Ascii as A
import Data.Attoparsec.Char8
import qualified Data.Collections.Newtype.TH as C
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
+import Data.Convertible.Utils
import Data.List (intersperse)
import qualified Data.Map as M (Map)
import Data.Collections
{-# INLINE nullA #-}
nullA = null ∘ A.toByteString
+instance ConvertSuccess Headers Ascii where
+ {-# INLINE convertSuccess #-}
+ convertSuccess = convertSuccessVia ((⊥) ∷ AsciiBuilder)
+
+instance ConvertSuccess Headers AsciiBuilder where
+ {-# INLINE convertSuccess #-}
+ convertSuccess (Headers m)
+ = mconcat (header <$> fromFoldable m) ⊕ cs ("\x0D\x0A" ∷ Ascii)
+ where
+ header ∷ (CIAscii, Ascii) → AsciiBuilder
+ header (name, value)
+ = cs name ⊕
+ cs (": " ∷ Ascii) ⊕
+ cs value ⊕
+ cs ("\x0D\x0A" ∷ Ascii)
+
+deriveAttempts [ ([t| Headers |], [t| Ascii |])
+ , ([t| Headers |], [t| AsciiBuilder |])
+ ]
+
{-
message-header = field-name ":" [ field-value ]
field-name = token
∘ mconcat
∘ intersperse (A.toAsciiBuilder "\x20")
∘ (A.toAsciiBuilder <$>)
-
-printHeaders ∷ Headers → AsciiBuilder
-printHeaders (Headers m)
- = mconcat (printHeader <$> fromFoldable m) ⊕
- A.toAsciiBuilder "\x0D\x0A"
- where
- printHeader ∷ (CIAscii, Ascii) → AsciiBuilder
- printHeader (name, value)
- = A.toAsciiBuilder (A.fromCIAscii name) ⊕
- A.toAsciiBuilder ": " ⊕
- A.toAsciiBuilder value ⊕
- A.toAsciiBuilder "\x0D\x0A"
{-# LANGUAGE
- OverloadedStrings
+ FlexibleInstances
+ , MultiParamTypeClasses
+ , OverloadedStrings
+ , TemplateHaskell
, UnicodeSyntax
#-}
-- |An internal module for HTTP version numbers.
module Network.HTTP.Lucu.HttpVersion
( HttpVersion(..)
- , printHttpVersion
, httpVersion
)
where
import Control.Applicative
import Control.Applicative.Unicode
-import Data.Ascii (AsciiBuilder)
-import qualified Data.Ascii as A
+import Data.Ascii (Ascii, AsciiBuilder)
import Data.Attoparsec.Char8
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
+import Data.Convertible.Utils
import Data.Monoid.Unicode
import Prelude hiding (min)
+import Prelude.Unicode
-- |An HTTP version consists of major and minor versions.
data HttpVersion
| minA < minB = LT
| otherwise = EQ
--- |Convert an 'HttpVersion' to 'AsciiBuilder'.
-printHttpVersion ∷ HttpVersion → AsciiBuilder
-printHttpVersion v
- = case v of
- -- Optimisation for special cases.
- HttpVersion 1 0 → A.toAsciiBuilder "HTTP/1.0"
- HttpVersion 1 1 → A.toAsciiBuilder "HTTP/1.1"
- -- General (but almost never stumbling) cases.
- HttpVersion maj min
- → A.toAsciiBuilder "HTTP/" ⊕
- A.toAsciiBuilder (A.unsafeFromString $ show maj) ⊕
- A.toAsciiBuilder "." ⊕
- A.toAsciiBuilder (A.unsafeFromString $ show min)
+instance ConvertSuccess HttpVersion Ascii where
+ {-# INLINE convertSuccess #-}
+ convertSuccess = convertSuccessVia ((⊥) ∷ AsciiBuilder)
+
+instance ConvertSuccess HttpVersion AsciiBuilder where
+ {-# INLINE convertSuccess #-}
+ convertSuccess v
+ = case v of
+ -- Optimisation for special cases.
+ HttpVersion 1 0 → cs ("HTTP/1.0" ∷ Ascii)
+ HttpVersion 1 1 → cs ("HTTP/1.1" ∷ Ascii)
+ -- General (but almost never occuring) cases.
+ HttpVersion maj min
+ → cs ("HTTP/" ∷ Ascii) ⊕
+ convertUnsafe (show maj) ⊕
+ cs ("." ∷ Ascii) ⊕
+ convertUnsafe (show min)
+
+deriveAttempts [ ([t| HttpVersion |], [t| Ascii |])
+ , ([t| HttpVersion |], [t| AsciiBuilder |])
+ ]
-- |'Parser' for an 'HttpVersion'.
httpVersion ∷ Parser HttpVersion
where
import Codec.Compression.GZip
import Control.Applicative
-import qualified Data.Ascii as A
import qualified Data.ByteString.Lazy as L
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
import Data.Digest.Pure.SHA
import Data.Maybe
import Data.Time
mkETagFromInput ∷ L.ByteString → ETag
mkETagFromInput input
- = strongETag $ A.unsafeFromString
+ = strongETag $ convertUnsafe
$ "SHA-1:" ⧺ showDigest (sha1 input)
where
import Codec.Compression.GZip
import Control.Monad
-import Data.Ascii (CIAscii)
+import Data.Ascii (Ascii, CIAscii)
import qualified Data.Ascii as A
import qualified Data.ByteString.Lazy as L
import Data.Char
import Data.Collections
-import Data.Convertible.Base
+import Data.Convertible.Utils
import Data.List (intersperse)
import Data.Monoid
import Data.Ratio
]
where
eTagToDoc ∷ ETag → Doc
- eTagToDoc = text ∘ A.toString ∘ A.fromAsciiBuilder ∘ cs
+ eTagToDoc = text ∘ convertSuccessVia ((⊥) ∷ Ascii)
mimeTypeToDoc ∷ MIMEType → Doc
mimeTypeToDoc = text ∘ A.toString ∘ A.fromAsciiBuilder ∘ printMIMEType
-- (<http://tools.ietf.org/html/rfc2231>).
module Network.HTTP.Lucu.MIMEParams
( MIMEParams
- , printMIMEParams
, mimeParams
)
where
import Data.Collections
import Data.Collections.BaseInstances ()
import qualified Data.Collections.Newtype.TH as C
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
+import Data.Convertible.Utils
import qualified Data.Map as M (Map)
import Data.Monoid.Unicode
import Data.Sequence (Seq)
instance SortingCollection MIMEParams (CIAscii, Text)
|]
--- |Convert MIME parameter values to an 'AsciiBuilder'.
-printMIMEParams ∷ MIMEParams → AsciiBuilder
-{-# INLINEABLE printMIMEParams #-}
-printMIMEParams = foldl' f (∅)
- where
- f ∷ AsciiBuilder → (CIAscii, Text) → AsciiBuilder
- {-# INLINE f #-}
- f ab (k, v) = ab ⊕ A.toAsciiBuilder "; " ⊕ printPair k v
+instance ConvertSuccess MIMEParams Ascii where
+ {-# INLINE convertSuccess #-}
+ convertSuccess = convertSuccessVia ((⊥) ∷ AsciiBuilder)
+
+instance ConvertSuccess MIMEParams AsciiBuilder where
+ {-# INLINE convertSuccess #-}
+ convertSuccess = foldl' f (∅)
+ where
+ f ∷ AsciiBuilder → (CIAscii, Text) → AsciiBuilder
+ {-# INLINE f #-}
+ f ab (k, v) = ab ⊕ cs ("; " ∷ Ascii) ⊕ printPair k v
printPair ∷ CIAscii → Text → AsciiBuilder
{-# INLINEABLE printPair #-}
printPairInUTF8 ∷ CIAscii → Text → AsciiBuilder
{-# INLINEABLE printPairInUTF8 #-}
printPairInUTF8 name value
- = A.toAsciiBuilder (A.fromCIAscii name) ⊕
- A.toAsciiBuilder "*=utf-8''" ⊕
+ = cs name ⊕
+ cs ("*=utf-8''" ∷ Ascii) ⊕
escapeUnsafeChars (encodeUtf8 value) (∅)
printPairInAscii ∷ CIAscii → Ascii → AsciiBuilder
{-# INLINEABLE printPairInAscii #-}
printPairInAscii name value
- = A.toAsciiBuilder (A.fromCIAscii name) ⊕
- A.toAsciiBuilder "=" ⊕
- if BS.any ((¬) ∘ isToken) (A.toByteString value) then
+ = cs name ⊕
+ cs ("=" ∷ Ascii) ⊕
+ if BS.any ((¬) ∘ isToken) (cs value) then
quoteStr value
else
- A.toAsciiBuilder value
+ cs value
escapeUnsafeChars ∷ BS.ByteString → AsciiBuilder → AsciiBuilder
{-# INLINEABLE escapeUnsafeChars #-}
Nothing → b
Just (c, bs')
| isToken c → escapeUnsafeChars bs' $
- b ⊕ A.toAsciiBuilder (A.unsafeFromString [c])
+ b ⊕ cs (A.unsafeFromString [c])
| otherwise → escapeUnsafeChars bs' $
b ⊕ toHex (fromIntegral $ fromEnum c)
toHex ∷ Word8 → AsciiBuilder
{-# INLINEABLE toHex #-}
-toHex o = A.toAsciiBuilder "%" ⊕
- A.toAsciiBuilder (A.unsafeFromString [ toHex' (o `shiftR` 8)
- , toHex' (o .&. 0x0F) ])
+toHex o = cs ("%" ∷ Ascii) ⊕
+ cs (A.unsafeFromString [ toHex' (o `shiftR` 8)
+ , toHex' (o .&. 0x0F) ])
where
toHex' ∷ Word8 → Char
{-# INLINEABLE toHex' #-}
import Data.Ascii (Ascii, AsciiBuilder, CIAscii)
import qualified Data.Ascii as A
import Data.Attoparsec.Char8 as P
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
+import Data.Convertible.Utils
import Data.Monoid.Unicode
import Data.Typeable
import Language.Haskell.TH.Syntax
= A.toAsciiBuilder (A.fromCIAscii mtMedia) ⊕
A.toAsciiBuilder "/" ⊕
A.toAsciiBuilder (A.fromCIAscii mtSub) ⊕
- printMIMEParams mtParams
+ cs mtParams
-- |Parse 'MIMEType' from an 'Ascii'. This function throws an
-- exception for parse error. For literals consider using
where
import Data.Ascii (AsciiBuilder)
import qualified Data.Ascii as A
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
+import Data.Convertible.Utils
import Data.Monoid.Unicode
import Network.HTTP.Lucu.Headers
import Network.HTTP.Lucu.HttpVersion
printResponse ∷ Response → AsciiBuilder
{-# INLINEABLE printResponse #-}
printResponse (Response {..})
- = printHttpVersion resVersion ⊕
+ = cs resVersion ⊕
A.toAsciiBuilder " " ⊕
printStatusCode resStatus ⊕
A.toAsciiBuilder "\x0D\x0A" ⊕
- printHeaders resHeaders
+ cs resHeaders
-- |@'isInformational' sc@ returns 'True' iff @sc '<' 200@.
isInformational ∷ StatusCode sc ⇒ sc → Bool