diff --git a/headphones/webfilters.py b/headphones/webfilters.py new file mode 100644 index 00000000..50d369c3 --- /dev/null +++ b/headphones/webfilters.py @@ -0,0 +1,50 @@ +# This file is part of Headphones. +# +# Headphones is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Headphones is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Headphones. If not, see . + +from cherrypy.filters.basefilter import BaseFilter +import cherrypy + + + +class HTTPSFilter(BaseFilter): + + """This filter is based on a guide at http://www.turbogears.org/1.0/docs/Install/RedirectHttpsRequests.html + + It's purpose is to allow Headphones to issue redirects with the + correct protocol (HTTP/HTTPS) when being served behind a + HTTPS-handling proxy. + """ + + def before_request_body(self): + forwarded_ssl_triggers = { + 'X-Forwarded-Protocol': 'SSL', + 'X-Forwarded-Ssl': 'On', + } + request = cherrypy.request + headers = request.headers + forwarded_ssl = reduce( + lambda x, y: x | headers.get(y).lower() == forwarded_ssl_triggers[y].lower(), + forwarded_ssl_triggers.keys(), + False + ) + if forwarded_ssl: + # base = config.get('https_filter.secure_base_url') + # if base is None: + # if config.get('base_url_filter.use_x_forwarded_host', False): + # base = headers.get('X-Forwarded-Host', 'localhost') + # else: + # base = 'localhost' + # request.base = 'https://' + base + request.headers['X-ForwardedSslDetected'] = Yes diff --git a/headphones/webserve.py b/headphones/webserve.py index 2b2e89fd..ec2045e4 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -25,7 +25,7 @@ import threading import headphones -from headphones import logger, searcher, db, importer, mb, lastfm, librarysync +from headphones import logger, searcher, db, importer, mb, lastfm, librarysync, webfilters from headphones.helpers import checked, radio import lib.simplejson as simplejson @@ -46,7 +46,9 @@ def serve_template(templatename, **kwargs): return exceptions.html_error_template().render() class WebInterface(object): - + + _cp_filters = [webfilters.HTTPSFilter()] + def index(self): raise cherrypy.HTTPRedirect("home") index.exposed=True