1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
{-# LANGUAGE OverloadedStrings, TypeFamilies, QuasiQuotes #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Network.TigHTTP.Papillon (
ContentType(..), Type(..), Subtype(..), Parameter(..), Charset(..),
parseContentType, showContentType,
) where
import Data.Char
import Text.Papillon
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (pack)
import qualified Data.ByteString as BS
import Network.TigHTTP.Token
data ContentType = ContentType Type Subtype [Parameter]
deriving (Show, Eq)
parseContentType :: BS.ByteString -> ContentType
parseContentType ct = case runError . contentType $ parse ct of
Left _ -> error "parseContentType"
Right (r, _) -> r
showContentType :: ContentType -> BS.ByteString
showContentType (ContentType t st ps) = showType t
`BS.append` "/"
`BS.append` showSubtype st
`BS.append` showParameters ps
data Type
= Text
| TypeRaw BS.ByteString
deriving (Show, Eq)
mkType :: BS.ByteString -> Type
mkType "text" = Text
mkType t = TypeRaw t
showType :: Type -> BS.ByteString
showType Text = "text"
showType (TypeRaw t) = t
data Subtype
= Plain
| Html
| Css
| SubtypeRaw BS.ByteString
deriving (Show, Eq)
mkSubtype :: BS.ByteString -> Subtype
mkSubtype "html" = Html
mkSubtype "plain" = Plain
mkSubtype "css" = Css
mkSubtype s = SubtypeRaw s
showSubtype :: Subtype -> BS.ByteString
showSubtype Plain = "plain"
showSubtype Html = "html"
showSubtype Css = "css"
showSubtype (SubtypeRaw s) = s
data Parameter
= Charset Charset
| ParameterRaw BS.ByteString BS.ByteString
deriving (Show, Eq)
mkParameter :: BS.ByteString -> BS.ByteString -> Parameter
mkParameter "charset" "UTF-8" = Charset Utf8
mkParameter "charset" v = Charset $ CharsetRaw v
mkParameter a v = ParameterRaw a v
showParameters :: [Parameter] -> BS.ByteString
showParameters [] = ""
showParameters (Charset v : ps) = "; " `BS.append` "charset"
`BS.append` "=" `BS.append` showCharset v `BS.append` showParameters ps
showParameters (ParameterRaw a v : ps) = "; " `BS.append` a
`BS.append` "=" `BS.append` v `BS.append` showParameters ps
data Charset
= Utf8
| CharsetRaw BS.ByteString
deriving (Show, Eq)
showCharset :: Charset -> BS.ByteString
showCharset Utf8 = "UTF-8"
showCharset (CharsetRaw cs) = cs
bsconcat :: [ByteString] -> ByteString
bsconcat = BS.concat
[papillon|
source: ByteString
contentType :: ContentType
= c:token '/' sc:token ps:(';' ' '* p:parameter { p })*
{ ContentType (mkType c) (mkSubtype sc) ps }
token :: ByteString
= t:<isTokenChar>+ { pack t }
quotedString :: ByteString
= '"' t:(qt:qdtext { qt } / qp:quotedPair { pack [qp] })* '"'
{ bsconcat t }
quotedPair :: Char
= '\\' c:<isAscii> { c }
crlf :: () = '\r' '\n'
lws :: () = _:crlf _:(' ' / '\t')+
-- text :: ByteString
-- = ts:(cs:<isTextChar>+ { cs } / _:lws { " " })+ { pack $ concat ts }
qdtext :: ByteString
= ts:(cs:<isQdtextChar>+ { cs } / _:lws { " " })+ { pack $ concat ts }
parameter :: Parameter
= a:attribute '=' v:value { mkParameter a v }
attribute :: ByteString = t:token { t }
value :: ByteString
= t:token { t }
/ qs:quotedString { qs }
|]
|