views:

89

answers:

2

My Rails app has some pages which are SSL-required and others which are SSL-optional. The optional pages use some assets which are served off-site (images from a vendor) which have both http and https URLs. I need to use https when the page is accessed via SSL to avoid the dreaded "this page contains both secure and insecure elements" warning.

I've written code to return the image URLs as http by default and https if requested. My problem now is determining in the view how the request came in. request.ssl? doesn't work in views.

I've tried using a before_filter which sets something like @ssl_request using request.ssl?, but that also always returns false. Is there a more elegant way to do this?

The server stack is Nginx and Passenger. Other apps with Apache => Mongrel stacks pass an X_FORWARDED_PROTO header to tell Rails that SSL is or isn't being used; is it possible that Nginx/Passenger doesn't do this?

+1  A: 
<img src="//cdn.example.com/images/99dbe20bc52e4caa.jpg">
Justice
Clever, but the domains are different in this case, e.g. cdn.example.com and cdn-secure.example.com.
pjmorse
Then you may need more complicated server-side logic. But I'm just curious, why do you have two separate CDN domains for HTTP and HTTPS?
Justice
They're not mine - they're Amazon's. Product photos are at http://ecx.images-amazon.com or https://images-na.ssl-images-amazon.com . I don't know why they did it that way.
pjmorse
+1  A: 

Try a before_filter in application.rb:

before_filter { |c| UMNAuthFilter.filter(c) if c.request.ssl? }

EDITed to add correctness :)

Chris McCall
This blog post doesn't say anything about how to tell whether the request was http or https within the view; it's all about how to control whether links within the app appear as http or https.
pjmorse
The edited version looks promising, but request.ssl? still returns false in all cases. I'm still suspecting there's some place in the Nginx->Passenger->Rails chain where the word that the request is SSL is getting dropped.
pjmorse
Kicked the server a few more times and got that working.
pjmorse