http-encodings 0.9.1 → 0.9.2
raw patch · 3 files changed
+22/−12 lines, 3 filesdep +textdep ~mime
Dependencies added: text
Dependency ranges changed: mime
Files
- LICENSE +1/−1
- Network/HTTP/Encoding/Character.hs +16/−7
- http-encodings.cabal +5/−4
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2012-2013 Stevens Institute of Technology+Copyright (c) 2012-2014 Stevens Institute of Technology All rights reserved.
Network/HTTP/Encoding/Character.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE OverloadedStrings #-}+ -- | Detection and of character encodings of HTTP message bodies module Network.HTTP.Encoding.Character (getContentTypeAndCharacterEncoding@@ -13,6 +15,9 @@ import Data.ByteString.Lazy.UTF8 (uncons) import Data.ByteString.Lazy (ByteString) import Control.Monad+import Data.Text (Text, unpack, pack)+import qualified Data.Text as Text+import Data.Maybe (listToMaybe) -- | Looks for and parses the ContentType header. Returns the -- (optional) content-type and (optional) the character encoding name.@@ -32,25 +37,29 @@ let (mtype, _) = parseContentTypeHdr str newhdr = case mtype of Nothing -> plainText enc- Just ty -> Header HdrContentType $ showType $ setMIMEEncoding ty enc+ Just ty -> Header HdrContentType $ unpack $ showType $ setMIMEEncoding ty enc in newhdr:rest plainText :: EncodingName -> Header-plainText enc = Header HdrContentType $ showType $+plainText enc = Header HdrContentType $ unpack $ showType $ Type {mimeType = (Text "plain")- ,mimeParams = [("charset", enc)]}+ ,mimeParams = [MIMEParam {paramName = "charset"+ ,paramValue = pack enc}]} setMIMEEncoding :: Type -> EncodingName -> Type setMIMEEncoding ty enc = ty {mimeParams = map replaceEnc $ mimeParams ty}- where replaceEnc (pname, _) | pname == "charset" = (pname, enc)+ where replaceEnc param | paramName param == "charset" =+ param {paramValue = pack enc} replaceEnc x = x parseContentTypeHdr :: String -> (Maybe Type, Maybe EncodingName)-parseContentTypeHdr str = - case parseContentType str of+parseContentTypeHdr str =+ case parseContentType (pack str) of Nothing -> (Nothing, Nothing) Just ctype -> (Just ctype, - (map toLower) <$> lookup "charset" (mimeParams ctype))+ (unpack . Text.map toLower . paramValue) <$> + listToMaybe (filter (\p -> paramName p == "charset")+ (mimeParams ctype))) -- | Tries to decode a bytestring as UTF-8. Returns nothing if any -- illegal characters are encountered
http-encodings.cabal view
@@ -1,5 +1,5 @@ Name: http-encodings-Version: 0.9.1+Version: 0.9.2 Synopsis: A library for encoding and decoding bodies of HTTP messages Description: A library for encoding and decoding bodies of HTTP messages, designed to work with the HTTP and http-server libraries. No heuristic encoding detection at this time. WANTED: a library implementing the Unix "compress" command encoding/decoding (or the LZW algorithm). @@ -9,7 +9,7 @@ Maintainer: Andrey Chudnov <oss@chudnov.com> Homepage: http://github.com/achudnov/http-encodings Bug-reports: http://github.com/achudnov/http-encodings/issues-Copyright: (c) 2012-2013 Stevens Institute of Technology+Copyright: (c) 2012-2014 Stevens Institute of Technology Category: Web Build-type: Simple Stability: Experimental@@ -22,7 +22,7 @@ source-repository this type: git location: git://github.com/achudnov/http-encodings.git- tag: 0.9.1+ tag: 0.9.2 Library Exposed-modules: Network.HTTP.Encoding, @@ -37,5 +37,6 @@ , iconv == 0.4.* , zlib == 0.5.* , mtl == 2.*- , mime == 0.3.*+ , mime == 0.4.*+ , text == 1.* Default-language: Haskell2010