Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
5f7f63cd
Commit
5f7f63cd
authored
8 years ago
by
Cam Saül
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #2956 from metabase/fix-is-url
Fix handling of localhost and ports in is-url?
parents
82ad0986
10e30b44
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/util.clj
+18
-14
18 additions, 14 deletions
src/metabase/util.clj
test/metabase/util_test.clj
+4
-0
4 additions, 0 deletions
test/metabase/util_test.clj
with
22 additions
and
14 deletions
src/metabase/util.clj
+
18
−
14
View file @
5f7f63cd
...
...
@@ -307,6 +307,13 @@
[
default
args
]))
(
defmacro
ignore-exceptions
"Simple macro which wraps the given expression in a try/catch block and ignores the exception if caught."
{
:style/indent
0
}
[
&
body
]
`
(
try
~@
body
(
catch
Throwable
~
'_
)))
;; TODO - rename to `email?`
(
defn
is-email?
"Is STRING a valid email address?"
...
...
@@ -317,15 +324,18 @@
;; TODO - rename to `url?`
(
defn
is-url?
"Is STRING a valid HTTP/HTTPS URL?"
"Is STRING a valid HTTP/HTTPS URL?
(This only handles `localhost` and domains like `metabase.com`; URLs containing IP addresses will return `false`.)
"
^
Boolean
[
^
String
s
]
(
boolean
(
when
s
(
when-let
[
^
java.net.URL
url
(
try
(
java.net.URL.
s
)
(
catch
java.net.MalformedURLException
_
; TODO - use ignore-exceptions
nil
))]
(
when
(
and
(
.getProtocol
url
)
(
.getAuthority
url
))
(
and
(
re-matches
#
"^https?$"
(
.getProtocol
url
))
; these are both automatically downcased
(
re-matches
#
"^.+\..{2,}$"
(
.getAuthority
url
))))))))
; this is the part like 'google.com'. Make sure it contains at least one period and 2+ letter TLD
(
boolean
(
when
(
seq
s
)
(
when-let
[
^
java.net.URL
url
(
ignore-exceptions
(
java.net.URL.
s
))]
;; these are both automatically downcased
(
let
[
protocol
(
.getProtocol
url
)
host
(
.getHost
url
)]
(
and
protocol
host
(
re-matches
#
"^https?$"
protocol
)
(
or
(
re-matches
#
"^.+\..{2,}$"
host
)
; 2+ letter TLD
(
=
host
"localhost"
))))))))
;; TODO - This should be made into a separate `sequence-of-maps?` function and a `maybe?` function
...
...
@@ -525,12 +535,6 @@
(
last
args
)
[(
last
args
)]))))
(
defmacro
ignore-exceptions
"Simple macro which wraps the given expression in a try/catch block and ignores the exception if caught."
{
:style/indent
0
}
[
&
body
]
`
(
try
~@
body
(
catch
Throwable
~
'_
)))
(
defn
deref-with-timeout
"Call `deref` on a FUTURE and throw an exception if it takes more than TIMEOUT-MS."
[
futur
timeout-ms
]
...
...
This diff is collapsed.
Click to expand it.
test/metabase/util_test.clj
+
4
−
0
View file @
5f7f63cd
...
...
@@ -67,6 +67,10 @@
(
expect
true
(
is-url?
"https://amazon.co.uk"
))
(
expect
true
(
is-url?
"http://google.com?q=my-query&etc"
))
(
expect
true
(
is-url?
"http://www.cool.com"
))
(
expect
true
(
is-url?
"http://localhost/"
))
(
expect
true
(
is-url?
"http://localhost:3000"
))
(
expect
true
(
is-url?
"http://www.cool.com:3000"
))
(
expect
true
(
is-url?
"http://localhost:3000/auth/reset_password/144_f98987de-53ca-4335-81da-31bb0de8ea2b#new"
))
(
expect
false
(
is-url?
"google.com"
))
; missing protocol
(
expect
false
(
is-url?
"ftp://metabase.com"
))
; protocol isn't HTTP/HTTPS
(
expect
false
(
is-url?
"http://metabasecom"
))
; no period / TLD
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment