Skip to content

Commit f0ef953

Browse files
committed
Merge pull request #6 from garyb/auth-part-parsing
Authority part parsing fixes
2 parents 7d0e872 + 3b6d8c7 commit f0ef953

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

src/Data/URI/HierarchicalPart.purs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ import Text.Parsing.StringParser.Combinators (optionMaybe)
1414
import Text.Parsing.StringParser.String (string)
1515

1616
parseHierarchicalPart :: Parser HierarchicalPart
17-
parseHierarchicalPart =
18-
(HierarchicalPart
19-
<$> optionMaybe (string "//" *> parseAuthority)
20-
<*> parsePathAbEmpty parseURIPathAbs)
17+
parseHierarchicalPart = withAuth <|> withoutAuth
18+
where
19+
withAuth =
20+
HierarchicalPart
21+
<$> Just <$> (string "//" *> parseAuthority)
22+
<*> parsePathAbEmpty parseURIPathAbs
2123

22-
<|> (HierarchicalPart Nothing
23-
<$> ((Just <$> parsePathAbsolute parseURIPathAbs)
24-
<|>
25-
(Just <$> parsePathRootless parseURIPathAbs)
26-
<|>
27-
pure Nothing))
24+
withoutAuth = HierarchicalPart Nothing <$> noAuthPath
25+
26+
noAuthPath
27+
= (Just <$> parsePathAbsolute parseURIPathAbs)
28+
<|> (Just <$> parsePathRootless parseURIPathAbs)
29+
<|> pure Nothing
2830

2931
printHierPart :: HierarchicalPart -> String
3032
printHierPart (HierarchicalPart a p) =

src/Data/URI/Path.purs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Control.Alt ((<|>))
1717
import Control.Bind ((=<<))
1818
import Data.Either (Either(..), either)
1919
import Data.Maybe (Maybe(..))
20-
import Data.Path.Pathy (parseAbsDir, parseRelDir, parseAbsFile, parseRelFile, sandbox, rootDir, (</>), unsafePrintPath)
21-
import Data.String (drop, length)
20+
import Data.Path.Pathy (Path(), parseAbsDir, parseRelDir, parseAbsFile, parseRelFile, sandbox, rootDir, (</>), unsafePrintPath)
21+
import Data.String as Str
2222
import Data.URI.Common
2323
import Data.URI.Types
2424
import Text.Parsing.StringParser (Parser(..), ParseError(..), try)
@@ -67,19 +67,24 @@ parseSegmentNonZeroNoColon = joinWith "" <$> many1 (parseUnreserved
6767

6868
parseURIPathAbs :: Parser URIPathAbs
6969
parseURIPathAbs = Parser \{ str: str, pos: i } fc sc ->
70-
case sandbox rootDir =<< parseAbsFile (drop i str) of
71-
Just file -> sc (Left $ rootDir </> file) { str: str, pos: length str }
72-
Nothing -> case sandbox rootDir =<< parseAbsDir (drop i str) of
73-
Just dir -> sc (Right $ rootDir </> dir) { str: str, pos: length str }
70+
case sandbox rootDir =<< parseAbsFile (Str.drop i str) of
71+
Just file -> sc (Left $ rootDir </> file) { str: str, pos: Str.length str }
72+
Nothing -> case sandbox rootDir =<< parseAbsDir (Str.drop i str) of
73+
Just dir -> sc (Right $ rootDir </> dir) { str: str, pos: Str.length str }
7474
Nothing -> fc i (ParseError $ "Expected a valid path")
7575

7676
parseURIPathRel :: Parser URIPathRel
7777
parseURIPathRel = Parser \{ str: str, pos: i } fc sc ->
78-
case parseRelFile (drop i str) of
79-
Just file -> sc (Left file) { str: str, pos: length str }
80-
Nothing -> case parseRelDir (drop i str) of
81-
Just dir -> sc (Right dir) { str: str, pos: length str }
78+
case parseRelFile (Str.drop i str) of
79+
Just file -> sc (Left file) { str: str, pos: Str.length str }
80+
Nothing -> case parseRelDir (Str.drop i str) of
81+
Just dir -> sc (Right dir) { str: str, pos: Str.length str }
8282
Nothing -> fc i (ParseError $ "Expected a valid path")
8383

8484
printPath :: forall a s. URIPath a s -> String
85-
printPath = either unsafePrintPath unsafePrintPath
85+
printPath = either print print
86+
where
87+
print :: forall a' b s'. Path a' b s' -> String
88+
print path =
89+
let printed = unsafePrintPath path
90+
in if Str.take 2 printed == "./" then Str.drop 2 printed else printed

src/Data/URI/RelativePart.purs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ import Text.Parsing.StringParser.Combinators (optionMaybe)
1414
import Text.Parsing.StringParser.String (string)
1515

1616
parseRelativePart :: Parser RelativePart
17-
parseRelativePart = (RelativePart <$> optionMaybe (string "//" *> parseAuthority) <*> parsePathAbEmpty parseURIPathRel)
18-
<|> (RelativePart Nothing <$> ((Just <$> parsePathAbsolute parseURIPathRel)
19-
<|> (Just <$> parsePathNoScheme parseURIPathRel)
20-
<|> pure Nothing))
17+
parseRelativePart = withAuth <|> withoutAuth
18+
where
19+
20+
withAuth =
21+
RelativePart
22+
<$> Just <$> (string "//" *> parseAuthority)
23+
<*> parsePathAbEmpty parseURIPathRel
24+
25+
withoutAuth = RelativePart Nothing <$> noAuthPath
26+
27+
noAuthPath
28+
= (Just <$> parsePathAbsolute parseURIPathRel)
29+
<|> (Just <$> parsePathNoScheme parseURIPathRel)
30+
<|> pure Nothing
2131

2232
printRelativePart :: RelativePart -> String
2333
printRelativePart (RelativePart a p) =

test/Main.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ main = do
3434
test runParseURIRef "foo://example.com:8042/over/there?name=ferret#nose"
3535
test runParseURIRef "foo://info.example.com?fred"
3636
test runParseURIRef "ftp://cnn.example.com&[email protected]/top_story.htm"
37+
test runParseURIRef "../top_story.htm"
38+
test runParseURIRef "top_story.htm"
3739

3840
C.log "\nFailing test cases: "
3941
testFails runParseURIRef "news:comp.infosystems.www.servers.unix"
4042
testFails runParseURIRef "tel:+1-816-555-1212"
4143
testFails runParseURIRef "urn:oasis:names:specification:docbook:dtd:xml:4.1.2"
4244
testFails runParseURIRef "mailto:[email protected]"
4345
testFails runParseURIRef "mailto:[email protected]"
44-
testFails runParseURIRef "../top_story.htm"
45-
testFails runParseURIRef "top_story.htm"
4646
testFails runParseURIRef "/top_story.htm"
4747

4848

0 commit comments

Comments
 (0)