Show More
@@ -1,465 +1,468 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2016 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | """ |
|
21 | """ | |
22 | Pylons middleware initialization |
|
22 | Pylons middleware initialization | |
23 | """ |
|
23 | """ | |
24 | import logging |
|
24 | import logging | |
25 | from collections import OrderedDict |
|
25 | from collections import OrderedDict | |
26 |
|
26 | |||
27 | from paste.registry import RegistryManager |
|
27 | from paste.registry import RegistryManager | |
28 | from paste.gzipper import make_gzip_middleware |
|
28 | from paste.gzipper import make_gzip_middleware | |
29 | from pylons.wsgiapp import PylonsApp |
|
29 | from pylons.wsgiapp import PylonsApp | |
30 | from pyramid.authorization import ACLAuthorizationPolicy |
|
30 | from pyramid.authorization import ACLAuthorizationPolicy | |
31 | from pyramid.config import Configurator |
|
31 | from pyramid.config import Configurator | |
32 | from pyramid.settings import asbool, aslist |
|
32 | from pyramid.settings import asbool, aslist | |
33 | from pyramid.wsgi import wsgiapp |
|
33 | from pyramid.wsgi import wsgiapp | |
34 | from pyramid.httpexceptions import HTTPError, HTTPInternalServerError |
|
34 | from pyramid.httpexceptions import HTTPError, HTTPInternalServerError | |
35 | from pylons.controllers.util import redirect |
|
35 | from pylons.controllers.util import redirect | |
36 | from pyramid.events import ApplicationCreated |
|
36 | from pyramid.events import ApplicationCreated | |
37 | import pyramid.httpexceptions as httpexceptions |
|
37 | import pyramid.httpexceptions as httpexceptions | |
38 | from pyramid.renderers import render_to_response |
|
38 | from pyramid.renderers import render_to_response | |
39 | from routes.middleware import RoutesMiddleware |
|
39 | from routes.middleware import RoutesMiddleware | |
40 | import routes.util |
|
40 | import routes.util | |
41 |
|
41 | |||
42 | import rhodecode |
|
42 | import rhodecode | |
43 | import rhodecode.integrations # do not remove this as it registers celery tasks |
|
43 | import rhodecode.integrations # do not remove this as it registers celery tasks | |
44 | from rhodecode.config import patches |
|
44 | from rhodecode.config import patches | |
45 | from rhodecode.config.routing import STATIC_FILE_PREFIX |
|
45 | from rhodecode.config.routing import STATIC_FILE_PREFIX | |
46 | from rhodecode.config.environment import ( |
|
46 | from rhodecode.config.environment import ( | |
47 | load_environment, load_pyramid_environment) |
|
47 | load_environment, load_pyramid_environment) | |
48 | from rhodecode.lib.middleware import csrf |
|
48 | from rhodecode.lib.middleware import csrf | |
49 | from rhodecode.lib.middleware.appenlight import wrap_in_appenlight_if_enabled |
|
49 | from rhodecode.lib.middleware.appenlight import wrap_in_appenlight_if_enabled | |
50 | from rhodecode.lib.middleware.disable_vcs import DisableVCSPagesWrapper |
|
50 | from rhodecode.lib.middleware.disable_vcs import DisableVCSPagesWrapper | |
51 | from rhodecode.lib.middleware.https_fixup import HttpsFixup |
|
51 | from rhodecode.lib.middleware.https_fixup import HttpsFixup | |
52 | from rhodecode.lib.middleware.vcs import VCSMiddleware |
|
52 | from rhodecode.lib.middleware.vcs import VCSMiddleware | |
53 | from rhodecode.lib.plugins.utils import register_rhodecode_plugin |
|
53 | from rhodecode.lib.plugins.utils import register_rhodecode_plugin | |
54 | from rhodecode.lib.utils2 import aslist as rhodecode_aslist |
|
54 | from rhodecode.lib.utils2 import aslist as rhodecode_aslist | |
55 | from rhodecode.subscribers import scan_repositories_if_enabled |
|
55 | from rhodecode.subscribers import scan_repositories_if_enabled | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | log = logging.getLogger(__name__) |
|
58 | log = logging.getLogger(__name__) | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | # this is used to avoid avoid the route lookup overhead in routesmiddleware |
|
61 | # this is used to avoid avoid the route lookup overhead in routesmiddleware | |
62 | # for certain routes which won't go to pylons to - eg. static files, debugger |
|
62 | # for certain routes which won't go to pylons to - eg. static files, debugger | |
63 | # it is only needed for the pylons migration and can be removed once complete |
|
63 | # it is only needed for the pylons migration and can be removed once complete | |
64 | class SkippableRoutesMiddleware(RoutesMiddleware): |
|
64 | class SkippableRoutesMiddleware(RoutesMiddleware): | |
65 | """ Routes middleware that allows you to skip prefixes """ |
|
65 | """ Routes middleware that allows you to skip prefixes """ | |
66 |
|
66 | |||
67 | def __init__(self, *args, **kw): |
|
67 | def __init__(self, *args, **kw): | |
68 | self.skip_prefixes = kw.pop('skip_prefixes', []) |
|
68 | self.skip_prefixes = kw.pop('skip_prefixes', []) | |
69 | super(SkippableRoutesMiddleware, self).__init__(*args, **kw) |
|
69 | super(SkippableRoutesMiddleware, self).__init__(*args, **kw) | |
70 |
|
70 | |||
71 | def __call__(self, environ, start_response): |
|
71 | def __call__(self, environ, start_response): | |
72 | for prefix in self.skip_prefixes: |
|
72 | for prefix in self.skip_prefixes: | |
73 | if environ['PATH_INFO'].startswith(prefix): |
|
73 | if environ['PATH_INFO'].startswith(prefix): | |
74 | # added to avoid the case when a missing /_static route falls |
|
74 | # added to avoid the case when a missing /_static route falls | |
75 | # through to pylons and causes an exception as pylons is |
|
75 | # through to pylons and causes an exception as pylons is | |
76 | # expecting wsgiorg.routingargs to be set in the environ |
|
76 | # expecting wsgiorg.routingargs to be set in the environ | |
77 | # by RoutesMiddleware. |
|
77 | # by RoutesMiddleware. | |
78 | if 'wsgiorg.routing_args' not in environ: |
|
78 | if 'wsgiorg.routing_args' not in environ: | |
79 | environ['wsgiorg.routing_args'] = (None, {}) |
|
79 | environ['wsgiorg.routing_args'] = (None, {}) | |
80 | return self.app(environ, start_response) |
|
80 | return self.app(environ, start_response) | |
81 |
|
81 | |||
82 | return super(SkippableRoutesMiddleware, self).__call__( |
|
82 | return super(SkippableRoutesMiddleware, self).__call__( | |
83 | environ, start_response) |
|
83 | environ, start_response) | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | def make_app(global_conf, static_files=True, **app_conf): |
|
86 | def make_app(global_conf, static_files=True, **app_conf): | |
87 | """Create a Pylons WSGI application and return it |
|
87 | """Create a Pylons WSGI application and return it | |
88 |
|
88 | |||
89 | ``global_conf`` |
|
89 | ``global_conf`` | |
90 | The inherited configuration for this application. Normally from |
|
90 | The inherited configuration for this application. Normally from | |
91 | the [DEFAULT] section of the Paste ini file. |
|
91 | the [DEFAULT] section of the Paste ini file. | |
92 |
|
92 | |||
93 | ``app_conf`` |
|
93 | ``app_conf`` | |
94 | The application's local configuration. Normally specified in |
|
94 | The application's local configuration. Normally specified in | |
95 | the [app:<name>] section of the Paste ini file (where <name> |
|
95 | the [app:<name>] section of the Paste ini file (where <name> | |
96 | defaults to main). |
|
96 | defaults to main). | |
97 |
|
97 | |||
98 | """ |
|
98 | """ | |
99 | # Apply compatibility patches |
|
99 | # Apply compatibility patches | |
100 | patches.kombu_1_5_1_python_2_7_11() |
|
100 | patches.kombu_1_5_1_python_2_7_11() | |
101 | patches.inspect_getargspec() |
|
101 | patches.inspect_getargspec() | |
102 |
|
102 | |||
103 | # Configure the Pylons environment |
|
103 | # Configure the Pylons environment | |
104 | config = load_environment(global_conf, app_conf) |
|
104 | config = load_environment(global_conf, app_conf) | |
105 |
|
105 | |||
106 | # The Pylons WSGI app |
|
106 | # The Pylons WSGI app | |
107 | app = PylonsApp(config=config) |
|
107 | app = PylonsApp(config=config) | |
108 | if rhodecode.is_test: |
|
108 | if rhodecode.is_test: | |
109 | app = csrf.CSRFDetector(app) |
|
109 | app = csrf.CSRFDetector(app) | |
110 |
|
110 | |||
111 | expected_origin = config.get('expected_origin') |
|
111 | expected_origin = config.get('expected_origin') | |
112 | if expected_origin: |
|
112 | if expected_origin: | |
113 | # The API can be accessed from other Origins. |
|
113 | # The API can be accessed from other Origins. | |
114 | app = csrf.OriginChecker(app, expected_origin, |
|
114 | app = csrf.OriginChecker(app, expected_origin, | |
115 | skip_urls=[routes.util.url_for('api')]) |
|
115 | skip_urls=[routes.util.url_for('api')]) | |
116 |
|
116 | |||
117 | # Establish the Registry for this application |
|
117 | # Establish the Registry for this application | |
118 | app = RegistryManager(app) |
|
118 | app = RegistryManager(app) | |
119 |
|
119 | |||
120 | app.config = config |
|
120 | app.config = config | |
121 |
|
121 | |||
122 | return app |
|
122 | return app | |
123 |
|
123 | |||
124 |
|
124 | |||
125 | def make_pyramid_app(global_config, **settings): |
|
125 | def make_pyramid_app(global_config, **settings): | |
126 | """ |
|
126 | """ | |
127 | Constructs the WSGI application based on Pyramid and wraps the Pylons based |
|
127 | Constructs the WSGI application based on Pyramid and wraps the Pylons based | |
128 | application. |
|
128 | application. | |
129 |
|
129 | |||
130 | Specials: |
|
130 | Specials: | |
131 |
|
131 | |||
132 | * We migrate from Pylons to Pyramid. While doing this, we keep both |
|
132 | * We migrate from Pylons to Pyramid. While doing this, we keep both | |
133 | frameworks functional. This involves moving some WSGI middlewares around |
|
133 | frameworks functional. This involves moving some WSGI middlewares around | |
134 | and providing access to some data internals, so that the old code is |
|
134 | and providing access to some data internals, so that the old code is | |
135 | still functional. |
|
135 | still functional. | |
136 |
|
136 | |||
137 | * The application can also be integrated like a plugin via the call to |
|
137 | * The application can also be integrated like a plugin via the call to | |
138 | `includeme`. This is accompanied with the other utility functions which |
|
138 | `includeme`. This is accompanied with the other utility functions which | |
139 | are called. Changing this should be done with great care to not break |
|
139 | are called. Changing this should be done with great care to not break | |
140 | cases when these fragments are assembled from another place. |
|
140 | cases when these fragments are assembled from another place. | |
141 |
|
141 | |||
142 | """ |
|
142 | """ | |
143 | # The edition string should be available in pylons too, so we add it here |
|
143 | # The edition string should be available in pylons too, so we add it here | |
144 | # before copying the settings. |
|
144 | # before copying the settings. | |
145 | settings.setdefault('rhodecode.edition', 'Community Edition') |
|
145 | settings.setdefault('rhodecode.edition', 'Community Edition') | |
146 |
|
146 | |||
147 | # As long as our Pylons application does expect "unprepared" settings, make |
|
147 | # As long as our Pylons application does expect "unprepared" settings, make | |
148 | # sure that we keep an unmodified copy. This avoids unintentional change of |
|
148 | # sure that we keep an unmodified copy. This avoids unintentional change of | |
149 | # behavior in the old application. |
|
149 | # behavior in the old application. | |
150 | settings_pylons = settings.copy() |
|
150 | settings_pylons = settings.copy() | |
151 |
|
151 | |||
152 | sanitize_settings_and_apply_defaults(settings) |
|
152 | sanitize_settings_and_apply_defaults(settings) | |
153 | config = Configurator(settings=settings) |
|
153 | config = Configurator(settings=settings) | |
154 | add_pylons_compat_data(config.registry, global_config, settings_pylons) |
|
154 | add_pylons_compat_data(config.registry, global_config, settings_pylons) | |
155 |
|
155 | |||
156 | load_pyramid_environment(global_config, settings) |
|
156 | load_pyramid_environment(global_config, settings) | |
157 |
|
157 | |||
158 | includeme_first(config) |
|
158 | includeme_first(config) | |
159 | includeme(config) |
|
159 | includeme(config) | |
160 | pyramid_app = config.make_wsgi_app() |
|
160 | pyramid_app = config.make_wsgi_app() | |
161 | pyramid_app = wrap_app_in_wsgi_middlewares(pyramid_app, config) |
|
161 | pyramid_app = wrap_app_in_wsgi_middlewares(pyramid_app, config) | |
162 | return pyramid_app |
|
162 | return pyramid_app | |
163 |
|
163 | |||
164 |
|
164 | |||
165 | def make_not_found_view(config): |
|
165 | def make_not_found_view(config): | |
166 | """ |
|
166 | """ | |
167 | This creates the view which should be registered as not-found-view to |
|
167 | This creates the view which should be registered as not-found-view to | |
168 | pyramid. Basically it contains of the old pylons app, converted to a view. |
|
168 | pyramid. Basically it contains of the old pylons app, converted to a view. | |
169 | Additionally it is wrapped by some other middlewares. |
|
169 | Additionally it is wrapped by some other middlewares. | |
170 | """ |
|
170 | """ | |
171 | settings = config.registry.settings |
|
171 | settings = config.registry.settings | |
172 | vcs_server_enabled = settings['vcs.server.enable'] |
|
172 | vcs_server_enabled = settings['vcs.server.enable'] | |
173 |
|
173 | |||
174 | # Make pylons app from unprepared settings. |
|
174 | # Make pylons app from unprepared settings. | |
175 | pylons_app = make_app( |
|
175 | pylons_app = make_app( | |
176 | config.registry._pylons_compat_global_config, |
|
176 | config.registry._pylons_compat_global_config, | |
177 | **config.registry._pylons_compat_settings) |
|
177 | **config.registry._pylons_compat_settings) | |
178 | config.registry._pylons_compat_config = pylons_app.config |
|
178 | config.registry._pylons_compat_config = pylons_app.config | |
179 |
|
179 | |||
180 | # Appenlight monitoring. |
|
180 | # Appenlight monitoring. | |
181 | pylons_app, appenlight_client = wrap_in_appenlight_if_enabled( |
|
181 | pylons_app, appenlight_client = wrap_in_appenlight_if_enabled( | |
182 | pylons_app, settings) |
|
182 | pylons_app, settings) | |
183 |
|
183 | |||
184 | # The VCSMiddleware shall operate like a fallback if pyramid doesn't find |
|
184 | # The VCSMiddleware shall operate like a fallback if pyramid doesn't find | |
185 | # a view to handle the request. Therefore we wrap it around the pylons app. |
|
185 | # a view to handle the request. Therefore we wrap it around the pylons app. | |
186 | if vcs_server_enabled: |
|
186 | if vcs_server_enabled: | |
187 | pylons_app = VCSMiddleware( |
|
187 | pylons_app = VCSMiddleware( | |
188 | pylons_app, settings, appenlight_client, registry=config.registry) |
|
188 | pylons_app, settings, appenlight_client, registry=config.registry) | |
189 |
|
189 | |||
190 | pylons_app_as_view = wsgiapp(pylons_app) |
|
190 | pylons_app_as_view = wsgiapp(pylons_app) | |
191 |
|
191 | |||
192 | # Protect from VCS Server error related pages when server is not available |
|
192 | # Protect from VCS Server error related pages when server is not available | |
193 | if not vcs_server_enabled: |
|
193 | if not vcs_server_enabled: | |
194 | pylons_app_as_view = DisableVCSPagesWrapper(pylons_app_as_view) |
|
194 | pylons_app_as_view = DisableVCSPagesWrapper(pylons_app_as_view) | |
195 |
|
195 | |||
196 | def pylons_app_with_error_handler(context, request): |
|
196 | def pylons_app_with_error_handler(context, request): | |
197 | """ |
|
197 | """ | |
198 | Handle exceptions from rc pylons app: |
|
198 | Handle exceptions from rc pylons app: | |
199 |
|
199 | |||
200 | - old webob type exceptions get converted to pyramid exceptions |
|
200 | - old webob type exceptions get converted to pyramid exceptions | |
201 | - pyramid exceptions are passed to the error handler view |
|
201 | - pyramid exceptions are passed to the error handler view | |
202 | """ |
|
202 | """ | |
203 | def is_vcs_response(request): |
|
203 | def is_vcs_response(request): | |
204 | return True == request.environ.get( |
|
204 | return True == request.environ.get( | |
205 | 'rhodecode.vcs.skip_error_handling') |
|
205 | 'rhodecode.vcs.skip_error_handling') | |
206 |
|
206 | |||
207 | def is_webob_error(response): |
|
207 | def is_webob_error(response): | |
208 | # webob type error responses |
|
208 | # webob type error responses | |
209 | return (400 <= response.status_int <= 599) |
|
209 | return (400 <= response.status_int <= 599) | |
210 |
|
210 | |||
211 | try: |
|
211 | try: | |
212 | response = pylons_app_as_view(context, request) |
|
212 | response = pylons_app_as_view(context, request) | |
213 | if is_webob_error(response) and not is_vcs_response(request): |
|
213 | if is_webob_error(response) and not is_vcs_response(request): | |
214 | return error_handler( |
|
214 | return error_handler( | |
215 | webob_to_pyramid_http_response(response), request) |
|
215 | webob_to_pyramid_http_response(response), request) | |
216 | except HTTPError as e: # pyramid type exceptions |
|
216 | except HTTPError as e: # pyramid type exceptions | |
217 | return error_handler(e, request) |
|
217 | return error_handler(e, request) | |
218 | except Exception: |
|
218 | except Exception: | |
219 | if settings.get('debugtoolbar.enabled', False): |
|
219 | if settings.get('debugtoolbar.enabled', False): | |
220 | raise |
|
220 | raise | |
221 | return error_handler(HTTPInternalServerError(), request) |
|
221 | return error_handler(HTTPInternalServerError(), request) | |
222 | return response |
|
222 | return response | |
223 |
|
223 | |||
224 | return pylons_app_with_error_handler |
|
224 | return pylons_app_with_error_handler | |
225 |
|
225 | |||
226 |
|
226 | |||
227 | def add_pylons_compat_data(registry, global_config, settings): |
|
227 | def add_pylons_compat_data(registry, global_config, settings): | |
228 | """ |
|
228 | """ | |
229 | Attach data to the registry to support the Pylons integration. |
|
229 | Attach data to the registry to support the Pylons integration. | |
230 | """ |
|
230 | """ | |
231 | registry._pylons_compat_global_config = global_config |
|
231 | registry._pylons_compat_global_config = global_config | |
232 | registry._pylons_compat_settings = settings |
|
232 | registry._pylons_compat_settings = settings | |
233 |
|
233 | |||
234 |
|
234 | |||
235 | def webob_to_pyramid_http_response(webob_response): |
|
235 | def webob_to_pyramid_http_response(webob_response): | |
236 | ResponseClass = httpexceptions.status_map[webob_response.status_int] |
|
236 | ResponseClass = httpexceptions.status_map[webob_response.status_int] | |
237 | pyramid_response = ResponseClass(webob_response.status) |
|
237 | pyramid_response = ResponseClass(webob_response.status) | |
238 | pyramid_response.status = webob_response.status |
|
238 | pyramid_response.status = webob_response.status | |
239 | pyramid_response.headers.update(webob_response.headers) |
|
239 | pyramid_response.headers.update(webob_response.headers) | |
240 | if pyramid_response.headers['content-type'] == 'text/html': |
|
240 | if pyramid_response.headers['content-type'] == 'text/html': | |
241 | pyramid_response.headers['content-type'] = 'text/html; charset=UTF-8' |
|
241 | pyramid_response.headers['content-type'] = 'text/html; charset=UTF-8' | |
242 | return pyramid_response |
|
242 | return pyramid_response | |
243 |
|
243 | |||
244 |
|
244 | |||
245 | def error_handler(exception, request): |
|
245 | def error_handler(exception, request): | |
246 | # TODO: dan: replace the old pylons error controller with this |
|
246 | # TODO: dan: replace the old pylons error controller with this | |
247 | from rhodecode.model.settings import SettingsModel |
|
247 | from rhodecode.model.settings import SettingsModel | |
248 | from rhodecode.lib.utils2 import AttributeDict |
|
248 | from rhodecode.lib.utils2 import AttributeDict | |
249 |
|
249 | |||
250 | try: |
|
250 | try: | |
251 | rc_config = SettingsModel().get_all_settings() |
|
251 | rc_config = SettingsModel().get_all_settings() | |
252 | except Exception: |
|
252 | except Exception: | |
253 | log.exception('failed to fetch settings') |
|
253 | log.exception('failed to fetch settings') | |
254 | rc_config = {} |
|
254 | rc_config = {} | |
255 |
|
255 | |||
256 | base_response = HTTPInternalServerError() |
|
256 | base_response = HTTPInternalServerError() | |
257 | # prefer original exception for the response since it may have headers set |
|
257 | # prefer original exception for the response since it may have headers set | |
258 | if isinstance(exception, HTTPError): |
|
258 | if isinstance(exception, HTTPError): | |
259 | base_response = exception |
|
259 | base_response = exception | |
260 |
|
260 | |||
261 | c = AttributeDict() |
|
261 | c = AttributeDict() | |
262 | c.error_message = base_response.status |
|
262 | c.error_message = base_response.status | |
263 | c.error_explanation = base_response.explanation or str(base_response) |
|
263 | c.error_explanation = base_response.explanation or str(base_response) | |
264 | c.visual = AttributeDict() |
|
264 | c.visual = AttributeDict() | |
265 |
|
265 | |||
266 | c.visual.rhodecode_support_url = ( |
|
266 | c.visual.rhodecode_support_url = ( | |
267 | request.registry.settings.get('rhodecode_support_url') or |
|
267 | request.registry.settings.get('rhodecode_support_url') or | |
268 | request.route_url('rhodecode_support') |
|
268 | request.route_url('rhodecode_support') | |
269 | ) |
|
269 | ) | |
270 | c.redirect_time = 0 |
|
270 | c.redirect_time = 0 | |
271 | c.rhodecode_name = rc_config.get('rhodecode_title', '') |
|
271 | c.rhodecode_name = rc_config.get('rhodecode_title', '') | |
272 | if not c.rhodecode_name: |
|
272 | if not c.rhodecode_name: | |
273 | c.rhodecode_name = 'Rhodecode' |
|
273 | c.rhodecode_name = 'Rhodecode' | |
274 |
|
274 | |||
275 | response = render_to_response( |
|
275 | response = render_to_response( | |
276 | '/errors/error_document.html', {'c': c}, request=request, |
|
276 | '/errors/error_document.html', {'c': c}, request=request, | |
277 | response=base_response) |
|
277 | response=base_response) | |
278 |
|
278 | |||
279 | return response |
|
279 | return response | |
280 |
|
280 | |||
281 |
|
281 | |||
282 | def includeme(config): |
|
282 | def includeme(config): | |
283 | settings = config.registry.settings |
|
283 | settings = config.registry.settings | |
284 |
|
284 | |||
285 | # plugin information |
|
285 | # plugin information | |
286 | config.registry.rhodecode_plugins = OrderedDict() |
|
286 | config.registry.rhodecode_plugins = OrderedDict() | |
287 |
|
287 | |||
288 | config.add_directive( |
|
288 | config.add_directive( | |
289 | 'register_rhodecode_plugin', register_rhodecode_plugin) |
|
289 | 'register_rhodecode_plugin', register_rhodecode_plugin) | |
290 |
|
290 | |||
291 | if asbool(settings.get('appenlight', 'false')): |
|
291 | if asbool(settings.get('appenlight', 'false')): | |
292 | config.include('appenlight_client.ext.pyramid_tween') |
|
292 | config.include('appenlight_client.ext.pyramid_tween') | |
293 |
|
293 | |||
294 | # Includes which are required. The application would fail without them. |
|
294 | # Includes which are required. The application would fail without them. | |
295 | config.include('pyramid_mako') |
|
295 | config.include('pyramid_mako') | |
296 | config.include('pyramid_beaker') |
|
296 | config.include('pyramid_beaker') | |
297 | config.include('rhodecode.channelstream') |
|
297 | config.include('rhodecode.channelstream') | |
298 | config.include('rhodecode.admin') |
|
298 | config.include('rhodecode.admin') | |
299 | config.include('rhodecode.authentication') |
|
299 | config.include('rhodecode.authentication') | |
300 | config.include('rhodecode.integrations') |
|
300 | config.include('rhodecode.integrations') | |
301 | config.include('rhodecode.login') |
|
301 | config.include('rhodecode.login') | |
302 | config.include('rhodecode.tweens') |
|
302 | config.include('rhodecode.tweens') | |
303 | config.include('rhodecode.api') |
|
303 | config.include('rhodecode.api') | |
304 | config.include('rhodecode.svn_support') |
|
304 | config.include('rhodecode.svn_support') | |
305 | config.add_route( |
|
305 | config.add_route( | |
306 | 'rhodecode_support', 'https://rhodecode.com/help/', static=True) |
|
306 | 'rhodecode_support', 'https://rhodecode.com/help/', static=True) | |
307 |
|
307 | |||
308 | # Add subscribers. |
|
308 | # Add subscribers. | |
309 | config.add_subscriber(scan_repositories_if_enabled, ApplicationCreated) |
|
309 | config.add_subscriber(scan_repositories_if_enabled, ApplicationCreated) | |
310 |
|
310 | |||
311 | # Set the authorization policy. |
|
311 | # Set the authorization policy. | |
312 | authz_policy = ACLAuthorizationPolicy() |
|
312 | authz_policy = ACLAuthorizationPolicy() | |
313 | config.set_authorization_policy(authz_policy) |
|
313 | config.set_authorization_policy(authz_policy) | |
314 |
|
314 | |||
315 | # Set the default renderer for HTML templates to mako. |
|
315 | # Set the default renderer for HTML templates to mako. | |
316 | config.add_mako_renderer('.html') |
|
316 | config.add_mako_renderer('.html') | |
317 |
|
317 | |||
318 | # include RhodeCode plugins |
|
318 | # include RhodeCode plugins | |
319 | includes = aslist(settings.get('rhodecode.includes', [])) |
|
319 | includes = aslist(settings.get('rhodecode.includes', [])) | |
320 | for inc in includes: |
|
320 | for inc in includes: | |
321 | config.include(inc) |
|
321 | config.include(inc) | |
322 |
|
322 | |||
323 | # This is the glue which allows us to migrate in chunks. By registering the |
|
323 | # This is the glue which allows us to migrate in chunks. By registering the | |
324 | # pylons based application as the "Not Found" view in Pyramid, we will |
|
324 | # pylons based application as the "Not Found" view in Pyramid, we will | |
325 | # fallback to the old application each time the new one does not yet know |
|
325 | # fallback to the old application each time the new one does not yet know | |
326 | # how to handle a request. |
|
326 | # how to handle a request. | |
327 | config.add_notfound_view(make_not_found_view(config)) |
|
327 | config.add_notfound_view(make_not_found_view(config)) | |
328 |
|
328 | |||
329 | if not settings.get('debugtoolbar.enabled', False): |
|
329 | if not settings.get('debugtoolbar.enabled', False): | |
330 | # if no toolbar, then any exception gets caught and rendered |
|
330 | # if no toolbar, then any exception gets caught and rendered | |
331 | config.add_view(error_handler, context=Exception) |
|
331 | config.add_view(error_handler, context=Exception) | |
332 |
|
332 | |||
333 | config.add_view(error_handler, context=HTTPError) |
|
333 | config.add_view(error_handler, context=HTTPError) | |
334 |
|
334 | |||
335 |
|
335 | |||
336 | def includeme_first(config): |
|
336 | def includeme_first(config): | |
337 | # redirect automatic browser favicon.ico requests to correct place |
|
337 | # redirect automatic browser favicon.ico requests to correct place | |
338 | def favicon_redirect(context, request): |
|
338 | def favicon_redirect(context, request): | |
339 | return redirect( |
|
339 | return redirect( | |
340 | request.static_path('rhodecode:public/images/favicon.ico')) |
|
340 | request.static_path('rhodecode:public/images/favicon.ico')) | |
341 |
|
341 | |||
342 | config.add_view(favicon_redirect, route_name='favicon') |
|
342 | config.add_view(favicon_redirect, route_name='favicon') | |
343 | config.add_route('favicon', '/favicon.ico') |
|
343 | config.add_route('favicon', '/favicon.ico') | |
344 |
|
344 | |||
345 | config.add_static_view( |
|
345 | config.add_static_view( | |
346 | '_static/deform', 'deform:static') |
|
346 | '_static/deform', 'deform:static') | |
347 | config.add_static_view( |
|
347 | config.add_static_view( | |
348 | '_static/rhodecode', path='rhodecode:public', cache_max_age=3600 * 24) |
|
348 | '_static/rhodecode', path='rhodecode:public', cache_max_age=3600 * 24) | |
349 |
|
349 | |||
350 |
|
350 | |||
351 | def wrap_app_in_wsgi_middlewares(pyramid_app, config): |
|
351 | def wrap_app_in_wsgi_middlewares(pyramid_app, config): | |
352 | """ |
|
352 | """ | |
353 | Apply outer WSGI middlewares around the application. |
|
353 | Apply outer WSGI middlewares around the application. | |
354 |
|
354 | |||
355 | Part of this has been moved up from the Pylons layer, so that the |
|
355 | Part of this has been moved up from the Pylons layer, so that the | |
356 | data is also available if old Pylons code is hit through an already ported |
|
356 | data is also available if old Pylons code is hit through an already ported | |
357 | view. |
|
357 | view. | |
358 | """ |
|
358 | """ | |
359 | settings = config.registry.settings |
|
359 | settings = config.registry.settings | |
360 |
|
360 | |||
361 | # enable https redirects based on HTTP_X_URL_SCHEME set by proxy |
|
361 | # enable https redirects based on HTTP_X_URL_SCHEME set by proxy | |
362 | pyramid_app = HttpsFixup(pyramid_app, settings) |
|
362 | pyramid_app = HttpsFixup(pyramid_app, settings) | |
363 |
|
363 | |||
364 | # Add RoutesMiddleware to support the pylons compatibility tween during |
|
364 | # Add RoutesMiddleware to support the pylons compatibility tween during | |
365 | # migration to pyramid. |
|
365 | # migration to pyramid. | |
366 | pyramid_app = SkippableRoutesMiddleware( |
|
366 | pyramid_app = SkippableRoutesMiddleware( | |
367 | pyramid_app, config.registry._pylons_compat_config['routes.map'], |
|
367 | pyramid_app, config.registry._pylons_compat_config['routes.map'], | |
368 | skip_prefixes=(STATIC_FILE_PREFIX, '/_debug_toolbar')) |
|
368 | skip_prefixes=(STATIC_FILE_PREFIX, '/_debug_toolbar')) | |
369 |
|
369 | |||
370 | pyramid_app, _ = wrap_in_appenlight_if_enabled(pyramid_app, settings) |
|
370 | pyramid_app, _ = wrap_in_appenlight_if_enabled(pyramid_app, settings) | |
371 |
|
371 | |||
372 | if settings['gzip_responses']: |
|
372 | if settings['gzip_responses']: | |
373 | pyramid_app = make_gzip_middleware( |
|
373 | pyramid_app = make_gzip_middleware( | |
374 | pyramid_app, settings, compress_level=1) |
|
374 | pyramid_app, settings, compress_level=1) | |
375 |
|
375 | |||
376 | return pyramid_app |
|
376 | return pyramid_app | |
377 |
|
377 | |||
378 |
|
378 | |||
379 | def sanitize_settings_and_apply_defaults(settings): |
|
379 | def sanitize_settings_and_apply_defaults(settings): | |
380 | """ |
|
380 | """ | |
381 | Applies settings defaults and does all type conversion. |
|
381 | Applies settings defaults and does all type conversion. | |
382 |
|
382 | |||
383 | We would move all settings parsing and preparation into this place, so that |
|
383 | We would move all settings parsing and preparation into this place, so that | |
384 | we have only one place left which deals with this part. The remaining parts |
|
384 | we have only one place left which deals with this part. The remaining parts | |
385 | of the application would start to rely fully on well prepared settings. |
|
385 | of the application would start to rely fully on well prepared settings. | |
386 |
|
386 | |||
387 | This piece would later be split up per topic to avoid a big fat monster |
|
387 | This piece would later be split up per topic to avoid a big fat monster | |
388 | function. |
|
388 | function. | |
389 | """ |
|
389 | """ | |
390 |
|
390 | |||
391 | # Pyramid's mako renderer has to search in the templates folder so that the |
|
391 | # Pyramid's mako renderer has to search in the templates folder so that the | |
392 | # old templates still work. Ported and new templates are expected to use |
|
392 | # old templates still work. Ported and new templates are expected to use | |
393 | # real asset specifications for the includes. |
|
393 | # real asset specifications for the includes. | |
394 | mako_directories = settings.setdefault('mako.directories', [ |
|
394 | mako_directories = settings.setdefault('mako.directories', [ | |
395 | # Base templates of the original Pylons application |
|
395 | # Base templates of the original Pylons application | |
396 | 'rhodecode:templates', |
|
396 | 'rhodecode:templates', | |
397 | ]) |
|
397 | ]) | |
398 | log.debug( |
|
398 | log.debug( | |
399 | "Using the following Mako template directories: %s", |
|
399 | "Using the following Mako template directories: %s", | |
400 | mako_directories) |
|
400 | mako_directories) | |
401 |
|
401 | |||
402 | # Default includes, possible to change as a user |
|
402 | # Default includes, possible to change as a user | |
403 | pyramid_includes = settings.setdefault('pyramid.includes', [ |
|
403 | pyramid_includes = settings.setdefault('pyramid.includes', [ | |
404 | 'rhodecode.lib.middleware.request_wrapper', |
|
404 | 'rhodecode.lib.middleware.request_wrapper', | |
405 | ]) |
|
405 | ]) | |
406 | log.debug( |
|
406 | log.debug( | |
407 | "Using the following pyramid.includes: %s", |
|
407 | "Using the following pyramid.includes: %s", | |
408 | pyramid_includes) |
|
408 | pyramid_includes) | |
409 |
|
409 | |||
410 | # TODO: johbo: Re-think this, usually the call to config.include |
|
410 | # TODO: johbo: Re-think this, usually the call to config.include | |
411 | # should allow to pass in a prefix. |
|
411 | # should allow to pass in a prefix. | |
412 | settings.setdefault('rhodecode.api.url', '/_admin/api') |
|
412 | settings.setdefault('rhodecode.api.url', '/_admin/api') | |
413 |
|
413 | |||
414 | # Sanitize generic settings. |
|
414 | # Sanitize generic settings. | |
415 | _list_setting(settings, 'default_encoding', 'UTF-8') |
|
415 | _list_setting(settings, 'default_encoding', 'UTF-8') | |
416 | _bool_setting(settings, 'is_test', 'false') |
|
416 | _bool_setting(settings, 'is_test', 'false') | |
417 | _bool_setting(settings, 'gzip_responses', 'false') |
|
417 | _bool_setting(settings, 'gzip_responses', 'false') | |
418 |
|
418 | |||
419 | # Call split out functions that sanitize settings for each topic. |
|
419 | # Call split out functions that sanitize settings for each topic. | |
420 | _sanitize_appenlight_settings(settings) |
|
420 | _sanitize_appenlight_settings(settings) | |
421 | _sanitize_vcs_settings(settings) |
|
421 | _sanitize_vcs_settings(settings) | |
422 |
|
422 | |||
423 | return settings |
|
423 | return settings | |
424 |
|
424 | |||
425 |
|
425 | |||
426 | def _sanitize_appenlight_settings(settings): |
|
426 | def _sanitize_appenlight_settings(settings): | |
427 | _bool_setting(settings, 'appenlight', 'false') |
|
427 | _bool_setting(settings, 'appenlight', 'false') | |
428 |
|
428 | |||
429 |
|
429 | |||
430 | def _sanitize_vcs_settings(settings): |
|
430 | def _sanitize_vcs_settings(settings): | |
431 | """ |
|
431 | """ | |
432 | Applies settings defaults and does type conversion for all VCS related |
|
432 | Applies settings defaults and does type conversion for all VCS related | |
433 | settings. |
|
433 | settings. | |
434 | """ |
|
434 | """ | |
435 | _string_setting(settings, 'vcs.svn.compatible_version', '') |
|
435 | _string_setting(settings, 'vcs.svn.compatible_version', '') | |
436 | _string_setting(settings, 'git_rev_filter', '--all') |
|
436 | _string_setting(settings, 'git_rev_filter', '--all') | |
437 | _string_setting(settings, 'vcs.hooks.protocol', 'pyro4') |
|
437 | _string_setting(settings, 'vcs.hooks.protocol', 'pyro4') | |
438 | _string_setting(settings, 'vcs.server', '') |
|
438 | _string_setting(settings, 'vcs.server', '') | |
439 | _string_setting(settings, 'vcs.server.log_level', 'debug') |
|
439 | _string_setting(settings, 'vcs.server.log_level', 'debug') | |
440 | _string_setting(settings, 'vcs.server.protocol', 'pyro4') |
|
440 | _string_setting(settings, 'vcs.server.protocol', 'pyro4') | |
441 | _bool_setting(settings, 'startup.import_repos', 'false') |
|
441 | _bool_setting(settings, 'startup.import_repos', 'false') | |
442 | _bool_setting(settings, 'vcs.hooks.direct_calls', 'false') |
|
442 | _bool_setting(settings, 'vcs.hooks.direct_calls', 'false') | |
443 | _bool_setting(settings, 'vcs.server.enable', 'true') |
|
443 | _bool_setting(settings, 'vcs.server.enable', 'true') | |
444 | _bool_setting(settings, 'vcs.start_server', 'false') |
|
444 | _bool_setting(settings, 'vcs.start_server', 'false') | |
445 | _list_setting(settings, 'vcs.backends', 'hg, git, svn') |
|
445 | _list_setting(settings, 'vcs.backends', 'hg, git, svn') | |
446 |
|
446 | |||
447 |
|
447 | |||
448 | def _bool_setting(settings, name, default): |
|
448 | def _bool_setting(settings, name, default): | |
449 |
|
|
449 | input = settings.get(name, default) | |
|
450 | if isinstance(input, unicode): | |||
|
451 | input = input.encode('utf8') | |||
|
452 | settings[name] = asbool(input) | |||
450 |
|
453 | |||
451 |
|
454 | |||
452 | def _list_setting(settings, name, default): |
|
455 | def _list_setting(settings, name, default): | |
453 | raw_value = settings.get(name, default) |
|
456 | raw_value = settings.get(name, default) | |
454 |
|
457 | |||
455 | old_separator = ',' |
|
458 | old_separator = ',' | |
456 | if old_separator in raw_value: |
|
459 | if old_separator in raw_value: | |
457 | # If we get a comma separated list, pass it to our own function. |
|
460 | # If we get a comma separated list, pass it to our own function. | |
458 | settings[name] = rhodecode_aslist(raw_value, sep=old_separator) |
|
461 | settings[name] = rhodecode_aslist(raw_value, sep=old_separator) | |
459 | else: |
|
462 | else: | |
460 | # Otherwise we assume it uses pyramids space/newline separation. |
|
463 | # Otherwise we assume it uses pyramids space/newline separation. | |
461 | settings[name] = aslist(raw_value) |
|
464 | settings[name] = aslist(raw_value) | |
462 |
|
465 | |||
463 |
|
466 | |||
464 | def _string_setting(settings, name, default): |
|
467 | def _string_setting(settings, name, default): | |
465 | settings[name] = settings.get(name, default).lower() |
|
468 | settings[name] = settings.get(name, default).lower() |
General Comments 0
You need to be logged in to leave comments.
Login now