Consult the "X-Forwarded-Proto" header.

This allows this code to work behind a proxy or load balancer,
where the actual client URI scheme is https, but the webapp sees
it as http.

"X-Forwarded-Proto" is the standard header used by Amazon ELB (and probably others) to pass this information along.