Skip to content

Commit c00f795

Browse files
committedJan 27, 2015
This patch backports URI#hostname to ruby 1.9.2 and older.
URI#hostname is used for IPv6 support in Excon. URI#hostname was added in stdlib in v1_9_3_0 in this commit: ruby/ruby@5fd45a4 Addressable::URI is also an URI parser accepted in some parts of Excon. Addressable::URI#hostname was added in addressable-2.3.5+ in this commit: sporkmonger/addressable@1b94abb Users who want to use Addressable::URI to parse URIs must upgrade to 2.3.5 or newer.
1 parent 5b92431 commit c00f795

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎lib/excon.rb

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
require 'zlib'
1212
require 'stringio'
1313

14+
require 'excon/extensions/uri'
15+
1416
require 'excon/middlewares/base'
1517
require 'excon/middlewares/expects'
1618
require 'excon/middlewares/idempotent'

‎lib/excon/extensions/uri.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# TODO: Remove this monkey patch once ruby 1.9.3+ is the minimum supported version.
2+
#
3+
# This patch backports URI#hostname to ruby 1.9.2 and older.
4+
# URI#hostname is used for IPv6 support in Excon.
5+
#
6+
# URI#hostname was added in stdlib in v1_9_3_0 in this commit:
7+
# https://github.com/ruby/ruby/commit/5fd45a4b79dd26f9e7b6dc41142912df911e4d7d
8+
#
9+
# Addressable::URI is also an URI parser accepted in some parts of Excon.
10+
# Addressable::URI#hostname was added in addressable-2.3.5+ in this commit:
11+
# https://github.com/sporkmonger/addressable/commit/1b94abbec1f914d5f707c92a10efbb9e69aab65e
12+
#
13+
# Users who want to use Addressable::URI to parse URIs must upgrade to 2.3.5 or newer.
14+
require 'uri'
15+
unless URI("http://foo/bar").respond_to?(:hostname)
16+
module URI
17+
class Generic
18+
# extract the host part of the URI and unwrap brackets for IPv6 addresses.
19+
#
20+
# This method is same as URI::Generic#host except
21+
# brackets for IPv6 (and future IP) addresses are removed.
22+
#
23+
# u = URI("http://[::1]/bar")
24+
# p u.hostname #=> "::1"
25+
# p u.host #=> "[::1]"
26+
#
27+
def hostname
28+
v = self.host
29+
/\A\[(.*)\]\z/ =~ v ? $1 : v
30+
end
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)
Please sign in to comment.