Recognize https behind a proxy

Fix problem when the web server is behind an ssl termination proxy (http://en.wikipedia.org/wiki/SSL_termination_proxy)
This conditional is getting pretty confusing, and I'm inclined to believe this doesn't actually work.  Can you add tests which test all scenarios here?