git @ Cat's Eye Technologies klaus / 6a93db9
Add ProxyFix, deprecate SubUri (incorrect behavior) Jonas Haag 9 years ago
2 changed file(s) with 50 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
119119 use_smarthttp,
120120 ctags_policy,
121121 )
122 app.wsgi_app = utils.SubUri(app.wsgi_app)
122 app.wsgi_app = utils.ProxyFix(app.wsgi_app)
123123
124124 if use_smarthttp:
125125 # `path -> Repo` mapping for Dulwich's web support
132132 backend=dulwich_backend,
133133 fallback_app=app.wsgi_app,
134134 )
135 dulwich_wrapped_app = utils.SubUri(dulwich_wrapped_app)
135 dulwich_wrapped_app = utils.ProxyFix(dulwich_wrapped_app)
136136
137137 # `receive-pack` is requested by the "client" on a push
138138 # (the "server" is asked to *receive* packs), i.e. we need to secure
44 import datetime
55 import mimetypes
66 import locale
7 import warnings
78 import six
89 try:
910 import chardet
1011 except ImportError:
1112 chardet = None
1213
14 from werkzeug.contrib.fixers import ProxyFix as WerkzeugProxyFix
1315 from humanize import naturaltime
16
17
18 class ProxyFix(WerkzeugProxyFix):
19 """This middleware can be applied to add HTTP (reverse) proxy support to a
20 WSGI application (klaus), making it possible to:
21
22 * Mount it under a sub-URL (http://example.com/git/...)
23 * Use a different HTTP scheme (HTTP vs. HTTPS)
24 * Make it appear under a different domain altogether
25
26 It sets `REMOTE_ADDR`, `HTTP_HOST` and `wsgi.url_scheme` from `X-Forwarded-*`
27 headers. It also sets `SCRIPT_NAME` from the `X-Script-Name` header.
28
29 For instance if you have klaus mounted under /git/ and your site uses SSL
30 (but your proxy doesn't), make the proxy pass ::
31
32 X-Script-Name = '/git'
33 X-Forwarded-Proto = 'https'
34 ...
35
36 If you have more than one proxy server in front of your app, set
37 `num_proxies` accordingly.
38
39 Do not use this middleware in non-proxy setups for security reasons.
40
41 The original values of `REMOTE_ADDR` and `HTTP_HOST` are stored in
42 the WSGI environment as `werkzeug.proxy_fix.orig_remote_addr` and
43 `werkzeug.proxy_fix.orig_http_host`.
44
45 :param app: the WSGI application
46 :param num_proxies: the number of proxy servers in front of the app.
47 """
48 def __call__(self, environ, start_response):
49 script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
50 if script_name.endswith('/'):
51 warnings.warn(
52 "'X-Script-Name' header should not end in '/' (found: %r). "
53 "Please fix your proxy's configuration." % script_name)
54 script_name = script_name.rstrip('/')
55 environ['SCRIPT_NAME'] = script_name
56 return super(ProxyFix, self).__call__(environ, start_response)
1457
1558
1659 class SubUri(object):
3073 Snippet stolen from http://flask.pocoo.org/snippets/35/
3174 """
3275 def __init__(self, app):
76 warnings.warn(
77 "'klaus.utils.SubUri' is deprecated and will be removed. "
78 "Please upgrade your code to use 'klaus.utils.ProxyFix' instead.",
79 DeprecationWarning
80 )
3381 self.app = app
3482
3583 def __call__(self, environ, start_response):