This project is archived and is in readonly mode.

#352 ✓stale
Ian Smith-Heisters

ActiveResource should support URI::Generic

Reported by Ian Smith-Heisters | June 13th, 2008 @ 04:43 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

  • Ian Smith-Heisters

    Ian Smith-Heisters June 6th, 2008 @ 02:12 AM

    I'll try that as an attachment

  • Gaius Centus Novus

    Gaius Centus Novus June 13th, 2008 @ 11: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

    Ian Smith-Heisters June 13th, 2008 @ 04:41 PM

    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

    Gaius Centus Novus June 13th, 2008 @ 04:55 PM

    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

    Ian Smith-Heisters June 13th, 2008 @ 05:40 PM

    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

    Gaius Centus Novus June 13th, 2008 @ 05:43 PM

    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.

  • Ian Smith-Heisters
  • Pratik

    Pratik August 21st, 2008 @ 12:48 PM

    • State changed from “new” to “invalid”
    • Tag set to activeresource, patch

    Missing tests.

  • Pratik

    Pratik August 21st, 2008 @ 12:48 PM

    • State changed from “invalid” to “incomplete”
  • Rohit Arondekar

    Rohit Arondekar October 6th, 2010 @ 06:44 AM

    • State changed from “incomplete” to “stale”
    • Importance changed from “” to “”

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages