4 | 4 |
import datetime
|
5 | 5 |
import mimetypes
|
6 | 6 |
import locale
|
|
7 |
import warnings
|
7 | 8 |
import six
|
8 | 9 |
try:
|
9 | 10 |
import chardet
|
10 | 11 |
except ImportError:
|
11 | 12 |
chardet = None
|
12 | 13 |
|
|
14 |
from werkzeug.contrib.fixers import ProxyFix as WerkzeugProxyFix
|
13 | 15 |
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)
|
14 | 57 |
|
15 | 58 |
|
16 | 59 |
class SubUri(object):
|
|
30 | 73 |
Snippet stolen from http://flask.pocoo.org/snippets/35/
|
31 | 74 |
"""
|
32 | 75 |
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 |
)
|
33 | 81 |
self.app = app
|
34 | 82 |
|
35 | 83 |
def __call__(self, environ, start_response):
|