-
-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d844362
Change URI#to_s to handle default ports for more schemes than http/https
lachlan 6077c5b
Change DEFAULT_PORTS to be a constant rather than class variable
lachlan 18a0728
Rename method to default_port? and move nil check back to #to_s
lachlan b886778
Remove unnecessary return statement from default_port? method
lachlan 23a825e
Add URI.default_ports method, which allows for default ports to
lachlan 5cb8564
Fix URI::DefaultPorts#[]= to downcase scheme on delete
lachlan 57a2936
Add URI.default_port method for returning a scheme's the default port
lachlan 965ccde
Rename URI.default_ports method to URI.default_port, allowing
lachlan a665fe4
Fix method comments to use back ticks consistently
lachlan 82fa4c4
Change DefaultPort class to use instance variable rather than class
lachlan 1fe8e52
Simplify to use two methods: URI.default_port and URI.set_default_port
lachlan 43df609
Fix the method documentation formatting
lachlan a43adff
Add more well-known default ports sourced from IANA via Mahmoud's
lachlan aeffdf4
Fix #default_port? to correctly handle nil scheme
lachlan 5923d8e
Change URI default port API to return a mutable subclass of Hash
lachlan 3afe221
Change DefaultPortHash seeding to use merge! method
lachlan 88c7a5f
Fix default_port? to not call scheme.downcase unnecessarily
lachlan b64d394
Add spec for URI with nil scheme and non-nil port
lachlan 1684fab
Change DefaultPortHash seeds to be an initialize parameter
lachlan 4c10f9f
Fix DefaultPortHash case insensitive keys for methods other than []= …
lachlan d8af953
Revert to using two methods: URI.default_port and URI.set_default_port
lachlan 4f8dd3e
Fix typo in URI.default_port comment
lachlan fa62ae9
Fix typo in URI.set_default_port comment
lachlan de6329b
Fix URI.set_default_port comment formatting for scheme parameter
lachlan 7dec28c
Fix URI#default_port? to be thread safe
lachlan bb3f56a
Change URI.set_default_port to always return nil
lachlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class URI | ||
# Global registry for URI scheme default ports. | ||
private class DefaultPorts | ||
# Well-known schemes and their respective default ports. | ||
@@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`. | ||
# | ||
# ``` | ||
# default_ports = DefaultPorts.new | ||
# default_ports["http"] # => 80 | ||
# default_ports["ponzi"] # => nil | ||
# ``` | ||
def [](scheme : String?) : Int32? | ||
return nil if scheme.nil? | ||
@@default_ports[scheme.downcase]? | ||
end | ||
|
||
# Globally registers the default `port` for the given `scheme`. | ||
# | ||
# ``` | ||
# default_ports = DefaultPorts.new | ||
# default_ports["ponzi"] # => nil | ||
# default_ports["ponzi"] = 9999 | ||
# default_ports["ponzi"] # => 9999 | ||
# ``` | ||
def []=(scheme : String, port : Int32?) | ||
if port | ||
@@default_ports[scheme.downcase] = port | ||
else | ||
@@default_ports.delete scheme | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why allow a nil scheme here? It should be String only imo