-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Change URI#to_s to handle default ports for more schemes than http/https #5233
Changes from 11 commits
d844362
6077c5b
18a0728
b886778
23a825e
5cb8564
57a2936
965ccde
a665fe4
82fa4c4
1fe8e52
43df609
a43adff
aeffdf4
5923d8e
3afe221
88c7a5f
b64d394
1684fab
4c10f9f
d8af953
4f8dd3e
fa62ae9
de6329b
7dec28c
bb3f56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,7 @@ class URI | |
if host | ||
io << host | ||
end | ||
if port && !((scheme == "http" && port == 80) || (scheme == "https" && port == 443)) | ||
unless port.nil? || default_port? | ||
io << ':' | ||
io << port | ||
end | ||
|
@@ -426,4 +426,52 @@ class URI | |
URI.escape(password, io) | ||
end | ||
end | ||
|
||
# A map of schemes and their respective default ports, seeded | ||
# with some well-known schemes. | ||
@@default_ports = { | ||
"ftp" => 21, | ||
"ftps" => 990, | ||
"gopher" => 70, | ||
"http" => 80, | ||
"https" => 443, | ||
"ldap" => 389, | ||
"ldaps" => 636, | ||
"nntp" => 119, | ||
"scp" => 22, | ||
"sftp" => 22, | ||
"ssh" => 22, | ||
"telnet" => 23, | ||
} | ||
|
||
# Returns the default port for the given scheme if known, otherwise | ||
# returns nil. | ||
# | ||
# ``` | ||
# URI.default_port "http" # => 80 | ||
# ``` | ||
def self.default_port(scheme : String) : Int32? | ||
@@default_ports[scheme.downcase]? | ||
end | ||
|
||
# Registers the default port for the given scheme. | ||
# | ||
# If port is nil, the existing default port for the scheme, if any, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the doc formatting should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
# will be unregistered. | ||
# | ||
# ``` | ||
# URI.set_default_port "ponzi" = 9999 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: |
||
# ``` | ||
def self.set_default_port(scheme : String, port : Int32?) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should I change it to always return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just restrict it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
if port | ||
@@default_ports[scheme.downcase] = port | ||
else | ||
@@default_ports.delete scheme.downcase | ||
end | ||
end | ||
|
||
# Returns true if this URI's port is the default port for its scheme. | ||
private def default_port? | ||
port == @@default_ports[scheme]? | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the
URI.default_port
method can returnnil
, should it be suffixed with a question mark (URI.default_port?
) as per other methods such asHash#first_key?
that can returnnil
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is only used when there is also a variant of the method which does not return
nil
(usually raises instead). This makes no sense here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks very much for the explanation!