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

Make url parsing less case sensitive #41

Merged
merged 2 commits into from
Jun 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ end
function _validate_azure(ok::Bool, host, account, container, blob)
return (
ok,
isnothing(host) ? nothing : replace(String(host), "azure" => "https"; count=1),
isnothing(host) ? nothing : replace(String(host), r"^azure"i => "https"; count=1),
String(validate_account_name(account)),
isnothing(container) ? "" : String(validate_container_name(container)),
isnothing(blob) ? "" : String(validate_blob(blob)),
Expand All @@ -95,7 +95,7 @@ function _validate_aws(ok::Bool, accelerate, host, bucket, region, key)
return (
ok,
accelerate,
isnothing(host) ? nothing : replace(String(host), "s3" => "http"; count=1),
isnothing(host) ? nothing : replace(String(host), r"^(s|S)3" => "http"; count=1),
String(validate_bucket_name(bucket, accelerate)),
isnothing(region) || isempty(region) ? "" : String(validate_region(region)),
isnothing(key) ? "" : String(validate_key(key)),
Expand All @@ -107,18 +107,18 @@ function parseAzureAccountContainerBlob(url; parseLocal::Bool=false)
url = String(url)
# https://myaccount.blob.core.windows.net/mycontainer/myblob
# https://myaccount.blob.core.windows.net/mycontainer
m = match(r"^(https|azure)://(?<account>[^\.]+?)(\.blob\.core\.windows\.net)?/(?<container>[^/]+?)(?:/(?<blob>.+))?$", url)
m = match(r"^(https|azure)://(?<account>[^\.]+?)(\.blob\.core\.windows\.net)?/(?<container>[^/]+?)(?:/(?<blob>.+))?$"i, url)
m !== nothing && return _validate_azure(true, nothing, m[:account], m[:container], m[:blob])
if parseLocal
# "https://127.0.0.1:45942/devstoreaccount1/jl-azurite-21807/"
m = match(r"^(?<host>(https|azure)://[\d|\.|:]+?)/(?<account>[^/]+?)/(?<container>[^/]+?)(?:/(?<blob>.+))?$", url)
m = match(r"^(?<host>(https|azure)://[\d|\.|:]+?)/(?<account>[^/]+?)/(?<container>[^/]+?)(?:/(?<blob>.+))?$"i, url)
m !== nothing && return _validate_azure(true, m[:host], m[:account], m[:container], m[:blob])
end
# azure://myaccount/mycontainer/myblob
# azure://myaccount/mycontainer
# azure://myaccount
m = match(r"^azure://(?<account>[^/]+)(?:/(?<container>.+))?(?:/(?<blob>.+))?$"i, url)
m !== nothing && return return _validate_azure(true, nothing, m[:account], m[:container], m[:blob])
m !== nothing && return _validate_azure(true, nothing, m[:account], m[:container], m[:blob])
return (false, nothing, "", "", "")
end

Expand All @@ -132,15 +132,15 @@ function parseAWSBucketRegionKey(url; parseLocal::Bool=false)
# https://bucket-name.s3.region-code.amazonaws.com
# https://bucket-name.s3.amazonaws.com/key-name
# https://bucket-name.s3.amazonaws.com
m = match(r"^https://(?<bucket>[^\.]+)\.s3(?<accelerate>-accelerate)?(?:\.(?<region>[^\.]+))?\.amazonaws\.com(?:/(?<key>.+))?$", url)
m = match(r"^https://(?<bucket>[^\.]+)\.s3(?<accelerate>-accelerate)?(?:\.(?<region>[^\.]+))?\.amazonaws\.com(?:/(?<key>.+))?$"i, url)
m !== nothing && return _validate_aws(true, !isnothing(m[:accelerate]), nothing, m[:bucket], m[:region], m[:key])
# https://s3.region-code.amazonaws.com/bucket-name/key-name
# https://s3.region-code.amazonaws.com/bucket-name
m = match(r"^https://s3(?:\.(?<region>[^\.]+))?\.amazonaws\.com/(?<bucket>[^/]+)(?:/(?<key>.+))?$", url)
m = match(r"^https://s3(?:\.(?<region>[^\.]+))?\.amazonaws\.com/(?<bucket>[^/]+)(?:/(?<key>.+))?$"i, url)
m !== nothing && return _validate_aws(true, false, nothing, m[:bucket], m[:region], m[:key])
if parseLocal
# "http://127.0.0.1:27181/jl-minio-4483/"
m = match(r"^(?<host>(http|s3)://[\d|\.|:]+?)/(?<bucket>[^/]+?)(?:/(?<key>.+))?$", url)
m = match(r"^(?<host>(http|s3)://[\d|\.|:]+?)/(?<bucket>[^/]+?)(?:/(?<key>.+))?$"i, url)
m !== nothing && return _validate_aws(true, false, m[:host], m[:bucket], "", m[:key])
end
# S3://bucket-name/key-name
Expand Down
30 changes: 28 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ end
end

@testset "URL Parsing Unit Tests" begin

azure = [
("https://myaccount.blob.core.windows.net/mycontainer/myblob", (true, nothing, "myaccount", "mycontainer", "myblob")),
("https://myaccount.blob.core.windows.net/mycontainer", (true, nothing, "myaccount", "mycontainer", "")),
Expand All @@ -241,7 +240,17 @@ end
("https://127.0.0.1:45942/myaccount/mycontainer/myblob", (true, "https://127.0.0.1:45942", "myaccount", "mycontainer", "myblob")),
("azure://127.0.0.1:45942/myaccount/mycontainer", (true, "https://127.0.0.1:45942", "myaccount", "mycontainer", "")),
("azure://127.0.0.1:45942/myaccount/mycontainer/myblob", (true, "https://127.0.0.1:45942", "myaccount", "mycontainer", "myblob")),
("azure://myaccount", (true, nothing, "myaccount", "", ""))
("azure://myaccount", (true, nothing, "myaccount", "", "")),

("HTTPS://myaccount.BLOB.core.windows.net/mycontainer/myblob", (true, nothing, "myaccount", "mycontainer", "myblob")),
("httpS://myaccount.blob.CORE.windows.net/mycontainer", (true, nothing, "myaccount", "mycontainer", "")),
("AZURE://myaccount.blob.core.WINDOWS.net/mycontainer/myblob", (true, nothing, "myaccount", "mycontainer", "myblob")),
("azurE://myaccount.blob.core.windows.NET/mycontainer", (true, nothing, "myaccount", "mycontainer", "")),
("Https://127.0.0.1:45942/myaccount/mycontainer", (true, "Https://127.0.0.1:45942", "myaccount", "mycontainer", "")),
("hTTPs://127.0.0.1:45942/myaccount/mycontainer/myblob", (true, "hTTPs://127.0.0.1:45942", "myaccount", "mycontainer", "myblob")),
("Azure://127.0.0.1:45942/myaccount/mycontainer", (true, "https://127.0.0.1:45942", "myaccount", "mycontainer", "")),
("aZURe://127.0.0.1:45942/myaccount/mycontainer/myblob", (true, "https://127.0.0.1:45942", "myaccount", "mycontainer", "myblob")),
("Azure://myaccount", (true, nothing, "myaccount", "", ""))
]
for (url, parts) in azure
ok, host, account, container, blob = CloudStore.parseAzureAccountContainerBlob(url; parseLocal=true)
Expand Down Expand Up @@ -269,6 +278,23 @@ end
("s3://bucket-name", (true, false, nothing, "bucket-name", "", "")),
("http://127.0.0.1:27181/bucket-name/key-name", (true, false, "http://127.0.0.1:27181", "bucket-name", "", "key-name")),
("http://127.0.0.1:27181/bucket-name", (true, false, "http://127.0.0.1:27181", "bucket-name", "", "")),

("Https://bucket-name.s3-ACCELERATE.us-east-1.amazonaws.com/key-name", (true, true, nothing, "bucket-name", "us-east-1", "key-name")),
("HTTPS://bucket-name.s3-accelerate.us-east-1.AMAZONAWS.com", (true, true, nothing, "bucket-name", "us-east-1", "")),
("httpS://bucket-name.S3-ACCELERATE.AMAZONAWS.com/key-name", (true, true, nothing, "bucket-name", "", "key-name")),
("hTTPs://bucket-name.s3-accelerate.amazonaws.com", (true, true, nothing, "bucket-name", "", "")),
("HTTPs://bucket-name.s3.us-east-1.amazonaws.COM/key-name", (true, false, nothing, "bucket-name", "us-east-1", "key-name")),
("httpS://bucket-name.S3.us-east-1.AMAZONAWS.COM", (true, false, nothing, "bucket-name", "us-east-1", "")),
("HTTPs://bucket-name.S3.amazonaws.COM/key-name", (true, false, nothing, "bucket-name", "", "key-name")),
("hTTPS://bucket-name.S3.AMAZONAWS.COM", (true, false, nothing, "bucket-name", "", "")),
("hTTpS://s3.us-east-1.AMAZONAWS.com/bucket-name/key-name", (true, false, nothing, "bucket-name", "us-east-1", "key-name")),
("HTTPS://s3.us-east-1.amazonaws.COM/bucket-name", (true, false, nothing, "bucket-name", "us-east-1", "")),
("hTTPs://S3.AMAZONAWS.COM/bucket-name/key-name", (true, false, nothing, "bucket-name", "", "key-name")),
("httPS://S3.AmAzonAws.com/bucket-name", (true, false, nothing, "bucket-name", "", "")),
("S3://bucket-name/key-name", (true, false, nothing, "bucket-name", "", "key-name")),
("S3://bucket-name", (true, false, nothing, "bucket-name", "", "")),
("HTtp://127.0.0.1:27181/bucket-name/key-name", (true, false, "HTtp://127.0.0.1:27181", "bucket-name", "", "key-name")),
("htTP://127.0.0.1:27181/bucket-name", (true, false, "htTP://127.0.0.1:27181", "bucket-name", "", "")),
]
for (url, parts) in s3
ok, accelerate, host, bucket, reg, key = CloudStore.parseAWSBucketRegionKey(url; parseLocal=true)
Expand Down