Skip to content

Commit 2ce6af4

Browse files
committed
Better handling of supported schemes.
1 parent 004dff5 commit 2ce6af4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/async/http/endpoint.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ module Async
1818
module HTTP
1919
# Represents a way to connect to a remote HTTP server.
2020
class Endpoint < ::IO::Endpoint::Generic
21+
SCHEMES = ['HTTP', 'HTTPS', 'WS', 'WSS'].to_h do |scheme|
22+
[scheme.downcase, URI.scheme_list[scheme]]
23+
end
24+
2125
def self.parse(string, endpoint = nil, **options)
2226
url = URI.parse(string).normalize
2327

2428
return self.new(url, endpoint, **options)
2529
end
2630

2731
# Construct an endpoint with a specified scheme, hostname, optional path, and options.
32+
#
33+
# @parameter scheme [String] The scheme to use, e.g. "http" or "https".
34+
# @parameter hostname [String]
2835
def self.for(scheme, hostname, path = "/", **options)
2936
# TODO: Consider using URI.for once it becomes available:
30-
uri_klass = URI.scheme_list[scheme.upcase] || URI::HTTP
37+
uri_klass = SCHEMES.fetch(scheme.downcase) do
38+
raise ArgumentError, "Unsupported scheme: #{scheme}"
39+
end
3140

3241
self.new(
3342
uri_klass.new(scheme, nil, hostname, nil, nil, path, nil, nil, nil).normalize,

0 commit comments

Comments
 (0)