Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning when HTTP used instead of HTTPS

I have a pure CherryPy server which has been running for a few years already. I decided recently to add SSL support. In this case it was enough to provide the certificate and key files and to assign correct values to the variables cherrypy.server.ssl_certificate and cherrypy.server.ssl_private_key.

I would like to give a warning about this change whenever somebody tries to access a page using "http://..." instead of "https://...". Is there a simple way of achieving this without many changes in my system? Another option would be to redirect the HTTP access to HTTPS—can that be done easily?

like image 825
Tsf Avatar asked May 06 '26 19:05

Tsf


1 Answers

I would create a custom handler to achieve what you're after. This automatically redirects to HTTPS.

class Functions():
    def check_ssl(self=None):
        # check if url is in https and redirect if http
        if cherrypy.request.scheme == "http":
            cherrypy.HTTPRedirect(Referer.replace("http:", "https:"))

    cherrypy.tools.Functions = cherrypy.Tool('before_handler', check_ssl)
like image 100
Andrew Kloos Avatar answered May 09 '26 09:05

Andrew Kloos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!