Show More
@@ -1,54 +1,62 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.lib.middleware.https_fixup |
|
3 | rhodecode.lib.middleware.https_fixup | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | middleware to handle https correctly |
|
6 | middleware to handle https correctly | |
7 |
|
7 | |||
8 | :created_on: May 23, 2010 |
|
8 | :created_on: May 23, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software: you can redistribute it and/or modify |
|
13 | # This program is free software: you can redistribute it and/or modify | |
14 | # it under the terms of the GNU General Public License as published by |
|
14 | # it under the terms of the GNU General Public License as published by | |
15 | # the Free Software Foundation, either version 3 of the License, or |
|
15 | # the Free Software Foundation, either version 3 of the License, or | |
16 | # (at your option) any later version. |
|
16 | # (at your option) any later version. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 |
|
25 | |||
26 | from rhodecode.lib import str2bool |
|
26 | from rhodecode.lib import str2bool | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | class HttpsFixup(object): |
|
29 | class HttpsFixup(object): | |
30 |
|
30 | |||
31 | def __init__(self, app, config): |
|
31 | def __init__(self, app, config): | |
32 | self.application = app |
|
32 | self.application = app | |
33 | self.config = config |
|
33 | self.config = config | |
34 |
|
34 | |||
35 | def __call__(self, environ, start_response): |
|
35 | def __call__(self, environ, start_response): | |
36 | self.__fixup(environ) |
|
36 | self.__fixup(environ) | |
37 | return self.application(environ, start_response) |
|
37 | return self.application(environ, start_response) | |
38 |
|
38 | |||
39 | def __fixup(self, environ): |
|
39 | def __fixup(self, environ): | |
40 | """ |
|
40 | """ | |
41 | Function to fixup the environ as needed. In order to use this |
|
41 | Function to fixup the environ as needed. In order to use this | |
42 | middleware you should set this header inside your |
|
42 | middleware you should set this header inside your | |
43 | proxy ie. nginx, apache etc. |
|
43 | proxy ie. nginx, apache etc. | |
44 | """ |
|
44 | """ | |
45 | proto = environ.get('HTTP_X_URL_SCHEME') |
|
|||
46 |
|
45 | |||
47 | if str2bool(self.config.get('force_https')): |
|
46 | if str2bool(self.config.get('force_https')): | |
48 | proto = 'https' |
|
47 | proto = 'https' | |
49 |
|
48 | else: | ||
|
49 | if 'HTTP_X_URL_SCHEME' in environ: | |||
|
50 | proto = environ.get('HTTP_X_URL_SCHEME') | |||
|
51 | elif 'HTTP_X_FORWARDED_SCHEME' in environ: | |||
|
52 | proto = environ.get('HTTP_X_FORWARDED_SCHEME') | |||
|
53 | elif 'HTTP_X_FORWARDED_PROTO' in environ: | |||
|
54 | proto = environ.get('HTTP_X_FORWARDED_PROTO') | |||
|
55 | else: | |||
|
56 | proto = 'http' | |||
50 | if proto == 'https': |
|
57 | if proto == 'https': | |
51 | environ['wsgi.url_scheme'] = proto |
|
58 | environ['wsgi.url_scheme'] = proto | |
52 | else: |
|
59 | else: | |
53 | environ['wsgi.url_scheme'] = 'http' |
|
60 | environ['wsgi.url_scheme'] = 'http' | |
|
61 | ||||
54 | return None |
|
62 | return None |
General Comments 0
You need to be logged in to leave comments.
Login now