|
Revision 998
(checked in by fumanchu, 3 years ago)
|
Fix for #476 (BaseURLFilter doesn't use socket_port).
|
- Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 |
import cherrypy |
|---|
| 2 |
from basefilter import BaseFilter |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
class BaseUrlFilter(BaseFilter): |
|---|
| 6 |
"""Filter that changes the base URL. |
|---|
| 7 |
|
|---|
| 8 |
Useful when running a CP server behind Apache. |
|---|
| 9 |
""" |
|---|
| 10 |
|
|---|
| 11 |
def before_request_body(self): |
|---|
| 12 |
if not cherrypy.config.get('base_url_filter.on', False): |
|---|
| 13 |
return |
|---|
| 14 |
|
|---|
| 15 |
request = cherrypy.request |
|---|
| 16 |
|
|---|
| 17 |
port = str(cherrypy.config.get('server.socket_port', '80')) |
|---|
| 18 |
if port == "80": |
|---|
| 19 |
defaultUrl = 'http://localhost' |
|---|
| 20 |
else: |
|---|
| 21 |
defaultUrl = 'http://localhost:%s' % port |
|---|
| 22 |
newBaseUrl = cherrypy.config.get('base_url_filter.base_url', defaultUrl) |
|---|
| 23 |
|
|---|
| 24 |
if cherrypy.config.get('base_url_filter.use_x_forwarded_host', True): |
|---|
| 25 |
newBaseUrl = request.headers.get("X-Forwarded-Host", newBaseUrl) |
|---|
| 26 |
|
|---|
| 27 |
if newBaseUrl.find("://") == -1: |
|---|
| 28 |
|
|---|
| 29 |
newBaseUrl = request.base[:request.base.find("://") + 3] + newBaseUrl |
|---|
| 30 |
|
|---|
| 31 |
request.base = newBaseUrl |
|---|