Over a long time I had believed there was a function or an environment variable I could use in Php to tell whether the current connection secure or not, i.e. a page requested via http or https protocol. Unfortunately I could not find a good solution. I can not find it does not mean it does not exist.
Recently I intend to believe the perfect solution does not exist, i.e. I can not write something in Php safely to tell whether the current connection is secure unless I know the server setting.
Look at the code snippet Magento use for isSecure function:
$secure = (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!='off')) || $_SERVER['SERVER_PORT']=='443';
This code is not perfect because it assumes the server is not listening http on 443 AND server is listening HTTPS on 443.
Unfortunately I can not do more than that. I am wondering whether the situation is the same in Python or Java.