@ -1,3 +1,4 @@
# -*- encoding: utf-8 -*-
import os
import logging
@ -7,8 +8,8 @@ try:
except ImportError :
from urllib . parse import urlparse
from werkzeug . w rappers import Request
from werkzeug . exceptions import ImATeapot
from werkzeug . w sgi import DispatcherMiddleware
from werkzeug . wrappers import Response
from isso import make_app , wsgi
from isso . core import Config
@ -16,40 +17,36 @@ from isso.core import Config
logger = logging . getLogger ( " isso " )
class Dispatcher ( object ) :
class Dispatcher ( DispatcherMiddleware ) :
"""
A dispatcher to support different websites . Dispatches based on
HTTP- Host . If HTTP - Host is not provided , display an error message .
a relative URI , e . g . / foo . example and / other . bar .
"""
def __init__ ( self , * confs ) :
self . isso = { }
for conf in map ( Config . load , confs ) :
for i, conf in enumerate ( map ( Config . load , confs ) ) :
app = make_app ( conf )
if not conf . get ( " general " , " name " ) :
logger . warn ( " unable to dispatch %r , no ' name ' set " , confs [ i ] )
continue
for origin in conf . getiter ( " general " , " host " ) :
self . isso [ origin . rstrip ( " / " ) ] = app
self . isso [ " / " + conf . get ( " general " , " name " ) ] = make_app ( conf )
def __call__ ( self , environ , start_response ) :
super ( Dispatcher , self ) . __init__ ( self . default , mounts = self . isso )
if Request ( environ ) . url . endswith ( ( " .js " , " .css " ) ) :
return self . isso . values ( ) [ 0 ] ( environ , start_response )
def __call__ ( self , environ , start_response ) :
if " HTTP_X_ORIGIN " in environ and " HTTP_ORIGIN " not in environ :
environ [ " HTTP_ORIGIN " ] = environ [ " HTTP_X_ORIGIN " ]
# clear X-Script-Name as the PATH_INFO is already adjusted
environ . pop ( ' HTTP_X_SCRIPT_NAME ' , None )
origin = environ . get ( " HTTP_ORIGIN " , wsgi . host ( environ ) )
return super ( Dispatcher , self ) . __call__ ( environ , start_response )
try :
# logger.info("dispatch %s", origin)
return self . isso [ origin ] ( environ , start_response )
except KeyError :
# logger.info("unable to dispatch %s", origin)
resp = ImATeapot ( " unable to dispatch %s " % origin )
return resp ( environ , start_response )
def default ( self , environ , start_response ) :
resp = Response ( " \n " . join ( self . isso . keys ( ) ) , 404 , content_type = " text/plain " )
return resp ( environ , start_response )
if " ISSO_SETTINGS " not in os . environ :
@ -61,4 +58,4 @@ else:
logger . fatal ( " %s : no such file " , path )
break
else :
application = Dispatcher( * confs )
application = wsgi. SubURI ( Dispatcher( * confs ) )