import os
import logging

logger = logging.getLogger(__name__)


def apply_ssl_bypass():
    ssl_verify = os.environ.get('CASDOOR_SSL_VERIFY', 'true').lower()
    if ssl_verify != 'false':
        return

    logger.warning("SSL verification DISABLED — sandbox only")

    import urllib3
    from requests.adapters import HTTPAdapter
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

    _original_send = HTTPAdapter.send

    def _patched_send(self, request, stream=False, timeout=None,
                      verify=True, cert=None, proxies=None):
        return _original_send(self, request, stream=stream,
                              timeout=timeout, verify=False,
                              cert=cert, proxies=proxies)

    HTTPAdapter.send = _patched_send


apply_ssl_bypass()
