ActiveResource should support URI::Generic
Reported by Ian Smith-Heisters | June 5th, 2008 @ 08:11 PM
ActiveResource detects whether to use SSL with is_a?(URI::HTTPS), which causes problems if you have a URI::Generic object, which is useful if your ActiveResource configuration could be HTTP or HTTPS. It is, of course, easy to workaround by forcing the URI::Generic instance through URI.parse which will determine the correct class. A more elegant solution is to look at the URI::HTTP#scheme attribute to determine whether to use SSL.
quick patch, sorry no test:
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/l
index 0c4ea43..4f653f0 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -180,7 +180,7 @@ module ActiveResource
# remote service and resources.
def http
http = Net::HTTP.new(@site.host, @site.port)
- http.use_ssl = @site.is_a?(URI::HTTPS)
+ http.use_ssl = @site.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
http.read_timeout = @timeout if @timeout # If timeout is not set, the d
http
Comments and changes to this ticket
-

-

Gaius Centus Novus June 13th, 2008 @ 05:01 AM
Seems fine; in fact, seems like the "truer" test of what's intended, but I can't see setting the connection URL to anything other than HTTP or HTTPS.
Maybe the use case is using ActiveResource to find things on the same server? In that case, you might pass a root-relative instead of absolute string to URI::parse (like '/my-other-app/'), which would return a URI::Generic. In that case, though, SSL (a) would be unnecessary and (b) still couldn't be determined from the URI.
-

Ian Smith-Heisters June 13th, 2008 @ 10:41 AM
My use case was allowing configuration with a YAML file like so:
# See URI::Generic#build for possible configuration options scheme: http host: 127.0.0.1 userinfo: 'username:password' port: 3000 path: /Then you read it into ActiveResource like so:
CONFIG = YAML.load(ERB.new(File.read(File.join(RAILS_ROOT, 'config', 'intranet.yml'))).result).with_indifferent_access self.site = URI::Generic.build(CONFIG)Generic is necessary because developers will be using http on localhost, while production servers will use https.
HTH
-

Gaius Centus Novus June 13th, 2008 @ 10:55 AM
Why not do this:
in conf/intranet.yml:
active_resource_url: http://user:password@127.0.0.1:3000/path
in loader.rb:
CONFIG = YAML.load(ERB.new(File.read(File.join(RAILS_ROOT, 'config',
'intranet.yml'))).result).with_indifferent_access
self.site = URI::parse(CONFIG[:active_resource_url)
That way, the developers can specify whatever they want, and you
always get an HTTP or HTTPS URI.
-James
-

Ian Smith-Heisters June 13th, 2008 @ 11:40 AM
yeah, that's essentially what I'm doing now, except that I approached it by modifying
self.site = URI::Generic.build(CONFIG)to
self.site = URI::Generic.build(CONFIG).to_s...exploiting the fact that self.site= will call URI.parse for me. IMO it's just a little nicer in that it still allows specifying the url in its various parts with labels (clearer), and that it keeps CONFIG namespaced to the ActiveResource descendant. Six of one, half a dozen of the other.
The point isn't that there isn't a workaround, but that the duck typing approach is just slightly more flexible and less surprising.
-

Gaius Centus Novus June 13th, 2008 @ 11:43 AM
No, I like your fix. My point was that the fix is really for the sake of "doing the right thing" rather than providing some unavailable functionality.
-

-
Pratik August 21st, 2008 @ 06:48 AM
- → State changed from new to invalid
- → Tag changed from to activeresource patch
Missing tests.
-
Pratik August 21st, 2008 @ 06:48 AM
- → State changed from invalid to incomplete
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
