annotate rhodecode/lib/middleware/https_fixup.py @ 4151:8b8f4a93c364 rhodecode-2.2.5-gpl

old style: show compare "Compare Revisions" inline ... and do the same with all other buttons
author Mads Kiilerich <madski@unity3d.com>
date Wed, 02 Jul 2014 19:03:24 -0400
parents ffd45b185016
children 7e5f8c12a3fc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
1 # -*- coding: utf-8 -*-
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
2 # This program is free software: you can redistribute it and/or modify
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
3 # it under the terms of the GNU General Public License as published by
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
4 # the Free Software Foundation, either version 3 of the License, or
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
5 # (at your option) any later version.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
6 #
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
7 # This program is distributed in the hope that it will be useful,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
10 # GNU General Public License for more details.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
11 #
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
12 # You should have received a copy of the GNU General Public License
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
14 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
15 rhodecode.lib.middleware.https_fixup
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
17
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
18 middleware to handle https correctly
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
19
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
20 :created_on: May 23, 2010
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
21 :author: marcink
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
22 :copyright: (c) 2013 RhodeCode GmbH.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
23 :license: GPLv3, see LICENSE for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
24 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3960
diff changeset
25
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 204
diff changeset
26
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents: 2054
diff changeset
27 from rhodecode.lib.utils2 import str2bool
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
28
1275
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
29
204
a8ea3ce3cdc4 Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 class HttpsFixup(object):
1275
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
31
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
32 def __init__(self, app, config):
204
a8ea3ce3cdc4 Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 self.application = app
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
34 self.config = config
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
35
204
a8ea3ce3cdc4 Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 def __call__(self, environ, start_response):
a8ea3ce3cdc4 Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 self.__fixup(environ)
3359
c394a564ab71 make the htsts headers optional and stored in .ini file.
Marcin Kuzminski <marcin@python-works.com>
parents: 3238
diff changeset
38 debug = str2bool(self.config.get('debug'))
3795
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
39 is_ssl = environ['wsgi.url_scheme'] == 'https'
3359
c394a564ab71 make the htsts headers optional and stored in .ini file.
Marcin Kuzminski <marcin@python-works.com>
parents: 3238
diff changeset
40
3795
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
41 def custom_start_response(status, headers, exc_info=None):
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
42 if is_ssl and str2bool(self.config.get('use_htsts')) and not debug:
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
43 headers.append(('Strict-Transport-Security',
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
44 'max-age=8640000; includeSubDomains'))
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
45 return start_response(status, headers, exc_info)
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
46
4ed55c851d1c fix broken handling of adding an htsts.
Marcin Kuzminski <marcin@python-works.com>
parents: 3359
diff changeset
47 return self.application(environ, custom_start_response)
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
48
204
a8ea3ce3cdc4 Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 def __fixup(self, environ):
1275
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
50 """
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
51 Function to fixup the environ as needed. In order to use this
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
52 middleware you should set this header inside your
204
a8ea3ce3cdc4 Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 proxy ie. nginx, apache etc.
a8ea3ce3cdc4 Created middleware package. Crated special middleware to handle https requests redirections.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 """
2668
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
55 # DETECT PROTOCOL !
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
56 if 'HTTP_X_URL_SCHEME' in environ:
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
57 proto = environ.get('HTTP_X_URL_SCHEME')
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
58 elif 'HTTP_X_FORWARDED_SCHEME' in environ:
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
59 proto = environ.get('HTTP_X_FORWARDED_SCHEME')
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
60 elif 'HTTP_X_FORWARDED_PROTO' in environ:
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
61 proto = environ.get('HTTP_X_FORWARDED_PROTO')
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
62 else:
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
63 proto = 'http'
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
64 org_proto = proto
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
65
2668
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
66 # if we have force, just override
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
67 if str2bool(self.config.get('force_https')):
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
68 proto = 'https'
2054
787f1d157984 extended https fixup middleware.
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
69
2668
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
70 environ['wsgi.url_scheme'] = proto
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
71 environ['wsgi._org_proto'] = org_proto