http-types 0.5.3 → 0.6.0
raw patch · 3 files changed
+44/−40 lines, 3 filesdep +case-insensitivedep ~asciiPVP ok
version bump matches the API change (PVP)
Dependencies added: case-insensitive
Dependency ranges changed: ascii
API changes (from Hackage documentation)
+ Network.HTTP.Types: type Ascii = ByteString
- Network.HTTP.Types: encodePath :: [Text] -> Query -> AsciiBuilder
+ Network.HTTP.Types: encodePath :: [Text] -> Query -> Builder
- Network.HTTP.Types: encodePathSegments :: [Text] -> AsciiBuilder
+ Network.HTTP.Types: encodePathSegments :: [Text] -> Builder
- Network.HTTP.Types: renderQueryBuilder :: Bool -> Query -> AsciiBuilder
+ Network.HTTP.Types: renderQueryBuilder :: Bool -> Query -> Builder
- Network.HTTP.Types: renderQueryText :: Bool -> QueryText -> AsciiBuilder
+ Network.HTTP.Types: renderQueryText :: Bool -> QueryText -> Builder
- Network.HTTP.Types: type Header = (CIAscii, Ascii)
+ Network.HTTP.Types: type Header = (CI Ascii, Ascii)
- Network.HTTP.Types: urlEncodeBuilder :: Bool -> ByteString -> AsciiBuilder
+ Network.HTTP.Types: urlEncodeBuilder :: Bool -> ByteString -> Builder
Files
- Network/HTTP/Types.hs +32/−28
- http-types.cabal +2/−2
- runtests.hs +10/−10
Network/HTTP/Types.hs view
@@ -1,8 +1,10 @@ {-# LANGUAGE OverloadedStrings #-} module Network.HTTP.Types (+ -- * General+ Ascii -- * Methods- Method+, Method , methodGet , methodPost , methodHead@@ -86,12 +88,14 @@ import Data.Text.Encoding.Error (lenientDecode) import Data.Word (Word8) import qualified Blaze.ByteString.Builder as Blaze-import qualified Data.Ascii as A import qualified Data.ByteString as B-import qualified Data.ByteString.Char8 as Ascii+import qualified Data.ByteString.Char8 as B8+import qualified Data.CaseInsensitive as CI +type Ascii = B.ByteString+ -- | HTTP method (flat string type).-type Method = A.Ascii+type Method = Ascii -- | HTTP Method constants. methodGet, methodPost, methodHead, methodPut, methodDelete, methodTrace, methodConnect, methodOptions :: Method@@ -120,17 +124,17 @@ -- lookup is probably faster for these few cases than setting up an elaborate data structure. methodArray :: Array StdMethod Method-methodArray = listArray (minBound, maxBound) $ map (A.unsafeFromString . show) [minBound :: StdMethod .. maxBound]+methodArray = listArray (minBound, maxBound) $ map (B8.pack . show) [minBound :: StdMethod .. maxBound] methodList :: [(Method, StdMethod)] methodList = map (\(a, b) -> (b, a)) (assocs methodArray) -- | Convert a method 'ByteString' to a 'StdMethod' if possible.-parseMethod :: Method -> Either A.Ascii StdMethod+parseMethod :: Method -> Either Ascii StdMethod parseMethod bs = maybe (Left bs) Right $ lookup bs methodList -- | Convert an algebraic method to a 'ByteString'.-renderMethod :: Either A.Ascii StdMethod -> Method+renderMethod :: Either Ascii StdMethod -> Method renderMethod = id ||| renderStdMethod -- | Convert a 'StdMethod' to a 'ByteString'.@@ -170,7 +174,7 @@ data Status = Status { statusCode :: Int- , statusMessage :: A.Ascii+ , statusMessage :: Ascii } deriving (Show) @@ -241,7 +245,7 @@ statusServerError = status500 -- | Header-type Header = (A.CIAscii, A.Ascii)+type Header = (CI.CI Ascii, Ascii) -- | Request Headers type RequestHeaders = [Header]@@ -250,7 +254,7 @@ type ResponseHeaders = [Header] -- | HTTP Headers-headerAccept, headerCacheControl, headerConnection, headerContentLength, headerContentType, headerContentMD5, headerDate :: A.Ascii -> Header+headerAccept, headerCacheControl, headerConnection, headerContentLength, headerContentType, headerContentMD5, headerDate :: Ascii -> Header headerAccept = (,) "Accept" headerCacheControl = (,) "Cache-Control" headerConnection = (,) "Connection"@@ -275,7 +279,7 @@ renderQueryText :: Bool -- ^ prepend a question mark? -> QueryText- -> A.AsciiBuilder+ -> Blaze.Builder renderQueryText b = renderQueryBuilder b . queryTextToQuery queryToQueryText :: Query -> QueryText@@ -299,16 +303,16 @@ renderQueryBuilder :: Bool -- ^ prepend a question mark? -> Query- -> A.AsciiBuilder+ -> Blaze.Builder renderQueryBuilder _ [] = mempty -- FIXME replace mconcat + map with foldr renderQueryBuilder qmark' (p:ps) = mconcat $ go (if qmark' then qmark else mempty) p : map (go amp) ps where- qmark = A.unsafeFromBuilder $ Blaze.copyByteString "?"- amp = A.unsafeFromBuilder $ Blaze.copyByteString "&"- equal = A.unsafeFromBuilder $ Blaze.copyByteString "="+ qmark = Blaze.copyByteString "?"+ amp = Blaze.copyByteString "&"+ equal = Blaze.copyByteString "=" go sep (k, mv) = mconcat [ sep , urlEncodeBuilder True k@@ -319,12 +323,12 @@ -- | Convert 'Query' to 'ByteString'. renderQuery :: Bool -- ^ prepend question mark?- -> Query -> A.Ascii-renderQuery qm = A.fromAsciiBuilder . renderQueryBuilder qm+ -> Query -> Ascii+renderQuery qm = Blaze.toByteString . renderQueryBuilder qm -- | Convert 'SimpleQuery' to 'ByteString'. renderSimpleQuery :: Bool -- ^ prepend question mark?- -> SimpleQuery -> A.Ascii+ -> SimpleQuery -> Ascii renderSimpleQuery useQuestionMark = renderQuery useQuestionMark . simpleQueryToQuery -- | Split out the query string into a list of keys and values. A few@@ -373,8 +377,8 @@ unreservedPI = map ord8 ":@&=+$," -- | Percent-encoding for URLs.-urlEncodeBuilder' :: [Word8] -> B.ByteString -> A.AsciiBuilder-urlEncodeBuilder' extraUnreserved = A.unsafeFromBuilder . mconcat . map encodeChar . B.unpack+urlEncodeBuilder' :: [Word8] -> B.ByteString -> Blaze.Builder+urlEncodeBuilder' extraUnreserved = mconcat . map encodeChar . B.unpack where encodeChar ch | unreserved ch = Blaze.fromWord8 ch | otherwise = h2 ch@@ -391,16 +395,16 @@ urlEncodeBuilder :: Bool -- ^ Whether input is in query string. True: Query string, False: Path element -> B.ByteString- -> A.AsciiBuilder+ -> Blaze.Builder urlEncodeBuilder True = urlEncodeBuilder' unreservedQS urlEncodeBuilder False = urlEncodeBuilder' unreservedPI -urlEncode :: Bool -> Ascii.ByteString -> A.Ascii-urlEncode q = A.fromAsciiBuilder . urlEncodeBuilder q+urlEncode :: Bool -> B.ByteString -> Ascii+urlEncode q = Blaze.toByteString . urlEncodeBuilder q -- | Percent-decoding. urlDecode :: Bool -- ^ Whether to decode '+' to ' '- -> B.ByteString -> B.ByteString+ -> B.ByteString -> B.ByteString urlDecode replacePlus z = fst $ B.unfoldrN (B.length z) go z where go bs =@@ -449,14 +453,14 @@ -- Huge thanks to Jeremy Shaw who created the original implementation of this -- function in web-routes and did such thorough research to determine all -- correct escaping procedures.-encodePathSegments :: [Text] -> A.AsciiBuilder+encodePathSegments :: [Text] -> Blaze.Builder encodePathSegments [] = mempty encodePathSegments (x:xs) =- A.unsafeFromBuilder (Blaze.copyByteString "/")+ Blaze.copyByteString "/" `mappend` encodePathSegment x `mappend` encodePathSegments xs -encodePathSegment :: Text -> A.AsciiBuilder+encodePathSegment :: Text -> Blaze.Builder encodePathSegment = urlEncodeBuilder False . encodeUtf8 decodePathSegments :: B.ByteString -> [Text]@@ -479,7 +483,7 @@ decodePathSegment :: B.ByteString -> Text decodePathSegment = decodeUtf8With lenientDecode . urlDecode False -encodePath :: [Text] -> Query -> A.AsciiBuilder+encodePath :: [Text] -> Query -> Blaze.Builder encodePath x [] = encodePathSegments x encodePath x y = encodePathSegments x `mappend` renderQueryBuilder True y
http-types.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version: 0.5.3+Version: 0.6.0 -- A short (one-line) description of the package. Synopsis: Generic HTTP types for Haskell (for both client and server code).@@ -60,7 +60,7 @@ Build-depends: base >= 4 && < 5, bytestring >=0.9.1.5 && <0.10, array >=0.3 && <0.4,- ascii >= 0.0.1.1 && < 0.1,+ case-insensitive >=0.2 && <0.3, blaze-builder >= 0.2.1.4 && < 0.3, text >= 0.11.0.2 && < 0.12
runtests.hs view
@@ -2,16 +2,16 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS_GHC -fno-warn-orphans #-}-import Data.Text (Text)+import Data.Text (Text) import Debug.Trace import Network.HTTP.Types import Test.Hspec import Test.Hspec.QuickCheck-import Test.QuickCheck (Arbitrary (..))-import qualified Data.Ascii as A-import qualified Data.ByteString as S-import qualified Data.ByteString.Char8 as S8-import qualified Data.Text as T+import Test.QuickCheck (Arbitrary (..))+import qualified Blaze.ByteString.Builder as Blaze+import qualified Data.ByteString as S+import qualified Data.ByteString.Char8 as S8+import qualified Data.Text as T main :: IO () main = hspec $ descriptions@@ -33,7 +33,7 @@ propEncodeDecodePath :: ([Text], Query) -> Bool propEncodeDecodePath (p', q') =- let x = A.toByteString $ A.fromAsciiBuilder $ encodePath a b+ let x = Blaze.toByteString $ encodePath a b y = decodePath x z = y == (a, b) in if z then z else traceShow (a, b, x, y) z@@ -43,14 +43,14 @@ propEncodeDecodeQuery :: Query -> Bool propEncodeDecodeQuery q' =- q == parseQuery (A.toByteString $ renderQuery True q)+ q == parseQuery (renderQuery True q) where q = filter (\(x, _) -> not (S.null x)) q' propQueryQuestionMark :: (Bool, Query) -> Bool propQueryQuestionMark (useQuestionMark, query) = actual == expected where- actual = case S8.uncons . A.toByteString $ renderQuery useQuestionMark query of+ actual = case S8.uncons $ renderQuery useQuestionMark query of Nothing -> False Just ('?', _) -> True _ -> False@@ -61,7 +61,7 @@ propEncodeDecodePathSegments :: [Text] -> Bool propEncodeDecodePathSegments p' =- p == decodePathSegments (A.toByteString $ A.fromAsciiBuilder $ encodePathSegments p)+ p == decodePathSegments (Blaze.toByteString $ encodePathSegments p) where p = if p' == [""] then [] else p'