Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails proxy controller not pulling images through properly, how to modify appropriately?

I'm having to implement a proxy on a Rails 3.1 app to overcome some cross-domain JS issues.

So far I have it retrieving web page text source seemingly right, however it commonly misses images (perhaps relative paths?) in the page and then when I direct it with an absolute path to an image it will show the ascii encoding of the image rather than the image itself, I think for obvious reasons from the code for someone familiar with the topic.

I was hopeful that someone would be able to revise the following code so as to work also properly with an image proxy situation:

proxy_controller.rb:

class ProxyController < ApplicationController
  def get
    url = URI.parse(params["url"])
    result = Net::HTTP.get_response(url)
      render :text => result.body
  end
end

routes.rb:

get "proxy" => "proxy#get", :as => "proxy"

Calling it via:

http://<my_dev_server>/proxy?url=http://<somedomain.tld>/path/to/page/images/image.jpg

or

http://<my_dev_server>/proxy?url=http://<somedomain.tld>/path/to/page
like image 892
ylluminate Avatar asked Jan 17 '12 07:01

ylluminate


1 Answers

instead of render, use send_data

like image 146
elijah Avatar answered Oct 26 '22 23:10

elijah