By | ~fr33domlover |
At | 2018-06-07 |
Title | Render URI to ByteString and perform percent encoding |
Description |
Edit file src/Network/IRI.hs 0 → 0
- 44 import Data.Char (isAsciiLower, isAsciiUpper, isHexDigit, digitToInt)
+ 44 import Data.Char hiding (isDigit)
- 49 import Data.Text.Encoding (decodeUtf8, decodeUtf8')
+ 49 import Data.Text.Encoding (encodeUtf8, decodeUtf8, decodeUtf8')
- 54 import qualified Data.ByteString as B (empty, null, pack)
- 55 import qualified Data.ByteString.Char8 as BC (cons)
+ 54 import qualified Data.ByteString as B (empty, null, pack, intercalate, concat)
+ 55 import qualified Data.ByteString.Char8 as BC (singleton, cons, pack, concatMap)
- 57 import qualified Data.Text as T (null, empty, pack, concat, intercalate)
+ 57 import qualified Data.Text as T (null, concat)
… … … … - 133 renderURI :: URI t -> Text
- 134 renderURI (URI (Scheme s) hier q f) = T.concat
+ 133 renderURI :: URI t -> ByteString
+ 134 renderURI (URI (Scheme s) hier q f) = B.concat
- 136 then T.empty
- 137 else decodeUtf8 s <> ":"
+ 136 then B.empty
+ 137 else s <> ":"
- 139 HierarchyAuthorized (Authority mui h mp) segs -> T.concat
+ 139 HierarchyAuthorized (Authority mui h mp) segs -> B.concat
- 142 Nothing -> T.empty
- 143 Just (UserInfo t) -> t <> "@"
+ 142 Nothing -> B.empty
+ 143 Just (UserInfo t) -> percentEncode userinfoChar t <> "@"
- 146 T.intercalate "." $ map hex $ split 0xff w
- 147 HostIPv6 (IPv6 w v) -> T.concat
+ 146 B.intercalate "." $ map hex $ split 0xff w
+ 147 HostIPv6 (IPv6 w v) -> B.concat
- 149 , T.intercalate ":" $
+ 149 , B.intercalate ":" $
- 154 HostIPvFuture (IPvFuture i b) -> T.concat
+ 154 HostIPvFuture (IPvFuture i b) -> B.concat
- 158 , decodeUtf8 b
+ 158 , b
- 162 T.intercalate "." $ map unsegD dsegs
+ 162 B.intercalate "." $ map unsegD dsegs
- 164 Nothing -> T.empty
- 165 Just (Port w) -> ":" <> T.pack (show w)
+ 164 Nothing -> B.empty
+ 165 Just (Port w) -> ":" <> BC.pack (show w)
- 167 then T.empty
- 168 else "/" <> T.intercalate "/" (map unsegP segs)
+ 167 then B.empty
+ 168 else "/" <> B.intercalate "/" (map unsegP segs)
- 171 "/" <> T.intercalate "/" (map unsegP segs)
- 172 HierarchyRootless segs ->
- 173 T.intercalate "/" $ map unsegP $ N.toList segs
- 174 HierarchyEmpty -> T.empty
+ 171 "/" <> B.intercalate "/" (map unsegP segs)
+ 172 HierarchyRootless (x :| xs) ->
+ 173 if B.null s
+ 174 then B.intercalate "/" $ unsegPC x : map unsegP xs
+ 175 else B.intercalate "/" $ map unsegP $ x : xs
+ 176 HierarchyEmpty -> B.empty
- 178 Nothing -> T.empty
- 179 Just (Query t) -> "?" <> t
+ 180 Nothing -> B.empty
+ 181 Just (Query t) -> "?" <> percentEncode queryOrFragmentChar t
- 181 Nothing -> T.empty
- 182 Just (Fragment t) -> "#" <> t
+ 183 Nothing -> B.empty
+ 184 Just (Fragment t) -> "#" <> percentEncode queryOrFragmentChar t
- 185 unsegP (PathSegment t) = t
- 186 unsegD (DomainSegment t) = t
+ 187 unsegP (PathSegment t) = percentEncode isPathCharLit t
+ 188 unsegPC (PathSegment t) = percentEncode isPathCharLitNoColon t
+ 189 unsegD (DomainSegment t) = percentEncode regNameSegChar t
- 193 hex :: (Integral i, Show i) => i -> Text
- 194 hex = T.pack . flip showHex ""
+ 196 hex :: (Integral i, Show i) => i -> ByteString
+ 197 hex = BC.pack . flip showHex ""
+ 198 + 199 percentEncode :: (Char -> Bool) -> Text -> ByteString
+ 200 percentEncode p =
+ 201 let enc c =
+ 202 if p c
+ 203 then BC.singleton c
+ 204 else BC.pack $ (:) '%' $ map toUpper $ showHex (ord c) ""
+ 205 in BC.concatMap enc . encodeUtf8
… … … … + 357 userinfoChar :: Char -> Bool
+ 358 userinfoChar c = unreserved c || subDelim c || c == ':'
+ 359 - 350 userinfo = UserInfo <$> bsOrPercentText0 uichar
- 351 where
- 352 uichar c = unreserved c || subDelim c || c == ':'
+ 364 userinfo = UserInfo <$> bsOrPercentText0 userinfoChar
… … … … + 459 regNameSegChar :: Char -> Bool
+ 460 regNameSegChar c = (unreserved c || subDelim c) && c /= '.'
+ 461 - 453 seg = DomainSegment <$> bsOrPercentText1 segchar
- 454 segchar c = (unreserved c || subDelim c) && c /= '.'
+ 468 seg = DomainSegment <$> bsOrPercentText1 regNameSegChar
… … … … + 504 queryOrFragmentChar :: Char -> Bool
+ 505 queryOrFragmentChar c = isPathCharLit c || c == '/' || c == '?'
+ 506 - 494 queryOrFragment =
- 495 bsOrPercentText0 $ \ c -> isPathCharLit c || c == '/' || c == '?'
+ 511 queryOrFragment = bsOrPercentText0 queryOrFragmentChar
… … … …