etwist/server.py
branchtls-sprint
changeset 1715 cba9f175da2d
parent 1543 dca9817bb337
child 1936 c5af2fbda5b6
--- a/etwist/server.py	Thu May 07 16:33:22 2009 +0200
+++ b/etwist/server.py	Thu May 07 16:42:34 2009 +0200
@@ -10,6 +10,7 @@
 import select
 from time import mktime
 from datetime import date, timedelta
+from urlparse import urlsplit, urlunsplit
 
 from twisted.application import service, strports
 from twisted.internet import reactor, task, threads
@@ -43,6 +44,14 @@
     # ensure no tasks will be further added
     repo._looping_tasks = ()
 
+def host_prefixed_baseurl(baseurl, host):
+    scheme, netloc, url, query, fragment = urlsplit(baseurl)
+    netloc_domain = '.' + '.'.join(netloc.split('.')[-2:])
+    if host.endswith(netloc_domain):
+        netloc = host
+    baseurl = urlunsplit((scheme, netloc, url, query, fragment))
+    return baseurl
+
 
 class LongTimeExpiringFile(static.File):
     """overrides static.File and sets a far futre ``Expires`` date
@@ -167,6 +176,9 @@
         else:
             https = False
             baseurl = self.base_url
+        if self.config['use-request-subdomain']:
+            baseurl = host_prefixed_baseurl(baseurl, host)
+            self.warning('used baseurl is %s for this request', baseurl)
         req = CubicWebTwistedRequestAdapter(request, self.appli.vreg, https, baseurl)
         if req.authmode == 'http':
             # activate realm-based auth