|
a/Allura/allura/config/middleware.py |
|
b/Allura/allura/config/middleware.py |
|
... |
|
... |
21 |
from ming.orm.middleware import MingMiddleware
|
21 |
from ming.orm.middleware import MingMiddleware
|
22 |
|
22 |
|
23 |
from allura.config.app_cfg import base_config
|
23 |
from allura.config.app_cfg import base_config
|
24 |
from allura.config.environment import load_environment
|
24 |
from allura.config.environment import load_environment
|
25 |
from allura.config.app_cfg import ForgeConfig
|
25 |
from allura.config.app_cfg import ForgeConfig
|
26 |
from allura.lib.custom_middleware import StatsMiddleware
|
26 |
from allura.lib.custom_middleware import AlluraTimerMiddleware
|
27 |
from allura.lib.custom_middleware import SSLMiddleware
|
27 |
from allura.lib.custom_middleware import SSLMiddleware
|
28 |
from allura.lib.custom_middleware import StaticFilesMiddleware
|
28 |
from allura.lib.custom_middleware import StaticFilesMiddleware
|
29 |
from allura.lib.custom_middleware import CSRFMiddleware
|
29 |
from allura.lib.custom_middleware import CSRFMiddleware
|
30 |
from allura.lib.custom_middleware import LoginRedirectMiddleware
|
30 |
from allura.lib.custom_middleware import LoginRedirectMiddleware
|
31 |
from allura.lib import patches
|
31 |
from allura.lib import patches
|
32 |
from allura.lib import helpers as h
|
32 |
from allura.lib import helpers as h
|
33 |
|
33 |
|
34 |
__all__ = ['make_app']
|
34 |
__all__ = ['make_app']
|
35 |
|
35 |
|
36 |
# Use base_config to setup the necessary PasteDeploy application factory.
|
36 |
# Use base_config to setup the necessary PasteDeploy application factory.
|
37 |
# make_base_app will wrap the TG2 app with all the middleware it needs.
|
37 |
# make_base_app will wrap the TG2 app with all the middleware it needs.
|
38 |
make_base_app = base_config.setup_tg_wsgi_app(load_environment)
|
38 |
make_base_app = base_config.setup_tg_wsgi_app(load_environment)
|
39 |
|
39 |
|
40 |
|
40 |
|
41 |
def make_app(global_conf, full_stack=True, **app_conf):
|
41 |
def make_app(global_conf, full_stack=True, **app_conf):
|
42 |
root = app_conf.get('override_root', 'root')
|
42 |
root = app_conf.get('override_root', 'root')
|
|
... |
|
... |
53 |
:type global_conf: dict
|
53 |
:type global_conf: dict
|
54 |
:param full_stack: Should the whole TG2 stack be set up?
|
54 |
:param full_stack: Should the whole TG2 stack be set up?
|
55 |
:type full_stack: str or bool
|
55 |
:type full_stack: str or bool
|
56 |
:return: The allura application with all the relevant middleware
|
56 |
:return: The allura application with all the relevant middleware
|
57 |
loaded.
|
57 |
loaded.
|
58 |
|
58 |
|
59 |
This is the PasteDeploy factory for the allura application.
|
59 |
This is the PasteDeploy factory for the allura application.
|
60 |
|
60 |
|
61 |
``app_conf`` contains all the application-specific settings (those defined
|
61 |
``app_conf`` contains all the application-specific settings (those defined
|
62 |
under ``[app:main]``.
|
62 |
under ``[app:main]``.
|
63 |
|
63 |
|
64 |
|
64 |
|
65 |
"""
|
65 |
"""
|
66 |
# Run all the initialization code here
|
66 |
# Run all the initialization code here
|
67 |
mimetypes.init(
|
67 |
mimetypes.init(
|
68 |
[pkg_resources.resource_filename('allura', 'etc/mime.types')]
|
68 |
[pkg_resources.resource_filename('allura', 'etc/mime.types')]
|
69 |
+ mimetypes.knownfiles)
|
69 |
+ mimetypes.knownfiles)
|
|
... |
|
... |
71 |
# Configure MongoDB
|
71 |
# Configure MongoDB
|
72 |
ming.configure(**app_conf)
|
72 |
ming.configure(**app_conf)
|
73 |
|
73 |
|
74 |
# Configure EW variable provider
|
74 |
# Configure EW variable provider
|
75 |
ew.render.TemplateEngine.register_variable_provider(get_tg_vars)
|
75 |
ew.render.TemplateEngine.register_variable_provider(get_tg_vars)
|
76 |
|
76 |
|
77 |
# Create base app
|
77 |
# Create base app
|
78 |
base_config = ForgeConfig(root)
|
78 |
base_config = ForgeConfig(root)
|
79 |
load_environment = base_config.make_load_environment()
|
79 |
load_environment = base_config.make_load_environment()
|
80 |
|
80 |
|
81 |
# Code adapted from tg.configuration, replacing the following lines:
|
81 |
# Code adapted from tg.configuration, replacing the following lines:
|
|
... |
|
... |
84 |
|
84 |
|
85 |
# Configure the Pylons environment
|
85 |
# Configure the Pylons environment
|
86 |
load_environment(global_conf, app_conf)
|
86 |
load_environment(global_conf, app_conf)
|
87 |
|
87 |
|
88 |
if config.get('zarkov.host'):
|
88 |
if config.get('zarkov.host'):
|
89 |
try:
|
89 |
try:
|
90 |
import zmq
|
90 |
import zmq
|
91 |
except ImportError:
|
91 |
except ImportError:
|
92 |
raise ImportError, "Unable to import the zmq library. Please"\
|
92 |
raise ImportError, "Unable to import the zmq library. Please"\
|
93 |
" check that zeromq is installed or comment out"\
|
93 |
" check that zeromq is installed or comment out"\
|
94 |
" the zarkov.host setting in your ini file."
|
94 |
" the zarkov.host setting in your ini file."
|
|
... |
|
... |
111 |
else:
|
111 |
else:
|
112 |
app = StatusCodeRedirect(app, base_config.handle_status_codes + [500])
|
112 |
app = StatusCodeRedirect(app, base_config.handle_status_codes + [500])
|
113 |
# Redirect 401 to the login page
|
113 |
# Redirect 401 to the login page
|
114 |
app = LoginRedirectMiddleware(app)
|
114 |
app = LoginRedirectMiddleware(app)
|
115 |
# Add instrumentation
|
115 |
# Add instrumentation
|
116 |
if app_conf.get('stats.sample_rate', '0.25') != '0':
|
116 |
app = AlluraTimerMiddleware(app, app_conf)
|
117 |
stats_config = dict(global_conf, **app_conf)
|
|
|
118 |
app = StatsMiddleware(app, stats_config)
|
|
|
119 |
# Clear cookies when the CSRF field isn't posted
|
117 |
# Clear cookies when the CSRF field isn't posted
|
120 |
if not app_conf.get('disable_csrf_protection'):
|
118 |
if not app_conf.get('disable_csrf_protection'):
|
121 |
app = CSRFMiddleware(app, '_session_id')
|
119 |
app = CSRFMiddleware(app, '_session_id')
|
122 |
# Setup the allura SOPs
|
120 |
# Setup the allura SOPs
|
123 |
app = allura_globals_middleware(app)
|
121 |
app = allura_globals_middleware(app)
|
|
... |
|
... |
143 |
# Set up the registry for stacked object proxies (SOPs).
|
141 |
# Set up the registry for stacked object proxies (SOPs).
|
144 |
# streaming=true ensures they won't be cleaned up till
|
142 |
# streaming=true ensures they won't be cleaned up till
|
145 |
# the WSGI application's iterator is exhausted
|
143 |
# the WSGI application's iterator is exhausted
|
146 |
app = RegistryManager(app, streaming=True)
|
144 |
app = RegistryManager(app, streaming=True)
|
147 |
return app
|
145 |
return app
|
148 |
|
146 |
|
149 |
def set_scheme_middleware(app):
|
147 |
def set_scheme_middleware(app):
|
150 |
def SchemeMiddleware(environ, start_response):
|
148 |
def SchemeMiddleware(environ, start_response):
|
151 |
if asbool(environ.get('HTTP_X_SFINC_SSL', 'false')):
|
149 |
if asbool(environ.get('HTTP_X_SFINC_SSL', 'false')):
|
152 |
environ['wsgi.url_scheme'] = 'https'
|
150 |
environ['wsgi.url_scheme'] = 'https'
|
153 |
return app(environ, start_response)
|
151 |
return app(environ, start_response)
|