File: virtualhostfilter.py

package info (click to toggle)
python-cherrypy 2.2.1-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 796 kB
  • ctags: 1,079
  • sloc: python: 7,869; makefile: 15
file content (39 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Virtual Host Filter

From http://groups.google.com/group/cherrypy-users/browse_thread/thread/f393540fe278e54d:

For various reasons I need several domains to point to different parts of a
single website structure as well as to their own "homepage"   EG

http://www.mydom1.com  ->  root
http://www.mydom2.com  ->  root/mydom2/
http://www.mydom3.com  ->  root/mydom3/
http://www.mydom4.com  ->  under construction page

but also to have  http://www.mydom1.com/mydom2/  etc to be valid pages in
their own right.
"""

import cherrypy
from basefilter import BaseFilter


class VirtualHostFilter(BaseFilter):
    """Filter that changes the ObjectPath based on the Host.
    
    Useful when running multiple sites within one CP server.
    """
    
    def before_request_body(self):
        if not cherrypy.config.get('virtual_host_filter.on', False):
            return
        
        domain = cherrypy.request.headers.get('Host', '')
        if cherrypy.config.get("virtual_host_filter.use_x_forwarded_host", True):
            domain = cherrypy.request.headers.get("X-Forwarded-Host", domain)
        
        prefix = cherrypy.config.get("virtual_host_filter." + domain, "")
        if prefix:
            cherrypy.request.object_path = prefix + "/" + cherrypy.request.object_path