Skip to content
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

HACK URI::Generic.build to allow a IPv6 host value. #2172

Merged
merged 1 commit into from
Mar 19, 2015

Conversation

jrafanie
Copy link
Member

It's a temporary workaround until ruby/ruby#765 is
readily available. As of this writing, ruby 2.0.0p643 and ruby 2.2.0 have this fixed.
Ruby 2.1.5, and ruby 2.0.0p598 and lower don't have this fixed and NEED this hack.
Ruby 2.0: https://bugs.ruby-lang.org/issues/10873 (backported and in p643)
Ruby 2.1: https://bugs.ruby-lang.org/issues/10875 (backported and not released yet)
Ruby 2.2: https://bugs.ruby-lang.org/issues/10874 (backported and in 2.2.0)

Workaround before this hack:

 # Don't pass :host => "::1" to .build as it throws URI::InvalidComponentError
 uri = URI::HTTPS.build(:path => "/sdk")
 uri.hostname = "::1"
 uri.to_s # => "https://[::1]/sdk"

After this hack the caller doesn't need to know to use [::1] or hostname= method:

 uri = URI::HTTPS.build(:host => "::1", :path => "/sdk")
 uri.to_s # => "https://[::1]/sdk"

This enables us to use the fixed URI::Generic.build API now and clean up 60c8f7f, 01cb19a, 5cf1f30, e806c46 and any other place we may do this in the future.

cc @matthewd @tenderlove @Fryguy @blomquisg @abonas

@@ -0,0 +1,39 @@
require "spec_helper"

$:.push(File.expand_path(File.join(File.dirname(__FILE__), %w{.. .. .. util extensions})))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need this. require 'util/miq-uri' should be sufficient for this spec

@Fryguy
Copy link
Member

Fryguy commented Mar 16, 2015

Looks good to me. Why is this WIP?

@jrafanie
Copy link
Member Author

Ok, so it's not technically WIP but I'm not really pleased with it though. I'd like to have this hack yell at us to remove it when it's not needed anymore although I don't know how to do that. Also, it feels like a lot of code to monkey patch one method. The alternative is to re-implement the whole of URI::Generic initialize in a monkey patch and hope that the code within that method doesn't change.

I was hoping this code would give someone an idea on a better implementation.

@Fryguy
Copy link
Member

Fryguy commented Mar 16, 2015

Can you just throw an else on the outer if and just do a ruby warning or puts something?

@jrafanie
Copy link
Member Author

It's when we move to a ruby 2.0.0 patchlevel that's guaranteed to be >= 643 (rpm based ruby is currently 353 with backports of security fixes) or ruby 2.1.6/2.2.0+ on all appliances/dev machines. I'm afraid it's going to be uselessly noisy if I do it in an outside if/else.

# uri = URI::HTTPS.build(:host => "::1", :path => "/sdk")
# uri.to_s # => "https://[::1]/sdk"
#
if RUBY_VERSION.start_with?("2.1") || (RUBY_VERSION == "2.0.0" && RUBY_PATCHLEVEL < 643)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than doing explicit version checking, we could do:

def supports_ipv6_on_build?
  URI::HTTPS.build(:host => "::1", :path => "/sdk")
  true
rescue URI::InvalidComponentError
  false
end

if !supports_ipv6_on_build?
  # etc

Then we don't have to update this when the 2.1 backport lands.

It's a temporary workaround until ruby/ruby#765 is
readily available.  As of this writing, ruby 2.0.0p643 and ruby 2.2.0 have this fixed.
Ruby 2.1.5, and ruby 2.0.0p598 and lower don't have this fixed and NEED this hack.
Ruby 2.0: https://bugs.ruby-lang.org/issues/10873 (backported and in p643)
Ruby 2.1: https://bugs.ruby-lang.org/issues/10875 (backported and not released yet)
Ruby 2.2: https://bugs.ruby-lang.org/issues/10874 (backported and in 2.2.0)

Workaround before this hack:
```ruby
 # Don't pass :host => "::1" to .build as it throws URI::InvalidComponentError
 uri = URI::HTTPS.build(:path => "/sdk")
 uri.hostname = "::1"
 uri.to_s # => "https://[::1]/sdk"
```

After this hack the caller doesn't need to know to use [::1] or hostname= method:
```ruby
 uri = URI::HTTPS.build(:host => "::1", :path => "/sdk")
 uri.to_s # => "https://[::1]/sdk"
```
@jrafanie jrafanie force-pushed the monkey_patch_uri_generic_build branch from 0b36065 to aa33f55 Compare March 17, 2015 16:45
@jrafanie jrafanie changed the title [WIP] Hack URI::Generic.build to allow IPv6 host values. HACK URI::Generic.build to allow a IPv6 host value. Mar 17, 2015
@jrafanie jrafanie removed the wip label Mar 17, 2015
@miq-bot
Copy link
Member

miq-bot commented Mar 17, 2015

Checked commit jrafanie@aa33f55 with rubocop 0.27.1
2 files checked, 7 offenses detected

lib/spec/util/extensions/miq-uri_spec.rb

lib/util/extensions/miq-uri.rb

@@ -0,0 +1,38 @@
require "spec_helper"

require 'util/extensions/miq-uri'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Fryguy, fixed here.

@Fryguy
Copy link
Member

Fryguy commented Mar 17, 2015

@jrafanie Did you still want to add the "yelling at us to remove the patch" code?

@jrafanie
Copy link
Member Author

@Fryguy we can't easily do it since upstream will constantly yell while downstream wouldn't. I'll have to remember somehow.

@jrafanie
Copy link
Member Author

BUMP @Fryguy @tenderlove please review/merge... to clarify... upstream appliances now use ruby 2.0.0p643(a fixed ruby), some developers may also be using a ruby that would have this fixed, meanwhile, downstream appliances most likely won't be at a "fixed" ruby version for months, therefore yelling at developers would be useless because we still need this hack... until we don't.

Fryguy added a commit that referenced this pull request Mar 19, 2015
HACK URI::Generic.build to allow a IPv6 host value.
@Fryguy Fryguy merged commit bbafca3 into ManageIQ:master Mar 19, 2015
@Fryguy Fryguy deleted the monkey_patch_uri_generic_build branch March 19, 2015 16:07
@Fryguy Fryguy modified the milestones: Roadmap, Sprint 21 Ending Mar 30, 2015 Mar 19, 2015
jrafanie added a commit to jrafanie/manageiq that referenced this pull request Mar 24, 2015
The backport in ManageIQ#2172 enables us to use the fixed syntax now.
@jrafanie jrafanie mentioned this pull request May 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants