Skip to content

Commit 5608e98

Browse files
authored
Merge pull request #21 from athanclark/v2.0.0-ipv4-fix
properly parsing ipv4 addresses
2 parents e2c5df9 + 4aa8853 commit 5608e98

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

bower.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
"package.json"
1818
],
1919
"dependencies": {
20-
"purescript-globals": "^2.0.0",
21-
"purescript-integers": "^2.0.0",
22-
"purescript-maps": "^2.0.0",
23-
"purescript-pathy": "^3.0.0",
24-
"purescript-string-parsers": "^2.0.0",
25-
"purescript-unfoldable": "^2.0.0"
20+
"purescript-globals": "2.x",
21+
"purescript-integers": "2.x",
22+
"purescript-maps": "2.x",
23+
"purescript-pathy": "3.x",
24+
"purescript-string-parsers": "2.x",
25+
"purescript-unfoldable": "2.x"
2626
},
2727
"devDependencies": {
28-
"purescript-test-unit": "10.0.1"
28+
"purescript-test-unit": "10.x",
29+
"purescript-psci-support": "2.x"
2930
}
3031
}

src/Data/URI/Host.purs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import Prelude
77

88
import Control.Alt ((<|>))
99

10-
import Data.String as S
10+
import Data.Int as Int
11+
import Data.Maybe (Maybe (Just))
1112
import Data.URI.Common (parseSubDelims, parsePCTEncoded, parseUnreserved, joinWith, rxPat)
1213
import Data.URI.Types (Host(..))
1314

14-
import Text.Parsing.StringParser (Parser, try)
15+
import Text.Parsing.StringParser (Parser, try, fail)
1516
import Text.Parsing.StringParser.Combinators ((<?>), many1)
16-
import Text.Parsing.StringParser.String (string)
17+
import Text.Parsing.StringParser.String (string, char)
1718

1819
parseHost Parser Host
1920
parseHost = parseIPv6Address <|> parseIPv4Address <|> parseRegName
@@ -23,12 +24,24 @@ parseIPv6Address ∷ Parser Host
2324
parseIPv6Address = IPv6Address <$> (string "[" *> rxPat "[a-f0-9\\.:]+" <* string "]") <?> "IPv6 address"
2425

2526
parseIPv4Address Parser Host
26-
parseIPv4Address = IPv4Address <$> rxPat pattern <?> "IPv4 address"
27+
parseIPv4Address = IPv4Address <$> parse <?> "IPv4 address"
2728
where
28-
pattern String
29-
pattern = S.joinWith "" ["(", octet, "\\.", octet, "\\.", octet, "\\.", octet, ")"]
30-
octet String
31-
octet = "(1[0-9]{2}|[1-9][0-9]|[0-9]|2[0-4][0-9]|25[0-5])"
29+
parse Parser String
30+
parse = do
31+
o1 <- octet
32+
_ <- char '.'
33+
o2 <- octet
34+
_ <- char '.'
35+
o3 <- octet
36+
_ <- char '.'
37+
o4 <- octet
38+
pure $ show o1 <> "." <> show o2 <> "." <> show o3 <> "." <> show o4
39+
octet Parser Int
40+
octet = do
41+
s <- rxPat "0|([1-9][0-9]{0,2})"
42+
case Int.fromString s of
43+
Just n | n >= 0 && n <= 255 -> pure n
44+
_ -> fail "Invalid IPv4 address octet"
3245

3346
parseRegName Parser Host
3447
parseRegName =

0 commit comments

Comments
 (0)