    -- ------------------------------------------------------------

    {-
       Module     : Text.XML.HXT.Parser.ProtocolHandlerUtil
       Copyright  : Copyright (C) 2008 Uwe Schmidt
       License    : MIT

       Maintainer : Uwe Schmidt (uwe@fh-wedel.de)
       Stability  : stable
       Portability: portable

       Protocol handler utility functions

    -}

    -- ------------------------------------------------------------

    module Text.XML.HXT.Parser.ProtocolHandlerUtil
        ( parseContentType
        )

    where

    import Text.XML.HXT.DOM.XmlKeywords

    import Text.XML.HXT.DOM.Util    ( stringToUpper
                                    , stringTrim
                                    )

    import qualified Text.ParserCombinators.Parsec as P

    -- ------------------------------------------------------------

    -- |
    -- Try to extract charset spec from Content-Type header
    -- e.g. \"text\/html; charset=ISO-8859-1\"
    --
    -- Sometimes the server deliver the charset spec in quotes
    -- these are removed

    parseContentType        :: P.Parser [(String, String)]
    parseContentType :: Parser [([Char], [Char])]
    parseContentType
        = Parser [([Char], [Char])] -> Parser [([Char], [Char])]
    forall tok st a. GenParser tok st a -> GenParser tok st a
    P.try ( do
                  mimeType <- ( do
                                mt <- ParsecT [Char] () Identity Char
    -> ParsecT [Char] () Identity [Char]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    P.many ([Char] -> ParsecT [Char] () Identity Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    P.noneOf [Char]
    ";")
                                rtMT mt
                              )
                  charset  <- ( do
                                _ <- P.char ';'
                                _ <- P.many  (P.oneOf " \t'")
                                _ <- P.string "charset="
                                _ <- P.option '"' (P.oneOf "\"'")
                                cs <- P.many1 (P.noneOf "\"'")
                                return [ (transferEncoding, stringToUpper cs) ]
                              )
                  return (mimeType ++ charset)
                )
          Parser [([Char], [Char])]
    -> Parser [([Char], [Char])] -> Parser [([Char], [Char])]
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    P.<|>
          ( do
            mt <- ParsecT [Char] () Identity Char
    -> ParsecT [Char] () Identity [Char]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    P.many ([Char] -> ParsecT [Char] () Identity Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    P.noneOf [Char]
    ";")
            rtMT mt
          )
        where
        rtMT :: [Char] -> m [([Char], [Char])]
    rtMT [Char]
    mt = [([Char], [Char])] -> m [([Char], [Char])]
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return [ ([Char]
    transferMimeType, [Char] -> [Char]
    stringTrim [Char]
    mt) ]

    -- ------------------------------------------------------------