views:

32

answers:

1

A certain action must load an html page according a uri parameter. It works fine for all valid uris, except those of the current server. Is there an easy way to request a page from within a controller?

require 'uri'
class MainController < ApplicationController
  def foo
    uri = URI(params[:uri])
    if uri.host =~ /localhost|mydomain\.com/
      # what goes here?
    else
      @html = Net::HTTP.get(uri)
    end
    # Do something useful with @html...
  end
end
# The code should work for http://localhost/foo?uri=http://apple.com/
# as well as for http://localhost/foo?uri=http://localhost/bar
A: 

Why make a distinction? Without more details regarding what you're trying to accomplish, I would just call Net::HTTP.get(uri) for any URL. You should obviously filter out /foo to avoid an infinite loop though.

jdl
As I stated, it doesn't work for local urls. The inside HTTP.get times out.
Marc-André Lafortune
In development mode under 1 mongrel server, or in production mode with something like Passenger that can handle more than one connection at a time?
jdl
Also in production, on Heroku. Not sure why, but with 2 dynos it didn't work, but with 3 it did. It'll do, then :-)
Marc-André Lafortune