Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for several parsing bugs #20

Merged
merged 4 commits into from
Jun 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 65 additions & 22 deletions src/Erl.elm
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ rightFrom delimiter str =
|> Maybe.withDefault ""


rightFromLeftMost : String -> String -> String
rightFromLeftMost delimiter str =
let
parts =
split delimiter str
in
case List.length parts of
0 ->
""

1 ->
""

_ ->
parts
|> List.tail
|> Maybe.withDefault []
|> join delimiter


rightFromOrSame : String -> String -> String
rightFromOrSame delimiter str =
let
Expand Down Expand Up @@ -215,26 +235,40 @@ extractProtocol str =
-}
extractHost : String -> String
extractHost str =
-- if str has // then we can assume that what follows is the host
-- if str starts with // or contains :// then we can assume that what follows is the host
-- if it doesn't then look for tld
if String.contains "//" str then
str
|> rightFromOrSame "//"
|> leftFromOrSame "/"
|> leftFromOrSame ":"
let
delim = schemeHostDelim str
in
case delim of
Just delim ->
str
|> rightFromLeftMost delim
|> leftFromOrSame "/"
|> leftFromOrSame ":"

Nothing ->
let
-- Look for something with a dot e.g. host.tld
rx =
"((\\w|-)+\\.)+(\\w|-)+"
in
str
|> leftFromOrSame "/"
|> Regex.find (Regex.AtMost 1) (Regex.regex rx)
|> List.map .match
|> List.head
|> Maybe.withDefault ""


schemeHostDelim : String -> Maybe String
schemeHostDelim str =
if String.startsWith "//" str then
Just "//"
else if String.contains "://" str then
Just "://"
else
let
-- Look for something with a dot e.g. host.tld
rx =
"((\\w|-)+\\.)+(\\w|-)+"
in
str
|> rightFromOrSame "//"
|> leftFromOrSame "/"
|> Regex.find (Regex.AtMost 1) (Regex.regex rx)
|> List.map .match
|> List.head
|> Maybe.withDefault ""
Nothing


parseHost : String -> List String
Expand Down Expand Up @@ -311,13 +345,22 @@ extractPath str =
let
host =
extractHost str

delim =
schemeHostDelim str

trimmed =
case delim of
Just delim ->
rightFromLeftMost delim str

Nothing ->
str
in
str
|> rightFromOrSame "//"
trimmed
|> leftFromOrSame "?"
|> leftFromOrSame "#"
|> Regex.replace (Regex.AtMost 1) (Regex.regex host) (\_ -> "")
|> Regex.replace (Regex.AtMost 1) (Regex.regex ":\\d+") (\_ -> "")
|> Regex.replace (Regex.AtMost 1) (Regex.regex ("^.*?" ++ (Regex.escape host) ++ "(:\\d+)?")) (\_ -> "")


parsePath : String -> List String
Expand Down
11 changes: 11 additions & 0 deletions src/ErlTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ testHostExtract =
, ( "http://notld", "notld" )
, ( "http://notld:80", "notld" )
, ( "http://notld/users", "notld" )
, ( "http://foo.com/bar//baz.txt", "foo.com" )
, ( "//foo.com/bar/baz.txt", "foo.com" )
, ( "//foo.com/bar//baz.txt", "foo.com" )
, ( "foo.com", "foo.com" )
, ( "foo-.com", "foo-.com" )
, ( "foo.com/users", "foo.com" )
, ( "api.foo.com", "api.foo.com" )
, ( "foo.com/bar//baz", "foo.com" )
, ( "users/1/edit", "" )
, ( "users/index.html", "" )
, ( "/users/index.html", "" )
, ( "/foo//bar.txt", "" )
, ( "file:///foo/bar", "" )
]

run ( input, expected ) =
Expand Down Expand Up @@ -165,14 +171,19 @@ testPathExtract =
let
inputs =
[ ( "http://foo.com/users/index.html", "/users/index.html" )
, ( "http://foo.com/users//index.html", "/users//index.html" )
, ( "//foo.com/users/index.html", "/users/index.html" )
, ( "http://localhost/users/index.html", "/users/index.html" )
, ( "//localhost/localhost/users/index.html", "/localhost/users/index.html" )
, ( "//localhost/localhost/users//index.html", "/localhost/users//index.html" )
, ( "foo.com/users//index.html", "/users//index.html" )
, ( "/users/index.html", "/users/index.html" )
, ( "users/index.html", "users/index.html" )
, ( "users/index.html#xyz", "users/index.html" )
, ( "users/index.html?a=1", "users/index.html" )
, ( "http://example.com:2000", "" )
, ( "http://[^a](a+)+$/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" )
, ( "file:///foo/bar", "/foo/bar" )
]

run ( input, expected ) =
Expand Down